warriorjs-engine
Version:
The bowels of WarriorJS
234 lines (200 loc) • 7 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _bind = Function.prototype.bind;
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; }; })();
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 _lodashCamelcase = require('lodash.camelcase');
var _lodashCamelcase2 = _interopRequireDefault(_lodashCamelcase);
var _lodashStartcase = require('lodash.startcase');
var _lodashStartcase2 = _interopRequireDefault(_lodashStartcase);
var _constantsAbilities = require('../constants/abilities');
var _decoratorsViewObject = require('../decorators/viewObject');
var _Turn = require('../Turn');
var _Turn2 = _interopRequireDefault(_Turn);
var _Logger = require('../Logger');
var _Logger2 = _interopRequireDefault(_Logger);
var viewObjectShape = {
name: function name() {
return this.name;
},
type: function type() {
return this.type;
},
x: function x() {
return this.position.x;
},
y: function y() {
return this.position.y;
},
facing: function facing() {
return this.position.direction;
}
};
var Unit = (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'].log(this.type, this.name + ' ' + message);
}
}, {
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.position = null;
this.say('dies');
}
}
}
}, {
key: 'addAbilities',
value: function addAbilities(newAbilities) {
var _this = this;
Object.entries(newAbilities).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2);
var name = _ref2[0];
var args = _ref2[1];
if (!(name in _constantsAbilities.ABILITIES)) {
throw new Error('Unknown ability \'' + name + '\'.');
}
_this._abilities[name] = new (_bind.apply(_constantsAbilities.ABILITIES[name], [null].concat([_this], _toConsumableArray(args))))();
});
}
}, {
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, _lodashStartcase2['default'])(this.constructor.name);
}
}, {
key: 'type',
get: function get() {
return (0, _lodashCamelcase2['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;
}
}]);
var _Unit = Unit;
Unit = (0, _decoratorsViewObject.viewObject)(viewObjectShape)(Unit) || Unit;
return Unit;
})();
exports['default'] = Unit;
module.exports = exports['default'];