Browse Source

⚡️ Modularize some parts of draw and update calls to reduce boilerplate on inheritance

Pedro Schneider 3 years ago
parent
commit
0184da6c5c

File diff suppressed because it is too large
+ 1 - 1
pandora.min.js


+ 2 - 19
pandora/game_objects/2d_objects/AnimatedSprite2D.js

@@ -201,21 +201,7 @@ class AnimatedSprite2D extends Sprite2D
      */
     update(delta)
     {
-        // Update global transform.
-        if (!this.parented || !(this.parent instanceof Object2D))
-        {
-            this.globalPosition = this.position;
-            this.globalRotationDegrees = this.rotationDegrees;
-            this.globalScale = this.globalScale;
-        }
-        else
-        {
-            this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
-            this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
-            this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
-            this.globalScale.x = this.parent.globalScale.x * this.scale.x;
-            this.globalScale.y = this.parent.globalScale.y * this.scale.y;
-        }
+        this.udpateGlobalTransform();
 
         // Forwards the animation.
         if (this.playing)
@@ -229,9 +215,6 @@ class AnimatedSprite2D extends Sprite2D
         }
         this.P5Image = this.getCurrentFrame();
 
-        // Callbacks
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].update(delta);
+        this.updateChildren(delta);
     }
 }

+ 6 - 28
pandora/game_objects/2d_objects/Area2D.js

@@ -103,27 +103,13 @@ class Area2D extends Object2D
      */
     update(delta)
     {
-        // Updates global transform
-        if (!this.parented || !(this.parent instanceof Object2D))
-        {
-            this.globalPosition = this.position;
-            this.globalRotationDegrees = this.rotationDegrees;
-            this.globalScale = this.globalScale;
-        }
-        else
-        {
-            this.globalPosition.x = this.parent.globalPosition.x + this.position.x;
-            this.globalPosition.y = this.parent.globalPosition.y + this.position.y;
-            this.globalRotationDegrees = this.parent.globalRotationDegrees + this.rotationDegrees;
-            this.globalScale.x = this.parent.globalScale.x * this.scale.x;
-            this.globalScale.y = this.parent.globalScale.y * this.scale.y;
-        }
+        this.udpateGlobalTransform();
 
         // Checks collision with mouse
         if (this.listenToMouse)
         {
             if (Collisions[this.shapeName].point(this.globalPosition.x, this.globalPosition.y, this.globalRotationDegrees, this.globalScale.x,
-                this.globalScale.y, this.shape, GameHandler.mouseX, GameHandler.mouseY))
+                    this.globalScale.y, this.shape, GameHandler.mouseX, GameHandler.mouseY))
             {
                 if (!this.mouseIn)
                     this.emitSignal("mouseEntered");
@@ -137,10 +123,7 @@ class Area2D extends Object2D
             }
         }
 
-        // Callbacks
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].update(delta);
+        this.updateChildren(delta);
     }
 
     /**
@@ -158,10 +141,9 @@ class Area2D extends Object2D
         if (!this.visible) return;
 
         db.push();
-        db.translate(this.position.x, this.position.y);
-        db.rotate(this.rotationDegrees / 180 * PI);
-        db.scale(this.scale.x, this.scale.y);
+        this.applyTransform(db);
 
+        // Draw the shape for debug purposes.
         if (this.drawDebug)
         {
             db.push();
@@ -181,11 +163,7 @@ class Area2D extends Object2D
             db.pop();
         }
 
-        this._draw(delta, db);
-
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].draw(delta, db);
-
+        this.drawChildren(delta, db);
         db.pop()
     }
 }

+ 33 - 24
pandora/game_objects/2d_objects/Object2D.js

@@ -134,7 +134,7 @@ class Object2D extends GameObject
         this.position.x += x;
         this.position.y += y;
     }
-    
+
     /**
      * Rotates this Object2D by a degrees.
      * 
@@ -158,16 +158,12 @@ class Object2D extends GameObject
     }
 
     /**
-     * Updates this Object2D's global transform and recursively calls the _update(delta)
-     * callback for this GameObject and all of it's children.
-     * 
-     * @param {number} delta    number of ellapsed seconds since the last frame.
+     * Updates this Object2D's global transform.
      * 
-     * @override
+     * ! This method only exists to modularize the code, and should not be used by the user.
      */
-    update(delta)
+    udpateGlobalTransform()
     {
-        // Update global transform
         if (!this.parented || !(this.parent instanceof Object2D))
         {
             this.globalPosition = this.position;
@@ -182,13 +178,34 @@ class Object2D extends GameObject
             this.globalScale.x = this.parent.globalScale.x * this.scale.x;
             this.globalScale.y = this.parent.globalScale.y * this.scale.y;
         }
+    }
 
