UNPKG

warriorjs-engine

Version:
163 lines (139 loc) 5.76 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _directions = require('./constants/directions'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var Position = function () { function Position(floor, x, y) { var direction = arguments.length <= 3 || arguments[3] === undefined ? _directions.NORTH : arguments[3]; _classCallCheck(this, Position); this._floor = floor; this._x = x; this._y = y; this._directionIndex = _directions.DIRECTION_ARRAY.indexOf(direction); } _createClass(Position, [{ key: 'isAt', value: function isAt(x, y) { return this.x === x && this.y === y; } }, { key: 'move', value: function move(forward) { var right = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; var _translateOffset2 = this._translateOffset(forward, right); var _translateOffset3 = _slicedToArray(_translateOffset2, 2); this._x = _translateOffset3[0]; this._y = _translateOffset3[1]; } }, { key: 'rotate', value: function rotate(amount) { this._directionIndex += amount; while (this._directionIndex > 3) { this._directionIndex -= 4; }while (this._directionIndex < 0) { this._directionIndex += 4; } } }, { key: 'getRelativeSpace', value: function getRelativeSpace(forward) { var right = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1]; var _translateOffset4 = this._translateOffset(forward, right); var _translateOffset5 = _slicedToArray(_translateOffset4, 2); var x = _translateOffset5[0]; var y = _translateOffset5[1]; return this.floor.getSpaceAt(x, y); } }, { key: 'getDistanceOf', value: function getDistanceOf(space) { var _space$location = _slicedToArray(space.location, 2); var x = _space$location[0]; var y = _space$location[1]; return Math.abs(this.x - x) + Math.abs(this.y - y); } }, { key: 'getDistanceFromStairs', value: function getDistanceFromStairs() { return this.getDistanceOf(this.floor.stairsSpace); } }, { key: 'getDirectionOf', value: function getDirectionOf(space) { var _space$location2 = _slicedToArray(space.location, 2); var x = _space$location2[0]; var y = _space$location2[1]; if (Math.abs(this.x - x) > Math.abs(this.y - y)) { return x > this.x ? _directions.EAST : _directions.WEST; } return y > this.y ? _directions.SOUTH : _directions.NORTH; } }, { key: 'getRelativeDirection', value: function getRelativeDirection(direction) { var offset = _directions.DIRECTION_ARRAY.indexOf(direction) - this._directionIndex; while (offset > 3) { offset -= 4; }while (offset < 0) { offset += 4; }return _directions.RELATIVE_DIRECTION_ARRAY[offset]; } }, { key: 'getRelativeDirectionOf', value: function getRelativeDirectionOf(space) { return this.getRelativeDirection(this.getDirectionOf(space)); } }, { key: 'getRelativeDirectionOfStairs', value: function getRelativeDirectionOfStairs() { return this.getRelativeDirectionOf(this.floor.stairsSpace); } }, { key: '_translateOffset', value: function _translateOffset(forward, right) { if (this.direction === _directions.NORTH) { return [this.x + right, this.y - forward]; } else if (this.direction === _directions.EAST) { return [this.x + forward, this.y + right]; } else if (this.direction === _directions.SOUTH) { return [this.x - right, this.y + forward]; } else if (this.direction === _directions.WEST) { return [this.x - forward, this.y - right]; } throw new Error('Unknown direction \'' + this.direction + '\''); } }, { key: 'floor', get: function get() { return this._floor; } }, { key: 'x', get: function get() { return this._x; } }, { key: 'y', get: function get() { return this._y; } }, { key: 'space', get: function get() { return this.floor.getSpaceAt(this.x, this.y); } }, { key: 'direction', get: function get() { return _directions.DIRECTION_ARRAY[this._directionIndex]; } }]); return Position; }(); exports.default = Position;