]> www.fi.muni.cz Git - slotcarman.git/blobdiff - SCX/Track.pm
Sound support
[slotcarman.git] / SCX / Track.pm
index 6c4b6447cf41f0afd99c55b7f3cd2cfd4abe8239..90724290bc5252bd2bb7b897486d7ec23d3ef72c 100644 (file)
@@ -5,9 +5,9 @@ package SCX::Track;
 use strict;
 use Carp;
 
-use Time::HiRes qw(gettimeofday);
 use Glib qw(TRUE FALSE);
 use SCX::Car;
+use SCX::Sound;
 
 our $SEMAPHORE_STEP = 1000;
 
@@ -21,6 +21,7 @@ sub new {
                round => 0,
                now => 0,
                qualification_setup => -100,
+               no_semaphore => $args->{no_semaphore},
        };
 
        bless $self, $class;
@@ -37,6 +38,8 @@ sub new {
        $self->{gui}->time(undef);
        $self->{gui}->best_lap(undef, undef);
 
+       $self->{sound} = new SCX::Sound;
+
        return $self;
 }
 
@@ -50,6 +53,11 @@ sub race_start {
 
        if ($time - $self->{qualification_setup} < 1) {
                $self->{qualification_running} = 1;
+       } elsif ($self->{no_semaphore}) {
+               $self->{round} = 0;
+               $self->{race_running} = 1;
+               $self->{race_running_since} = $self->{now};
+               $self->{start_in_progress} = undef;
        } else {
                $self->{round} = 0;
                $self->{race_running} = 0;
@@ -75,13 +83,14 @@ sub semaphore_step {
                Glib::Timeout->add($timeout, \&semaphore_step, $self);
        } elsif ($self->{semaphore} == 6) {
                $self->{race_running} = 1;
-               $self->{race_running_since} = gettimeofday;
+               $self->{race_running_since} = $self->{now};
                $self->{start_in_progress} = undef;
                $self->{gui}->show_semaphore(0);
                Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
        } else {
                $self->{gui}->show_semaphore(undef);
                $self->{semaphore} = undef;
+               $self->{sound}->start();
        }
        return FALSE;
 }
@@ -110,27 +119,13 @@ sub race_setup {
        $self->{best_lap} = undef;
 
        $self->{gui}->show_semaphore(undef);
-       $self->{race_running} = 0;
-       $self->{qualification_running} = 0;
-       $self->{start_in_progress} = 0;
-
-       $self->{gui}->time(undef);
-       $self->{gui}->best_lap(undef);
-}
-
-sub reset {
-       my ($self) = @_;
-
        $self->{race_running} = 0;
        $self->{qualification_running} = 0;
        $self->{start_in_progress} = 0;
        $self->{race_finishing} = 0;
-       $self->{best_lap} = undef;
-       $self->{round} = 0;
 
-       $self->print_rounds;
-       $self->{gui}->best_lap(undef);
        $self->{gui}->time(undef);
+       $self->{gui}->best_lap(undef);
 
        for my $car (0..5) {
                $self->car($car)->reset;
@@ -154,8 +149,8 @@ sub print_rounds {
        $self->{gui}->rounds($msg);
 }
 
-sub check_best_lap {
-       my ($self, $time, $who) = @_;
+sub notify_best_lap {
+       my ($self, $id, $time, $who) = @_;
 
        return if !defined $time || $time == 0;
 
@@ -166,6 +161,16 @@ sub check_best_lap {
                || $time < $self->{best_lap}) {
                $self->{best_lap} = $time;
                $self->{gui}->best_lap($time, $who);
+
+               for my $car (0..5) {
+                       $self->car($car)->set_global_best($car == $id);
+                       $self->car($car)->print_best_lap;
+               }
+
+               if (!$self->{race_running} || $self->{round} > 1) {
+                       # skip the first round in the race
+                       $self->{sound}->best_lap($id);
+               }
                return 1;
        }
        return 0;
@@ -230,6 +235,9 @@ sub recalc_order {
 
        if ($self->{round} && $self->{race_rounds}
                && $self->{round} > $self->{race_rounds}) {
+               if (!$self->{race_finishing}) {
+                       $self->{sound}->winner($new_order[0]);
+               }
                $self->{race_finishing} = 1;
        }