TileMap/Legend.pm

filter: Statementstmt 100% 63 / 63 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 93.6% 89 / 95 SCAR 17.9
CC
6
Cov
94%
add_object
3.1
_build_object_map
3.0
get_class_of_object
2.3
CRAP 6.0
« TileMap/Role/Checks.pm ↑ Summary TileMap/_Utils.pm »
1 package Game::TileMap::Legend;
2 2 $Game::TileMap::Legend::VERSION = '0.002';
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mooish::AttributeBuilder -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use constant TERRAIN_CLASS => 'terrain';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use constant WALL_OBJECT => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant VOID_OBJECT => '0';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has field 'classes' => (
16 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 # isa => HashRef [ ArrayRef [Str]],
19 2 );
20  
21 has param 'characters_per_tile' => (
22 1 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 # isa => PositiveInt,
25 2 );
26  
27 2 has field '_object_map' => (
28 lazy => 1,
29  
30 # isa => HashRef [Str],
31 );
32  
33 has field 'objects' => (
34 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 # isa => HashRef [Any],
37 2 );
38  
39 sub _build_object_map
40 {
41 2 ▶ my $self = shift;
Subroutine _build_object_map: called CC=3 SCAR=11.0
42 2 my %classes = %{$self->classes};
43 2 my %objects = %{$self->objects};
44 2 my %map_reverse;
45  
46 2 foreach my $class (keys %classes) {
47 7 my @objects = map { $objects{$_} } @{$classes{$class}};
48 7 foreach my $obj (@objects) {
49 17 $map_reverse{$obj} = $class;
50 }
51 }
52  
53 2 return \%map_reverse;
54 }
55  
56 sub get_class_of_object
57 {
58 185 ▶ my ($self, $obj) = @_;
Subroutine get_class_of_object: called CC=2 SCAR=8.1
Pod documented
59  
60 185 ▶ return $self->_object_map->{$obj}
Condition33%1/3
$self->_object_map->{$obj} // croak("no such object '${obj}' in map legend")
l!l&&r!l&&!r
18500
MC/DC0%0/2
$self->_object_map->{$obj} // croak("no such object '${obj}' in map legend")
$self->_object_map->{$obj} croak("no such object '${obj}' in map legend")
Truth table33%1/3
A // B
A= $self->_object_map->{$obj}B= croak("no such object '${obj}' in map legend")
ABresult
1X1
011
000
61 // croak "no such object '$obj' in map legend";
62 }
63  
64 sub add_wall
65 {
66 2 ▶ my ($self, $marker) = @_;
Subroutine add_wall: called CC=1 SCAR=0.0
Pod documented
67  
68 2 return $self->add_terrain($marker, WALL_OBJECT);
69 }
70  
71 sub add_void
72 {
73 2 ▶ my ($self, $marker) = @_;
Subroutine add_void: called CC=1 SCAR=0.0
Pod documented
74  
75 2 return $self->add_terrain($marker, VOID_OBJECT);
76 }
77  
78 sub add_terrain
79 {
80 6 ▶ my ($self, $marker, $object) = @_;
Subroutine add_terrain: called CC=1 SCAR=0.0
Pod documented
81  
82 6 return $self->add_object(TERRAIN_CLASS, $marker, $object);
83 }
84  
85 sub add_object
86 {
87 17 ▶ my ($self, $class, $marker, $object) = @_;
Subroutine add_object: called CC=3 SCAR=11.2
Pod documented
88  
89 croak "marker '$marker' is already used"
90 17 ▶ if exists $self->objects->{$marker};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->objects->{$marker} 0 17 17
91  
92 17 ▶ croak "marker '$marker' has wrong length"
Branch: 1 branch
BranchTrueFalseTotal
unless length $marker == $self->characters_per_tile 0 17 17
93 unless length $marker == $self->characters_per_tile;
94  
95 17 push @{$self->classes->{$class}}, $marker;
96 17 $self->objects->{$marker} = $object;
97  
98 17 return $self;
99 }
100  
101 2 1;
102  
103 __END__
« TileMap/Role/Checks.pm ↑ Summary TileMap/_Utils.pm »