-        // Callbacks
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-        {
-            this.children[i].update(delta);
-        }
+    /**
+     * Updates this Object2D's global transform and recursively calls the _update(delta)
+     * callback for this GameObject and all of it's children.
+     * 
+     * @param {number} delta    number of ellapsed seconds since the last frame.
+     * 
+     * @override
+     */
+    update(delta)
+    {
+        this.udpateGlobalTransform();
+        this.updateChildren(delta);
+    }
+
+    /**
+     * Apply this Object2D's transform to the secondary buffer.
+     * 
+     * ! This method only exists to modularize the code, and should not be used by the user. 
+     * 
+     * @param {p5.Graphics} db   secondary buffer to draw to. 
+     */
+    applyTransform(db)
+    {
+        db.translate(this.position.x, this.position.y);
+        db.rotate(this.rotationDegrees / 180 * PI);
+        db.scale(this.scale.x, this.scale.y);
     }
 
     /**
@@ -204,17 +221,9 @@ class Object2D extends GameObject
     draw(delta, db)
     {
         if (!this.visible) return;
-
         db.push();
-        db.translate(this.position.x, this.position.y);
-        db.rotate(this.rotationDegrees / 180 * PI);
-        db.scale(this.scale.x, this.scale.y);
-
-        this._draw(delta, db);
-
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].draw(delta, db);
-
+        this.applyTransform(db);
+        this.drawChildren(delta, db);
         db.pop()
     }
 }

+ 2 - 10
pandora/game_objects/2d_objects/Sprite2D.js

@@ -61,17 +61,9 @@ class Sprite2D extends Object2D
     draw(delta, db)
     {
         db.push();
-        db.translate(this.position.x, this.position.y);
-        db.rotate(this.rotationDegrees / 180 * PI);
-        db.scale(this.scale.x, this.scale.y);
-
+        this.applyTransform(db);
         db.image(this.P5Image, 0, 0, this.P5Image.width, this.P5Image.height);
-
-        this._draw(delta, db);
-
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].draw(delta, db);
-
+        this.drawChildren(delta, db);
         db.pop();
     }
 }

+ 33 - 4
pandora/game_objects/GameObject.js

@@ -308,6 +308,23 @@ class GameObject
         }
     }
 
+    /**
+     * Serves as a caller to this GameObject's _update method and recursively updates all of
+     * this GameObject's children.
+     * 
+     * ! This method only exists to modularize the code, and should not be used by the user. 
+     *
+     * @param {number} delta    ellapsed seconds since the last frame.
+     */
+    updateChildren(delta)
+    {
+        this._update(delta);
+        for (let i = 0; i < this.children.length; i++)
+        {
+            this.children[i].update(delta);
+        }
+    }
+
     /**
      * Caller for the _update(delta) callback. Recrusively calls itself for
      * all of this GameOject's children.
@@ -316,25 +333,37 @@ class GameObject
      */
     update(delta)
     {
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].update(delta);
+        this.updateChildren(delta);
     }
 
     /**
      * Caller for the _draw(delta, db) callback. Recursively calls itself for
      * all of this GameObject's children.
      * 
+     * ! This method only exists to modularize the code, and should not be used by the user. 
+     * 
      * @param {number} delta    ellapsed seconds since the last frame. 
      * @param {p5.Graphics} db  secondary buffer to draw on. 
      */
-    draw(delta, db)
+    drawChildren(delta, db)
     {
         this._draw(delta, db);
         for (let i = 0; i < this.children.length; i++)
             this.children[i].draw(delta, db);
     }
 
+    /**
+     * Caller for the _draw(delta, db) callback. Recursively calls itself for
+     * all of this GameObject's children.
+     * 
+     * @param {number} delta    ellapsed seconds since the last frame. 
+     * @param {p5.Graphics} db  secondary buffer to draw on. 
+     */
+    draw(delta, db)
+    {
+        this.drawChildren(delta, db);
+    }
+
     /**
      * ! This function should be overriden, it provides no default functionality.
      * This function is called once when the GameObject is created and should declare

+ 1 - 3
pandora/game_objects/Timer.js

@@ -127,9 +127,7 @@ class Timer extends GameObject
             if (this.timeLeft <= 0) this.onFinish();
         }
 
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].update(delta);
+        this.updateChildren(delta);
     }
 
     /**

+ 3 - 5
pandora/game_objects/Tween.js

@@ -435,7 +435,7 @@ class Tween extends GameObject
         {
             // Ignores TweenData that aren't playing.
             if (!this.tweenData[i].playing) continue;
-            
+
             // Interpolates TweenData that are out of the delay.
             if (this.tweenData[i].t >= 0)
                 this.interpolate(this.tweenData[i]);
@@ -443,7 +443,7 @@ class Tween extends GameObject
             // Checks if the TweenData just went out of the delay (just started).
             if (this.tweenData[i].t <= 0 && this.tweenData[i].t + delta >= 0)
                 this.emitSignal("tweenStarted", this.tweenData[i]);
-            
+
             // Updates TweenData's current time.
             this.tweenData[i].t = min(this.tweenData[i].t + delta, this.tweenData[i].duration);
 
@@ -456,8 +456,6 @@ class Tween extends GameObject
             }
         }
 
-        this._update(delta);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].update(delta);
+        this.updateChildren(delta);
     }
 }

+ 1 - 3
pandora/game_objects/ui_objects/UIObject.js

@@ -336,9 +336,7 @@ class UIObject extends GameObject
             "font-size": `${this.fontSize * ar}px`
         });
 
-        this._draw(delta, db);
-        for (let i = 0; i < this.children.length; i++)
-            this.children[i].draw(delta, db);
+        this.drawChildren(delta, db);
     }
 
     /**