瀏覽代碼

✨ Add AnimatedSprite2D GameObject

Pedro Schneider 3 年之前
父節點
當前提交
1be8b6cde0
共有 2 個文件被更改,包括 100 次插入0 次删除
  1. 1 0
      index.html
  2. 99 0
      pandora/game_objects/2d_objects/AnimatedSprite2D.js

+ 1 - 0
index.html

@@ -30,6 +30,7 @@
       <script type="text/javascript" src="pandora/game_objects/2d_objects/Object2D.js"></script>
       <script type="text/javascript" src="pandora/game_objects/2d_objects/Shape2D.js"></script>
       <script type="text/javascript" src="pandora/game_objects/2d_objects/Sprite2D.js"></script>
+      <script type="text/javascript" src="pandora/game_objects/2d_objects/AnimatedSprite2D.js"></script>
     
       <!-- UI Game Objects -->
       <script type="text/javascript" src="pandora/game_objects/ui_objects/UIObject.js"></script>

+ 99 - 0
pandora/game_objects/2d_objects/AnimatedSprite2D.js

@@ -0,0 +1,99 @@
+class AnimatedSprite2D extends Sprite2D
+{
+    constructor(name, p5Image, spriteFrames)
+    {
+        super(name, p5Image);
+
+        this.spriteFrames = spriteFrames;
+        this.playing = false;
+        this.frame = 0;
+        this.currentAnimation = 0;
+        this.timeSinceLastFrame = 0;
+    }
+
+    // Setters
+    setCurrentAnimationByIndex(idx)
+    {
+        if (idx < this.spriteFrames.numAnimations)
+            this.currentAnimation = idx;
+        else this.currentAnimation = null;
+        this.frame = 0;
+        this.timeSinceLastFrame = 0;
+    }
+
+    setCurrentAnimationByName(name)
+    {
+        this.currentAnimation = this.spriteFrames.getAnimationIndexByName(name);
+        this.frame = 0;
+        this.timeSinceLastFrame = 0;
+    }
+
+    setCurrentFrameTime(time)
+    {
+        this.getCurrentAnimation().setFrameTime(time);
+    }
+
+    setCurrentFPS(fps)
+    {
+        this.getCurrentAnimation().setFPS(fps);
+    }
+
+    // Getters
+    getAnimationByIndex(idx)
+    {
+        return this.spriteFrames.getAnimationByIndex(idx);
+    }
+
+    getCurrentAnimation()
+    {
+        return this.spriteFrames.getAnimationByIndex(this.currentAnimation);
+    }
+
+    getCurrentFrame()
+    {
+        return this.getCurrentAnimation().getFrame(this.frame);
+    }
+
+    getCurrentFrameTime()
+    {
+        return this.getCurrentAnimation().getFrameTime();
+    }
+
+    getCurrentNumFrames()
+    {
+        return this.getCurrentAnimation().getNumFrames();
+    }
+
+    // Methods
+    play()
+    {
+        this.playing = true;
+    }
+
+    stop()
+    {
+        this.playing = false;
+    }
+
+    isPlaying()
+    {
+        return this.playing;
+    }
+
+    update(delta)
+    {
+        if (this.playing)
+        {
+            this.timeSinceLastFrame += delta;
+            if (this.timeSinceLastFrame >= this.getCurrentFrameTime())
+            {
+                this.frame = (this.frame + 1) % this.getCurrentNumFrames();
+                this.timeSinceLastFrame = 0;
+            }
+        }
+        this.P5Image = this.getCurrentFrame();
+        this._update(delta);
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].update(delta);
+    }
+}