rotate airplane image
[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                 my $to = SDL::Rect->new(
40                         -x => $self->{path}[$pos],
41                         -y => $self->{path}[$pos+1],
42                 );
43 #               $plane->blit( $plane->rect, $self->{app}, $to );
44
45                 my $radian = atan2(
46                         $self->{path}[$pos+1] - $self->{path}[$pos+3],
47                         $self->{path}[$pos]   - $self->{path}[$pos+2],
48                 );
49                 my $deg = $radian / 3.1459265 * 180;
50
51                 $deg = -$deg + 90; # plain nose at 90 deg.
52
53                 my $plane = SDL::Surface->new( -name => 'artwork/airplane.png' );
54                 my $rotated = $tool->rotoZoom( $plane, $deg, 1, 1 );
55                 $rotated->blit( $rotated->rect, $self->{app}, $to );
56
57                 warn "$pos $radian $deg ", $rotated->width, ' ', $rotated->height;
58                 $self->{tick} = $self->{app}->ticks + $self->{speed};
59         }
60 }
61
62 1;