const Phaser = require('phaser'); const { countBy } = require('lodash'); const Item = require('./elements/item'); const { TEXT, POSITIONS: { ITEM_LIST }, } = require('./constants'); const X = ITEM_LIST.x(); const Y = ITEM_LIST.y(); const WIDTH = ITEM_LIST.width(); const HEIGHT = ITEM_LIST.height(); const ITEM_WIDTH = ITEM_LIST.itemWidth(); const ITEM_HEIGHT = ITEM_LIST.itemHeight(); const INV_X = X + ITEM_WIDTH * 0.325; const INV_Y = Y + ITEM_HEIGHT; const INV_ROWS = 3; const INV_COLUMNS = 3; const COMB_X = INV_X + ITEM_WIDTH * 5.75; const COMB_Y = INV_Y; const COMB_ROWS = 2; const COMB_COLUMNS = 2; const BOX_X = X + ITEM_WIDTH * 2; const BOX_Y = Y + ITEM_HEIGHT * 5; const BOX_ROWS = 3; const BOX_COLUMNS = 6; const drawVbox = (graphics) => { const boxDrawX = BOX_X - ITEM_WIDTH * 0.05; const boxDrawY = BOX_Y - ITEM_HEIGHT * 0.05; for (let i = 0; i <= BOX_COLUMNS; i += 1) { const x = boxDrawX + i * ITEM_WIDTH * 1.1; graphics.lineBetween(x, boxDrawY, x, boxDrawY + ITEM_HEIGHT * 1.1 * BOX_ROWS); } for (let i = 0; i <= BOX_ROWS; i += 1) { const y = boxDrawY + i * ITEM_HEIGHT * 1.1; graphics.lineBetween(boxDrawX, y, boxDrawX + ITEM_WIDTH * 1.1 * BOX_COLUMNS, y); } }; const drawInventory = (graphics) => { const invDrawX = INV_X - ITEM_WIDTH * 0.05; const invDrawY = INV_Y - ITEM_HEIGHT * 0.05; for (let i = 0; i <= INV_COLUMNS; i += 1) { const x = invDrawX + i * ITEM_WIDTH * 1.1; graphics.lineBetween(x, invDrawY, x, invDrawY + ITEM_HEIGHT * 1.1 * INV_ROWS); } for (let i = 0; i <= INV_ROWS; i += 1) { const y = invDrawY + i * ITEM_HEIGHT * 1.1; graphics.lineBetween(invDrawX, y, invDrawX + ITEM_WIDTH * 1.1 * INV_COLUMNS, y); } }; const drawCombiner = (graphics) => { const combDrawX = COMB_X - ITEM_WIDTH * 0.05; const combDrawY = COMB_Y - ITEM_HEIGHT * 0.05; for (let i = 0; i <= COMB_COLUMNS; i += 1) { const x = combDrawX + i * ITEM_WIDTH * 1.1; graphics.lineBetween(x, combDrawY, x, combDrawY + ITEM_HEIGHT * 1.1 * COMB_ROWS); } for (let i = 0; i <= COMB_ROWS; i += 1) { const y = combDrawY + i * ITEM_HEIGHT * 1.1; graphics.lineBetween(combDrawX, y, combDrawX + ITEM_WIDTH * 1.1 * COMB_COLUMNS, y); } }; class CombinerHitBox extends Phaser.GameObjects.Rectangle { constructor(scene, x, y, i) { super(scene, x, y, ITEM_WIDTH, ITEM_HEIGHT, 0x000000); this.setOrigin(0); this.x = x; this.y = y; this.slot = i; this.item = false; this.itemSelect = () => this.setFillStyle(0x222222); this.itemDeselect = () => this.setFillStyle(0x000000); } allocate(item) { if (this.item) this.deallocate(); item.setPosition(this.x + item.width / 2, this.y + item.height / 2); this.item = item; } deallocate() { if (this.item) this.item.setPosition(this.item.origX, this.item.origY); this.item = false; } } class DeleteHitBox extends Phaser.GameObjects.Rectangle { constructor(scene, x, y) { super(scene, x, y, ITEM_WIDTH * 1.25, ITEM_HEIGHT * 1.25, 0x222222); this.setOrigin(0); this.itemSelect = () => this.setFillStyle(0xff0000); this.itemDeselect = () => this.setFillStyle(0x222222); } } const itemCheckHitbox = (scene, pointer) => { const { list } = scene.scene.get('MenuCrypList').children; const hitboxes = list.filter(c => c.cryp) .concat(scene.children.list.filter(c => c instanceof CombinerHitBox || c instanceof DeleteHitBox)); let found; for (let i = 0; i < hitboxes.length; i += 1) { if (Phaser.Geom.Rectangle.ContainsPoint(hitboxes[i].getBounds(), pointer.position)) { found = hitboxes[i]; } else { hitboxes[i].itemDeselect(); } } return found; }; class ItemList extends Phaser.Scene { constructor() { super({ key: 'ItemList', active: true }); } updateData(parent, key, data) { if (key === 'vbox') { this.registry.events.off('changedata', this.updateData, this); this.registry.events.off('setdata', this.updateData, this); this.scene.restart(data); } } create(vbox) { this.registry.events.on('changedata', this.updateData, this); this.registry.events.on('setdata', this.updateData, this); if (!vbox.bound) return false; this.combinerItems = this.registry.get('combinerItems'); if (!this.combinerItems || vbox.bound.length < this.registry.get('boundLength')) { this.combinerItems = [-1, -1, -1]; } this.registry.set('boundLength', vbox.bound.length); const ws = this.registry.get('ws'); // Static Elements const graphics = this.add.graphics(); graphics.lineStyle(5, 0x808080, 1.0); drawCombiner(graphics); drawInventory(graphics); drawVbox(graphics); this.add.text(X + WIDTH * 0.1, Y, 'Inventory', TEXT.HEADER); this.add.text(X + WIDTH * 0.47, Y, 'Combiner', TEXT.HEADER); this.add.text(X + WIDTH * 0.35, Y + HEIGHT / 2, 'Varibox', TEXT.HEADER); const reroll = this.add .rectangle(WIDTH * 0.01, Y + HEIGHT * 0.775, ITEM_WIDTH * 1.25, ITEM_HEIGHT * 1.25, 0x222222) .setInteractive() .setOrigin(0) .on('pointerdown', () => this.registry.get('ws').sendVboxDiscard(vbox.game)); this.add.text(reroll.getCenter().x, reroll.getCenter().y, 'Reroll', TEXT.HEADER) .setOrigin(0.5, 0.5); const combine = this.add .rectangle(ITEM_WIDTH * 1.1 + COMB_X, ITEM_HEIGHT * 1.1 + COMB_Y, ITEM_WIDTH, ITEM_HEIGHT, 0x222222) .setInteractive() .setOrigin(0) .on('pointerdown', () => { ws.sendVboxCombine(vbox.game, this.combinerItems); this.combinerItems = [-1, -1, -1]; this.children.list.filter(obj => obj instanceof CombinerHitBox).forEach(cBox => cBox.deallocate()); }); this.add.text(combine.getCenter().x, combine.getCenter().y, 'C', TEXT.HEADER) .setOrigin(0.5, 0.5); for (let i = 0; i < 3; i += 1) { const ITEM_X = ITEM_WIDTH * 1.1 * (i % COMB_COLUMNS) + COMB_X; const ITEM_Y = ITEM_HEIGHT * 1.1 * Math.floor(i / COMB_COLUMNS) + COMB_Y; this.add.existing(new CombinerHitBox(this, ITEM_X, ITEM_Y, i)); } const del = this.add.existing(new DeleteHitBox(this, WIDTH * 0.01, Y + HEIGHT * 0.6)); this.add.text(del.getCenter().x, del.getCenter().y, 'Del', TEXT.HEADER) .setOrigin(0.5, 0.5); // Generate Items vbox.bound.forEach((action, i) => { const ITEM_X = ITEM_WIDTH * 1.1 * (i % INV_COLUMNS) + INV_X + ITEM_WIDTH * 0.5; const ITEM_Y = ITEM_HEIGHT * 1.1 * Math.floor(i / INV_COLUMNS) + INV_Y + ITEM_HEIGHT * 0.5; const itemBox = new Item(this, action, i, ITEM_X, ITEM_Y, ITEM_WIDTH, ITEM_HEIGHT); this.input.setDraggable(itemBox); this.add.existing(itemBox); }); vbox.free.forEach((action, i) => { const ITEM_X = ITEM_WIDTH * 1.1 * (i % BOX_COLUMNS) + BOX_X + ITEM_WIDTH * 0.5; const ITEM_Y = ITEM_HEIGHT * 1.1 * Math.floor(i / BOX_COLUMNS) + BOX_Y + ITEM_HEIGHT * 0.5; const itemBox = new Item(this, action, i, ITEM_X, ITEM_Y, ITEM_WIDTH, ITEM_HEIGHT); itemBox .setInteractive() .on('pointerdown', () => { ws.sendVboxAccept(vbox.game, i); }); this.add.existing(itemBox); }); // Restore previous combiner item slots this.combinerItems.forEach((index, i) => { if (index === -1) return false; const item = this.children.list.filter(obj => obj instanceof Item).find(it => it.index === index); const hitBox = this.children.list.filter(obj => obj instanceof CombinerHitBox).find(hb => hb.slot === i); hitBox.allocate(item); return true; }); // allocation functions const allocate = (item, hitBox) => { hitBox.allocate(item); this.combinerItems[hitBox.slot] = item.index; this.registry.set('combinerItems', this.combinerItems); }; const deallocate = (item) => { const clearIndex = this.combinerItems.indexOf(item.index); if (clearIndex !== -1) { this.children.list.filter(obj => obj instanceof CombinerHitBox) .forEach((cBox) => { if (cBox.item === item) cBox.deallocate(); }); this.combinerItems[clearIndex] = -1; } item.setPosition(item.origX, item.origY); }; const findUnallocated = () => { for (let i = 0; i <= 2; i += 1) { if (this.combinerItems[i] === -1) { return this.children.list.filter(obj => obj instanceof CombinerHitBox).find(hb => hb.slot === i); } } return false; }; // Add Handlers this.input.on('dragstart', (pointer, item) => { if (!(item instanceof Item)) return false; item.clickHandler(); return true; }); this.input.on('drag', (pointer, item, dragX, dragY) => { if (!(item instanceof Item)) return false; item.setPosition(dragX, dragY); const hitBox = itemCheckHitbox(this, pointer); if (hitBox) hitBox.itemSelect(); return true; }); this.input.on('dragend', (pointer, item) => { if (!(item instanceof Item)) return false; item.deselect(); // Allocate to combiner if clicked without movement form inventory (return 0) if (!Math.hypot(item.x - item.origX, item.y - item.origY)) { const cBox = findUnallocated(); if (cBox) allocate(item, cBox); return true; } // Check for hitboxes const hitBox = itemCheckHitbox(this, pointer); if (hitBox) { hitBox.itemDeselect(); // Allocate to specific combiner slot if (hitBox instanceof CombinerHitBox) { allocate(item, hitBox); return true; } // Allocate to cryp hitbox if (hitBox instanceof DeleteHitBox) ws.sendVboxDrop(vbox.game, item.index); else ws.sendVboxApply(vbox.game, hitBox.cryp.id, item.index); } // If the item hasn't been allocated deallocate the item // Scene will restart if there is vbox change deallocate(item); return true; }); return this; } cleanUp() { this.registry.events.off('changedata', this.updateData, this); this.registry.events.off('setdata', this.updateData, this); this.scene.remove(); } } module.exports = ItemList;