]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
d48cbd37bb8f6b44616df4b1bda4bdeb9823bc79
[slotcarman.git] / SCX / Car.pm
1 #!/usr/bin/perl -w
2
3 package SCX::Car;
4
5 use strict;
6
7 sub new {
8         my ($class, $args) = @_;
9
10         my $self = {
11                 gui => $args->{gui},
12                 throttle => -1,
13                 fuel => -1,
14                 order => $args->{order},
15         };
16
17         bless $self, $class;
18
19         $self->set_throttle(undef);
20         $self->set_fuel(undef);
21
22         return $self;
23 }
24
25 sub gui { return shift->{gui}; }
26
27 sub set_throttle {
28         my ($self, $val) = @_;
29
30         return if (!defined $self->{throttle} && !defined $val)
31                 || (defined $self->{throttle} && defined $val
32                         && $self->{throttle} == $val);
33
34         if ($self->{in_pit_lane} && defined $val && $val > 3) {
35                 delete $self->{in_pit_lane};
36                 $self->gui->leave_pit_lane;
37         }
38
39         $self->{throttle} = $val;
40         $self->gui->set_throttle($self->{order}, $val);
41 }
42
43
44 sub set_fuel {
45         my ($self, $val) = @_;
46
47         return if (!defined $self->{fuel} && !defined $val)
48                 || (defined $self->{fuel} && defined $val
49                         && $self->{fuel} == $val);
50
51         $self->{fuel} = $val;
52         $self->gui->set_fuel($self->{order}, $val);
53 }
54
55 sub set_light {
56         # TODO
57 }
58
59 sub set_backbutton {
60         # TODO
61 }
62
63 sub set_model {
64         my ($self, $name) = @_;
65
66         return if (!defined $self->{car_icon} && !defined $name)
67                 || (defined $self->{car_icon} && defined $name
68                         && $self->{car_icon} eq $name);
69
70         $self->{car_icon} = $name;
71         $self->gui->set_car_icon($self->{order}, $name);
72 }
73
74 sub set_lap {
75         my ($self, $lap_nr) = @_;
76
77         return if (!defined $self->{lap} && !defined $lap_nr)
78                 || (defined $self->{lap} && defined $lap_nr
79                         && $self->{lap} == $lap_nr);
80
81         $self->{lap} = $lap_nr;
82         $self->gui->set_lap($self->{order}, defined $lap_nr ? $lap_nr : '--');
83 }
84
85 sub set_laptime {
86         my ($self, $lap_time) = @_;
87
88         if (defined $lap_time) {
89                 $lap_time = sprintf("%.2f", $lap_time);
90                 if ($lap_time > 1.0 && (
91                         !$self->{in_pit_lane} && !defined $self->{best_lap}
92                         || $self->{best_lap} > $lap_time
93                         )) {
94
95                         $self->gui->set_best_lap($self->{order}, $lap_time);
96                         $self->{best_lap} = $lap_time;
97                 }
98                 
99         } else {
100                 $self->gui->set_best_lap($self->{order}, '--');
101                 $self->{best_lap} = undef;
102         }
103
104         return if (!defined $self->{laptime} && !defined $lap_time)
105                 || (defined $self->{laptime} && defined $lap_time
106                         && $self->{laptime} == $lap_time);
107
108         $self->{laptime} = $lap_time;
109
110         $self->gui->set_laptime($self->{order}, defined $lap_time
111                 ? $lap_time : '--');
112 }
113
114 sub set_driver {
115         my ($self, $driver_name) = @_;
116
117         return if (!defined $self->{driver} && !defined $driver_name)
118                 || (defined $self->{driver} && defined $driver_name
119                         && $self->{driver} == $driver_name);
120
121         $self->{driver} = $driver_name;
122         $self->gui->set_driver($self->{order}, $driver_name || '--');
123 }
124
125 sub enter_pit_lane {
126         my ($self) = @_;
127
128         $self->{in_pit_lane} = 1;
129         $self->gui->enter_pit_lane;
130 }
131
132 sub set_order {
133         my ($self, $pos) = @_;
134
135         $self->{order} = $pos;
136         
137         $self->gui->set_driver($self->{order}, $self->{driver}
138                 || '--');
139         $self->gui->set_car_icon($self->{order}, $self->{car_icon});
140         $self->gui->set_throttle($self->{order}, $self->{throttle});
141         $self->gui->set_lap($self->{order}, defined $self->{lap}
142                 ? $self->{lap} : '--');
143         $self->gui->set_laptime($self->{order}, defined $self->{laptime}
144                 ? $self->{laptime} : '--');
145         $self->gui->set_best_lap($self->{order}, defined $self->{best_lap}
146                 ? $self->{best_lap} : '--');
147         $self->gui->set_fuel($self->{order}, $self->{fuel});
148 }
149         
150 1;
151