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 1     1   6 use Game::TextMapper::Constants qw($dx $dy);
  1         2  
  1         72  
39 1     1   6 use Game::TextMapper::Point;
  1         2  
  1         6  
40              
41 1     1   23 use Modern::Perl '2018';
  1         1  
  1         15  
42 1     1   104 use Mojo::Base 'Game::TextMapper::Line';
  1         2  
  1         14  
43              
44             sub pixels {
45 1798     1798 0 2221 my ($self, $point) = @_;
46 1798         2494 my ($x, $y) = ($point->x * $dy, ($point->y + $self->offset->[$point->z]) * $dy);
47 1798 50       15966 return ($x, $y) if wantarray;
48 0         0 return sprintf("%d,%d", $x, $y);
49             }
50              
51             sub one_step {
52 404     404 0 1196 my ($self, $from, $to) = @_;
53 404         443 my ($min, $best);
54 404         647 my $dx = $to->x - $from->x;
55 404         2081 my $dy = $to->y - $from->y;
56 404 100       2014 if (abs($dx) >= abs($dy)) {
57 144 100       216 my $x = $from->x + ($dx > 0 ? 1 : -1);
58 144         510 return Game::TextMapper::Point->new(x => $x, y => $from->y, z => $from->z);
59             } else {
60 260 100       377 my $y = $from->y + ($dy > 0 ? 1 : -1);
61 260         887 return Game::TextMapper::Point->new(x => $from->x, y => $y, z => $from->z);
62             }
63             }
64              
65             1;