]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
8bb7bb9a00e8c5a1f15f596b19e0587dace11eeb
[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                 throttle => -1,
14                 fuel     => -1,
15                 id       => $args->{id},
16                 track    => $args->{track},
17         };
18
19         bless $self, $class;
20
21         return $self;
22 }
23
24 sub gui { return shift->{gui}; }
25
26 sub track { return shift->{track}; }
27
28 sub set_throttle {
29         my ($self, $val) = @_;
30
31         return if (!defined $self->{throttle} && !defined $val)
32                 || (defined $self->{throttle} && defined $val
33                         && $self->{throttle} == $val);
34
35         $self->{throttle} = $val;
36         $self->gui->set_throttle($self->{order}, $val);
37 }
38
39
40 sub set_fuel {
41         my ($self, $val) = @_;
42
43         return if (!defined $self->{fuel} && !defined $val)
44                 || (defined $self->{fuel} && defined $val
45                         && $self->{fuel} == $val);
46
47         $self->{fuel} = $val;
48         $self->gui->set_fuel($self->{order}, $val);
49 }
50
51 sub set_light {
52         # TODO
53 }
54
55 sub set_backbutton {
56         # TODO
57 }
58
59 sub set_model {
60         my ($self, $name) = @_;
61
62         return if (!defined $self->{car_icon} && !defined $name)
63                 || (defined $self->{car_icon} && defined $name
64                         && $self->{car_icon} eq $name);
65
66         $self->{car_icon} = $name;
67         $self->gui->set_car_icon($self->{order}, $name);
68 }
69
70 sub set_lap {
71         my ($self, $lap_nr) = @_;
72
73         return if (!defined $self->{lap} && !defined $lap_nr)
74                 || (defined $self->{lap} && defined $lap_nr
75                         && $self->{lap} == $lap_nr);
76
77         $self->{lap} = $lap_nr;
78         $self->gui->set_lap($self->{order}, defined $lap_nr ? $lap_nr : '--');
79         if ($self->track->{race_running} && $self->{lap} > 1) {
80                 my $now = gettimeofday;
81                 my $avg = ($now - $self->track->{race_running_since})
82                         / ($self->{lap} - 1);
83                 $self->{avg_lap} = $avg;
84                 $self->gui->set_avg_lap($self->{order}, $avg);
85         } else {
86                 $self->gui->set_avg_lap($self->{order}, '--');
87         }
88 }
89
90 sub set_laptime {
91         my ($self, $lap_time) = @_;
92
93         if (defined $lap_time) {
94                 $lap_time = sprintf("%.2f", $lap_time);
95                 if ($lap_time > 1.0 && !$self->{in_pit_lane}
96                         && (!defined $self->{best_lap}
97                                 || $self->{best_lap} > $lap_time
98                         )) {
99
100                         $self->{best_lap} = $lap_time;
101                         my $global = $self->{track}->check_best_lap($lap_time,
102                                 $self->{driver}
103                         );
104                         $self->gui->set_best_lap($self->{order}, $lap_time,
105                                 $global);
106                 }
107         } else {
108                 $self->gui->set_best_lap($self->{order}, '--');
109                 $self->{best_lap} = undef;
110         }
111
112         return if (!defined $self->{laptime} && !defined $lap_time)
113                 || (defined $self->{laptime} && defined $lap_time
114                         && $self->{laptime} == $lap_time);
115
116         $self->{laptime} = $lap_time;
117
118         $self->gui->set_laptime($self->{order}, defined $lap_time
119                 ? $lap_time : '--');
120 }
121
122 sub set_driver {
123         my ($self, $driver_name) = @_;
124
125         return if (!defined $self->{driver} && !defined $driver_name)
126                 || (defined $self->{driver} && defined $driver_name
127                         && $self->{driver} == $driver_name);
128
129         $self->{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                 || '--');
158         $self->gui->set_car_icon($self->{order}, $self->{car_icon});
159         $self->gui->set_throttle($self->{order}, $self->{throttle});
160         $self->gui->set_lap($self->{order}, defined $self->{lap}
161                 ? $self->{lap} : '--');
162         $self->gui->set_laptime($self->{order}, defined $self->{laptime}
163                 ? $self->{laptime} : '--');
164         $self->gui->set_best_lap($self->{order}, defined $self->{best_lap}
165                 ? $self->{best_lap} : '--');
166         $self->gui->set_avg_lap($self->{order}, defined $self->{avg_lap}
167                 ? $self->{avg_lap} : '--');
168         $self->gui->set_fuel($self->{order}, $self->{fuel});
169 }
170         
171 1;
172