TileMap.pm

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