UNPKG

warriorjs-engine

Version:
217 lines (187 loc) 6.35 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 _dec, _class; var _lodash = require('lodash.camelcase'); var _lodash2 = _interopRequireDefault(_lodash); var _lodash3 = require('lodash.startcase'); var _lodash4 = _interopRequireDefault(_lodash3); var _viewObject = require('../decorators/viewObject'); var _viewObject2 = _interopRequireDefault(_viewObject); var _Turn = require('../Turn'); var _Turn2 = _interopRequireDefault(_Turn); var _Logger = require('../Logger'); var _Logger2 = _interopRequireDefault(_Logger); 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 = { name: function name() { return this.name; }, type: function type() { return this.type; }, health: function health() { return this.health; }, x: function x() { return this.position.x; }, y: function y() { return this.position.y; }, facing: function facing() { return this.position.direction; } }; var Unit = (_dec = (0, _viewObject2.default)(viewObjectShape), _dec(_class = function () { function Unit() { _classCallCheck(this, Unit); this._position = null; this._attackPower = 0; this._shootPower = 0; this._maxHealth = 0; this._health = null; this._abilities = {}; this._currentTurn = null; } _createClass(Unit, [{ key: 'isAlive', value: function isAlive() { return this.position !== null; } }, { key: 'isBound', value: function isBound() { return this._bound; } }, { key: 'unbind', value: function unbind() { this._bound = false; this.say('released from bonds'); } }, { key: 'bind', value: function bind() { this._bound = true; } }, { key: 'say', value: function say(message) { _Logger2.default.unitSpoke(this.name + ' ' + message, this.type); } }, { key: 'takeDamage', value: function takeDamage(amount) { if (this.isBound()) { this.unbind(); } if (this.health) { var revisedAmount = this.health - amount < 0 ? this.health : amount; this.health -= revisedAmount; this.say('takes ' + revisedAmount + ' damage, ' + this.health + ' health power left'); if (!this.health) { this.say('dies'); this.position = null; } } } }, { key: 'playTurn', value: function playTurn(turn) {// eslint-disable-line no-unused-vars // To be overriden by subclass } }, { key: 'prepareTurn', value: function prepareTurn() { this._currentTurn = this._nextTurn(); this.playTurn(this._currentTurn); } }, { key: 'performTurn', value: function performTurn() { if (this.isAlive()) { Object.values(this.abilities).forEach(function (ability) { return ability.passTurn(); }); if (this._currentTurn.action && !this.isBound()) { var _abilities$name; var _currentTurn$action = _slicedToArray(this._currentTurn.action, 2); var name = _currentTurn$action[0]; var args = _currentTurn$action[1]; (_abilities$name = this.abilities[name]).perform.apply(_abilities$name, _toConsumableArray(args)); } } } }, { key: 'earnPoints', value: function earnPoints(points) {// eslint-disable-line no-unused-vars // To be overriden by subclass } }, { key: 'toString', value: function toString() { return this.name; } }, { key: '_nextTurn', value: function _nextTurn() { return new _Turn2.default(this.abilities); } }, { key: 'name', get: function get() { return (0, _lodash4.default)(this.constructor.name); } }, { key: 'type', get: function get() { return (0, _lodash2.default)(this.constructor.name); } }, { key: 'position', get: function get() { return this._position; }, set: function set(position) { this._position = position; } }, { key: 'attackPower', get: function get() { return this._attackPower; } }, { key: 'shootPower', get: function get() { return this._shootPower; } }, { key: 'maxHealth', get: function get() { return this._maxHealth; } }, { key: 'health', get: function get() { this._health = this._health === null ? this._maxHealth : this._health; return this._health; }, set: function set(health) { this._health = health; } }, { key: 'abilities', get: function get() { return this._abilities; } }]); return Unit; }()) || _class); exports.default = Unit;