#!/usr/bin/perl
use strict;
use warnings;
use lib "../adv";
use include;

my ($unit_size, $area) = (2, "squiggle");

# SET UP UI
use HexedUI;
my $ui = create HexedUI {
  Main => {
    Type   => "Map",
    At     => [0, 0],
    Size   => ["100%", "100%"],
    Border => 1,
    Render => sub {
      my $tile = shift;
      my ($char, $color);
      if (ref $tile->{_}) {
        ($char, $color) = $tile->{_}->symbol;
      } elsif ($tile->{color}) {
        $char = $tile->{_};
        $color = $tile->{color};
      } else {
        $char = $tile->{_};
        $color = {
          "#" => "orange",
          "." => "grey"
        }->{$char};
        $color ||= "yellow";
      }
      return ord($char) | HexedUI::paint($color);
    }
  },
};
GAME->{UI} = $ui;

# SET UP MAP
use BDSM::Transform;
my $map = loadmap("../maps/$area");
$ui->{Main}->setup($map);
$map->{OnChange} = sub {
  my ($map, $y, $x, $new) = @_;
  $ui->{Main}->_mod($y, $x, $map->{Map}[$y][$x]);
};

sub run {
  # Initialize units
  use Unit;
  my @units;
  foreach (1 .. $unit_size) {
    my $unit = GAME->{Classes}{Unit}->new(Map => $map, Num => $_);
    if ($_ != 1) {
      # Whilst assembling, just form a line.
      push @{ $unit->{Goals} }, [ $units[-1], 0, +1 ];
    }
    push @units, $unit;
  }

  while (1) {
    $units[1]->magnet;
    GAME->{UI}{Main}->draw;
    GAME->{UI}->input;
  }
}

run;
