]> www.fi.muni.cz Git - slotcarman.git/commitdiff
Start semaphore tweaks.
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 6 Dec 2010 22:41:28 +0000 (23:41 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 6 Dec 2010 22:41:28 +0000 (23:41 +0100)
SCX/GUI.pm
SCX/Track.pm

index 0945af80c0dcae3b86417a14db61408710195848..573843172d28fff1825f0bcbae8a0f8e4f362ddc 100755 (executable)
@@ -22,7 +22,12 @@ sub new {
                        load_image_set('img/fuel%d.svg', 8, 60)
                ],
                car_images => load_image_dir('img/SCXCars', 100),
+               semaphore_images => [
+                       load_image_set('img/semaphore%d.svg', 5, 200)
+               ],
                builder => Gtk2::Builder->new,
+               semaphore_win => Gtk2::Window->new('popup'),
+               semaphore_img => Gtk2::Image->new,
        };
 
        bless $self, $class;
@@ -31,6 +36,16 @@ sub new {
 
        $self->{builder}->connect_signals(undef);
 
+       my $pixbuf = $self->{semaphore_images}->[0];
+       $self->{semaphore_img}->set_from_pixbuf($pixbuf);
+       $self->{semaphore_img}->show;
+       $self->{semaphore_win}->add($self->{semaphore_img});
+       $self->{semaphore_win}->set_type_hint('splashscreen');
+       $self->{semaphore_win}->set_position('center-on-parent');
+       $self->{semaphore_win}->set_transient_for(
+               $self->{builder}->get_object('slotcarman')
+       );
+
        return $self;
 }
 
@@ -166,5 +181,19 @@ sub leave_pit_lane {
        $self->set_laptime($row, '--');
 }
 
+sub show_semaphore {
+       my ($self, $num) = @_;
+
+       my $win = $self->{semaphore_win};
+       if (defined $num) {
+               my $img = $self->{semaphore_img};
+               my $pixbuf = $self->{semaphore_images}->[$num];
+               $img->set_from_pixbuf($pixbuf);
+               $win->show;
+       } else {
+               $win->hide;
+       }
+}
+
 1;
 
index f6200de651989d50c18d85f64a503b99a77a1905..c75659282f57061f35e3002efb567482b213b732 100644 (file)
@@ -5,8 +5,11 @@ package SCX::Track;
 use strict;
 use Carp;
 
+use Glib qw(TRUE FALSE);
 use SCX::Car;
 
+our $SEMAPHORE_STEP = 1000;
+
 sub new {
        my ($class, $args) = @_;
 
@@ -31,8 +34,36 @@ sub car { return shift->{cars}->[shift]; }
 sub race_start {
        my ($self) = @_;
 
-       $self->{race_running} = 1;
+       return if $self->{race_running} || $self->{start_in_progress};
+       $self->{race_running} = 0;
+       $self->{start_in_progress} = 1;
+       $self->{semaphore} = 0;
+       $self->{gui}->show_semaphore(0);
+       Glib::Timeout->add($SEMAPHORE_STEP, \&semaphore_step, $self);
+}
 
+sub semaphore_step {
+       my ($self) = @_;
+
+       return if !$self->{start_in_progress} && !$self->{race_running};
+
+       $self->{semaphore}++;
+       if ($self->{semaphore} <= 5) {
+               $self->{gui}->show_semaphore($self->{semaphore});
+               my $timeout = $SEMAPHORE_STEP;
+               $timeout += $SEMAPHORE_STEP * rand()
+                       if $self->{semaphore} == 5;
+               Glib::Timeout->add($timeout, \&semaphore_step, $self);
+       } elsif ($self->{semaphore} == 6) {
+               $self->{race_running} = 1;
+               $self->{start_in_progress} = undef;
+               $self->{gui}->show_semaphore(0);
+               Glib::Timeout->add(3*$SEMAPHORE_STEP, \&semaphore_step, $self);
+       } else {
+               $self->{gui}->show_semaphore(undef);
+               $self->{semaphore} = undef;
+       }
+       return FALSE;
 }
 
 sub race_end {
@@ -41,5 +72,6 @@ sub race_end {
        $self->{race_running} = 0;
 }
 
+
 1;