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         75  
39 1     1   5 use Game::TextMapper::Point;
  1         22  
  1         8  
40              
41 1     1   30 use Modern::Perl '2018';
  1         2  
  1         4  
42 1     1   126 use Mojo::Base 'Game::TextMapper::Line';
  1         2  
  1         4  
43              
44             sub pixels {
45 2240     2240 0 2835 my ($self, $point) = @_;
46 2240         3383 my ($x, $y) = ($point->x * $dy, ($point->y + $self->offset->[$point->z]) * $dy);
47 2240 50       20258 return ($x, $y) if wantarray;
48 0         0 return sprintf("%d,%d", $x, $y);
49             }
50              
51             sub one_step {
52 508     508 0 1621 my ($self, $from, $to) = @_;
53 508         592 my ($min, $best);
54 508         742 my $dx = $to->x - $from->x;
55 508         2653 my $dy = $to->y - $from->y;
56 508 100       2450 if (abs($dx) >= abs($dy)) {
57 227 100       362 my $x = $from->x + ($dx > 0 ? 1 : -1);
58 227         863 return Game::TextMapper::Point->new(x => $x, y => $from->y, z => $from->z);
59             } else {
60 281 100       403 my $y = $from->y + ($dy > 0 ? 1 : -1);
61 281         1009 return Game::TextMapper::Point->new(x => $from->x, y => $y, z => $from->z);
62             }
63             }
64              
65             1;