blib/lib/Game/LevelMap.pm

filter: Statementstmt 100% 92 / 92 Branchbran 94.4% 17 / 18 Conditioncond 74.0% 20 / 27 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 16 / 16 Podpod 85.7% 6 / 7 totaltotal 90.8% 159 / 175 CC 28 SCAR 17.5
CC
28
Cov
94%
to_panel
12.0
__ANON__
7.1
BUILD
4.0
CRAP 5.8
↑ Summary
1 # -*- Perl -*-
2 #
3 # a small level map implementation that uses characters (or strings
4 # consisting of escape sequences or combining characters, or objects
5 # that ideally stringify themselves properly) in an array of arrays
6 # representing a level map as might be used in a game
7  
8 package Game::LevelMap;
9  
10 2 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.02';
17  
18 has level => (
19 is => 'rw',
20 5 ▶ default => sub { [ [] ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 13 ▶ isa => sub {
Subroutine __ANON__: called CC=6 SCAR=19.6
22 13 my ($lm) = @_;
23 13 ▶ croak "LevelMap must be an AoA"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lm or ref $lm ne "ARRAY" or ref $lm->[0] ne "ARRAY" 0 13 13
Condition33%2/6
not defined $lm or ref $lm ne "ARRAY" or ref $lm->[0] ne "ARRAY"
l!l&&r!l&&!r
0013
not defined $lm or ref $lm ne "ARRAY"
l!l&&r!l&&!r
0013
MC/DC0%0/3
not defined $lm or ref $lm ne "ARRAY" or ref $lm->[0] ne "ARRAY"
not defined $lm ref $lm ne "ARRAY" ref $lm->[0] ne "ARRAY"
Truth table25%1/4
not(A) or B or C
A= not defined $lmB= ref $lm ne "ARRAY"C= ref $lm->[0] ne "ARRAY"
ABCresult
1XX1
01X1
0011
0000
24 if !defined $lm
25 or ref $lm ne 'ARRAY'
26 or ref $lm->[0] ne 'ARRAY';
27 13 my $cols = $lm->[0]->$#*;
28 13 for my $row ( 1 .. $lm->$#* ) {
29 24 ▶ if ( $cols != $lm->[$row]->$#* ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols != $#{$lm->[$row]}) 1 23 24
30 1 croak 'unequal column length at row index ' . $row;
31 }
32 }
33 },
34 7 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 7 my ( $self, $lm ) = @_;
36 7 $self->_set_rows( $lm->$#* );
37 7 $self->_set_cols( $lm->[0]->$#* );
38 }
39 2 );
40 2 has rows => ( is => 'rwp' );
41 2 has cols => ( is => 'rwp' );
42  
43 8 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
44 8 my ( $self, $args ) = @_;
45 croak "level and from_string both may not be set"
46 8 ▶ if exists $args->{level} and exists $args->{from_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"level"} and exists $args->{"from_string"} 1 7 8
Condition100%3/3
exists $args->{"level"} and exists $args->{"from_string"}
!ll&&!rl&&r
521
MC/DC100%2/2
exists $args->{"level"} and exists $args->{"from_string"}
exists $args->{"level"} exists $args->{"from_string"}
Truth table100%3/3
A and B
A= exists $args->{"level"}B= exists $args->{"from_string"}
ABresult
0X0
100
111
47 7 ▶ $self->from_string( $args->{from_string} ) if exists $args->{from_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"from_string"} 4 3 7
48 }
49  
50 1 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
51 1 my ($self) = @_;
52 1 my $lm = $self->level;
53 1 my @map;
54 1 for my $rown ( 0 .. $lm->$#* ) {
55 3 for my $coln ( 0 .. $lm->$#* ) {
56 9 $map[$rown][$coln] = $lm->[$rown][$coln];
57 }
58 }
59 1 return __PACKAGE__->new( level => \@map );
60 }
61  
62 5 ▶sub from_string {
Subroutine from_string: called CC=4 SCAR=13.9
Pod documented
63 5 my ( $self, $s ) = @_;
64 5 my @map;
65 my $cols;
66 5 for my $row ( split $/, $s ) {
67 23 push @map, [ split '', $row ];
68 23 my $newcols = $map[-1]->$#*;
69 23 ▶ if ( defined $cols ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols) { } 18 5 23
70 18 ▶ if ( $cols != $newcols ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols != $newcols) 1 17 18
71 1 croak 'unequal column length at row index ' . $#map;
72 }
73 } else {
74 5 $cols = $newcols;
75 }
76 }
77 4 $self->level( \@map );
78 4 return $self;
79 }
80  
81 # TODO this might buffer and only print what differs across successive
82 # calls (for less bandwidth over an SSH connection)
83 8 ▶sub to_panel {
Subroutine to_panel: called CC=12 SCAR=24.9
Pod documented
84 8 my $self = shift;
85 8 my ( $col, $row, $width, $height, $x, $y ) = map int, @_[ 0 .. 5 ];
86 8 ▶ my $oobfn = $_[6] // sub { return ' ' };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$_[6] // sub { return " "; }
l!l&&r!l&&!r
260
MC/DC0%0/2
$_[6] // sub { return " "; }
$_[6] sub { return " "; }
Truth table66%2/3
A // B
A= $_[6]B= sub { return " "; }
ABresult
1X1
011
000
87 8 my $lm = $self->level;
88 8 my $map_cols = $lm->$#*;
89 8 my $map_rows = $lm->[0]->$#*;
90 8 ▶ croak "x must be within the level map" if $x < 0 or $x > $map_cols;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $x > $map_cols 2 6 8
Condition100%3/3
$x < 0 or $x > $map_cols
l!l&&r!l&&!r
116
MC/DC100%2/2
$x < 0 or $x > $map_cols
$x < 0 $x > $map_cols
Truth table100%3/3
A or B
A= $x < 0B= $x > $map_cols
ABresult
1X1
011
000
91 6 ▶ croak "y must be within the level map" if $y < 0 or $y > $map_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 0 or $y > $map_rows 2 4 6
Condition100%3/3
$y < 0 or $y > $map_rows
l!l&&r!l&&!r
114
MC/DC100%2/2
$y < 0 or $y > $map_rows
$y < 0 $y > $map_rows
Truth table100%3/3
A or B
A= $y < 0B= $y > $map_rows
ABresult
1X1
011
000
92 4 my $scol = $x - int( $width / 2 );
93 4 my $srow = $y - int( $height / 2 );
94 4 my $s = '';
95  
96 4 for my $r ( $srow .. $srow + $height - 1 ) {
97 14 $s .= "\e[" . $row++ . ';' . $col . 'H';
98 14 for my $c ( $scol .. $scol + $width - 1 ) {
99 52 ▶ if ( $c < 0 or $c > $map_cols or $r < 0 or $r > $map_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < 0 or $c > $map_cols or $r < 0 or $r > $map_rows) { } 26 26 52
Condition77%7/9
$c < 0 or $c > $map_cols or $r < 0 or $r > $map_rows
l!l&&r!l&&!r
26026
$c < 0 or $c > $map_cols or $r < 0
l!l&&r!l&&!r
161026
$c < 0 or $c > $map_cols
l!l&&r!l&&!r
16036
MC/DC50%2/4
$c < 0 or $c > $map_cols or $r < 0 or $r > $map_rows
$c < 0 $c > $map_cols $r < 0 $r > $map_rows
Truth table60%3/5
A or B or C or D
A= $c < 0B= $c > $map_colsC= $r < 0D= $r > $map_rows
ABCDresult
1XXX1
01XX1
001X1
00011
00000
100 26 $s .= $oobfn->( $lm, $c, $r, $map_cols, $map_rows );
101 } else {
102 26 $s .= $lm->[$r][$c];
103 }
104 }
105 }
106 4 print $s;
107 4 return $self;
108 }
109  
110 3 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
111 3 my ($self) = @_;
112 3 my $lm = $self->level;
113 3 my $s = '';
114 3 for my $rowref ( $lm->@* ) { $s .= join( '', $rowref->@* ) . $/ }
115 3 return $s;
116 }
117  
118 2 ▶sub to_terminal {
Subroutine to_terminal: called CC=2 SCAR=6.9
Pod documented
119 2 my $self = shift;
120 2 my ( $col, $row ) = map int, @_;
121 2 my $lm = $self->level;
122 2 my $s = '';
123 2 for my $rowref ( $lm->@* ) {
124 6 $s .= "\e[" . $row++ . ';' . $col . 'H' . join( '', $rowref->@* );
125 }
126 2 print $s;
127 2 return $self;
128 }
129  
130 1 ▶sub update_terminal {
Subroutine update_terminal: called CC=2 SCAR=6.9
Pod documented
131 1 my $self = $_[0];
132 1 my ( $col, $row ) = map int, @_[ 1 .. 2 ];
133 1 my $lm = $self->level;
134 1 my $s = '';
135 1 for my $point ( @_[ 3 .. $#_ ] ) {
136 2 $s .= "\e["
137 . ( $row + $point->[1] ) . ';'
138 . ( $col + $point->[0] ) . 'H'
139 . $lm->[ $point->[1] ][ $point->[0] ];
140 }
141 1 print $s;
142 1 return $self;
143 }
144  
145 2 1;
146 __END__
↑ Summary