775599c665a416b849ba188885085d4a0fb47a7c
[perl-landing-airplanes.git] / Airplane.pm
1 package Airplane;
2
3 use warnings;
4 use strict;
5
6 sub new {
7         my ( $class, $app ) = @_;
8         my $self = {
9                 app => $app,
10                 path => [],
11                 speed => 100,
12                 tick => 0,
13         };
14         bless $self, $class;
15         return $self;
16 }
17
18 sub set_path {
19         my $self = shift;
20         $self->{path} = [ @_ ];
21         $self->{path_position} = 0;
22         $self->{tick} = $self->{app}->ticks + $self->{speed};
23 }
24
25 our $plane = SDL::Surface->new( -name => 'artwork/airplane.png' );
26
27 sub draw {
28         my $self = shift;
29         return unless $self->{tick};
30         if ( $self->{app}->ticks > $self->{tick} ) {
31                 my $pos = $self->{path_position} += 2;
32                 if ( $pos > $#{ $self->{path} } ) {
33                         warn "end of path for $self";
34                         $self->{tick} = 0;
35                         return;
36                 }
37                 my $to = SDL::Rect->new(
38                         -x => $self->{path}[$pos],
39                         -y => $self->{path}[$pos+1],
40                 );
41                 $plane->blit( $plane->rect, $self->{app}, $to );
42                 warn $pos;
43                 $self->{tick} = $self->{app}->ticks + $self->{speed};
44         }
45 }
46
47 1;