mnml/client/src/scenes/combat.render.resolutions.js
2018-12-05 17:07:26 +10:00

106 lines
3.7 KiB
JavaScript
Executable File

const { eachSeries } = require('async');
const CombatAnimations = require('./combat.animations');
const {
DELAYS: { ANIMATION_DURATION, MOVE_CREEP, DAMAGE_TICK },
POSITIONS: { COMBAT },
} = require('./constants');
const randomAnimation = () => {
const animations = ['wall', 'spit', 'gravBlast', 'gravBomb', 'chargeBall'];
return animations[Math.floor(Math.random() * 5)];
};
function findResolutionCryps(scene, group, resolution, allyTeam, enemyTeam) {
const allyCryp = allyTeam.cryps.find(
c => c.id === resolution.source_cryp_id || c.id === resolution.target_cryp_id
);
const allySpawn = group.children.entries
.find(c => c.cryp.id === allyCryp.id);
const enemyCryp = enemyTeam.cryps.find(
c => c.id === resolution.source_cryp_id || c.id === resolution.target_cryp_id
);
const enemySpawn = group.children.entries
.find(c => c.cryp.id === enemyCryp.id);
const target = allyCryp.id === resolution.target_cryp_id ? allySpawn : enemySpawn;
return { allySpawn, enemySpawn, target };
}
function animatePhase(scene, group, game, resolution, cb) {
// Iterate the log
// return early for disabled skills
if (resolution.resolution.disable.disabled) return cb();
const animations = new CombatAnimations(scene);
// Find cryps and targets
const tweenParams = (targets, centreSpot, enemy) => {
let x = centreSpot ? COMBAT.width() * 0.3 : targets.x;
x = (enemy && centreSpot) ? x + COMBAT.width() * 0.4 : x;
const y = centreSpot ? COMBAT.height() * 13.25 / 35 : targets.y;
const ease = 'Power1';
const duration = MOVE_CREEP;
return { targets, x, y, ease, duration };
};
// find the teams
const account = scene.registry.get('account');
const allyTeam = game.teams.find(t => t.id === account.id);
// in future there will be more than one
const [enemyTeam] = game.teams.filter(t => t.id !== account.id);
const { allySpawn, enemySpawn, target } = findResolutionCryps(scene, group, resolution, allyTeam, enemyTeam);
const moveAllyBattle = tweenParams(allySpawn, true, false);
const moveAllyOrig = tweenParams(allySpawn, false, false);
const moveEnemyBattle = tweenParams(enemySpawn, true, true);
const moveEnemyOrig = tweenParams(enemySpawn, false, true);
// Move cryps into posistion
scene.tweens.add(moveAllyBattle);
scene.tweens.add(moveEnemyBattle);
// animate animation
const animation = randomAnimation();
scene.time.delayedCall(MOVE_CREEP, () => {
const isAlly = resolution.target_team_id !== account.id;
animations[animation](isAlly);
// Target cryp takes damage
scene.time.delayedCall(ANIMATION_DURATION, () => {
const damage = resolution.resolution.results[0][1].amount;
target.takeDamage(damage);
scene.registry.set('gameLog', scene.registry.get('gameLog') + 1);
// Move cryps back
scene.time.delayedCall(DAMAGE_TICK, () => {
scene.tweens.add(moveAllyOrig);
scene.tweens.add(moveEnemyOrig);
// all done
scene.time.delayedCall(MOVE_CREEP, () => {
animations.destroy(true);
return cb();
});
});
});
});
}
function renderResolutions(scene, game, group, resolutions) {
scene.registry.set('gameAnimating', true);
eachSeries(
resolutions,
(resolution, cb) => animatePhase(scene, group, game, resolution, cb),
(err) => {
if (err) return console.error(err);
scene.registry.set('gameAnimating', false);
return true;
}
);
return true;
}
module.exports = renderResolutions;