Browse Source

✨ Add Timer GameObject

Pedro Schneider 3 years ago
parent
commit
8a42f267bd
3 changed files with 72 additions and 13 deletions
  1. 1 0
      index.html
  2. 61 0
      pandora/game_objects/Timer.js
  3. 10 13
      src/sketch.js

+ 1 - 0
index.html

@@ -32,6 +32,7 @@
     <script type="text/javascript" src="pandora/game_objects/GameObject.js"></script>
     <script type="text/javascript" src="pandora/game_objects/AudioPlayer.js"></script>
     <script type="text/javascript" src="pandora/game_objects/Tween.js"></script>
+    <script type="text/javascript" src="pandora/game_objects/Timer.js"></script>
     
       <!-- 2D Game Objects -->
       <script type="text/javascript" src="pandora/game_objects/2d_objects/Object2D.js"></script>

+ 61 - 0
pandora/game_objects/Timer.js

@@ -0,0 +1,61 @@
+class Timer extends GameObject
+{
+    constructor(name, duration = 1, autostart = false, oneShot = false)
+    {
+        super(name);
+
+        this.duration = duration;
+        this.timeLeft = this.duration;
+        this.paused = !autostart;
+        this.autostart = autostart;
+        this.oneShot = oneShot;
+    }
+
+    start(timeSec = this.duration)
+    {
+        if (!this.paused) return;
+        this.duration = timeSec;
+        this.paused = false;
+        this.timeLeft = this.duration;
+    }
+
+    stop()
+    {
+        this.paused = true;
+    }
+
+    resume()
+    {
+        this.paused = false;
+    }
+
+    isStopped()
+    {
+        return this.paused;
+    }
+
+    update(delta)
+    {
+        if (!this.paused)
+        {
+            this.timeLeft -= delta;
+            if (this.timeLeft <= 0) this.onFinish();
+        }
+
+        this._update(delta);
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].update(delta);
+    }
+
+    onFinish()
+    {
+        if (this.oneShot) this.paused = true
+        this.timeLeft = this.duration;
+        this._onFinish();
+    }
+
+    _onFinish()
+    {
+        console.log("doneskis");
+    }
+}

+ 10 - 13
src/sketch.js

@@ -4,29 +4,21 @@ class TestObject extends Object2D
 {
     _setup()
     {
-        this.currTime = 0;
-        this.duration = 3;
         this.position.y = 300;
         this.position.x = 100;
 
-        this.tween = new Tween("myTween");
-        this.tween.interpolateProperty(this.position, "x", PROPERTY_TYPE.NUMBER, this.position.x, 400, 2, TRANS_TYPE.LINEAR, EASE_TYPE.OUT, 0);
-        // this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 2, TRANS_TYPE.LINEAR, EASE_TYPE.OUT, 0);
-        // this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 2, TRANS_TYPE.SINE, EASE_TYPE.OUT, 0);
-        this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 3, TRANS_TYPE.CUBIC, EASE_TYPE.IN_OUT, 0);
-        // this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 2, TRANS_TYPE.BOUNCE, EASE_TYPE.OUT, 0);
-        // this.tween.interpolateProperty(this.position, "y", PROPERTY_TYPE.NUMBER, this.position.y, 100, 2, TRANS_TYPE.ELASTIC, EASE_TYPE.OUT, 0);
-        this.addChild(this.tween);
+        this.timer = new Timer("myTimer", 2);
+        this.addChild(this.timer);
     }
 
     _update(delta)
     {
-        // console.log(this.color)
+
     }
 
     _draw(delta)
     {
-        ellipse(0, 0, 20, 20);
+
     }
 }
 
@@ -34,7 +26,12 @@ class TestButton extends Button
 {
     _onMousePressed()
     {
-        this.getParent().tween.play();
+        this.getParent().getChildByName("myTimer").start();
+    }
+
+    _onMouseReleased()
+    {
+        this.getParent().getChildByName("myTimer").stop();
     }
 }