Mario/Hearts.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 6 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 86.1% 62 / 72 CC 7 SCAR 7.3
CC
7
Cov
90%
powerup
4.1
damage
3.0
hearts
2.0
CRAP 2.1
« Mario.pm ↑ Summary Mario/SMB2.pm »
1 2 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Games::Nintendo::Mario::Hearts 0.208;
4 # ABSTRACT: a superclass for Italian plubmers who can take a beating
5  
6 2 ▶use parent qw(Games::Nintendo::Mario);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Hash::Util::FieldHash qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #pod =head1 SYNOPSIS
10 #pod
11 #pod use Games::Nintendo::Mario::Hearts;
12 #pod
13 #pod my $noone = Games::Nintendo::Mario::Hearts->new;
14 #pod
15 #pod print $hero->hearts . '/' . $hero->max_hearts; # at 1/3 health
16 #pod $hero->powerup('heart'); # up to 2/3!
17 #pod $hero->powerup('heart'); # full health!
18 #pod
19 #pod print "It's-a me! ", $hero->name, "!\n"; # 'Mario'
20 #pod
21 #pod $hero->powerup('heart'); # Nothing happens.
22 #pod
23 #pod $hero->damage for (1 .. 3); # cue the Mario Death Music
24 #pod
25 #pod =head1 DESCRIPTION
26 #pod
27 #pod This class subclasses Games::Nintendo::Mario, providing a class for further
28 #pod subclassing. It adds the C<hearts> and C<max_hearts> methods, described below,
29 #pod and it causes Mario to die when his hearts count reaches zero. This behavior
30 #pod is found in SMB2 and the Wario games.
31 #pod
32 #pod All of the methods described in the Mario interface exist as documented, but
33 #pod the only powerup in this class is 'heart' and the only state is 'normal'
34 #pod
35 #pod =method hearts
36 #pod
37 #pod This method returns the number of hearts the plumber currently has. It
38 #pod defaults to 1 at creation.
39 #pod
40 #pod =method max_hearts
41 #pod
42 #pod This method returns the number of heart containers currently in the plumber's
43 #pod heart meter. It defaults to 3 at creation.
44 #pod
45 #pod =method damage
46 #pod
47 #pod =method powerup
48 #pod
49 #pod These methods are defined in Games::Nintendo::Mario.
50 #pod
51 #pod =method games
52 #pod
53 #pod This is an abstract subclass for Mario classes, and does not represent any one
54 #pod game.
55 #pod
56 #pod =cut
57  
58  
59 1 ▶sub _states { qw[normal] }
Subroutine _states: called CC=1 SCAR=0.0
60 1 ▶sub _items { qw[heart] }
Subroutine _items: called CC=1 SCAR=0.0
61 3 ▶sub _other_defaults { ( max_hearts => 3 ) }
Subroutine _other_defaults: called CC=1 SCAR=0.0
62 3 ▶sub __default_hearts { 1 };
Subroutine __default_hearts: called CC=1 SCAR=0.0
63  
64 3 ▶sub _goto_hash { {} } # not used by base Hearts class
Subroutine _goto_hash: called CC=1 SCAR=0.0
65  
66 10 ▶sub max_hearts {
Subroutine max_hearts: called CC=1 SCAR=0.0
Pod documented
67 return $_[0]->{max_hearts}
68 10 }
69  
70 2 fieldhash my %hearts;
71 41 ▶sub hearts {
Subroutine hearts: called CC=2 SCAR=7.0
Pod documented
72 41 my ($self) = @_;
73 41 ▶ $hearts{ $self } //= $self->__default_hearts;
Condition66%2/3
$hearts{$self} //= $self->__default_hearts
l!l&&r!l&&!r
3830
MC/DC0%0/2
$hearts{$self} //= $self->__default_hearts
$hearts{$self} $self->__default_hearts
Truth table66%2/3
A //= B
A= $hearts{$self}B= $self->__default_hearts
ABresult
1X1
011
000
74 41 return $hearts{ $self };
75 }
76  
77 5 ▶sub powerup {
Subroutine powerup: called CC=4 SCAR=14.0
Pod documented
78 5 my $plumber = shift;
79 5 my $item = shift;
80  
81 5 ▶ if (($item eq 'heart') and ($plumber->hearts) and ($plumber->hearts < $plumber->max_hearts)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq "heart" and $plumber->hearts and $plumber->hearts < $plumber->max_hearts) 2 3 5
Condition66%4/6
$item eq "heart" and $plumber->hearts and $plumber->hearts < $plumber->max_hearts
!ll&&!rl&&r
302
$item eq "heart" and $plumber->hearts
!ll&&!rl&&r
302
MC/DC33%1/3
$item eq "heart" and $plumber->hearts and $plumber->hearts < $plumber->max_hearts
$item eq "heart" $plumber->hearts $plumber->hearts < $plumber->max_hearts
Truth table50%2/4
A and B and C
A= $item eq "heart"B= $plumber->heartsC= $plumber->hearts < $plumber->max_hearts
ABCresult
0XX0
10X0
1100
1111
82 2 $hearts{ $plumber }++;
83 }
84 5 $plumber->SUPER::powerup($item);
85 }
86  
87 4 ▶sub damage {
Subroutine damage: called CC=3 SCAR=11.0
Pod documented
88 4 my $self = shift;
89 4 my $item = shift;
90  
91 4 ▶ if ($self->hearts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hearts) 4 0 4
92 4 ▶ $self->{state} = 'dead' unless --$hearts{ $self };
Branch: 1 branch
BranchTrueFalseTotal
unless --$hearts{$self} 2 2 4
93 }
94  
95 4 $self->SUPER::damage;
96 }
97  
98 0 ▶sub games {
Subroutine games: not called CC=1 SCAR=6.9
Pod documented
99 0 return ();
100 }
101  
102 2 "It's-a me! Mario!";
103  
104 __END__
« Mario.pm ↑ Summary Mario/SMB2.pm »