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