Browse Source

✨ Add signal system to GameObjects

Pedro Schneider 3 years ago
parent
commit
49c108fb3d
2 changed files with 100 additions and 28 deletions
  1. 44 0
      pandora/game_objects/GameObject.js
  2. 56 28
      src/sketch.js

+ 44 - 0
pandora/game_objects/GameObject.js

@@ -32,6 +32,9 @@ class GameObject
         this.parent = null;
         this.isOnTree = false;
 
+        this.signals = [];
+        this.initSignals();
+
         GameHandler.instanceGameObject(this);
     }
 
@@ -62,6 +65,37 @@ class GameObject
     }
 
     // Methods
+    addSignal(name)
+    {
+        this.signals.push(new Signal(name));
+    }
+
+    connect(signalName, target, callback)
+    {
+        for (let i = 0; i < this.signals.length; i++)
+        {
+            if (this.signals[i].name == signalName)
+            {
+                this.signals[i].targets.push(target);
+                this.signals[i].callbacks.push(callback);
+                return;
+            }
+        }
+    }
+
+    emitSignal(signalName, ...params)
+    {
+        for (let i = 0; i < this.signals.length; i++)
+        {
+            if (this.signals[i].name == signalName)
+            {
+                for (let j = 0; j < this.signals[i].callbacks.length; j++)
+                    this.signals[i].targets[j][this.signals[i].callbacks[j]](...params);
+                return;
+            }
+        }
+    }
+
     addChild(child)
     {
         child.parent = this;
@@ -71,6 +105,11 @@ class GameObject
         if (this.isOnTree) child.setup();
     }
 
+    initSignals()
+    {
+        this._initSignals();
+    }
+
     setup()
     {
         this.isOnTree = true;
@@ -96,6 +135,11 @@ class GameObject
     }
 
     // Callbacks
+    _initSignals()
+    {
+
+    }
+
     _setup()
     {
 

+ 56 - 28
src/sketch.js

@@ -1,16 +1,50 @@
-let test, but, bu2;
+let test, but;
 
 class TestObject extends Object2D
 {
     _setup()
     {
-        this.position.y = 300;
-        this.position.x = 100;
+        this.visible = true;
+        this.position = new Vector2(100, 100);
+        this.getChildByIndex(0).connect("buttonPressed", this, "_onSignal");
+    }
+
+    _onSignal(param1, param2, param3, param4)
+    {
+        this.visible = !this.visible;
+        console.log(param1);
+        console.log(param2);
+        console.log(param3);
+        console.log(param4);
+    }
+
+    _update(delta)
+    {
+
+    }
+
+    _draw(delta)
+    {
+        if (this.visible) ellipse(0, 0, 20);
+    }
+}
 
-        this.tween = new Tween("myTween");
-        this.tween.interpolateProperty(this.position, "x", PROPERTY_TYPE.NUMBER, this.position.x, 400, 2, TRANS_TYPE.LINEAR);
-        this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 2, TRANS_TYPE.BOUNCE, EASE_TYPE.OUT);
-        this.addChild(this.tween);
+class TestObject2 extends Object2D
+{
+    _setup()
+    {
+        this.visible = true;
+        this.position = new Vector2(200, 100);
+        but.connect("buttonPressed", this, "_onSignal");
+    }
+
+    _onSignal(param1, param2, param3, param4)
+    {
+        this.visible = !this.visible;
+        console.log(param1);
+        console.log(param2);
+        console.log(param3);
+        console.log(param4);
     }
 
     _update(delta)
@@ -20,23 +54,26 @@ class TestObject extends Object2D
 
     _draw(delta)
     {
-        ellipse(0, 0, 20);
+        if (this.visible) ellipse(0, 0, 20);
     }
 }
 
 class TestButton extends Button
 {
-    _onMousePressed()
+    _initSignals()
     {
-        if (this.name == "b1")
-            this.getParent().tween.start();
-        else if (this.name == "b2")
-            this.getParent().tween.stop();
-        else if (this.name == "b3")
-            this.getParent().tween.stopByIndex(0);
-        else if (this.name == "b4")
-            this.getParent().tween.startByIndex(0);
+        this.addSignal("buttonPressed");
+    }
 
+    _setup()
+    {
+        this.setPosition(200, 200);
+        this.setSize(100, 50)
+    }
+
+    _onMousePressed()
+    {
+        this.emitSignal("buttonPressed", 1, 2, 3, 4);
     }
 }
 
@@ -56,19 +93,10 @@ function setup()
     textFont(AssetHandler.getP5FontByName("Lato"));
 
     test = new TestObject("myTest");
-    but = new TestButton("b1", "play");
-    but.setPosition(0, 200);
+    but = new TestButton("b1", "Emit signal");
     test.addChild(but);
-    but2 = new TestButton("b2", "stop");
-    but2.setPosition(0, 230);
-    test.addChild(but2);
-    but2 = new TestButton("b3", "stop1");
-    but2.setPosition(0, 260);
-    test.addChild(but2);
-    but2 = new TestButton("b4", "play1");
-    but2.setPosition(0, 290);
-    test.addChild(but2);
     GameHandler.addRootObject(test);
+    GameHandler.addRootObject(new TestObject2("myTest2"))
 }
 
 function draw()