]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
c6e2aaaa5d974d96f24c712e8ad279dadac9528d
[slotcarman.git] / SCX / Car.pm
1 #!/usr/bin/perl -w
2
3 package SCX::Car;
4
5 use strict;
6 use Time::HiRes qw(gettimeofday);
7
8 sub new {
9         my ($class, $args) = @_;
10
11         my $self = {
12                 gui      => $args->{gui},
13                 driver   => $args->{driver} || '--',    
14                 throttle => undef,
15                 fuel     => 0,
16                 lap      => 0,
17                 laptime  => 0,
18                 avg_lap  => 0,
19                 car_img  => $args->{car_img},
20                 id       => $args->{id},
21                 track    => $args->{track},
22         };
23
24         bless $self, $class;
25
26         return $self;
27 }
28
29 sub gui { return shift->{gui}; }
30
31 sub track { return shift->{track}; }
32
33 sub same {
34         my ($self, $key, $val) = @_;
35
36         return 1 if !defined $self->{$key} && !defined $val
37                 || (defined $self->{$key} && defined $val
38                         && $self->{$key} eq $val);
39
40         $self->{$key} = $val;
41         return undef;
42 }
43
44 sub set_throttle {
45         my ($self, $val) = @_;
46
47         return if $self->same('throttle', $val);
48
49         if ($self->track->{start_in_progress} && $val) {
50                 $self->{early_start} = 1;
51                 $self->gui->set_driver('Early start!');
52         }
53
54         if ($self->{in_pit_lane} && defined $val && $val > 3) {
55                 $self->leave_pit_lane;
56         }
57
58         $self->gui->set_throttle($self->{order}, $val);
59 }
60
61
62 sub set_fuel {
63         my ($self, $val) = @_;
64
65         return if $self->same('fuel', $val);
66         $self->gui->set_fuel($self->{order}, $val);
67 }
68
69 sub set_light {
70         # TODO
71 }
72
73 sub set_backbutton {
74         # TODO
75 }
76
77 sub set_model {
78         my ($self, $name) = @_;
79
80         return if $self->same('car_img', $name);
81         $self->gui->set_car_icon($self->{order}, $name);
82 }
83
84 sub set_lap {
85         my ($self, $lap_nr) = @_;
86
87         return if $self->same('lap', $lap_nr);
88         $self->gui->set_lap($self->{order}, defined $lap_nr ? $lap_nr : '--');
89         if ($self->track->{race_running} && $self->{lap} > 1) {
90                 my $now = $self->{last_finish_time};
91                 my $avg = ($now - $self->track->{race_running_since})
92                         / ($self->{lap} - 1);
93                 $self->{avg_lap} = $avg;
94                 $self->gui->set_avg_lap($self->{order}, $avg);
95         } else {
96                 $self->gui->set_avg_lap($self->{order}, undef);
97         }
98 }
99
100 sub set_laptime {
101         my ($self, $lap_time) = @_;
102
103         return if $self->same('laptime', $lap_time);
104
105         $self->gui->set_laptime($self->{order}, $lap_time);
106
107         if (defined $lap_time) {
108                 if ($lap_time > 1.0 && !$self->{in_pit_lane}
109                         && (!defined $self->{best_lap}
110                                 || $self->{best_lap} > $lap_time
111                         )) {
112
113                         $self->{best_lap} = $lap_time;
114                         my $global = $self->{track}->check_best_lap($lap_time,
115                                 $self->{driver}
116                         );
117                         $self->gui->set_best_lap($self->{order}, $lap_time,
118                                 $global);
119                 }
120         } else {
121                 $self->gui->set_best_lap($self->{order}, $lap_time);
122                 $self->{best_lap} = undef;
123         }
124 }
125
126 sub set_driver {
127         my ($self, $driver_name) = @_;
128
129         return if $self->same('driver', $driver_name);
130         $self->gui->set_name($self->{order}, $driver_name);
131 }
132
133 sub enter_pit_lane {
134         my ($self) = @_;
135
136         return if $self->{in_pit_lane};
137
138         $self->{in_pit_lane} = 1;
139         $self->gui->enter_pit_lane;
140 }
141
142 sub leave_pit_lane {
143         my ($self) = @_;
144
145         return if !$self->{in_pit_lane};
146
147         $self->{in_pit_lane} = 0;
148         $self->gui->leave_pit_lane;
149 }
150
151 sub set_order {
152         my ($self, $pos) = @_;
153
154         $self->{order} = $pos;
155         
156         $self->gui->set_name($self->{order}, $self->{driver});
157         $self->gui->set_car_icon($self->{order}, $self->{car_icon});
158         $self->gui->set_throttle($self->{order}, $self->{throttle});
159         $self->gui->set_lap($self->{order}, $self->{lap});
160         $self->gui->set_laptime($self->{order}, $self->{laptime});
161         $self->gui->set_best_lap($self->{order}, $self->{best_lap});
162         $self->gui->set_avg_lap($self->{order}, $self->{avg_lap});
163         $self->gui->set_fuel($self->{order}, $self->{fuel});
164         if ($self->{in_pit_lane}) {
165                 $self->gui->enter_pit_lane;
166         }
167 }
168
169 sub reset {
170         my ($self) = @_;
171
172         $self->set_lap(0);
173         $self->set_laptime(undef);
174         $self->{in_pit_lane} = 0;
175         $self->{early_start} = undef;
176 }
177
178 sub finish_line {
179         my ($self, $time, $regular) = @_;
180
181         return undef
182                 if defined $self->{last_finish_time}
183                 && $time - $self->{last_finish_time} < 2;
184
185         if ($regular) {
186                 if (defined $self->{last_finish_time}) {
187                         $self->set_laptime($time - $self->{last_finish_time});
188                 }
189         } else {
190                 $self->enter_pit_lane;
191         }
192
193         $self->{last_finish_time} = $time;
194         $self->set_lap(++$self->{lap});
195
196         return 1;
197 }
198
199 1;
200