use curve points for plane path
[perl-landing-airplanes.git] / Airplane.pm
1 package Airplane;
2
3 use warnings;
4 use strict;
5
6 use SDL::Tool::Graphic;
7
8 sub new {
9         my ( $class, $app ) = @_;
10         my $self = {
11                 app => $app,
12                 path => [],
13                 speed => 100,
14                 tick => 0,
15         };
16         bless $self, $class;
17         return $self;
18 }
19
20 sub set_path {
21         my $self = shift;
22         $self->{path} = [ @_ ];
23         $self->{path_position} = 0;
24         $self->{tick} = $self->{app}->ticks + $self->{speed};
25 }
26
27 my $tool  = SDL::Tool::Graphic->new;
28
29 sub draw {
30         my $self = shift;
31         return unless $self->{tick};
32         if ( $self->{app}->ticks > $self->{tick} ) {
33                 my $pos = $self->{path_position} += 2;
34                 if ( $pos > $#{ $self->{path} } ) {
35                         warn "end of path for $self";
36                         $self->{tick} = 0;
37                         return;
38                 }
39 #               $plane->blit( $plane->rect, $self->{app}, $to );
40
41                 my $radian = atan2(
42                         $self->{path}[$pos+1] - $self->{path}[$pos+3],
43                         $self->{path}[$pos]   - $self->{path}[$pos+2],
44                 );
45                 my $deg = $radian / 3.1459265 * 180;
46
47                 $deg = -$deg + 90; # plain nose at 90 deg.
48
49                 my $plane = SDL::Surface->new( -name => 'artwork/airplane.png' );
50                 my $rotated = $tool->rotoZoom( $plane, $deg, 1, 1 );
51
52                 my $to = SDL::Rect->new(
53                         -x => $self->{path}[$pos]       - $rotated->width / 2,
54                         -y => $self->{path}[$pos+1]     - $rotated->height / 2,
55                 );
56
57                 $rotated->blit( $rotated->rect, $self->{app}, $to );
58
59                 warn "$pos $radian $deg ", $rotated->width, ' ', $rotated->height;
60                 $self->{tick} = $self->{app}->ticks + $self->{speed};
61         }
62 }
63
64 1;