RailRoad/Node.pm

filter: Statementstmt 33.8% 23 / 68 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 47.0% 8 / 17 Podpod 100% 7 / 7 totaltotal 35.5% 38 / 107 CC 9 SCAR 19.8
CC
9
Cov
28%
connect
20.0
draw
12.0
_draw_segment
12.0
CRAP 7.2
↑ Summary RailRoad/Vector.pm »
1 #
2 # This file is part of Games-RailRoad
3 #
4 # This software is copyright (c) 2008 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 46 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Games::RailRoad::Node;
14 46 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 46 $Games::RailRoad::Node::VERSION = '1.101330';
16 }
17 # ABSTRACT: a node object
18  
19 46 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 46 ▶use MooseX::Has::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 46 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 46 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # -- attributes
26  
27  
28 46 has position => ( ro, isa=>'Games::RailRoad::Vector', required );
29  
30  
31 # -- constructor & initializers
32  
33  
34 # provided by moose
35  
36  
37 # -- public methods
38  
39  
40 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
41 0 my ($self, $dir) = @_;
42  
43 # check if the node can be extended in the wanted $dir.
44 0 my $map = $self->_transform_map;
45 0 ▶ return unless exists $map->{$dir};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map->{$dir} 0 0 0
46  
47 # rebless the object in its new class.
48 0 $map->{$dir}->require;
49 0 bless $self, $map->{$dir};
50  
51 # initialize switch if needed.
52 # FIXME: shouldn't it be it GRN:Switch:_init with an inconditional
53 # call to _init?
54 0 ▶ if ( $self->isa('Games::RailRoad::Node::Switch')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Games::RailRoad::Node::Switch") and not defined $self->_switch) 0 0 0
Condition0%0/3
$self->isa("Games::RailRoad::Node::Switch") and not defined $self->_switch
!ll&&!rl&&r
000
MC/DC0%0/2
$self->isa("Games::RailRoad::Node::Switch") and not defined $self->_switch
$self->isa("Games::RailRoad::Node::Switch") not defined $self->_switch
Truth table0%0/3
A and not(B)
A= $self->isa("Games::RailRoad::Node::Switch")B= not defined $self->_switch
ABresult
0X0
100
111
55 && not defined $self->_switch ) {
56 0 $self->_switch(0);
57 }
58 }
59  
60  
61  
62 0 ▶sub connectable {
Subroutine connectable: not called CC=1 SCAR=6.9
Pod documented
63 0 my ($self, $dir) = @_;
64 0 my $map = $self->_transform_map;
65 0 return exists $map->{$dir};
66 }
67  
68  
69  
70 0 ▶sub connections {
Subroutine connections: not called CC=2 SCAR=17.9
Pod documented
71 0 my ($self) = @_;
72 0 my $pkg = ref $self;
73 0 ▶ return () if $pkg eq 'Games::RailRoad::Node';
Branch: 1 branch
BranchTrueFalseTotal
if $pkg eq "Games::RailRoad::Node" 0 0 0
74 0 $pkg =~ s/^.*:://;
75 0 return map { lc $_ } split /_/, $pkg;
76 }
77  
78  
79  
80 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
81 0 my ($self, $canvas) = @_;
82 0 my $pos = $self->position;
83 0 $canvas->delete("$pos");
84 }
85  
86  
87  
88 0 ▶sub draw {
Subroutine draw: not called CC=3 SCAR=24.8
Pod documented
89 0 my ($self, $canvas, $tilelen) = @_;
90 0 $self->delete($canvas);
91  
92 0 my $class = ref $self;
93 0 $class =~ s/^.*:://;
94 0 ▶ return if $class eq 'Node'; # naked node
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Node" 0 0 0
95 $self->_draw_segment(lc($_), $canvas, $tilelen)
96 0 foreach split /_/, $class;
97 }
98  
99  
100  
101 0 ▶sub next_dir {
Subroutine next_dir: not called CC=1 SCAR=6.9
Pod documented
102 0 my ($self, $from) = @_;
103 # each node class is defining a _next_map() method that returns a
104 # hashref of {from=>to}
105 0 return $self->_next_map->{$from};
106 }
107  
108  
109  
110 0 ▶sub switch {}
Subroutine switch: not called CC=1 SCAR=0.0
Pod documented
111  
112  
113 # -- private methods
114  
115 #
116 # $node->_draw_segment( $segment, $canvas, $tilelen )
117 #
118 # draw $segment of $node (at the correct col / row) on $canvas, assuming
119 # a square length of $tilelen. $segment can be one of nw, n, ne, w, e,
120 # sw, s, se.
121 #
122 0 ▶sub _draw_segment {
Subroutine _draw_segment: not called CC=3 SCAR=24.8
123 0 my ($self, $segment, $canvas, $tilelen) = @_;
124  
125 0 my $pos = $self->position;
126 0 my $col1 = $pos->posx;
127 0 my $row1 = $pos->posy;
128 0 my ($col2, $row2) = ($col1, $row1);
129  
130 # since each node is overlapping with the surrounding ones, we just
131 # need to draw half of the segments.
132 0 ▶ return unless $segment ~~ [ qw{ e sw s se } ];
Branch: 1 branch
BranchTrueFalseTotal
unless $segment ~~ ["e", "sw", "s", "se"] 0 0 0
133 0 my $move = Games::RailRoad::Vector->new_dir($segment);
134 0 my $end = $pos + $move;
135  
136 # create the line.
137 0 my $tags = [ "$pos", "$pos-$end" ];
138 0 $canvas->createLine(
139 $tilelen * $pos->posx, $tilelen * $pos->posy,
140 $tilelen * $end->posx, $tilelen * $end->posy,
141 -tags=>$tags
142 );
143  
144 # add some fancy drawing
145 0 my $div = 3;
146 0 my $radius = 1;
147 0 foreach my $i ( 0 .. $div ) {
148 0 my $x = $tilelen * ( $pos->posx + $move->posx * $i / $div );
149 0 my $y = $tilelen * ( $pos->posy + $move->posy * $i / $div );
150 0 $canvas->createOval(
151 $x-$radius, $y-$radius,
152 $x+$radius, $y+$radius,
153 -fill => 'brown',
154 -tags => $tags,
155 );
156 }
157  
158 }
159  
160  
161 #
162 # my $map = $node->_transform_map;
163 #
164 # return a hashref, which keys are the directions where the node can be
165 # extended, and the values are the new class of the node after being
166 # extended.
167 #
168 0 ▶sub _transform_map {
Subroutine _transform_map: not called CC=1 SCAR=6.9
169 0 my $prefix = 'Games::RailRoad::Node::';
170 return {
171 0 'e' => $prefix . 'Half::E',
172 'n' => $prefix . 'Half::N',
173 'ne' => $prefix . 'Half::NE',
174 'nw' => $prefix . 'Half::NW',
175 's' => $prefix . 'Half::S',
176 'se' => $prefix . 'Half::SE',
177 'sw' => $prefix . 'Half::SW',
178 'w' => $prefix . 'Half::W',
179 };
180 }
181  
182  
183  
184 46 1;
185  
186  
187 =pod
188  
189 =head1 NAME
190  
191 Games::RailRoad::Node - a node object
192  
193 =head1 VERSION
194  
195 version 1.101330
196  
197 =head1 DESCRIPTION
198  
199 This module provides a node object. This is the base class for the
200 following classes:
201  
202 =over 4
203  
204 =item *
205  
206 L<Games::RailRoad::Node::Half> is a node with only one segment, from the
207 center to one of the 8 extremities of a square.
208  
209 =item *
210  
211 L<Games::RailRoad::Node::Straight> is a node with two segments, linking
212 two of the 8 extremities of a square.
213  
214 =item *
215  
216 L<Games::RailRoad::Node::Switch> is a node with three segments, linking
217 three of the 8 extremities of a square through the center. The I<active>
218 segment taken by a train riding this node can switch between two of the
219 segments.
220  
221 =item *
222  
223 L<Games::RailRoad::Node::Cross> is a node with four segments: two
224 straight lines crossing in the center of the square.
225  
226 =back
227  
228 Each of those classes also has subclasses, one for each configuration
229 allowed. They are named after each of the existing extremity of the
230 square linked (in uppercase), sorted and separated by underscore (C<_>).
231 For example: L<Games::RailRoad::Node::Switch::N_S_SE>.
232  
233 Note that each segment coming out of a node belongs to 2 different
234 (adjacent) nodes.
235  
236 =head1 ATTRIBUTES
237  
238 =head2 position
239  
240 The node position (a L<Games::RailRoad::Vector>).
241  
242 =head1 METHODS
243  
244 =head2 my $node = Games::RailRoad::Node->new( \%opts );
245  
246 Create a new node object. One can pass a hash reference with the
247 available attributes.
248  
249 =head2 $node->connect( $dir );
250  
251 Try to extend C<$node> in the wanted C<$dir>. Return undef if it isn't
252 possible. In practice, note that the object will change of base class.
253  
254 C<$dir> should be one of C<nw>, C<n>, C<ne>, C<w>, C<e>, C<sw>, C<s>,
255 C<se>. Of course, other values are accepted but won't result in a node
256 extension.
257  
258 =head2 $node->connectable( $dir );
259  
260 Return true if C<$node> can be connected to the wanted C<$dir>. Return
261 false otherwise.
262  
263 C<$dir> should be one of C<nw>, C<n>, C<ne>, C<w>, C<e>, C<sw>, C<s>,
264 C<se>. Of course, other values are accepted but will always return
265 false.
266  
267 =head2 my @dirs = $node->connections;
268  
269 Return a list of dirs in which the node is connected.
270  
271 =head2 $node->delete( $canvas );
272  
273 Request C<$node> to remove itself from C<$canvas>.
274  
275 =head2 $node->draw( $canvas, $tilelen );
276  
277 Request C<$node> to draw itself on C<$canvas>, assuming that each square
278 has a length of C<$tilelen>. Note that this method calls the C<delete()>
279 method first.
280  
281 =head2 my $to = $node->next_dir( $from );
282  
283 When C<$node> is reached by a train, this method will return the next
284 direction to head to, assuming the train was coming from C<$from>.
285  
286 Note that the method can return undef if there's no such C<$from>
287 configured, or if the node is a dead-end.
288  
289 =head2 $node->switch;
290  
291 Request a node to change its exit, if possible. This is a no-op for most
292 nodes, except C<Games::Railroad::Node::Switch::*>.
293  
294 =head1 AUTHOR
295  
296 Jerome Quelin
297  
298 =head1 COPYRIGHT AND LICENSE
299  
300 This software is copyright (c) 2008 by Jerome Quelin.
301  
302 This is free software; you can redistribute it and/or modify it under
303 the same terms as the Perl 5 programming language system itself.
304  
305 =cut
306  
307  
308 __END__
↑ Summary RailRoad/Vector.pm »