X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=SCX%2FTrack.pm;h=bd6ceea620e9d3626cba484c34f5f9f8c094944d;hb=44baa43e3817096004883f4fac2d8008c96520a2;hp=c75659282f57061f35e3002efb567482b213b732;hpb=3043b1b70f247549f10fa5e3ce713c2a80cd2200;p=slotcarman.git diff --git a/SCX/Track.pm b/SCX/Track.pm index c756592..bd6ceea 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -5,6 +5,7 @@ package SCX::Track; use strict; use Carp; +use Time::HiRes qw(gettimeofday); use Glib qw(TRUE FALSE); use SCX::Car; @@ -19,14 +20,17 @@ sub new { $self->{race_running} = 0; $self->{lap_counting_up} = 1; + bless $self, $class; + for my $i (0..5) { $self->{cars}->[$i] = SCX::Car->new({ gui => $self->{gui}, order => $i, + track => $self, }); } - bless $self, $class; + return $self; } sub car { return shift->{cars}->[shift]; } @@ -45,7 +49,7 @@ sub race_start { sub semaphore_step { my ($self) = @_; - return if !$self->{start_in_progress} && !$self->{race_running}; + return FALSE if !$self->{start_in_progress} && !$self->{race_running}; $self->{semaphore}++; if ($self->{semaphore} <= 5) { @@ -56,6 +60,7 @@ sub semaphore_step { Glib::Timeout->add($timeout, \&semaphore_step, $self); } elsif ($self->{semaphore} == 6) { $self->{race_running} = 1; + $self->{race_running_since} = gettimeofday; $self->{start_in_progress} = undef; $self->{gui}->show_semaphore(0); Glib::Timeout->add(3*$SEMAPHORE_STEP, \&semaphore_step, $self); @@ -72,6 +77,46 @@ sub race_end { $self->{race_running} = 0; } +sub race_setup { + my ($self, $rounds) = @_; + + if ($rounds) { + $self->{gui}->rounds('0/' . $rounds); + $self->{race_rounds} = $rounds; + } else { + $self->{gui}->rounds('0'); + $self->{race_rounds} = 0; + } + $self->{race_time} = 0; + $self->{best_lap} = undef; + + $self->{gui}->show_semaphore(undef); + $self->{race_running} = 0; + $self->{start_in_progress} = 0; + + $self->{gui}->time('00:00'); + $self->{gui}->best_lap('0.00'); + + for my $car (0..5) { + $self->car($car)->set_order($car); + $self->car($car)->set_lap(0); + $self->car($car)->set_laptime(undef); + } +} + +sub check_best_lap { + my ($self, $time, $who) = @_; + + return if !defined $time || $time == 0; + + if (!defined $self->{best_lap} + || $time < $self->{best_lap}) { + $self->{best_lap} = $time; + $self->{gui}->best_lap(sprintf("%.2f", $time), $who); + return 1; + } + return 0; +} 1;