| 1 | 8 | ▶ | use 5.20.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Games::Nintendo::Mario 0.209; | ||||||||||
| 4 | # ABSTRACT: a class for jumping Italian plumbers | ||||||||||
| 5 | |||||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod use Games::Nintendo::Mario; | ||||||||||
| 9 | #pod | ||||||||||
| 10 | #pod my $hero = Games::Nintendo::Mario->new(name => 'Luigi'); | ||||||||||
| 11 | #pod | ||||||||||
| 12 | #pod $hero->damage; # cue the Mario Death Music | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod =head1 DESCRIPTION | ||||||||||
| 15 | #pod | ||||||||||
| 16 | #pod This module provides a base class for representing the Mario Brothers from | ||||||||||
| 17 | #pod Nintendo's long-running Mario franchise of games. Each Mario object keeps | ||||||||||
| 18 | #pod track of the plumber's current state and can be damaged or given powerups to | ||||||||||
| 19 | #pod change his state. | ||||||||||
| 20 | #pod | ||||||||||
| 21 | #pod =cut | ||||||||||
| 22 | |||||||||||
| 23 | 8 | ▶ | use Carp qw(cluck); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | sub _names { qw[Mario Luigi] } | ||||||||
|
Subroutine
_names: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | sub _states { qw[normal] } | ||||||||
|
Subroutine
_states: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | sub _items { () } | ||||||||
|
Subroutine
_items: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 38 | ▶ | sub _other_defaults { () } | ||||||||
|
Subroutine
_other_defaults: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub _goto_hash { | ||||||||
|
Subroutine
_goto_hash: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | 0 | { damage => 'dead' } | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 28 | ▶ | sub _goto { | ||||||||
|
Subroutine
_goto: called CC=4 SCAR=13.9
| |||||||||||
| 35 | 28 | my $self = shift; | |||||||||
| 36 | 28 | my ($state, $item) = @_; | |||||||||
| 37 | 28 | my $goto = $self->_goto_hash; | |||||||||
| 38 | |||||||||||
| 39 | 28 | ▶ | return unless exists $goto->{$item}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 19 | ▶ | return $goto->{$item} unless ref $goto->{$item} eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 18 | ▶ | return $goto->{$item}{_else} unless $goto->{$item}{$state}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 5 | return $goto->{$item}{$state}; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | #pod =method new | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod my $hero = Games::Nintendo::Mario->new(name => 'Luigi'); | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod The constructor for Mario objects takes two named parameters, C<name> and | ||||||||||
| 50 | #pod C<state>. C<name> must be either "Mario" or "Luigi" and C<state> must be | ||||||||||
| 51 | #pod "normal" | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod If left undefined, C<name> and C<state> will default to "Mario" and "normal" | ||||||||||
| 54 | #pod respectively. | ||||||||||
| 55 | #pod | ||||||||||
| 56 | #pod =cut | ||||||||||
| 57 | |||||||||||
| 58 | 41 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||
| 59 | 41 | my $class = shift; | |||||||||
| 60 | 41 | my %args = (name => 'Mario', state => 'normal', @_); | |||||||||
| 61 | |||||||||||
| 62 | 41 | ▶ | unless (grep { $_ eq $args{name} } $class->_names) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | cluck "bad name for plumber"; | |||||||||
| 64 | 0 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | 41 | ▶ | unless (grep { $_ eq $args{state} } $class->_states) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | cluck "bad starting state for plumber"; | |||||||||
| 68 | 0 | return; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | my $plumber = { | ||||||||||
| 72 | state => $args{state}, | ||||||||||
| 73 | name => $args{name}, | ||||||||||
| 74 | 41 | $class->_other_defaults | |||||||||
| 75 | }; | ||||||||||
| 76 | |||||||||||
| 77 | 41 | bless $plumber => $class; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | #pod =method powerup | ||||||||||
| 81 | #pod | ||||||||||
| 82 | #pod $hero->powerup('hammer'); # this won't work | ||||||||||
| 83 | #pod | ||||||||||
| 84 | #pod As the base Games::Nintendo::Mario class represents Mario from the original | ||||||||||
| 85 | #pod Mario Bros., there is no valid way to call this method. Subclasses | ||||||||||
| 86 | #pod representing Mario in other games may allow various powerup names to be passed. | ||||||||||
| 87 | #pod | ||||||||||
| 88 | #pod =cut | ||||||||||
| 89 | |||||||||||
| 90 | 19 | ▶ | sub powerup { | ||||||||
|
Subroutine
powerup: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||
| 91 | 19 | my $plumber = shift; | |||||||||
| 92 | 19 | my $item = shift; | |||||||||
| 93 | |||||||||||
| 94 | 19 | ▶ | if ($plumber->state eq 'dead') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | cluck "$plumber->{name} can't power up when dead"; | |||||||||
| 96 | 0 | return $plumber; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 19 | ▶ | unless (grep { $_ eq $item } $plumber->_items) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | cluck "$plumber->{name} can't power up with that!"; | |||||||||
| 101 | 0 | return $plumber; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 19 | my $goto = $plumber->_goto($plumber->state,$item); | |||||||||
| 105 | |||||||||||
| 106 | 19 | ▶ | $plumber->{state} = $goto if $goto; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 19 | return $plumber; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | #pod =method damage | ||||||||||
| 112 | #pod | ||||||||||
| 113 | #pod $hero->damage; | ||||||||||
| 114 | #pod | ||||||||||
| 115 | #pod This method causes the object to react as if Mario has been attacked or | ||||||||||
| 116 | #pod damaged. In the base Games::Nintendo::Mario class, this will always result in | ||||||||||
| 117 | #pod his death. | ||||||||||
| 118 | #pod | ||||||||||
| 119 | #pod =cut | ||||||||||
| 120 | |||||||||||
| 121 | 9 | ▶ | sub damage { | ||||||||
|
Subroutine
damage: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 122 | 9 | my $plumber = shift; | |||||||||
| 123 | |||||||||||
| 124 | 9 | my $goto = $plumber->_goto($plumber->state,'damage'); | |||||||||
| 125 | |||||||||||
| 126 | 9 | ▶ | $plumber->{state} = $goto if $goto; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | 9 | return $plumber; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | #pod =method state | ||||||||||
| 132 | #pod | ||||||||||
| 133 | #pod print $hero->state; | ||||||||||
| 134 | #pod | ||||||||||
| 135 | #pod This method accesses the name of Mario's current state. | ||||||||||
| 136 | #pod | ||||||||||
| 137 | #pod =cut | ||||||||||
| 138 | |||||||||||
| 139 | 110 | ▶ | sub state { ## no critic Homonym | ||||||||
|
Subroutine
state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 140 | 110 | my $plumber = shift; | |||||||||
| 141 | |||||||||||
| 142 | 110 | return $plumber->{state}; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | #pod =method name | ||||||||||
| 146 | #pod | ||||||||||
| 147 | #pod print $hero->name; | ||||||||||
| 148 | #pod | ||||||||||
| 149 | #pod This method returns the name of the plumber's current form. (In the base | ||||||||||
| 150 | #pod class, this is always the same as the name passed to the constructor.) | ||||||||||
| 151 | #pod | ||||||||||
| 152 | #pod =cut | ||||||||||
| 153 | |||||||||||
| 154 | 6 | ▶ | sub name { | ||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 155 | 6 | my $plumber = shift; | |||||||||
| 156 | |||||||||||
| 157 | 6 | ▶ | return $plumber->{name} if $plumber->state eq 'normal'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | |||||||||||
| 159 | 1 | my $name = $plumber->state . q{ } . $plumber->{name}; | |||||||||
| 160 | 1 | $name =~ s/(^.)/\u$1/; | |||||||||
| 161 | 1 | return $name; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | #pod =method games | ||||||||||
| 165 | #pod | ||||||||||
| 166 | #pod if (grep /World/, $hero->games) { ... } | ||||||||||
| 167 | #pod | ||||||||||
| 168 | #pod This returns a list of the games in which Mario behaved according to the model | ||||||||||
| 169 | #pod provided by this class. | ||||||||||
| 170 | #pod | ||||||||||
| 171 | #pod =cut | ||||||||||
| 172 | |||||||||||
| 173 | 0 | ▶ | sub games { | ||||||||
|
Subroutine
games: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 174 | 0 | return ('Mario Bros.'); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | #pod =head1 TODO | ||||||||||
| 178 | #pod | ||||||||||
| 179 | #pod Wario, SMW. | ||||||||||
| 180 | #pod | ||||||||||
| 181 | #pod =cut | ||||||||||
| 182 | |||||||||||
| 183 | 8 | "It's-a me! Mario!"; | |||||||||
| 184 | |||||||||||
| 185 | __END__ | ||||||||||