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