File Coverage

blib/lib/Game/TextMapper/Line/Square.pm
Criterion Covered Total %
statement 24 25 96.0
branch 7 8 87.5
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 37 41 90.2


line stmt bran cond sub pod time code
1             # Copyright (C) 2009-2021 Alex Schroeder
2             #
3             # This program is free software: you can redistribute it and/or modify it under
4             # the terms of the GNU Affero General Public License as published by the Free
5             # Software Foundation, either version 3 of the License, or (at your option) any
6             # later version.
7             #
8             # This program is distributed in the hope that it will be useful, but WITHOUT
9             # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10             # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11             # details.
12             #
13             # You should have received a copy of the GNU Affero General Public License along
14             # with this program. If not, see .
15              
16             =encoding utf8
17              
18             =head1 NAME
19              
20             Game::TextMapper::Line::Square - a line implementation for square maps
21              
22             =head1 DESCRIPTION
23              
24             The line connects two points on a square map. This class knows how to compute
25             all the regions between these two points, how to compute the next region along
26             the line, and how to output SVG.
27              
28             =head1 SEE ALSO
29              
30             L
31             L
32             L
33              
34             =cut
35              
36             package Game::TextMapper::Line::Square;
37              
38 11     11   88 use Game::TextMapper::Constants qw($dx $dy);
  11         23  
  11         1068  
39 11     11   54 use Game::TextMapper::Point;
  11         18  
  11         81  
40              
41 11     11   332 use Modern::Perl '2018';
  11         18  
  11         57  
42 11     11   2467 use Mojo::Base 'Game::TextMapper::Line';
  11         38  
  11         85  
43              
44             sub pixels {
45 2188     2188 0 2347 my ($self, $point) = @_;
46 2188         2641 my ($x, $y) = ($point->x * $dy, ($point->y + $self->offset->[$point->z]) * $dy);
47 2188 50       14351 return ($x, $y) if wantarray;
48 0         0 return sprintf("%d,%d", $x, $y);
49             }
50              
51             sub one_step {
52 496     496 0 1155 my ($self, $from, $to) = @_;
53 496         482 my ($min, $best);
54 496         609 my $dx = $to->x - $from->x;
55 496         1913 my $dy = $to->y - $from->y;
56 496 100       1913 if (abs($dx) >= abs($dy)) {
57 200 100       240 my $x = $from->x + ($dx > 0 ? 1 : -1);
58 200         548 return Game::TextMapper::Point->new(x => $x, y => $from->y, z => $from->z);
59             } else {
60 296 100       402 my $y = $from->y + ($dy > 0 ? 1 : -1);
61 296         779 return Game::TextMapper::Point->new(x => $from->x, y => $y, z => $from->z);
62             }
63             }
64              
65             1;