71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
const Phaser = require('phaser');
|
|
|
|
const {
|
|
TEXT,
|
|
COLOURS,
|
|
} = require('../constants');
|
|
|
|
class Item extends Phaser.GameObjects.Container {
|
|
constructor(scene, item, index, x, y, width, height) {
|
|
super(scene, x, y);
|
|
|
|
this.state = 'deselect';
|
|
this.scene = scene;
|
|
this.item = item;
|
|
this.index = index;
|
|
this.origX = x;
|
|
this.origY = y;
|
|
this.width = width;
|
|
this.height = height;
|
|
|
|
this.box = scene.add
|
|
.rectangle(0, 0, width, height, 0x222222);
|
|
|
|
this.text = scene.add
|
|
// .text(0, 0, `${action} x${count}`, TEXT.NORMAL)
|
|
.text(0, 0, `${item}`, TEXT.NORMAL)
|
|
.setOrigin(0.5, 0.5);
|
|
|
|
this.add(this.box);
|
|
this.add(this.text);
|
|
|
|
this.setSize(width, height);
|
|
this.setInteractive();
|
|
}
|
|
|
|
changeOrigin(x, y) {
|
|
this.origX = x + this.width / 2;
|
|
this.origY = y + this.height / 2;
|
|
}
|
|
|
|
clickHandler() {
|
|
this.scene.activeItem = this;
|
|
// Set the main context to display the item info
|
|
this.scene.registry.set('itemInfo', this.action);
|
|
this.select();
|
|
}
|
|
|
|
select() {
|
|
this.scene.children.list.forEach((item) => {
|
|
if (item.state === 'select') item.deselect();
|
|
});
|
|
this.box.setFillStyle(COLOURS.SELECT);
|
|
this.state = 'select';
|
|
}
|
|
|
|
activate() {
|
|
this.scene.children.list.forEach((item) => {
|
|
if (item.state === 'select') item.deselect();
|
|
});
|
|
this.box.setFillStyle(0xff0000);
|
|
this.state = 'activate';
|
|
}
|
|
|
|
deselect() {
|
|
this.box.setFillStyle(0x222222);
|
|
this.state = 'deselect';
|
|
}
|
|
}
|
|
|
|
module.exports = Item;
|