package Airplane;

use warnings;
use strict;

use SDL::Tool::Graphic;

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};
}

my $tool  = SDL::Tool::Graphic->new;

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;
		}
#		$plane->blit( $plane->rect, $self->{app}, $to );

		my $radian = atan2(
			$self->{path}[$pos+1] - $self->{path}[$pos+3],
			$self->{path}[$pos]   - $self->{path}[$pos+2],
		);
		my $deg = $radian / 3.1459265 * 180;

		$deg = -$deg + 90; # plain nose at 90 deg.

		my $plane = SDL::Surface->new( -name => 'artwork/airplane.png' );
		my $rotated = $tool->rotoZoom( $plane, $deg, 1, 1 );

		my $to = SDL::Rect->new(
			-x => $self->{path}[$pos]	- $rotated->width / 2,
			-y => $self->{path}[$pos+1]	- $rotated->height / 2,
		);

		$rotated->blit( $rotated->rect, $self->{app}, $to );

		warn "$pos $radian $deg ", $rotated->width, ' ', $rotated->height;
		$self->{tick} = $self->{app}->ticks + $self->{speed};
	}
}

1;
