]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/Car.pm
21d6c682051f765ecf8e1ea85b9e56954351f93c
[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         $self->{throttle} = $val;
35         $self->gui->set_throttle($self->{order}, $val);
36 }
37
38
39 sub set_fuel {
40         my ($self, $val) = @_;
41
42         return if (!defined $self->{fuel} && !defined $val)
43                 || (defined $self->{fuel} && defined $val
44                         && $self->{fuel} == $val);
45
46         $self->{fuel} = $val;
47         $self->gui->set_fuel($self->{order}, $val);
48 }
49
50 sub set_light {
51         # TODO
52 }
53
54 sub set_backbutton {
55         # TODO
56 }
57
58 sub set_model {
59         my ($self, $name) = @_;
60
61         return if (!defined $self->{car_icon} && !defined $name)
62                 || (defined $self->{car_icon} && defined $name
63                         && $self->{car_icon} ne $name);
64
65         $self->{car_icon} = $name;
66         $self->gui->set_car_icon($self->{order}, $name);
67 }
68
69 sub set_lap {
70         my ($self, $lap_nr) = @_;
71
72         return if (!defined $self->{lap} && !defined $lap_nr)
73                 || (defined $self->{lap} && defined $lap_nr
74                         && $self->{lap} != $lap_nr);
75
76         $self->{lap} = $lap_nr;
77         $self->gui->set_lap($self->{order}, $lap_nr || 'N/A');
78 }
79
80 sub set_laptime {
81         my ($self, $lap_time) = @_;
82
83         if (defined $lap_time) {
84                 $lap_time = sprintf("%.2f", $lap_time);
85                 if (!defined $self->{best_lap}
86                         || $self->{best_lap} > $lap_time) {
87
88                         $self->gui->set_best_lap($self->{order}, $lap_time);
89                         $self->{best_lap} = $lap_time;
90                 }
91                 
92         }
93
94         return if (!defined $self->{laptime} && !defined $lap_time)
95                 || (defined $self->{laptime} && defined $lap_time
96                         && $self->{laptime} != $lap_time);
97
98         $self->{laptime} = $lap_time;
99
100         $self->gui->set_laptime($self->{order}, $lap_time || 'N/A');
101 }
102
103 sub set_driver {
104         my ($self, $driver_name) = @_;
105
106         return if (!defined $self->{driver} && !defined $driver_name)
107                 || (defined $self->{driver} && defined $driver_name
108                         && $self->{driver} != $driver_name);
109
110         $self->{driver} = $driver_name;
111         $self->gui->set_driver($self->{order}, $driver_name || 'N/A');
112 }
113
114 1;
115