UNPKG

@irysius/grid-math

Version:

Tools to assist with grid math and algorithms

146 lines (145 loc) 4.97 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "../CellCoord", "../Vector2", "../Direction", "../helpers/Iterable"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const CellCoord_1 = require("../CellCoord"); const Vector2_1 = require("../Vector2"); const Direction_1 = require("../Direction"); const Iterable_1 = require("../helpers/Iterable"); function simpleSquare(start) { let steps = [ start, Vector2_1.add(start, CellCoord_1.create(0, 1)), Vector2_1.add(start, CellCoord_1.create(1, 1)), Vector2_1.add(start, CellCoord_1.create(1, 0)) ]; return steps; } exports.simpleSquare = simpleSquare; // paths should only move forward. // if you need a reverse path, provide reversed steps // To support forward and backwards // interface IWrapOptions { // over(): void; // under(): void; // } // function wrapIndex(count: number, options?: IWrapOptions) { // return function (index: number) { // if (index >= count) { // if (options) { options.over(); } // return 0; // } // if (index < 0) { // if (options) { options.under(); } // return count - 1; // } // return index; // }; // } /** * Returns an iterator for the steps provided. * @param steps * @param timesToLoop Number of times to loop through the steps. For infinite loop, set this to 0. */ function createPath(steps, timesToLoop = 0) { function* inner() { let index = 0, loopCount = 0, stepCount = steps.length; let cond = () => { return timesToLoop > 0 ? (loopCount < timesToLoop) : true; }; while (cond()) { yield steps[index]; index++; if (index >= stepCount) { index = 0; if (timesToLoop > 0) { loopCount++; } } } } return inner(); } exports.createPath = createPath; // Don't need this anymore... function getDirection(before, after) { if (!after) { return Direction_1.default.None; } let delta = Vector2_1.subtract(after, before); let ew = (function () { if (delta.x > 0) { return Direction_1.default.East; } else if (delta.x < 0) { return Direction_1.default.West; } else { return Direction_1.default.None; } })(); let ns = (function () { if (delta.y > 0) { return Direction_1.default.South; } else if (delta.y < 0) { return Direction_1.default.North; } else { return Direction_1.default.None; } })(); return ew | ns; } function test() { let path = createPath(simpleSquare(CellCoord_1.create(0, 0)), 2); let mw = Iterable_1.SlidingWindow(path, { prevCapacity: 0, nextCapacity: 1 }); mw.currValue; mw.nextValues[0]; let sample = mw; function mwLoop() { mw.next(); let d = getDirection(mw.currValue, mw.nextValues[0]); console.log(Direction_1.directionToString(d)); } // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); // mwLoop(); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); console.log(sample.next()); } exports.test = test; });