File Coverage

blib/lib/Games/Nintendo/Mario.pm
Criterion Covered Total %
statement 46 56 82.1
branch 16 20 80.0
condition n/a
subroutine 13 15 86.6
pod 6 6 100.0
total 81 97 83.5


line stmt bran cond sub pod time code
1 8     8   3514 use 5.20.0;
  8         37  
2 8     8   40 use warnings;
  8         16  
  8         363  
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     8   42 use Carp qw(cluck);
  8         16  
  8         5961  
24              
25 2     2   38 sub _names { qw[Mario Luigi] }
26 1     1   3 sub _states { qw[normal] }
27 1     1   907 sub _items { () }
28 38     38   104 sub _other_defaults { () }
29              
30             sub _goto_hash {
31 0     0   0 { damage => 'dead' }
32             }
33              
34             sub _goto {
35 28     28   49 my $self = shift;
36 28         61 my ($state, $item) = @_;
37 28         67 my $goto = $self->_goto_hash;
38              
39 28 100       80 return unless exists $goto->{$item};
40 19 100       61 return $goto->{$item} unless ref $goto->{$item} eq 'HASH';
41 18 100       75 return $goto->{$item}{_else} unless $goto->{$item}{$state};
42 5         19 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 and
50             #pod C. C must be either "Mario" or "Luigi" and C must be
51             #pod "normal"
52             #pod
53             #pod If left undefined, C and C will default to "Mario" and "normal"
54             #pod respectively.
55             #pod
56             #pod =cut
57              
58             sub new {
59 41     41 1 850 my $class = shift;
60 41         156 my %args = (name => 'Mario', state => 'normal', @_);
61              
62 41 50       137 unless (grep { $_ eq $args{name} } $class->_names) {
  106         314  
63 0         0 cluck "bad name for plumber";
64 0         0 return;
65             }
66 41 50       137 unless (grep { $_ eq $args{state} } $class->_states) {
  210         414  
67 0         0 cluck "bad starting state for plumber";
68 0         0 return;
69             }
70              
71             my $plumber = {
72             state => $args{state},
73             name => $args{name},
74 41         163 $class->_other_defaults
75             };
76              
77 41         184 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             sub powerup {
91 19     19 1 41 my $plumber = shift;
92 19         37 my $item = shift;
93              
94 19 50       40 if ($plumber->state eq 'dead') {
95 0         0 cluck "$plumber->{name} can't power up when dead";
96 0         0 return $plumber;
97             }
98              
99 19 50       53 unless (grep { $_ eq $item } $plumber->_items) {
  52         120  
100 0         0 cluck "$plumber->{name} can't power up with that!";
101 0         0 return $plumber;
102             }
103              
104 19         47 my $goto = $plumber->_goto($plumber->state,$item);
105              
106 19 100       51 $plumber->{state} = $goto if $goto;
107              
108 19         64 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             sub damage {
122 9     9 1 20 my $plumber = shift;
123              
124 9         32 my $goto = $plumber->_goto($plumber->state,'damage');
125              
126 9 100       29 $plumber->{state} = $goto if $goto;
127              
128 9         29 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             sub state { ## no critic Homonym
140 110     110 1 186 my $plumber = shift;
141              
142 110         412 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             sub name {
155 6     6 1 3746 my $plumber = shift;
156              
157 6 100       38 return $plumber->{name} if $plumber->state eq 'normal';
158              
159 1         3 my $name = $plumber->state . q{ } . $plumber->{name};
160 1         10 $name =~ s/(^.)/\u$1/;
161 1         18 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             sub games {
174 0     0 1   return ('Mario Bros.');
175             }
176              
177             #pod =head1 TODO
178             #pod
179             #pod Wario, SMW.
180             #pod
181             #pod =cut
182              
183             "It's-a me! Mario!";
184              
185             __END__