Check for bad edge direction and abort path

main
Daniel Asher Resnick 11 months ago
parent ddb01005c8
commit 3b086b0ebe
  1. 38
      HexGrid/Path.pm

@ -1,3 +1,7 @@
=head1 HexGrid::Path
=cut
package HexGrid::Path; package HexGrid::Path;
use v5.30; use v5.30;
@ -34,14 +38,12 @@ sub get_edge_direction($tile1, $tile2)
return $HexGrid::DIR{ne} if $nw_diff == 0 && $sw_diff == -1; return $HexGrid::DIR{ne} if $nw_diff == 0 && $sw_diff == -1;
return $HexGrid::DIR{n} if $nw_diff == 1 && $sw_diff == -1; return $HexGrid::DIR{n} if $nw_diff == 1 && $sw_diff == -1;
#TODO: render and splinter should check this returns successfully;
carp("Tiles are not adjacent: " . $tile1->nw . "," . $tile1->sw . "—" carp("Tiles are not adjacent: " . $tile1->nw . "," . $tile1->sw . "—"
. $tile2->nw . "," . $tile2->sw); . $tile2->nw . "," . $tile2->sw);
return undef; return undef;
} }
# Instance # Instance
sub clone_settings($this) sub clone_settings($this)
@ -90,7 +92,13 @@ sub splinter($this, $grid)
# Don't set source on first tile # Don't set source on first tile
if($i >= 1) if($i >= 1)
{ {
$splinter->{starts_from} = get_edge_direction($this->{tiles}[$i], $this->{tiles}[$i-1]); my $starting_edge = get_edge_direction($this->{tiles}[$i], $this->{tiles}[$i-1]);
unless($starting_edge)
{
carp("Path " . $this->{id} . " has non-adjacent edges, aborting.");
return;
}
$splinter->{starts_from} = $starting_edge;
} }
push @{$splinter->tiles}, $this->{tiles}[$i]; push @{$splinter->tiles}, $this->{tiles}[$i];
push @splinters, $splinter; push @splinters, $splinter;
@ -108,8 +116,14 @@ sub splinter($this, $grid)
{ {
# In a splinter but tile not present, set previous tile sink to this missing tile # In a splinter but tile not present, set previous tile sink to this missing tile
$in_splinter = 0; $in_splinter = 0;
$splinters[$#splinters]{ends_to} = my $ending_edge = get_edge_direction($this->{tiles}[$i-1], $this->{tiles}[$i]);
get_edge_direction($this->{tiles}[$i-1], $this->{tiles}[$i]); unless($ending_edge)
{
carp("Path " . $this->{id} . " has non-adjacent edges, aborting.");
return;
}
$splinters[$#splinters]{ends_to} = $ending_edge;
} }
} }
} }
@ -178,6 +192,11 @@ sub render($this, $grid, $svg)
my $previous_tile = $current_tile; my $previous_tile = $current_tile;
$current_tile = shift @tiles; $current_tile = shift @tiles;
my $next_edge = get_edge_direction($previous_tile, $current_tile); my $next_edge = get_edge_direction($previous_tile, $current_tile);
unless($next_edge)
{
carp("Path " . $this->{id} . " has non-adjacent edges, aborting.");
return;
}
($x, $y) = $grid->coords_of_edge($previous_tile->nw, $previous_tile->sw, $next_edge); ($x, $y) = $grid->coords_of_edge($previous_tile->nw, $previous_tile->sw, $next_edge);
if($this->starts_from) if($this->starts_from)
{ {
@ -199,6 +218,11 @@ sub render($this, $grid, $svg)
$next_tile = shift @tiles; $next_tile = shift @tiles;
$previous_edge = -$next_edge; $previous_edge = -$next_edge;
$next_edge = get_edge_direction($current_tile, $next_tile); $next_edge = get_edge_direction($current_tile, $next_tile);
unless($next_edge)
{
carp("Path " . $this->{id} . " has non-adjacent edges, aborting.");
return;
}
my ($qx,$qy) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw); my ($qx,$qy) = $grid->coords_of_centre($current_tile->nw, $current_tile->sw);
($x,$y) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $next_edge); ($x,$y) = $grid->coords_of_edge($current_tile->nw, $current_tile->sw, $next_edge);
@ -228,4 +252,8 @@ sub render($this, $grid, $svg)
return $g; return $g;
} }
=back
=cut
1; 1;

Loading…
Cancel
Save