You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.0 KiB

class Game extends Phaser.Scene {
constructor(father) {
super('Game');
this.father = father;
}
preload() {
this.gm = new GamingBoard(this);
this.father.scene.add('GamingBoard', this.gm);
}
create() {
this.selected = null;
this.createButton();
this.scene.launch('GamingBoard');
this.scene.pause('GamingBoard');
let centerX = WIDTH_WINDOW / 4 * 3;
let diffX = WIDTH_WINDOW / 10;
this.tree = new RectangleNode(centerX, HEIGHT_WINDOW / 10, this, 'waria');
this.tree.addRect(new RectangleNode(centerX - 2 * diffX, HEIGHT_WINDOW / 3, this, 'move', true, false));
this.tree.addRect(new RectangleNode(centerX + diffX, HEIGHT_WINDOW / 3, this, 'move', true));
this.tree.addRect(new RectangleNode(centerX, HEIGHT_WINDOW / 3, this, 'attack', 1 / 3));
let condition = new RectangleNode(centerX - diffX, HEIGHT_WINDOW / 3, this, 'condition', true, true, false, 1 / 3, 0);
condition.addRect(new RectangleNode(centerX, HEIGHT_WINDOW / 1.5, this, 'move', false));
condition.addRect(new RectangleNode(centerX - diffX, HEIGHT_WINDOW / 1.5, this, 'attack'))
this.tree.addRect(condition);
this.gm.addRobot(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, this.gm, MYSELF));
this.gm.addRobot(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.2, WIDTH_MAP * 0.1, 0xff0000, this.gm));
this.gm.addRobot(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, 0xff0000, this.gm));
this.gm.listRobot[0].setSpeed(3);
}
update(time, delta) {
super.update(time, delta);
this.tree.updateLine();
}
finish() {
console.log("FINISH");
let winner;
if (this.gm.winner()) {
winner = new Winner(this.father);
} else {
winner = new Looser(this.father);
}
this.father.scene.add('Finish', winner);
this.scene.pause('Game');
this.scene.launch('Finish');
}
clickNode() {
this.pencil.setVisible(false);
this.bin.setVisible(false);
this.plus.setVisible(false);
if (this.selected != null) {
this.selected.deselect();
}
this.selected = this.tree.getSelected();
if (this.selected == null)
return;
if (this.selected !== this.tree) {
this.pencil.setVisible(true);
this.bin.setVisible(true);
}
if (this.selected.canAddNode) {
this.plus.setVisible(true);
}
}
doDrag(pointer, target, dragX, dragY) {
target.setX(dragX);
target.setY(dragY);
}
pauseScene(btn) {
this.line = 0;
this.changeFrame(btn, 0);
this.gm.pause();
}
resume() {
this.line = 1;
const sortDesc = (a, b) => a.getX() - b.getX();
this.tree.lRect.sort(sortDesc);
this.tree.lRect.forEach(function (element) {
if (element.lRect !== undefined) {
element.lRect.sort(sortDesc);
}
})
this.gm.modifyNodes(this.tree.getNodes());
this.gm.resume();
}
changeFrame(btn, bonus) {
btn.setFrame(this.line * 2 + bonus);
}
createButton() {
let x = WIDTH_MAP + WIDTH_WINDOW / 10;
let y = WIDTH_MAP + HEIGHT_WINDOW / 10 + 5;
let diffX = (HEIGHT_WINDOW / 10);
let width = HEIGHT_WINDOW / 15;
let play = this.add.sprite(x, y, 'play').setOrigin(1, 0).setInteractive();
let back = this.add.sprite(10, 10, 'back').setOrigin(0, 0).setInteractive();
this.pencil = this.add.sprite(x - diffX, y, 'pencil').setOrigin(1, 0).setInteractive().setVisible(false);
this.bin = this.add.sprite(x - diffX * 2, y, 'bin').setOrigin(1, 0).setInteractive().setVisible(false);
this.plus = this.add.sprite(x - diffX * 3, y, 'plus').setOrigin(1, 0).setInteractive().setVisible(false);
this.line = 0;
play.displayHeight = play.displayWidth = width;
back.displayHeight = back.displayWidth = width;
this.pencil.displayHeight = this.pencil.displayWidth = width;
this.bin.displayHeight = this.bin.displayWidth = width;
this.plus.displayHeight = this.plus.displayWidth = width;
this.creatBtnOutAndOver(play);
this.creatBtnOutAndOver(back);
this.creatBtnOutAndOver(this.pencil);
this.creatBtnOutAndOver(this.bin);
this.creatBtnOutAndOver(this.plus);
play.on('pointerdown', () => this.clickPlay(play));
back.on('pointerdown', () => this.clickBack());
this.pencil.on('pointerdown', () => this.clickPencil());
this.bin.on('pointerdown', () => this.clickBin());
this.plus.on('pointerdown', () => this.clickPlus());
this.input.on('pointerdown', () => this.clickNode());
this.input.on('drag', this.doDrag);
this.input.on('dragstart', () => this.pauseScene(play));
}
creatBtnOutAndOver(btn) {
btn.on('pointerout', () => this.changeFrame(btn, 0));
btn.on('pointerover', () => this.changeFrame(btn, 1));
}
clickPlay(btn) {
if (this.scene.isPaused('GamingBoard'))
this.resume();
else
this.pauseScene(btn);
this.changeFrame(btn, 1);
}
clickPencil() {
console.log("PENCIL");
this.changeFrame(this.pencil, 0);
}
clickBin() {
console.log("BIN");
this.tree.delete(this.selected);
this.changeFrame(this.bin, 0);
}
clickPlus() {
console.log("PLUS");
this.father.scene.add('PlusNode', new PlusNode(this.father, this, this.selected));
this.scene.launch('PlusNode');
this.changeFrame(this.plus, 0);
this.scene.pause('Game');
}
clickBack() {
console.log("BACK");
this.father.scene.stop('GamingBoard');
this.father.scene.remove('GamingBoard');
this.father.scene.stop('Game');
this.father.scene.start('Type');
}
}