lib/Game/HeroesVsAliens/Role/Hero.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Game/HeroesVsAliens/Hero/Blue.pm ↑ Summary lib/Game/HeroesVsAliens.pm »
1 package Game::HeroesVsAliens::Role::Hero;
2  
3 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 with 'Game::HeroesVsAliens::Role::ResourceDirectory';
6  
7 1 ▶use Data::LnArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use SDLx::Sprite::Animated;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 has level => (
11 is => 'rw',
12 required => 1
13 );
14  
15 has x => (
16 is => 'rw'
17 );
18  
19 has y => (
20 is => 'rw'
21 );
22  
23 has width => (
24 is => 'rw',
25 default => sub { 100 }
26 );
27  
28 has height => (
29 is => 'rw',
30 default => sub { 100 }
31 );
32  
33 has shooting => (
34 is => 'rw',
35 default => sub { 0 }
36 );
37  
38 has block_shot => (
39 is => 'rw',
40 default => sub { 0 }
41  
42 );
43  
44 has last_click_frame => (
45 is => 'rw',
46 );
47  
48 has projectiles => (
49 is => 'rw',
50 default => sub { Data::LnArray->new }
51 );
52  
53 has timer => (
54 is => 'rw',
55 default => sub { 0 }
56 );
57  
58 sub draw {
59 my ($self, $app) = @_;
60 if ($self->health <= 0) {
61 $self->death_sprite->rect->x($self->x);
62 $self->death_sprite->rect->y($self->y);
63 if ($app->frame % 3 == 0) {
64 $self->block_shot(0);
65 $self->death_sprite->next;
66 }
67 $self->death_sprite->draw($app->ctx);
68 } elsif ($self->shooting) {
69 $self->shooting_sprite->rect->x($self->x);
70 $self->shooting_sprite->rect->y($self->y);
71 if ($app->frame % 3 == 0) {
72 $self->block_shot(0);
73 $self->shooting_sprite->next;
74 }
75 $self->shooting_sprite->draw($app->ctx);
76 } else {
77 $self->idle_sprite->rect->x($self->x);
78 $self->idle_sprite->rect->y($self->y);
79 if ($app->frame % 3 == 0) {
80 $self->idle_sprite->next;
81 }
82 $self->idle_sprite->draw($app->ctx);
83 }
84 $app->draw_text($self->x + 15, $self->y + 15, 18, [255, 255, 255, 255], int($self->health))
85 }
86  
87 sub update {
88 my ($self, $app) = @_;
89 if ($self->shooting && !$self->block_shot) {
90 if ($self->shooting_sprite->current_frame == 1) {
91 $self->add_projectile($app);
92 $self->block_shot(1);
93 }
94 } else {
95 $self->timer(0);
96 }
97 $self->handle_projectiles($app);
98 }
99  
100 sub handle_projectiles {
101 my ($self, $app) = @_;
102 my $projectiles = $self->projectiles;
103 for (my $i = 0; $i < scalar @{$projectiles}; $i++) {
104 $projectiles->[$i]->update($app);
105 $projectiles->[$i]->draw($app);
106 if ($projectiles->[$i]->collision) {
107 if ( $projectiles->[$i]->explode_sprite->current_loop > 1 ) {
108 $projectiles->splice($i, 1);
109 $i--;
110 }
111 } else {
112 my $aliens = $app->aliens;
113 for (my $j = 0; $j < scalar @{$aliens}; $j++) {
114 if ($aliens->[$j] && $projectiles->[$i] && $app->collision($projectiles->[$i], $aliens->[$j])) {
115 $aliens->[$j]->health($aliens->[$j]->health - $projectiles->[$i]->power);
116 $projectiles->[$i]->collision(1);
117 }
118 }
119 }
120  
121 if ($projectiles->[$i] && $projectiles->[$i]->x > $app->app_width - $app->cell_size) {
122 $projectiles->splice($i, 1);
123 $i--;
124 }
125 }
126 }
127  
128 1;
« lib/Game/HeroesVsAliens/Hero/Blue.pm ↑ Summary lib/Game/HeroesVsAliens.pm »