Another DK Test

by Sam Fereday

HTML

<script src="https://rawgit.com/prettymuchbryce/easystarjs/v0.3.1/bin/easystar-0.3.1.min.js"></script>
<button id="start">
  START
</button>

JavaScript

// A simple implementation of an Imp runnning a tile if reachable.
// Next up: Job queueing.
console.clear();

// Path finding bit
var es = new EasyStar.js();

var pather = {

  // Config
  pathInterval: 500,
  nextInterval: 0,

  getNewPath: function(x1, y1, x2, y2, cb) {

    var npath = [];

    // moving entity, start x, y, end x, y
    es.findPath(x1, y1, x2, y2, function(path) {

      if (path === null) {

        console.log("The path to the destination point was not found.");
        return {
          nopath: true
        };

      } else {

        for (var i = 0; i < path.length; i++) {
          // console.log("P: " + i + ", X: " + path[i].x + ", Y: " + path[i].y);
          npath.push({
            x: path[i].x,
            y: path[i].y
          });
        }

      }

      if (path && typeof cb === 'function')
        cb(path);

    });

  },

  update: function() {
      es.calculate();
  },

  initialize: function(arr) {

    // Pathfinding Setup
    es.setGrid(arr);
    es.setAcceptableTiles([0]);
    es.enableDiagonals();

  }

};

function findPointForEntity(position, entity, area) {

            var tile = area.worldData.map.getTileWorldXY(position.x, position.y);

            if (!tile)
                return { nopath: true };

            var targetDataAtLocation = area.getEntityAt(tile.x, tile.y),
                self = this;

            var cardinals = [{
                    x: tile.x,
                    y: tile.y - 1
                },
                {
                    x: tile.x + 1,
                    y: tile.y
                },
                {
                    x: tile.x,
                    y: tile.y + 1
                },
                {
                    x: tile.x - 1,
                    y: tile.y
                }
            ];

            var entityCoord = {
                x: Math.ceil(entity.x / tile.width),
                y: Math.ceil(entity.y / tile.height)
            };

            var closest = null,
...