Implement door
authorMegaBrutal <code+git@megabrutal.com>
Mon, 10 Sep 2018 22:15:00 +0000 (00:15 +0200)
committerMegaBrutal <code+git@megabrutal.com>
Mon, 10 Sep 2018 22:15:00 +0000 (00:15 +0200)
Door can now open & close, test runs at ~5 and ~10 seconds after game
starts.
Yet to add collision and control methods.

modified:   wgj58.js

wgj58.js

index e034b01..988d662 100644 (file)
--- a/wgj58.js
+++ b/wgj58.js
@@ -40,10 +40,37 @@ class Dialogue {
 }
 
 class Door extends Phaser.TileSprite {
-       constructor(x, y, name, vector, longpanel) {
+       constructor(x, y, name, rotation, vector, longpanel) {
                super(game, x, y, 64, 64, 'objects');
-               this.tilePosition = new Phaser.Point(-64, -64);
-               console.log("New door:", this);
+               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);
+       }
+
+       update() {
+               if ((this.opentween) && (game.time.now > 5000)) {
+                       console.log(game.time.now, "Opening door.");
+                       this.opentween.start();
+                       this.opentween = null;
+               }
+               else if ((this.closetween) && (game.time.now > 10000)) {
+                       console.log(game.time.now, "Closing door.");
+                       this.closetween.start();
+                       this.closetween = null;
+               }
        }
 }
 
@@ -424,16 +451,17 @@ class GameLogic {
        }
 
        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); break;
-                       case 90:        vector = new Phaser.Point(  0, -1); break;
-                       case 180:       vector = new Phaser.Point(  1,  0); break;
-                       case 270:       vector = new Phaser.Point(  0,  1); break;
+                       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, vector, object.properties.longpanel));
+               this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
        }
 
        callMenu(menuitem) {