File Coverage

blib/lib/Game/TextMapper/Point.pm
Criterion Covered Total %
statement 11 21 52.3
branch 4 10 40.0
condition 4 8 50.0
subroutine 4 7 57.1
pod 5 5 100.0
total 28 51 54.9


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::Point - a point on the map
21              
22             =head1 DESCRIPTION
23              
24             This is a simple class to hold points. Points have coordinates and know how to
25             print them.
26              
27             =cut
28              
29             package Game::TextMapper::Point;
30 1     1   6 use Modern::Perl '2018';
  1         2  
  1         8  
31 1     1   132 use Mojo::Base -base;
  1         1  
  1         6  
32              
33             =head2 Attributes
34              
35             C, C, C are coordinates.
36              
37             C, C
38             actual implementations, L and
39             L.
40              
41             C is a reference to L from which to get
42             C (for the coordinates), and both C and
43             C (for the label).
44              
45             =cut
46              
47             has 'x';
48             has 'y';
49             has 'z';
50             has 'type';
51             has 'label';
52             has 'size';
53             has 'map';
54              
55             =head2 Methods
56              
57             =head3 str
58              
59             Returns "(1,2,3)" or "(1,2)" depending on whether the z coordinate is defined or
60             not; use this for log output.
61              
62             =cut
63              
64             sub str {
65 0     0 1 0 my $self = shift;
66 0 0       0 if (defined $self->z) {
67 0         0 return '(' . $self->x . ',' . $self->y . ',' . $self->z . ')';
68             } else {
69 0         0 return '(' . $self->x . ',' . $self->y . ')';
70             }
71             }
72              
73             =head3 equal($other)
74              
75             True if all three coordinates match.
76              
77             =cut
78              
79             sub equal {
80 1884     1884 1 5374 my ($self, $other) = @_;
81 1884   66     2555 return $self->x == $other->x && $self->y == $other->y && $self->z == $other->z;
82             }
83              
84             =head3 cmp($other)
85              
86             Return -1, 0, or 1 depending on the three coordinates.
87              
88             =cut
89              
90             sub cmp {
91 0     0 1 0 my ($a, $b) = @_;
92 0   0     0 return $a->x <=> $b->x || $a->y <=> $b->y || $a->z <=> $b->z;
93             }
94              
95             =head3 coordinates
96              
97             Return "1,1,1" or "1,1" for coordinates in scalar context, depending on whether
98             the z coordinate is defined or not, or it returns the three coordinates in list
99             context.
100              
101             =cut
102              
103             sub coordinates {
104 0     0 1 0 my ($self) = @_;
105 0 0       0 return $self->x, $self->y, $self->z if wantarray;
106 0 0       0 return $self->x . "," . $self->y . "," . $self->z if defined $self->z;
107 0         0 return $self->x . "," . $self->y;
108             }
109              
110             =head3 coord($x, $y, $separator)
111              
112             Return "0101" or "-01-01" for coordinates. Often this what we want in text.
113              
114             =cut
115              
116             sub coord {
117 44355     44355 1 64148 my ($x, $y, $separator) = @_;
118 44355   100     117126 $separator //= "";
119             # print (1,1) as 0101; print (-1,-1) as -01-01
120 44355 100       152737 return sprintf("%0*d$separator%0*d",
    100          
121             ($x < 0 ? 3 : 2), $x,
122             ($y < 0 ? 3 : 2), $y);
123             }
124              
125             =head2 Abstract methods
126              
127             These methods must be implemented by derived classes. The $offset argument is an
128             array with the offsets to add to the C based on C coordinate. The idea is
129             that if we have two dungeon levels, for example, and we want to generate a
130             single SVG document, then the first level is at the top of the page, as usual,
131             and the next level is further down on the page: all the C coordinates were
132             increased by the offset.
133              
134             =head3 svg_region($attributes, $offset)
135              
136             This returns an SVG fragment, a string with a C or C element, for
137             example.
138              
139             This is used for the group containing the regions in the resulting SVG.
140              
141             =head3 svg($offset)
142              
143             This returns an SVG fragment, a string with a C element.
144              
145             This is used for the group containing the background colours in the resulting
146             SVG.
147              
148             =head3 svg_coordinates($offset)
149              
150             This returns an SVG fragment, a string with a C element.
151              
152             This is used for the group containing the coordinates in the resulting SVG.
153              
154             =head3 svg_label($url, $offset)
155              
156             This returns an SVG fragment, a string with a C element containing two
157             C elements, and possibly an C element: the "glow", the label itself,
158             and possibly a link to the URL.
159              
160             This is used for C (the group containing the labels) in the resulting
161             SVG.
162              
163             =head1 SEE ALSO
164              
165             L uses this class. Internally, it calls C
166             which is implemented by either L or
167             L. Depending on the implementation,
168             L or L are used
169             to implement this class.
170              
171             =cut
172              
173             1;