Procházet zdrojové kódy

✨ Add some utility functions to Tweens

- Add startByIndex function,
- Add stopAll and stopByIndex functions,
- Add resumeAll and resumeByIndex functions,
- Add resetAll and resetByIndex functions,
- Add removeAll and removeByIndex functions,
- Add seekAll and seekByIndex functions.
Pedro Schneider před 3 roky
rodič
revize
dfbc85a7b9
2 změnil soubory, kde provedl 106 přidání a 14 odebrání
  1. 80 7
      pandora/game_objects/Tween.js
  2. 26 7
      src/sketch.js

+ 80 - 7
pandora/game_objects/Tween.js

@@ -151,14 +151,92 @@ class Tween extends GameObject
         }
     }
 
-    play()
+    start()
     {
         for (let i = 0; i < this.tweenData.length; i++)
-        {
             this.tweenData[i].playing = true;
+    }
+
+    startByIndex(idx)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.tweenData[idx].playing = true;
+    }
+
+    stopAll()
+    {
+        for (let i = 0; i < this.tweenData.length; i++)
+            this.tweenData[i].playing = false;
+    }
+
+    stopByIndex(idx)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.tweenData[idx].playing = false;
+    }
+
+    resumeAll()
+    {
+        for (let i = 0; i < this.tweenData.length; i++)
+            this.tweenData[i].playing = true;
+    }
+
+    resumeByIndex(idx)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.tweenData[idx].playing = true;
+    }
+
+    resetAll()
+    {
+        this.doneTweens = 0;
+        this.done = false;
+        for (let i = 0; i < this.tweenData.length; i++)
+        {
+            this.tweenData[i].t = 0;
+            this.tweenData[i].done = false;
         }
     }
 
+    resetByIndex(idx)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.doneTweens--;
+        this.done = false;
+        this.tweenData[idx].t = 0;
+        this.tweenData[idx].done = false;
+    }
+
+    removeAll()
+    {
+        while (this.tweenData.length > 0)
+            this.tweenData.pop();
+    }
+
+    removeByIndex(idx)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.tweenData.splice(idx, 1);
+    }
+
+    seekAll(time)
+    {
+        if (time < 0) return;
+        for (let i = 0; i < this.tweenData.length; i++)
+            this.tweenData[i].t = min(time, this.tweenData[i].duration);
+    }
+
+    seekByIndex(idx, time)
+    {
+        if (idx < 0 && idx >= this.tweenData.length) return;
+        this.tweenData[idx].t = min(time, this.tweenData[idx].duration);
+    }
+
+    allDone()
+    {
+        this.done = true;
+    }
+
     update(delta)
     {
         if (!this.done && this.doneTweens == this.tweenData.length) this.allDone();
@@ -183,9 +261,4 @@ class Tween extends GameObject
         for (let i = 0; i < this.children.length; i++)
             this.children[i].update(delta);
     }
-
-    allDone()
-    {
-        this.done = true;
-    }
 }

+ 26 - 7
src/sketch.js

@@ -1,4 +1,4 @@
-let test;
+let test, but, bu2;
 
 class TestObject extends Object2D
 {
@@ -7,8 +7,10 @@ class TestObject extends Object2D
         this.position.y = 300;
         this.position.x = 100;
 
-        this.timer = new Timer("myTimer", 2);
-        this.addChild(this.timer);
+        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);
     }
 
     _update(delta)
@@ -18,7 +20,7 @@ class TestObject extends Object2D
 
     _draw(delta)
     {
-
+        ellipse(0, 0, 20);
     }
 }
 
@@ -26,7 +28,15 @@ class TestButton extends Button
 {
     _onMousePressed()
     {
-        this.getParent().getChildByName("myTimer").start();
+        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);
+
     }
 }
 
@@ -46,15 +56,24 @@ function setup()
     textFont(AssetHandler.getP5FontByName("Lato"));
 
     test = new TestObject("myTest");
-    but = new TestButton("myTestButton", "play");
+    but = new TestButton("b1", "play");
     but.setPosition(0, 200);
     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);
-    background(220);
 }
 
 function draw()
 {
+    background(220);
     GameHandler.update();
     GameHandler.draw();
 }