PangZero/GameObject.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 104 CC 13 SCAR 40.5
CC
13
Cov
0%
Collisions
110.0
Delete
6.0
Advance
6.0
CRAP 57.2
1 ##########################################################################
2 package Games::PangZero::GameObject;
3 ##########################################################################
4  
5 sub new {
6 my ($class) = @_;
7 my $self = {
8 'rect' => SDL::Rect->new( 0, 0, 0, 0 ),
9 'speedX' => 0,
10 'speedY' => 0,
11 'x' => 0,
12 'y' => 0,
13 'w' => 10,
14 'h' => 10,
15 };
16 bless $self, $class;
17 }
18  
19 sub Delete {
20 my $self = shift;
21  
22 for (my $i = 0; $i < scalar @Games::PangZero::GameObjects; ++$i) {
23 if ($Games::PangZero::GameObjects[$i] eq $self) {
24 splice @Games::PangZero::GameObjects, $i, 1;
25 last;
26 }
27 }
28 $self->{deleted} = 1;
29 $self->Clear();
30 }
31  
32 sub Advance {
33 my $self = shift;
34
35 $self->{advance}->($self) if $self->{advance};
36 }
37  
38 sub Clear {
39 my ($self) = @_;
40 SDL::Video::blit_surface($Games::PangZero::Background, $self->{rect}, $Games::PangZero::App, $self->{rect});
41 }
42  
43 sub TransferRect {
44 my ($self) = @_;
45  
46 $self->{rect}->x($self->{x} + $Games::PangZero::ScreenMargin);
47 $self->{rect}->y($self->{y} + $Games::PangZero::ScreenMargin);
48 $self->{rect}->w($self->{w});
49 $self->{rect}->h($self->{h});
50 }
51  
52 sub Draw {
53 my ($self) = @_;
54  
55 $self->TransferRect();
56 if ($self->{draw}) {
57 $self->{draw}->($self);
58 } else {
59 SDL::Video::fill_rect( $Games::PangZero::App, $self->{rect}, SDL::Color->new(0x80, 0, 0) );
60 }
61 }
62  
63 sub SetupCollisions {
64 my ($self) = @_;
65
66 $self->{collisionw} = ($self->{collisionw} or $self->{w});
67 $self->{collisionh} = ($self->{collisionh} or $self->{h});
68 $self->{collisionmarginw1} = ( $self->{w} - $self->{collisionw} ) / 2;
69 $self->{collisionmarginw2} = $self->{collisionmarginw1} + $self->{collisionw};
70 $self->{collisionmarginh1} = ( $self->{h} - $self->{collisionh} ) / 2;
71 $self->{collisionmarginh2} = $self->{collisionmarginh1} + $self->{collisionh};
72 $self->{centerx} = $self->{w} / 2;
73 $self->{centery} = $self->{y} / 2;
74 }
75  
76 sub Collisions {
77 my ($self, $other) = @_;
78
79 # Bounding box detection
80
81 unless ($self->{collisionmarginw1} and $other->{collisionmarginw1}) {
82 return 0 if $self->{x} >= $other->{x} + $other->{w};
83 return 0 if $other->{x} >= $self->{x} + $self->{w};
84 return 0 if $self->{y} >= $other->{y} + $other->{h};
85 return 0 if $other->{y} >= $self->{y} + $self->{h};
86 return 1;
87 }
88
89 return 0 if $self->{x} + $self->{collisionmarginw1} >= $other->{x} + $other->{collisionmarginw2};
90 return 0 if $other->{x} + $other->{collisionmarginw1} >= $self->{x} + $self->{collisionmarginw2};
91 return 0 if $self->{y} + $self->{collisionmarginh1} >= $other->{y} + $other->{collisionmarginh2};
92 return 0 if $other->{y} + $other->{collisionmarginh1} >= $self->{y} + $self->{collisionmarginh2};
93 return 1;
94 }
95  
96 1;