| 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
| |||||||||||||||||||||||||||||
| 55 | $self->from_string($args->{map}) | ||||||||||||||||||||||||||||
| 56 | 4 | ▶ | if !ref $args->{map}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | $self->from_array($args->{map}) | ||||||||||||||||||||||||||||
| 59 | 4 | ▶ | if ref $args->{map} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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}'")
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$with //= "!" x $self->legend->characters_per_tile
$with
Truth table100%2/2
A
A= $with
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$x < 0 or $y < 0
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
| |||||||||||||||||||||||||||||
| 166 | 26 | my $coord = $self->coordinates->[$x]; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 26 | ▶ | return $coord && $coord->[$y]; | ||||||||||||||||||||||||||
|
Condition100%3/3
$coord && $coord->[$y]
MC/DC100%2/2
$coord && $coord->[$y]
$coord
$coord->[$y]
Truth table100%3/3
A && B
A= $coordB= $coord->[$y]
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 4 | 1; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||