Add methods to open & close doors
[wgj58.git] / wgj58.js
index d29f4e8fe3f37bf1a4d6f929150055e23071e9b3..8fe220f63a39ac710d93b8402fe35192e682dfe9 100644 (file)
--- a/wgj58.js
+++ b/wgj58.js
@@ -39,6 +39,44 @@ class Dialogue {
        }
 }
 
+class Door extends Phaser.TileSprite {
+       constructor(x, y, name, rotation, vector, longpanel) {
+               super(game, x, y, 64, 64, 'objects');
+               this.name = name;
+               if (!longpanel) {
+                       this.anchor = new Phaser.Point(0.5, 0.5);
+                       this.tilePosition = new Phaser.Point(-64, -64);
+                       this.openvector = Phaser.Point.multiply(vector, new Phaser.Point(56, 56));
+               }
+               else {
+                       this.width *= 2;
+                       this.anchor = new Phaser.Point(0.75, 0.5);
+                       this.tilePosition = new Phaser.Point(0, -64);
+                       this.openvector = Phaser.Point.multiply(vector, new Phaser.Point(116, 116));
+               }
+               this.rotation = rotation * (Math.PI / 180);
+               console.log("Calculated vector:", this.openvector);
+               this.closetween = game.add.tween(this).to({ x: this.position.x, y: this.position.y }, 1000, Phaser.Easing.Sinusoidal.InOut, false, 0, 0, false);
+               this.openposition = Phaser.Point.add(this.position, this.openvector);
+               this.opentween = game.add.tween(this).to({ x: this.openposition.x, y: this.openposition.y }, 1000, Phaser.Easing.Sinusoidal.InOut, false, 0, 0, false);
+               this.isOpen = false;
+       }
+
+       open() {
+               if (!this.isOpen) {
+                       this.opentween.start();
+                       this.isOpen = true;
+               }
+       }
+
+       close() {
+               if (this.isOpen) {
+                       this.closetween.start();
+                       this.isOpen = false;
+               }
+       }
+}
+
 class Player extends Phaser.Sprite {
        constructor(x, y) {
                super(game, x, y, 'player');
@@ -162,6 +200,7 @@ class NPC_Clara extends GameNPC {
                                        { actor: this, text: "Wish it worked like that... Is it some superstition like the belief that having an umbrella with you prevents rain?" },
                                        { actor: logic.player, text: "Nah, that actually works; it's not a superstition, but Murphy's Law!" },
                                        { actor: this, text: "If you say so..." } ] ));
+                               logic.openDoor("cutedoor");
                                break;
                        case 1:
                                logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "John, have you ever thought about losing your employee card?" },
@@ -173,6 +212,7 @@ class NPC_Clara extends GameNPC {
                                        { actor: this, text: "If I had no card, would I still exist?" },
                                        { actor: logic.player, text: "..." },
                                        { actor: logic.player, text: "You sound very philosophical today." } ] ));
+                               logic.closeDoor("cutedoor");
                                break;
                        case 2:
                        case 3:
@@ -381,12 +421,14 @@ class GameLogic {
        constructor() {
                this.player = this.clara = this.carlos = this.saiki = this.peter = this.bianca = null;
                this.gameinterface = new GameInterface(game, game.stage);
+               //this.doors = game.add.group(game.world, "doors");
                this.last_menuselect = 0;
        }
 
        createObject(object) {
                switch (object.type) {
                        case 'spawnpoint':              this.createCharacter(object); break;
+                       case 'door':                    this.createDoor(object); break;
                        case '':                                console.error("Object type is empty:", object); break;
                        default:                                console.error("Unknown object type:", object);
                }
@@ -413,6 +455,20 @@ class GameLogic {
                console.log(newChar);
        }
 
+       createDoor(object) {
+               /* Calculate movement vector and correct position (32 = half tile width/height). */
+               var vector;
+               switch (object.rotation) {
+                       case undefined:
+                       case 0:         vector = new Phaser.Point( -1,  0); object.x += 32; object.y -= 32; break;
+                       case 90:        vector = new Phaser.Point(  0, -1); object.x += 32; object.y += 32; break;
+                       case 180:       vector = new Phaser.Point(  1,  0); object.x -= 32; object.y += 32; break;
+                       case 270:       vector = new Phaser.Point(  0,  1); object.x -= 32; object.y -= 32; break;
+                       default:        console.error("Invalid rotation:", object.rotation);
+               }
+               this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
+       }
+
        callMenu(menuitem) {
                console.log("Menu callback received:", menuitem);
                this.last_menuselect = game.time.now;
@@ -444,6 +500,15 @@ class GameLogic {
                }
        }
 
+       openDoor(doorname) {
+               console.log("Opening doors.");
+               this.doors.children.forEach(function(o) { if (o.name == doorname) o.open(); });
+       }
+
+       closeDoor(doorname) {
+               this.doors.children.forEach(function(o) { if (o.name == doorname) o.close(); });
+       }
+
        update() {
                if ((game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_menuselect, WAIT_MENUSTEP))) {
                        if ((this.player.interactablenpc) && (this.player.interactablenpc.interactable) && (!this.gameinterface.inMenu) && (!this.gameinterface.inTalk)) {
@@ -493,6 +558,10 @@ class GamePlay extends Phaser.State {
                map.setCollisionBetween(1, 100, true, this.layer_walls);
                this.layer_furniture = map.createLayer('furniture');
                map.setCollisionBetween(1, 100, true, this.layer_furniture);
+
+               // FIXME: Don't create a group here like this, it's too ugly! Now I have it here due to the display order.
+               logic.doors = game.add.group(game.world, "doors");
+
                map.objects['objects'].forEach(function(o) { logic.createObject(o); });
                console.log(map.objects);