| 1 | package Game::TileMap::Legend; | ||||||||||||
| 2 | 4 | $Game::TileMap::Legend::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 Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 4 | ▶ | use constant TERRAIN_CLASS => 'terrain'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use constant WALL_OBJECT => 'wall'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 4 | ▶ | use constant VOID_OBJECT => 'void'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | has field 'classes' => ( | ||||||||||||
| 15 | isa => HashRef [ ArrayRef [Str]], | ||||||||||||
| 16 | 4 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 4 | ); | |||||||||||
| 18 | |||||||||||||
| 19 | has param 'characters_per_tile' => ( | ||||||||||||
| 20 | isa => PositiveInt, | ||||||||||||
| 21 | 3 | ▶ | default => sub { 1 }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 4 | ); | |||||||||||
| 23 | |||||||||||||
| 24 | 4 | has field '_object_map' => ( | |||||||||||
| 25 | isa => HashRef [Str], | ||||||||||||
| 26 | lazy => 1, | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | has field 'objects' => ( | ||||||||||||
| 30 | isa => HashRef [Any], | ||||||||||||
| 31 | 4 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 4 | ); | |||||||||||
| 33 | |||||||||||||
| 34 | has field 'walls' => ( | ||||||||||||
| 35 | isa => HashRef [Bool], | ||||||||||||
| 36 | 4 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | 4 | ); | |||||||||||
| 38 | |||||||||||||
| 39 | has field 'voids' => ( | ||||||||||||
| 40 | isa => HashRef [Bool], | ||||||||||||
| 41 | 4 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 4 | ); | |||||||||||
| 43 | |||||||||||||
| 44 | sub _build_object_map | ||||||||||||
| 45 | 4 | ▶ | { | ||||||||||
|
Subroutine
_build_object_map: called CC=3 SCAR=11.0
| |||||||||||||
| 46 | 4 | my $self = shift; | |||||||||||
| 47 | 4 | my %classes = %{$self->classes}; | |||||||||||
| 48 | 4 | my %objects = %{$self->objects}; | |||||||||||
| 49 | 4 | my %map_reverse; | |||||||||||
| 50 | |||||||||||||
| 51 | 4 | foreach my $class (keys %classes) { | |||||||||||
| 52 | 9 | my @objects = map { $objects{$_} } @{$classes{$class}}; | |||||||||||
| 53 | 9 | foreach my $obj (@objects) { | |||||||||||
| 54 | 23 | $map_reverse{$obj} = $class; | |||||||||||
| 55 | } | ||||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 4 | return \%map_reverse; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | sub get_class_of_object | ||||||||||||
| 62 | 200 | ▶ | { | ||||||||||
|
Subroutine
get_class_of_object: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||
| 63 | 200 | my ($self, $obj) = @_; | |||||||||||
| 64 | |||||||||||||
| 65 | 200 | ▶ | return $self->_object_map->{$obj} | ||||||||||
|
Condition50%1/2
$self->_object_map->{$obj} // croak("no such object '${obj}' in map legend")
MC/DC0%0/1
$self->_object_map->{$obj} // croak("no such object '${obj}' in map legend")
$self->_object_map->{$obj}
Truth table50%1/2
A
A= $self->_object_map->{$obj}
| |||||||||||||
| 66 | // croak "no such object '$obj' in map legend"; | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | sub add_wall | ||||||||||||
| 70 | 5 | ▶ | { | ||||||||||
|
Subroutine
add_wall: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 71 | 5 | my ($self, $marker, $object) = @_; | |||||||||||
| 72 | 5 | ▶ | $object //= WALL_OBJECT; | ||||||||||
|
Condition100%2/2
$object //= "wall"
MC/DC100%1/1
$object //= "wall"
$object
Truth table100%2/2
A
A= $object
| |||||||||||||
| 73 | |||||||||||||
| 74 | 5 | $self->add_terrain($marker, $object); | |||||||||||
| 75 | 5 | $self->walls->{$object} = !!1; | |||||||||||
| 76 | 5 | return $self; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | sub add_void | ||||||||||||
| 80 | 5 | ▶ | { | ||||||||||
|
Subroutine
add_void: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 81 | 5 | my ($self, $marker, $object) = @_; | |||||||||||
| 82 | 5 | ▶ | $object //= VOID_OBJECT; | ||||||||||
|
Condition100%2/2
$object //= "void"
MC/DC100%1/1
$object //= "void"
$object
Truth table100%2/2
A
A= $object
| |||||||||||||
| 83 | |||||||||||||
| 84 | 5 | $self->add_terrain($marker, $object); | |||||||||||
| 85 | 5 | $self->voids->{$object} = !!1; | |||||||||||
| 86 | 5 | return $self; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | sub add_terrain | ||||||||||||
| 90 | 12 | ▶ | { | ||||||||||
|
Subroutine
add_terrain: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 91 | 12 | my ($self, $marker, $object) = @_; | |||||||||||
| 92 | |||||||||||||
| 93 | 12 | return $self->add_object(TERRAIN_CLASS, $marker, $object); | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | sub add_object | ||||||||||||
| 97 | 23 | ▶ | { | ||||||||||
|
Subroutine
add_object: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 98 | 23 | my ($self, $class, $marker, $object) = @_; | |||||||||||
| 99 | |||||||||||||
| 100 | croak "marker '$marker' is already used" | ||||||||||||
| 101 | 23 | ▶ | if exists $self->objects->{$marker}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | |||||||||||||
| 103 | 23 | ▶ | croak "marker '$marker' has wrong length" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | unless length $marker == $self->characters_per_tile; | ||||||||||||
| 105 | |||||||||||||
| 106 | 23 | push @{$self->classes->{$class}}, $marker; | |||||||||||
| 107 | 23 | $self->objects->{$marker} = $object; | |||||||||||
| 108 | |||||||||||||
| 109 | 23 | return $self; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 4 | 1; | |||||||||||
| 113 | |||||||||||||
| 114 | __END__ | ||||||||||||