107 lines
4.0 KiB
JavaScript
Executable File
107 lines
4.0 KiB
JavaScript
Executable File
const Phaser = require('phaser');
|
|
const fs = require('fs');
|
|
const { throttle } = require('lodash');
|
|
|
|
const CombatLog = require('./combat.log');
|
|
const CombatCryps = require('./combat.cryps');
|
|
const CombatSkills = require('./combat.skills');
|
|
const renderResolutions = require('./combat.render.resolutions');
|
|
|
|
|
|
class Combat extends Phaser.Scene {
|
|
constructor() {
|
|
super({ key: 'Combat' });
|
|
}
|
|
|
|
preload() {
|
|
// Textures already loaded can do proper check in future when theres more textures
|
|
if (!(this.textures.getTextureKeys().length > 2)) {
|
|
this.textures.addBase64('alk', `data:image/png;base64,${new Buffer.from(fs.readFileSync('./assets/alakazam-f.png')).toString('base64')}`);
|
|
this.textures.addBase64('magmar', `data:image/png;base64,${new Buffer.from(fs.readFileSync('./assets/magmar.png')).toString('base64')}`);
|
|
this.load.image('proj', 'https://labs.phaser.io/assets/sprites/bullet.png');
|
|
this.load.image('blue', 'https://labs.phaser.io/assets/particles/blue.png');
|
|
this.load.image('green', 'https://labs.phaser.io/assets/particles/green.png');
|
|
this.load.image('red', 'https://labs.phaser.io/assets/particles/red.png');
|
|
this.load.image('white', 'https://labs.phaser.io/assets/particles/white.png');
|
|
this.load.image('yellow', 'https://labs.phaser.io/assets/particles/yellow.png');
|
|
}
|
|
}
|
|
|
|
create() {
|
|
this.registry.events.off('changedata', this.updateData);
|
|
this.registry.events.on('changedata', this.updateData, this);
|
|
this.input.keyboard.on('keydown_S', () => {
|
|
this.endGame();
|
|
}, 0, this);
|
|
this.registry.set('gamePhase', false);
|
|
this.registry.set('gameAnimating', false);
|
|
this.account = this.registry.get('account');
|
|
this.fetchGame = throttle(() => {
|
|
const game = this.registry.get('game');
|
|
if (game) {
|
|
const ws = this.registry.get('ws');
|
|
return ws.sendGameState(game.id);
|
|
}
|
|
return false;
|
|
}, 1000);
|
|
return true;
|
|
}
|
|
|
|
startGame(game) {
|
|
this.scene.manager.add('CombatCryps', CombatCryps, true, game);
|
|
this.scene.manager.add('CombatLog', CombatLog, true, game);
|
|
this.scene.manager.add('CombatSkills', CombatSkills, true, game);
|
|
this.renderedResolves = game.resolved.length; // In case you rejoin mid way
|
|
this.registry.set('gamePhase', game.phase);
|
|
return true;
|
|
}
|
|
|
|
endGame() {
|
|
this.scene.switch('CrypList'); // Switch back to cryp list
|
|
this.registry.set('game', null);
|
|
this.scene.get('CombatLog').cleanUp();
|
|
this.scene.get('CombatCryps').cleanUp();
|
|
this.scene.get('CombatSkills').cleanUp();
|
|
this.scene.remove();
|
|
return true;
|
|
}
|
|
|
|
update() {
|
|
this.fetchGame();
|
|
return true;
|
|
}
|
|
|
|
updateData(parent, key, data) {
|
|
if (key === 'game') {
|
|
if (!data) return false;
|
|
const startGame = this.registry.get('gamePhase') === false;
|
|
if (startGame) { this.startGame(data); return true; }
|
|
this.checkAnimation(data);
|
|
// Game over?
|
|
if (data.phase === 'Finish') {
|
|
this.time.delayedCall(10000, () => {
|
|
this.endGame();
|
|
});
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
checkAnimation(game) {
|
|
// do we need to render resolution animations?
|
|
const isAnimating = this.registry.get('gameAnimating');
|
|
if (isAnimating) return false;
|
|
if (game.resolved.length !== this.renderedResolves) {
|
|
this.registry.set('gameLog', this.registry.get('gameLog') + 1);
|
|
const newResolutions = game.resolved.slice(this.renderedResolves);
|
|
renderResolutions(this, game, this.scene.get('CombatCryps').cryps, newResolutions);
|
|
this.renderedResolves = game.resolved.length;
|
|
return true;
|
|
}
|
|
this.registry.set('gameLog', game.log.length);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = Combat;
|