UNPKG

warriorjs-engine

Version:
155 lines (135 loc) 4.25 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; 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 _dec, _class; var _lodash = require('lodash.uniqby'); var _lodash2 = _interopRequireDefault(_lodash); var _viewObject = require('./decorators/viewObject'); var _viewObject2 = _interopRequireDefault(_viewObject); var _Position = require('./Position'); var _Position2 = _interopRequireDefault(_Position); var _Space = require('./Space'); var _Space2 = _interopRequireDefault(_Space); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var viewObjectShape = { size: { width: function width() { return this.width; }, height: function height() { return this.height; } }, stairs: { x: function x() { return this.stairsLocation[0]; }, y: function y() { return this.stairsLocation[1]; } }, warrior: function warrior() { return this.warrior; }, units: function units() { return this.otherUnits; } }; var Floor = (_dec = (0, _viewObject2.default)(viewObjectShape), _dec(_class = function () { function Floor(width, height) { _classCallCheck(this, Floor); this._stairsLocation = [-1, -1]; this._units = []; this._width = width; this._height = height; } _createClass(Floor, [{ key: 'placeStairs', value: function placeStairs(x, y) { this._stairsLocation = [x, y]; } }, { key: 'addUnit', value: function addUnit(unit, x, y, direction) { var positionedUnit = unit; positionedUnit.position = new _Position2.default(this, x, y, direction); this._units.push(positionedUnit); } }, { key: 'getSpaceAt', value: function getSpaceAt(x, y) { return new _Space2.default(this, x, y); } }, { key: 'getUnitAt', value: function getUnitAt(x, y) { return this.units.find(function (unit) { return unit.position.isAt(x, y); }); } }, { key: 'isOutOfBounds', value: function isOutOfBounds(x, y) { return x < 0 || y < 0 || x > this.width - 1 || y > this.height - 1; } }, { key: 'width', get: function get() { return this._width; }, set: function set(width) { this._width = width; } }, { key: 'height', get: function get() { return this._height; }, set: function set(height) { this._height = height; } }, { key: 'stairsLocation', get: function get() { return this._stairsLocation; } }, { key: 'stairsSpace', get: function get() { return this.getSpaceAt.apply(this, _toConsumableArray(this.stairsLocation)); } }, { key: 'units', get: function get() { return this._units.filter(function (unit) { return unit.position; }); } }, { key: 'warrior', get: function get() { return this.units.find(function (unit) { return unit.type === 'warrior'; }); } }, { key: 'otherUnits', get: function get() { return this.units.filter(function (unit) { return unit.type !== 'warrior'; }); } }, { key: 'uniqueUnits', get: function get() { return (0, _lodash2.default)(this.units, 'type'); } }]); return Floor; }()) || _class); exports.default = Floor;