const Phaser = require('phaser'); const { TEXT, COLOURS, POSITIONS: { CRYP_LIST } } = require('./constants'); const genAvatar = require('./avatar'); const ROW_HEIGHT = CRYP_LIST.height() * 0.2; const ROW_WIDTH = CRYP_LIST.width(); const TEXT_MARGIN = 24; const KEY_MAP = [ 'keydown_ONE', 'keydown_TWO', 'keydown_THREE', ]; class MenuCrypList extends Phaser.Scene { constructor() { super({ key: 'MenuCrypList' }); } create() { this.cameras.main.setViewport(CRYP_LIST.x(), CRYP_LIST.y(), CRYP_LIST.width(), CRYP_LIST.height()); this.registry.events.on('changedata', this.updateData, this); this.registry.events.on('setdata', this.updateData, this); this.addCrypRows(this.registry.get('cryps')); this.addControls(); } updateData(parent, key, data) { if (key === 'cryps') { KEY_MAP.forEach(k => this.input.keyboard.removeListener(k)); this.addCrypRows(data); } } addCrypRows(cryps) { if (!cryps) return true; if (this.crypRows) this.crypRows.destroy(true); this.crypRows = this.add.group(); // We only display 3 cryps others can be viewed in cryp list (soon TM) for (let i = 0; i < 3; i += 1) { const cryp = cryps[i]; const ROW_X = 0; const ROW_Y = (i * ROW_HEIGHT); const BOX_WIDTH = Math.floor(ROW_WIDTH / 5); const ACTIVE_FILL = cryp.active ? 1 : 0.2; const FILL = Object.values(COLOURS)[i]; // Selection of cryps const selectFn = () => { this.game.events.emit('CRYP_ACTIVE', cryp); }; if (KEY_MAP[i]) { this.input.keyboard.on(KEY_MAP[i], selectFn, this); } const crypSelect = this.add .rectangle(ROW_X, ROW_Y, BOX_WIDTH, ROW_HEIGHT, FILL, 1) .setInteractive() .setOrigin(0) .on('pointerdown', selectFn); this.crypRows.add(crypSelect); this.crypRows.add(this.add .text(crypSelect.getCenter().x, crypSelect.y + TEXT_MARGIN, i + 1, TEXT.HEADER) .setOrigin(0.5, 0.5)); // Cryp avatar and interaction box const crypInteract = this.add .rectangle(ROW_X + BOX_WIDTH, ROW_Y, BOX_WIDTH * 2, ROW_HEIGHT, FILL, ACTIVE_FILL) .setInteractive() .setOrigin(0) .on('pointerdown', () => { this.registry.set('crypStats', cryp); }); crypInteract.itemSelect = () => { crypInteract.setFillStyle(COLOURS.SELECT); }; crypInteract.itemDeselect = () => { crypInteract.setFillStyle(FILL, ACTIVE_FILL); }; crypInteract.cryp = cryp; this.crypRows.add(this.add.image( crypInteract.getCenter().x, crypInteract.getCenter().y, 'aztec', genAvatar(cryp.name) )); this.crypRows.add(crypInteract); // Cryp information box (names + skills) const crypInfo = this.add .rectangle(ROW_X + BOX_WIDTH * 3, ROW_Y, BOX_WIDTH * 2, ROW_HEIGHT, FILL, ACTIVE_FILL) .setOrigin(0); this.crypRows.add(crypInfo); this.crypRows.add(this.add .text(crypInfo.getCenter().x, crypInfo.y + TEXT_MARGIN, cryp.name, TEXT.HEADER) .setOrigin(0.5, 0.5)); this.crypRows.add(this.add .text(crypInfo.getCenter().x, crypInfo.y + TEXT_MARGIN * 2, `Level: ${cryp.lvl}`, TEXT.NORMAL) .setOrigin(0.5, 0.5)); } return true; } addControls() { // Add Spawn Cryp Option const spawn = this.add .rectangle(ROW_WIDTH * 0.05, ROW_HEIGHT * 3.5, ROW_WIDTH * 0.4, ROW_HEIGHT, 0x888888) .setInteractive() .setOrigin(0) .on('pointerdown', () => { this.game.events.emit('CRYP_SPAWN'); }); this.add .text(spawn.getCenter().x, spawn.getCenter().y, '+', TEXT.HEADER) .setOrigin(0.5, 0.5); // Dialog to view all cryps // const crypList = this.add // .rectangle(ROW_WIDTH * 0.55, ROW_HEIGHT * 3.5, ROW_WIDTH * 0.4, ROW_HEIGHT, 0xff9215) // .setInteractive() // .setOrigin(0) // .on('pointerdown', () => { // // this.game.events.emit('CRYP_LIST'); // // Placeholder will give a full list of all cryps in the center // }); // this.add // .text(crypList.getCenter().x, crypList.getCenter().y, 'Cryp List (soon)', TEXT.NORMAL) // .setOrigin(0.5, 0.5); } cleanUp() { KEY_MAP.forEach(k => this.input.keyboard.removeListener(k)); this.registry.events.off('changedata', this.updateData, this); this.registry.events.off('setdata', this.updateData, this); this.scene.remove(); } } module.exports = MenuCrypList;