Game-TextMapper

 view release on metacpan or  search on metacpan

lib/Game/TextMapper/Line.pm  view on Meta::CPAN

# Copyright (C) 2009-2022  Alex Schroeder <alex@gnu.org>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

Game::TextMapper::Line - a line between two points

=head1 SYNOPSIS

    use Modern::Perl;
    use Game::TextMapper::Line::Hex;
    use Game::TextMapper::Point::Hex;
    my $line = Game::TextMapper::Line::Hex->new();
    my $from = Game::TextMapper::Point::Hex->new(x => 1, y => 1, z => 0);
    my $to   = Game::TextMapper::Point::Hex->new(x => 5, y => 3, z => 0);
    $line->points([$from, $to]);
    my @line = $line->compute_missing_points;
    say join(" ", map { $_->str } @line);
    # (1,1,0) (2,1,0) (3,2,0) (4,2,0) (5,3,0)

=head1 DESCRIPTION

The line connects two points. This class knows how to compute all the regions
between these two points, how to compute the next region along the line, and how
to output SVG.

In order to do this, the class needs to know how to work with the regions on the
map. This is different for hexes and squares. Therefore you should always be
using the appropriate Hex or Square class instead.

=cut

package Game::TextMapper::Line;

use Modern::Perl '2018';
use Mojo::Util qw(url_escape);
use Mojo::Base -base;

our $debug;

=head1 ATTRIBUTES

=head2 points

An array reference of points using a class derived from
L<Game::TextMapper::Point>, i.e. L<Game::TextMapper::Line::Hex> uses
L<Game::TextMapper::Point::Hex> and L<Game::TextMapper::Line::Square> uses
L<Game::TextMapper::Point::Square>.

=cut

has 'id';
has 'points';
has 'offset';
has 'type';
has 'label';
has 'map';
has 'side';
has 'start';

=head1 METHODS

=head2 compute_missing_points

Compute the missing points between the points in C<points> and return it.

=cut

sub compute_missing_points {
  my $self = shift;
  my $i = 0;
  my $current = $self->points->[$i++];
  my $z = $current->z;
  my @result = ($current);
  while ($self->points->[$i]) {
    $current = $self->one_step($current, $self->points->[$i]);
    return unless $z == $current->z; # must all be on the same plane
    push(@result, $current);
    $i++ if $current->equal($self->points->[$i]);
  }

  return @result;
}

sub partway {
  my ($self, $from, $to, $q) = @_;
  my ($x1, $y1) = $self->pixels($from);
  my ($x2, $y2) = $self->pixels($to);
  $q ||= 1;
  return $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q if wantarray;
  return sprintf("%.1f,%.1f", $x1 + ($x2 - $x1) * $q, $y1 + ($y2 - $y1) * $q);
}

=head2 svg($offset)



( run in 0.767 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )