| 1 | package Game::HeroesVsAliens::Hero; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | with 'Game::HeroesVsAliens::Role::ResourceDirectory'; | ||
| 6 | |||
| 7 | 1 | ▶ | use Game::HeroesVsAliens::Projectile; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Data::LnArray; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use SDLx::Sprite::Animated; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | |||
| 11 | has x => ( | ||
| 12 | is => 'rw' | ||
| 13 | ); | ||
| 14 | |||
| 15 | has y => ( | ||
| 16 | is => 'rw' | ||
| 17 | ); | ||
| 18 | |||
| 19 | has width => ( | ||
| 20 | is => 'rw', | ||
| 21 | default => sub { 100 } | ||
| 22 | ); | ||
| 23 | |||
| 24 | has height => ( | ||
| 25 | is => 'rw', | ||
| 26 | default => sub { 100 } | ||
| 27 | ); | ||
| 28 | |||
| 29 | has shooting => ( | ||
| 30 | is => 'rw', | ||
| 31 | default => sub { 1 } | ||
| 32 | ); | ||
| 33 | |||
| 34 | has block_shot => ( | ||
| 35 | is => 'rw', | ||
| 36 | default => sub { 0 } | ||
| 37 | |||
| 38 | ); | ||
| 39 | |||
| 40 | has health => ( | ||
| 41 | is => 'rw', | ||
| 42 | default => sub { 100 } | ||
| 43 | ); | ||
| 44 | |||
| 45 | has projectiles => ( | ||
| 46 | is => 'rw', | ||
| 47 | default => sub { Data::LnArray->new } | ||
| 48 | ); | ||
| 49 | |||
| 50 | has timer => ( | ||
| 51 | is => 'rw', | ||
| 52 | default => sub { 0 } | ||
| 53 | ); | ||
| 54 | |||
| 55 | has idle_sprite => ( | ||
| 56 | is => 'rw', | ||
| 57 | default => sub { | ||
| 58 | my $sprite = SDLx::Sprite::Animated->new( | ||
| 59 | image => $_[0]->resource_directory . 'resources/boy-hero-idle.png', | ||
| 60 | width => 100, | ||
| 61 | height => 100, | ||
| 62 | type => 'reverse' | ||
| 63 | ); | ||
| 64 | return $sprite; | ||
| 65 | } | ||
| 66 | ); | ||
| 67 | |||
| 68 | has shooting_sprite => ( | ||
| 69 | is => 'rw', | ||
| 70 | default => sub { | ||
| 71 | my $sprite = SDLx::Sprite::Animated->new( | ||
| 72 | image => $_[0]->resource_directory . 'resources/boy-hero-shooting.png', | ||
| 73 | width => 100, | ||
| 74 | height => 100, | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | ); | ||
| 78 | |||
| 79 | sub draw { | ||
| 80 | my ($self, $app) = @_; | ||
| 81 | |||
| 82 | if ($self->shooting) { | ||
| 83 | $self->shooting_sprite->rect->x($self->x); | ||
| 84 | $self->shooting_sprite->rect->y($self->y); | ||
| 85 | if ($app->frame % 3 == 0) { | ||
| 86 | $self->block_shot(0); | ||
| 87 | $self->shooting_sprite->next; | ||
| 88 | } | ||
| 89 | $self->shooting_sprite->draw($app->ctx); | ||
| 90 | } else { | ||
| 91 | $self->idle_sprite->rect->x($self->x); | ||
| 92 | $self->idle_sprite->rect->y($self->y); | ||
| 93 | if ($app->frame % 3 == 0) { | ||
| 94 | $self->idle_sprite->next; | ||
| 95 | } | ||
| 96 | $self->idle_sprite->draw($app->ctx); | ||
| 97 | } | ||
| 98 | $app->draw_text($self->x + 15, $self->y + 15, 18, [100, 100, 100, 255], int($self->health)) | ||
| 99 | } | ||
| 100 | |||
| 101 | sub update { | ||
| 102 | my ($self, $app) = @_; | ||
| 103 | if ($self->shooting && !$self->block_shot) { | ||
| 104 | if ($self->shooting_sprite->current_frame == 1) { | ||
| 105 | $self->projectiles->push(Game::HeroesVsAliens::Projectile->new({ | ||
| 106 | x => $self->x + 70, | ||
| 107 | y => $self->y + 15, | ||
| 108 | })); | ||
| 109 | $self->block_shot(1); | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 | $self->timer(0); | ||
| 113 | } | ||
| 114 | $self->handle_projectiles($app); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub handle_projectiles { | ||
| 118 | my ($self, $app) = @_; | ||
| 119 | my $projectiles = $self->projectiles; | ||
| 120 | for (my $i = 0; $i < scalar @{$projectiles}; $i++) { | ||
| 121 | $projectiles->[$i]->update($app); | ||
| 122 | $projectiles->[$i]->draw($app); | ||
| 123 | if ($projectiles->[$i]->collision) { | ||
| 124 | if ( $projectiles->[$i]->explode_sprite->current_loop > 1 ) { | ||
| 125 | $projectiles->splice($i, 1); | ||
| 126 | $i--; | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | my $aliens = $app->aliens; | ||
| 130 | for (my $j = 0; $j < scalar @{$aliens}; $j++) { | ||
| 131 | if ($aliens->[$j] && $projectiles->[$i] && $app->collision($projectiles->[$i], $aliens->[$j])) { | ||
| 132 | $aliens->[$j]->health($aliens->[$j]->health - $projectiles->[$i]->power); | ||
| 133 | $projectiles->[$i]->collision(1); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | if ($projectiles->[$i] && $projectiles->[$i]->x > $app->app_width - $app->cell_size) { | ||
| 139 | $projectiles->splice($i, 1); | ||
| 140 | $i--; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||