@warriorjs/core
Version:
WarriorJS core
122 lines (102 loc) • 3.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _Floor = require('./Floor');
var _Floor2 = _interopRequireDefault(_Floor);
var _Level = require('./Level');
var _Level2 = _interopRequireDefault(_Level);
var _Unit = require('./Unit');
var _Unit2 = _interopRequireDefault(_Unit);
var _Warrior = require('./Warrior');
var _Warrior2 = _interopRequireDefault(_Warrior);
var _loadPlayer = require('./loadPlayer');
var _loadPlayer2 = _interopRequireDefault(_loadPlayer);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Loads the abilities onto the unit.
*
* @param {Unit} unit The unit.
* @param {Object} abilities The abilities to load.
*/
function loadAbilities(unit, abilities = {}) {
Object.entries(abilities).forEach(([abilityName, abilityCreator]) => {
const ability = abilityCreator(unit);
unit.addAbility(abilityName, ability);
});
}
/**
* Loads the effects onto the unit.
*
* @param {Unit} unit The unit.
* @param {Object} effects The effects to load.
*/
function loadEffects(unit, effects = {}) {
Object.entries(effects).forEach(([effectName, effectCreator]) => {
const effect = effectCreator(unit);
unit.addEffect(effectName, effect);
});
}
/**
* Loads the warrior.
*
* @param {Object} warriorConfig The config of the warrior.
* @param {Floor} floor The floor of the level.
* @param {string} [playerCode] The code of the player.
*/
function loadWarrior({ name, character, color, maxHealth, abilities, effects, position }, floor, playerCode) {
const warrior = new _Warrior2.default(name, character, color, maxHealth);
loadAbilities(warrior, abilities);
loadEffects(warrior, effects);
warrior.playTurn = playerCode ? (0, _loadPlayer2.default)(playerCode) : () => {};
floor.addWarrior(warrior, position);
}
/**
* Loads a unit.
*
* @param {Object} unitConfig The config of the unit.
* @param {Floor} floor The floor of the level.
*/
function loadUnit({
name,
character,
color,
maxHealth,
reward,
enemy,
bound,
abilities,
effects,
playTurn,
position
}, floor) {
const unit = new _Unit2.default(name, character, color, maxHealth, reward, enemy, bound);
loadAbilities(unit, abilities);
loadEffects(unit, effects);
unit.playTurn = playTurn;
floor.addUnit(unit, position);
}
/**
* Loads a level from a level config.
*
* @param {Object} levelConfig The config of the level.
* @param {string} [playerCode] The code of the player.
*
* @returns {Level} The loaded level.
*/
function loadLevel({
number,
description,
tip,
clue,
floor: { size, stairs, warrior, units = [] }
}, playerCode) {
const { width, height } = size;
const stairsLocation = [stairs.x, stairs.y];
const floor = new _Floor2.default(width, height, stairsLocation);
loadWarrior(warrior, floor, playerCode);
units.forEach(unit => loadUnit(unit, floor));
return new _Level2.default(number, description, tip, clue, floor);
}
exports.default = loadLevel;
module.exports = exports.default;
;