]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Track.pm
Move qualification start to SCX::Track
[slotcarman.git] / SCX / Track.pm
1 #!/usr/bin/perl -w
2
3 package SCX::Track;
4
5 use strict;
6 use Carp;
7
8 use Time::HiRes qw(gettimeofday);
9 use Glib qw(TRUE FALSE);
10 use SCX::Car;
11
12 our $SEMAPHORE_STEP = 1000;
13
14 sub new {
15         my ($class, $args) = @_;
16
17         my $self;
18
19         $self->{gui} = $args->{gui} or croak;
20         $self->{race_running} = 0;
21         $self->{lap_counting_up} = 1;
22
23         bless $self, $class;
24
25         for my $i (0..5) {
26                 $self->{cars}->[$i] = SCX::Car->new({
27                         gui => $self->{gui},
28                         id => $i,
29                         track => $self,
30                 });
31                 $self->car($i)->set_order($i);
32         }
33
34         return $self;
35 }
36
37 sub car { return shift->{cars}->[shift]; }
38
39 sub race_start {
40         my ($self) = @_;
41
42         return if $self->{race_running} || $self->{start_in_progress};
43         $self->{race_running} = 0;
44         $self->{start_in_progress} = 1;
45         $self->{semaphore} = 0;
46         $self->{gui}->show_semaphore(0);
47         Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
48 }
49
50 sub semaphore_step {
51         my ($self) = @_;
52
53         return FALSE if !$self->{start_in_progress} && !$self->{race_running};
54
55         $self->{semaphore}++;
56         if ($self->{semaphore} <= 5) {
57                 $self->{gui}->show_semaphore($self->{semaphore});
58                 my $timeout = $SEMAPHORE_STEP;
59                 $timeout += $SEMAPHORE_STEP * rand()
60                         if $self->{semaphore} == 5;
61                 Glib::Timeout->add($timeout, \&semaphore_step, $self);
62         } elsif ($self->{semaphore} == 6) {
63                 $self->{race_running} = 1;
64                 $self->{race_running_since} = gettimeofday;
65                 $self->{start_in_progress} = undef;
66                 $self->{gui}->show_semaphore(0);
67                 Glib::Timeout->add(3*$SEMAPHORE_STEP, \&semaphore_step, $self);
68         } else {
69                 $self->{gui}->show_semaphore(undef);
70                 $self->{semaphore} = undef;
71         }
72         return FALSE;
73 }
74
75 sub race_end {
76         my ($self) = @_;
77
78         $self->{race_running} = 0;
79 }
80
81 sub race_setup {
82         my ($self, $rounds) = @_;
83
84         if ($rounds) {
85                 $self->{gui}->rounds('0/' . $rounds);
86                 $self->{race_rounds} = $rounds;
87         } else {
88                 $self->{gui}->rounds('0');
89                 $self->{race_rounds} = 0;
90         }
91         $self->{race_time} = 0;
92         $self->{best_lap} = undef;
93
94         $self->{gui}->show_semaphore(undef);
95         $self->{race_running} = 0;
96         $self->{start_in_progress} = 0;
97
98         $self->{gui}->time('00:00');
99         $self->{gui}->best_lap('0.00');
100
101         for my $car (0..5) {
102                 $self->car($car)->set_order($car);
103                 $self->car($car)->set_lap(0);
104                 $self->car($car)->set_laptime(undef);
105         }
106 }
107
108 sub check_best_lap {
109         my ($self, $time, $who) = @_;
110
111         return if !defined $time || $time == 0;
112
113         if (!defined $self->{best_lap}
114                 || $time < $self->{best_lap}) {
115                 $self->{best_lap} = $time;
116                 $self->{gui}->best_lap(sprintf("%.2f", $time), $who);
117                 return 1;
118         }
119         return 0;
120 }
121
122 sub qualification_start {
123         my ($self) = @_;
124
125         return if $self->{qualification_running};
126         for my $car (0..5) {
127                 $self->car($car)->set_lap(undef);
128                 $self->car($car)->set_laptime(undef);
129         }
130
131         $self->{qualification_running};
132         $self->{gui}->lap('Qualification');
133         $self->{gui}->time('00:00');
134         $self->{gui}->best_lap('0:00');
135 }
136
137 1;
138