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         28  
  11         1391  
39 11     11   83 use Game::TextMapper::Point;
  11         27  
  11         87  
40              
41 11     11   529 use Modern::Perl '2018';
  11         23  
  11         80  
42 11     11   3926 use Mojo::Base 'Game::TextMapper::Line';
  11         23  
  11         97  
43              
44             sub pixels {
45 1636     1636 0 3264 my ($self, $point) = @_;
46 1636         5037 my ($x, $y) = ($point->x * $dy, ($point->y + $self->offset->[$point->z]) * $dy);
47 1636 50       39829 return ($x, $y) if wantarray;
48 0         0 return sprintf("%d,%d", $x, $y);
49             }
50              
51             sub one_step {
52 366     366 0 1716 my ($self, $from, $to) = @_;
53 366         634 my ($min, $best);
54 366         874 my $dx = $to->x - $from->x;
55 366         2892 my $dy = $to->y - $from->y;
56 366 100       2670 if (abs($dx) >= abs($dy)) {
57 134 100       303 my $x = $from->x + ($dx > 0 ? 1 : -1);
58 134         730 return Game::TextMapper::Point->new(x => $x, y => $from->y, z => $from->z);
59             } else {
60 232 100       550 my $y = $from->y + ($dy > 0 ? 1 : -1);
61 232         1262 return Game::TextMapper::Point->new(x => $from->x, y => $y, z => $from->z);
62             }
63             }
64              
65             1;