TileMap.pm

filter: Statementstmt 100% 98 / 98 Branchbran 75.0% 12 / 16 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 15 / 15 Podpod 85.7% 6 / 7 totaltotal 95.3% 145 / 152 CC 19 SCAR 13.3
CC
19
Cov
96%
to_string_and_mark
7.0
BUILD
4.4
from_string
4.0
CRAP 3.8
↑ Summary TileMap/Role/Checks.pm »
1 package Game::TileMap;
2 4 $Game::TileMap::VERSION = '1.001';
3 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Game::TileMap::Legend;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Game::TileMap::Tile;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 has param 'legend' => (
15 isa => InstanceOf ['Game::TileMap::Legend'],
16 );
17  
18 4 has field 'coordinates' => (
19 isa => ArrayRef [ArrayRef [Any]],
20 writer => -hidden,
21 );
22  
23 4 has field 'size_x' => (
24 isa => PositiveInt,
25 writer => -hidden,
26 );
27  
28 4 has field 'size_y' => (
29 isa => PositiveInt,
30 writer => -hidden,
31 );
32  
33 4 has field '_guide' => (
34 isa => HashRef [ArrayRef [InstanceOf ['Game::TileMap::Tile']]],
35 writer => 1,
36 );
37  
38 4 with qw(
39 Game::TileMap::Role::Checks
40 Game::TileMap::Role::Helpers
41 );
42  
43 sub new_legend
44 4 ▶{
Subroutine new_legend: called CC=1 SCAR=0.0
Pod documented
45 4 my $self = shift;
46  
47 4 return Game::TileMap::Legend->new(@_);
48 }
49  
50 sub BUILD
51 4 ▶{
Subroutine BUILD: called CC=4 SCAR=14.9
Pod undocumented
52 4 my ($self, $args) = @_;
53  
54 4 ▶ if ($args->{map}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"map"}) 4 0 4
55 $self->from_string($args->{map})
56 4 ▶ if !ref $args->{map};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"map"} 4 0 4
57  
58 $self->from_array($args->{map})
59 4 ▶ if ref $args->{map} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"map"} eq "ARRAY" 0 4 4
60 }
61 }
62  
63 sub from_string
64 4 ▶{
Subroutine from_string: called CC=4 SCAR=13.9
Pod documented
65 4 my ($self, $map_str) = @_;
66 4 my $per_tile = $self->legend->characters_per_tile;
67  
68 my @map_lines =
69 reverse
70 25 grep { length }
71 4 map { s/\s//g; $_ }
72 split "\n", $map_str
73 ;
74  
75 4 my @map;
76 4 foreach my $line (@map_lines) {
77 25 my @objects;
78 25 ▶ while (length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 195 25 220
79 195 my $marker = substr $line, 0, $per_tile, '';
80 195 ▶ push @objects, ($self->legend->objects->{$marker} // croak "Invalid map marker '$marker'");
Condition50%1/2
$self->legend->objects->{$marker} // croak("Invalid map marker '${marker}'")
l!l
1950
MC/DC0%0/1
$self->legend->objects->{$marker} // croak("Invalid map marker '${marker}'")
$self->legend->objects->{$marker}
Truth table50%1/2
A
A= $self->legend->objects->{$marker}
Aresult
11
00
81 }
82  
83 25 push @map, \@objects;
84 }
85  
86 4 return $self->from_array(\@map);
87 }
88  
89 sub from_array
90 4 ▶{
Subroutine from_array: called CC=4 SCAR=13.9
Pod documented
91 4 my ($self, $map_aref) = @_;
92 4 my @map = @{$map_aref};
93  
94 4 my @map_size = (scalar @{$map[0]}, scalar @map);
95 4 my %guide;
96  
97 my @new_map;
98 4 foreach my $line (0 .. $#map) {
99 croak "invalid map size on line $line"
100 25 ▶ if @{$map[$line]} != $map_size[0];
Branch: 1 branch
BranchTrueFalseTotal
if @{$map[$line];} != $map_size[0] 0 25 25
101  
102 25 for my $col (0 .. $#{$map[$line]}) {
103 195 my $prev_obj = $map[$line][$col];
104 195 my $obj = Game::TileMap::Tile->new(
105 legend => $self->legend,
106 contents => $prev_obj,
107 x => $col,
108 y => $line,
109 );
110  
111 195 $new_map[$col][$line] = $obj;
112 195 push @{$guide{$self->legend->get_class_of_object($prev_obj)}}, $obj;
113 }
114 }
115  
116 4 $self->_set_coordinates(\@new_map);
117 4 $self->_set_size_x($map_size[0]);
118 4 $self->_set_size_y($map_size[1]);
119 4 $self->_set_guide(\%guide);
120  
121 4 return $self;
122 }
123  
124 sub to_string
125 2 ▶{
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
126 2 return shift->to_string_and_mark;
127 }
128  
129 sub to_string_and_mark
130 5 ▶{
Subroutine to_string_and_mark: called CC=7 SCAR=19.5
Pod documented
131 5 my ($self, $mark_positions, $with) = @_;
132 5 ▶ $with //= '!' x $self->legend->characters_per_tile;
Condition100%2/2
$with //= "!" x $self->legend->characters_per_tile
l!l
14
MC/DC100%1/1
$with //= "!" x $self->legend->characters_per_tile
$with
Truth table100%2/2
A
A= $with
Aresult
11
00
133  
134 5 my @lines;
135 my %markers_rev = map {
136 45 $self->legend->objects->{$_} => $_
137 5 } keys %{$self->legend->objects};
138  
139 5 my $mark = \undef;
140 5 my $coordinates = $self->coordinates;
141 5 ▶ if ($mark_positions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark_positions) 3 2 5
142 3 $coordinates = dclone $coordinates;
143  
144 3 foreach my $pos (@{$mark_positions}) {
145 5 $coordinates->[$pos->[0]][$pos->[1]] = $mark;
146 }
147 }
148  
149 5 foreach my $pos_x (0 .. $#$coordinates) {
150 46 foreach my $pos_y (0 .. $#{$coordinates->[$pos_x]}) {
151 474 my $obj = $coordinates->[$pos_x][$pos_y];
152 474 ▶ $lines[$pos_y][$pos_x] = $obj eq $mark ? $with : $markers_rev{$obj->type};
Branch: 1 branch
BranchTrueFalseTotal
$obj eq $mark ? : 5 469 474
153 }
154 }
155  
156 return join "\n",
157 reverse
158 5 map { join '', @{$_} } @lines;
159 }
160  
161 sub get_tile
162 31 ▶{
Subroutine get_tile: called CC=4 SCAR=13.9
Pod documented
163 31 my ($self, $x, $y) = @_;
164  
165 31 ▶ return undef if $x < 0 || $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $y < 0 5 26 31
Condition100%3/3
$x < 0 or $y < 0
l!l&&r!l&&!r
3226
MC/DC100%2/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table100%3/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
166 26 my $coord = $self->coordinates->[$x];
167  
168 26 ▶ return $coord && $coord->[$y];
Condition100%3/3
$coord && $coord->[$y]
!ll&&!rl&&r
4418
MC/DC100%2/2
$coord && $coord->[$y]
$coord $coord->[$y]
Truth table100%3/3
A && B
A= $coordB= $coord->[$y]
ABresult
0X0
100
111
169 }
170  
171  
172 4 1;
173  
174 __END__
↑ Summary TileMap/Role/Checks.pm »