added Airplane object
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 20 May 2010 12:54:20 +0000 (14:54 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 20 May 2010 12:54:20 +0000 (14:54 +0200)
Airplane.pm [new file with mode: 0644]
trace-path.pl

diff --git a/Airplane.pm b/Airplane.pm
new file mode 100644 (file)
index 0000000..775599c
--- /dev/null
@@ -0,0 +1,47 @@
+package Airplane;
+
+use warnings;
+use strict;
+
+sub new {
+       my ( $class, $app ) = @_;
+       my $self = {
+               app => $app,
+               path => [],
+               speed => 100,
+               tick => 0,
+       };
+       bless $self, $class;
+       return $self;
+}
+
+sub set_path {
+       my $self = shift;
+       $self->{path} = [ @_ ];
+       $self->{path_position} = 0;
+       $self->{tick} = $self->{app}->ticks + $self->{speed};
+}
+
+our $plane = SDL::Surface->new( -name => 'artwork/airplane.png' );
+
+sub draw {
+       my $self = shift;
+       return unless $self->{tick};
+       if ( $self->{app}->ticks > $self->{tick} ) {
+               my $pos = $self->{path_position} += 2;
+               if ( $pos > $#{ $self->{path} } ) {
+                       warn "end of path for $self";
+                       $self->{tick} = 0;
+                       return;
+               }
+               my $to = SDL::Rect->new(
+                       -x => $self->{path}[$pos],
+                       -y => $self->{path}[$pos+1],
+               );
+               $plane->blit( $plane->rect, $self->{app}, $to );
+               warn $pos;
+               $self->{tick} = $self->{app}->ticks + $self->{speed};
+       }
+}
+
+1;
index afb304d..0fe9353 100755 (executable)
@@ -14,6 +14,9 @@ use Algorithm::Line::Bresenham qw(line);
 use Carp qw(cluck);
 use Data::Dump qw(dump);
 
+use Airplane;
+our @airplanes;
+
 our $debug = 0;
 
 my ( $w, $h ) = ( 800, 480 );
@@ -146,6 +149,8 @@ sub curve {
                        sub { $app->pixel( @_, $path_color ) }
                );
        }
+       push @airplanes, Airplane->new( $app );
+       $airplanes[-1]->set_path( @path );
        $app->sync;
        reset_path;
 }
@@ -166,7 +171,7 @@ sub clear_screen {
 
 sub handle_events {
 
-       while ( $event->wait ) {
+       while ( $event->poll ) {
                my $type = $event->type();
 
                if ( $type == SDL_MOUSEBUTTONDOWN() ) {
@@ -210,7 +215,7 @@ sub handle_events {
                                        $app->fill( $rect, $mouse_color );
                                        $app->update( $rect );
                                        $last_x = $x;
-                                       $last_y = $y;
+                               $last_y = $y;
                                } else {
                                        $mouse_down = 0;
                                        curve;
@@ -224,8 +229,9 @@ sub handle_events {
 
 while(1) {
        handle_events;
+       $_->draw foreach @airplanes;
+       $app->delay(50);
        $app->sync;
-       $app->delay(1);
 }
 
 1;