lib/Game/HeroesVsAliens.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Game/HeroesVsAliens/Hero.pm ↑ Summary
1 package Game::HeroesVsAliens;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 our $VERSION = '0.01';
7  
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Game::HeroesVsAliens::Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Game::HeroesVsAliens::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Game::HeroesVsAliens::Hero;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Game::HeroesVsAliens::Alien;
14  
15 use SDL::Event;
16 use SDLx::App;
17 use SDL::GFX::Primitives;
18 use SDLx::Text;
19 use SDL::GFX::Primitives;
20 use Data::LnArray;
21  
22 with 'Game::HeroesVsAliens::Role::ResourceDirectory';
23  
24 has game_over => (
25 is => 'rw',
26 default => sub { 0 }
27 );
28  
29 has game_grid => (
30 is => 'rw',
31 default => sub { Data::LnArray->new; }
32 );
33  
34 has heroes => (
35 is => 'rw',
36 default => sub { Data::LnArray->new; }
37 );
38  
39 has aliens => (
40 is => 'rw',
41 default => sub { Data::LnArray->new; }
42 );
43  
44 has alien_positions => (
45 is => 'rw',
46 default => sub { Data::LnArray->new; }
47 );
48  
49 has aliens_interval => (
50 is => 'rw',
51 default => sub { 400 }
52 );
53  
54 has frame => (
55 is => 'rw',
56 default => sub { 0 }
57 );
58  
59 has cell_size => (
60 is => 'rw',
61 default => sub { 100 }
62 );
63  
64 has cell_gap => (
65 is => 'rw',
66 default => sub { 3 }
67 );
68  
69 has app_width => (
70 is => 'rw',
71 default => sub { 800 }
72 );
73  
74 has app_height => (
75 is => 'rw',
76 default => sub { 600 }
77 );
78  
79 has score => (
80 is => 'rw',
81 default => sub { 0 }
82 );
83  
84 has winning_score => (
85 is => 'rw',
86 default => sub { 1000 }
87 );
88  
89 has score_text => (
90 is => 'rw',
91 default => sub {
92 my $score = SDLx::Text->new(
93 size => 18,
94 x => 10,
95 y => 10
96 );
97 return $score;
98 }
99 );
100  
101 has resources => (
102 is => 'rw',
103 default => sub { 1000 }
104 );
105  
106 has resource_text => (
107 is => 'rw',
108 default => sub {
109 my $resource = SDLx::Text->new(
110 size => 18,
111 x => 10,
112 y => 30
113 );
114 return $resource;
115 }
116 );
117  
118 has ctx => (
119 is => 'rw'
120 );
121  
122 has background => (
123 is => 'rw',
124 default => sub {
125 my $sprite = SDLx::Sprite->new(
126 image => $_[0]->resource_directory . "/" . 'resources/game-background.png',
127 );
128 $sprite->rect->x(0);
129 $sprite->rect->y(0);
130 return $sprite;
131 }
132 );
133  
134 has mouse => (
135 is => 'rw',
136 default => sub {
137 return Game::HeroesVsAliens::Mouse->new();
138 }
139 );
140  
141 sub run {
142 my ($self) = @_;
143  
144 $self->ctx(
145 SDLx::App->new(
146 title => 'Heroes Vs Aliens',
147 width => $self->app_width,
148 height => $self->app_height,
149 )
150 );
151  
152 $self->resource_directory;
153  
154 $self->ctx->add_event_handler(sub {
155 my $e = shift;
156 if ($e->type == SDL_MOUSEMOTION) {
157 $self->mouse->x($e->motion_x);
158 $self->mouse->y($e->motion_y);
159 }
160 if ($e->type == SDL_MOUSEBUTTONDOWN) {
161 $self->add_hero();
162 }
163 $self->ctx->stop if ($e->type == SDL_QUIT);
164 });
165  
166 $self->ctx->add_show_handler(sub {
167 my $e = shift;
168 if (!$self->game_over) {
169 $self->background->draw($self->ctx);
170 $self->show_grid();
171 $self->handle_aliens();
172 $self->handle_heroes();
173 $self->handle_game_status();
174 $self->frame($self->frame + 1);
175 $self->ctx->update;
176 }
177 });
178  
179 $self->create_grid();
180  
181 $self->ctx->run();
182 }
183  
184  
185 sub create_grid {
186 my ($self) = @_;
187 for (my $y = $self->cell_size; $y < $self->app_height; $y += $self->cell_size) {
188 for (my $x = 0; $x < $self->app_width; $x += $self->cell_size) {
189 $self->game_grid->push(Game::HeroesVsAliens::Cell->new({
190 x => $x,
191 y => $y
192 }));
193 }
194 }
195 }
196  
197 sub show_grid {
198 my ($self) = @_;
199 for (@{$self->game_grid}) {
200 $_->draw($self);
201 }
202 }
203  
204 sub handle_game_status {
205 my $self = shift;
206 $self->score_text->write_to($self->ctx, "Score: " . $self->score);
207 $self->resource_text->write_to($self->ctx, "Resources: " . $self->resources);
208 if ($self->game_over) {
209 $self->draw_text(
210 130,
211 200,
212 100,
213 [255, 255, 255, 255],
214 'Game Over'
215 );
216 }
217 if ($self->score >= $self->winning_score && $self->aliens->length == 0) {
218 $self->draw_text(
219 130,
220 200,
221 100,
222 [255, 255, 255, 255],
223 'Level Complete'
224 );
225 $self->draw_text(
226 130,
227 300,
228 50,
229 [255, 255, 255, 255],
230 'You win with ' . $self->score . ' points'
231 );
232 $self->game_over(1);
233 }
234 }
235  
236 sub add_hero {
237 my $self = shift;
238 my $pos_x = $self->mouse->x - ($self->mouse->x % $self->cell_size) + $self->cell_gap;
239 my $pos_y = $self->mouse->y - ($self->mouse->y % $self->cell_size) + $self->cell_gap;
240 return if ($pos_y < $self->cell_size);
241 for my $hero (@{$self->heroes}) {
242 if ($hero->x == $pos_x && $hero->y == $pos_y) {
243 return;
244 }
245 }
246  
247 my $hero_cost = 100;
248 if ($self->resources >= $hero_cost) {
249 $self->heroes->push(Game::HeroesVsAliens::Hero->new(
250 x => $pos_x,
251 y => $pos_y,
252 width => $self->cell_size - $self->cell_gap * 2,
253 height => $self->cell_size - $self->cell_gap * 2
254 ));
255 $self->resources( $self->resources - $hero_cost );
256 }
257 }
258  
259 sub handle_heroes {
260 my $self = shift;
261 my $heroes = $self->heroes;
262 for (my $i = 0; $i < scalar @{$heroes}; $i++) {
263 $heroes->[$i]->draw($self);
264 $heroes->[$i]->update($self);
265  
266 if ($self->alien_positions->indexOf($heroes->[$i]->y) != -1) {
267 $heroes->[$i]->shooting(1);
268 } else {
269 $heroes->[$i]->shooting(0);
270 }
271  
272 my $aliens = $self->aliens;
273 for (my $j = 0; $j < scalar @{$aliens}; $j++) {
274 if ($heroes->[$i] && $self->collision($heroes->[$i], $aliens->[$j])) {
275 $aliens->[$j]->movement(0);
276 $heroes->[$i]->health($heroes->[$i]->health - 0.2);
277 }
278 }
279  
280 if ($heroes->[$i]->health <= 0) {
281 for (my $j = 0; $j < scalar @{$aliens}; $j++) {
282 if ($self->collision($heroes->[$i], $aliens->[$j])) {
283 $aliens->[$j]->movement($aliens->[$j]->speed);
284 }
285 }
286 $heroes->splice($i, 1);
287 $i--;
288 }
289 }
290 }
291  
292 sub handle_aliens {
293 my $self = shift;
294 my $aliens = $self->aliens;
295 for (my $i = 0; $i < scalar @{$aliens}; $i++) {
296 $aliens->[$i]->draw($self);
297 $aliens->[$i]->update($self);
298 if ($aliens->[$i]->x < 0) {
299 $self->game_over(1);
300 }
301 if ($aliens->[$i]->dead) {
302 if ($aliens->[$i]->death_sprite->current_loop > 1) {
303 $aliens->splice($i, 1);
304 $i--;
305 }
306 } elsif ($aliens->[$i]->health <= 0) {
307 my $gained = $aliens->[$i]->max_health / 10;
308  
309 $self->resources(
310 $self->resources + $gained
311 );
312 $self->score(
313 $self->score + $gained
314 );
315 my $index = $self->alien_positions->indexOf($aliens->[$i]->y);
316 $self->alien_positions->splice($index, 1);
317 $aliens->[$i]->dead(1);
318 }
319 }
320 if ($self->frame % $self->aliens_interval == 0 && $self->score < $self->winning_score) {
321 my $y_pos = int(rand(5) + 1) * $self->cell_size + $self->cell_gap;
322 $self->aliens->push(Game::HeroesVsAliens::Alien->new({
323 y => $y_pos,
324 x => $self->app_width,
325 width => $self->cell_size - $self->cell_gap * 2,
326 height => $self->cell_size - $self->cell_gap * 2
327 }));
328 $self->alien_positions->push($y_pos);
329 if ($self->aliens_interval > 50) {
330 $self->aliens_interval($self->aliens_interval - 50);
331 }
332 }
333 }
334  
335  
336 sub collision {
337 my ($self, $first, $second) = @_;
338 if (
339 !(
340 $first->x > $second->x + $second->width ||
341 $first->x + $first->width < $second->x ||
342 $first->y > $second->y + $second->width ||
343 $first->y + $first->height < $second->y
344 )
345 ) {
346 return 1;
347 }
348 return 0;
349 }
350  
351 sub draw_rect {
352 my ($self, $x, $y, $w, $h, $colour) = @_;
353 my $rect = $self->ctx->draw_rect([$x, $y, $w, $h], $colour );
354 $self->ctx->update;
355 }
356  
357 sub draw_outline_rect {
358 my ($self, $x, $y, $w, $h, $r, $g, $b, $a) = @_;
359 SDL::GFX::Primitives::rectangle_RGBA(
360 $self->ctx,
361 $x,
362 $y,
363 $x + $w,
364 $y + $h,
365 $r,
366 $g,
367 $b,
368 $a
369 );
370 }
371  
372 sub draw_text {
373 my ($self, $x, $y, $size, $color, $text) = @_;
374 my $draw = SDLx::Text->new(
375 size => $size,
376 x => $x,
377 y => $y,
378 color => $color
379 );
380 $draw->write_to($self->ctx, $text);
381 }
382  
383 sub draw_arc {
384 my ($self, $x, $y, $w, $h, $color) = @_;
385 SDL::GFX::Primitives::arc_RGBA(
386 $self->ctx,
387 $x,
388 $y,
389 $w,
390 -90,
391 90,
392 @{$color}
393 );
394 }
395  
396 sub draw_circle {
397 my ($self, $x, $y, $w, $h, $color) = @_;
398 SDL::GFX::Primitives::filled_circle_RGBA(
399 $self->ctx,
400 $x,
401 $y,
402 $w,
403 @{$color}
404 );
405 }
406  
407 1;
408  
409 __END__
« lib/Game/HeroesVsAliens/Hero.pm ↑ Summary