const Phaser = require('phaser'); const fs = require('fs'); const { POSITIONS: { COMBAT }, TEXT } = require('./constants'); const { DrawCrypTeams, CrypImage, CrypSkill } = require('./combat.cryps'); const CombatSkills = require('./combat.skills'); const renderResolutions = require('./combat.render'); 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 }, 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); } }); const logX = COMBAT.LOG.x(); const logY = COMBAT.LOG.y(); const logWidth = COMBAT.LOG.width(); // this.skills = new CombatSkills(this); this.renderedResolves = 0; this.registry.set('gameAnimating', false); this.account = this.registry.get('account'); this.log = this.add.text(logX, logY, '', TEXT.NORMAL); this.log.setWordWrapWidth(logWidth); return true; } updateData(parent, key, data) { if (key === 'game') { this.redrawGame(data); } return true; } redrawGame(game) { if (!game) return false; const updatedNeeded = !this.registry.get('activeSkill') && !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.crypTeamRender, newResolutions); this.renderedResolves = game.resolved.length; return true; } // If not animating resolutions render static skill / block phase if (this.crypTeamRender) this.crypTeamRender.destroy(true); this.crypTeamRender = new DrawCrypTeams(this, game); // update log // shallow copy because reverse mutates this.log.setText(Array.from(game.log).slice(0, this.logIter).reverse()); return true; } iterateLog(game) { this.logIter += 1; this.renderedResolves += 1; this.log.setText(Array.from(game.log).slice(0, this.logIter).reverse()); return this.resolvedIter === game.resolved.length; } 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;