const Phaser = require('phaser'); const fs = require('fs'); const { throttle } = require('lodash'); const { POSITIONS: { COMBAT }, TEXT } = require('./constants'); const { CombatCryps, CrypImage, CrypSkill } = require('./combat.cryps'); const renderResolutions = require('./combat.render.resolutions'); const CRYP_KEY_MAP = ['keydown_ONE', 'keydown_TWO', 'keydown_THREE']; const SKILL_KEY_MAP = ['keydown_Q', 'keydown_W', 'keydown_E', 'keydown_R']; class Combat extends Phaser.Scene { constructor() { super({ key: 'Combat' }); } preload() { /* Static Images */ 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', 'http://labs.phaser.io/assets/sprites/bullet.png'); this.load.image('blue', 'http://labs.phaser.io/assets/particles/blue.png'); this.load.image('green', 'http://labs.phaser.io/assets/particles/green.png'); this.load.image('red', 'http://labs.phaser.io/assets/particles/red.png'); this.load.image('white', 'http://labs.phaser.io/assets/particles/white.png'); this.load.image('yellow', 'http://labs.phaser.io/assets/particles/yellow.png'); } create() { this.registry.events.on('changedata', this.updateData, this); this.input.keyboard.on('keydown_S', () => { this.scene.switch('CrypList'); // Switch back to cryp list this.registry.set('game', null); }, 0, this); this.input.on('pointerup', (pointer, obj) => { if (obj[0] instanceof CrypImage || obj[0] instanceof CrypSkill) { obj[0].clickHandler(); } else if (this.registry.get('activeSkill')) { this.registry.get('activeSkill').clearTint(); this.registry.set('activeSkill', null); } }); this.combatCryps = new CombatCryps(this); this.renderedResolves = 0; this.registry.set('gameAnimating', false); this.account = this.registry.get('account'); this.log = this.add.text(COMBAT.LOG.x(), COMBAT.LOG.y(), '', TEXT.NORMAL); this.log.setWordWrapWidth(COMBAT.LOG.width()); 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; } update() { this.fetchGame(); return true; } updateData(parent, key, data) { if (key === 'game') { if (!data) return false; this.redrawGame(data); } return true; } redrawGame(game) { // Only redraw if the game is animating or the phase has changed const updatedNeeded = !this.registry.get('gameAnimating'); if (!updatedNeeded) return false; // do we need to render resolution animations? if (game.resolved.length !== this.renderedResolves) { const newResolutions = game.resolved.slice(this.renderedResolves); renderResolutions(this, game, this.combatCryps, newResolutions); this.renderedResolves = game.resolved.length; return true; } this.combatCryps.clear(true, true); this.combatCryps.update(game); // update log // shallow copy because reverse mutates this.log.setText(Array.from(game.log).slice(0, this.logIter).reverse()); return true; } crypKeyHandler(cryp, iter) { if (CRYP_KEY_MAP[iter]) { this.input.keyboard.removeListener(CRYP_KEY_MAP[iter]); if (cryp.skills.length > 0) { // check there are cryp skills this.input.keyboard.on(CRYP_KEY_MAP[iter], () => { SKILL_KEY_MAP.forEach(k => this.input.keyboard.removeListener(k)); cryp.skills.forEach((skill, i) => { this.input.keyboard.on(SKILL_KEY_MAP[i], () => { this.game.events.emit('SET_ACTIVE_SKILL', skill); skill.setActive(); }, this); }); }, this); } } } } module.exports = Combat;