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