Selaa lähdekoodia

✨ Add SpriteFrames resource

Pedro Schneider 3 vuotta sitten
vanhempi
commit
9361a7727d
2 muutettua tiedostoa jossa 103 lisäystä ja 0 poistoa
  1. 1 0
      index.html
  2. 102 0
      pandora/resources/SpriteFrames.js

+ 1 - 0
index.html

@@ -50,6 +50,7 @@
     <script type="text/javascript" src="pandora/resources/AudioRes.js"></script>
     <script type="text/javascript" src="pandora/resources/FontRes.js"></script>
     <script type="text/javascript" src="pandora/resources/TextureRes.js"></script>
+    <script type="text/javascript" src="pandora/resources/SpriteFrames.js"></script>
     
     <!-- Singletons -->
     <script type="text/javascript" src="pandora/Enums.js"></script>

+ 102 - 0
pandora/resources/SpriteFrames.js

@@ -0,0 +1,102 @@
+class SpriteAnimation
+{
+    constructor(name = "default", p5Image, rows, cols, indices)
+    {
+        this.name = name;
+        this.fullP5Image = p5Image;
+        this.rows = rows;
+        this.columns = cols;
+        this.indices = indices;
+        this.numFrames = this.indices.length;
+        this.frameTime = 1 / 24;
+
+        this.frames = [];
+        for (let i = 0; i < this.numFrames; i++)
+        {
+            this.frames.push(this.makeFrame(i));
+        }
+    }
+
+    setFrameTime(time)
+    {
+        this.frameTime = time;
+    }
+
+    setFPS(fps)
+    {
+        this.frameTime = 1 / fps;
+    }
+
+    getFrame(idx)
+    {
+        if (idx < this.numFrames)
+            return this.frames[idx];
+        return null;
+    }
+
+    getFrameTime()
+    {
+        return this.frameTime;
+    }
+
+    getNumFrames()
+    {
+        return this.numFrames;
+    }
+
+    makeFrame(idx)
+    {
+        let r = int(this.indices[idx] / this.columns);
+        let c = this.indices[idx] % this.columns;
+        let w = this.fullP5Image.width / this.columns;
+        let h = this.fullP5Image.height / this.rows;
+        let x = c * w;
+        let y = r * h;
+        return this.fullP5Image.get(x, y, w, h);
+    }
+}
+
+class SpriteFrames
+{
+    constructor()
+    {
+        this.animations = [];
+        this.numAnimations = 0;
+    }
+
+    getAnimationByIndex(idx)
+    {
+        if (idx < this.numAnimations) return this.animations[idx];
+        return null;
+    }
+
+    getAnimationByName(name)
+    {
+        for (let i = 0; this.numAnimations; i++)
+        {
+            if (this.animations[i].name == name)
+            {
+                return this.animations[i];
+            }
+        }
+        return null;
+    }
+
+    getAnimationIndexByName(name)
+    {
+        for (let i = 0; this.numAnimations; i++)
+        {
+            if (this.animations[i].name == name)
+            {
+                return i;
+            }
+        }
+        return null;
+    }
+
+    addAnimation(name, p5Image, rows, cols, indices)
+    {
+        this.animations.push(new SpriteAnimation(name, p5Image, rows, cols, indices));
+        this.numAnimations++;
+    }
+}