class GamingBoard extends Phaser.Scene { constructor() { super('GamingBoard'); } preload() { this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, WIDTH_MAP, WIDTH_MAP); } create() { this.add.image(0, 0, 'background').alpha = 0.1; this.listRobot = []; this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, this)); this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.2, WIDTH_MAP * 0.1, 0xff33cc, this)); this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, 0xffffff, this)); for (let i = 0; i < this.listRobot.length; i++) { this.chooseTarget(this.listRobot[i]); } this.events.on('resume', () => this.resume()); } update(time, delta) { super.update(time, delta); this.listRobot.forEach(function (robot) { robot.read(); }) for (let i = 0; i < this.listRobot.length; i++) { if (!this.listRobot[i].isAlive()) { this.listRobot.splice(i, 1); i -= 1; } else { if (!this.listRobot[i].haveTarget()) { this.chooseTarget(this.listRobot[i]); } else { this.listRobot[i].updateTarget(); } } } } resume() { } modifyNodes(lNodes) { this.listRobot[0].cleanNodes(); lNodes.forEach(element => this.listRobot[0].addNode(element)); } chooseTarget(robot) { let minDist = WIDTH_MAP * 2; let l = []; this.listRobot.forEach(function (item) { if (item !== robot && item.isAlive()) { let hypot = Math.hypot(item.x - robot.x, item.y - robot.y); if (minDist >= hypot) { if (minDist > hypot) { minDist = hypot; l = []; } l.push(item); } } }); if (this.listRobot.length > 1) { robot.setTarget(l[Math.floor(Math.random() * (l.length + 1))]); } else { robot.setTarget(l[0]); } } }