From d4bf444021ef081beab89a401e1369bb933cedb8 Mon Sep 17 00:00:00 2001 From: Mashy Date: Tue, 13 Nov 2018 14:30:30 +1000 Subject: [PATCH] passives --- client/src/components/phaser.passives.js | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 client/src/components/phaser.passives.js diff --git a/client/src/components/phaser.passives.js b/client/src/components/phaser.passives.js new file mode 100755 index 00000000..53c689bb --- /dev/null +++ b/client/src/components/phaser.passives.js @@ -0,0 +1,86 @@ +const Phaser = require('phaser'); +const PassiveNode = require('./passive.node'); + + +class PhaserPassives extends Phaser.Scene { + preload() { + this.load.image('eye', 'https://labs.phaser.io/assets/particles/green-orb.png'); + } + + create() { + this.passiveNodes = [ + { x: 400, y: 300, id: 'A', alloc: false }, + { x: 100, y: 100, id: 'B', alloc: false }, + { x: 300, y: 200, id: 'C', alloc: false }, + { x: 300, y: 800, id: 'D', alloc: false }, + { x: 600, y: 500, id: 'E', alloc: false }, + { x: 700, y: 400, id: 'F', alloc: false }]; + + this.passiveEdges = [ + ['A', 'B'], + ['F', 'A'], + ['D', 'A'], + ]; + + this.graphics = this.add.graphics(); + this.addCamera(); + this.addPassiveNodes(); + this.drawPassiveEdges(); + } + + addPassiveNodes() { + this.passiveNodes.forEach((n) => { + this.add.existing(new PassiveNode(this, n.x, n.y, n.id, n.alloc)).setInteractive(); + }); + this.input.on('pointerover', (event, gameObjects) => { + if (gameObjects[0] instanceof PassiveNode) { + if (!gameObjects[0].alloc) gameObjects[0].setTint(0xff00ff); + } + }); + this.input.on('pointerout', (event, gameObjects) => { + if (gameObjects[0] instanceof PassiveNode) { + if (!gameObjects[0].alloc) gameObjects[0].clearTint(); + } + }); + this.input.on('pointerdown', (event, gameObjects) => { + if (gameObjects[0] instanceof PassiveNode) { + gameObjects[0].allocate(); + } + }); + } + + drawPassiveEdges() { + this.graphics.clear(); + this.passiveEdges.forEach((e) => { + const drawEdge = this.passiveNodes.filter(n => (e[0] === n.id || e[1] === n.id)); + if (drawEdge[0].alloc && drawEdge[1].alloc) { + this.graphics.lineStyle(50, 0xfff00f, 0.2); + } else { + this.graphics.lineStyle(10, 0x00ffff, 0.2); + } + this.graphics.lineBetween(drawEdge[0].x, drawEdge[0].y, drawEdge[1].x, drawEdge[1].y); + }); + } + + addCamera() { + this.cursors = this.input.keyboard.createCursorKeys(); + this.controls = new Phaser.Cameras.Controls.SmoothedKeyControl({ + camera: this.cameras.main, + left: this.cursors.left, + right: this.cursors.right, + up: this.cursors.up, + down: this.cursors.down, + zoomIn: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q), + zoomOut: this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E), + acceleration: 0.005, + drag: 0.0005, + maxSpeed: 0.001, + }); + } + + update(delta) { + this.controls.update(delta); + } +} + +module.exports = PhaserPassives;