Przeglądaj źródła

✨ Add Srptie2D game object and Texture resource

Pedro Schneider 3 lat temu
rodzic
commit
3aa8e4fea0

+ 4 - 0
index.html

@@ -26,9 +26,13 @@
     <script src="pandora/game_objects/GameObject.js"></script>
     <script src="pandora/game_objects/2d_objects/Object2D.js"></script>
     <script src="pandora/game_objects/2d_objects/Shape2D.js"></script>
+    <script src="pandora/game_objects/2d_objects/Sprite2D.js"></script>
 
     <!-- Handlers -->
     <script src="pandora/handlers/GameHandler.js"></script>
+
+    <!-- Resources -->
+    <script src="pandora/resources/Texture.js"></script>
     
     <!-- Singletons -->
     <script src="pandora/Enums.js"></script>

+ 27 - 0
pandora/game_objects/2d_objects/Sprite2D.js

@@ -0,0 +1,27 @@
+class Sprite2D extends Object2D
+{
+    constructor(name, p5Image)
+    {
+        super(name);
+
+        if (p5Image)
+            this.texture = new Texture(p5Image, p5Image.width, p5Image.height);
+    }
+
+    draw(delta)
+    {
+        push();
+        translate(this.position.x, this.position.y);
+        rotate(this.rotationDegrees);
+        scale(this.scale.x, this.scale.y);
+
+        image(this.texture.image, this.position.x, this.position.y, this.texture.width, this.texture.height);
+
+        this._draw(delta);
+
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].draw(delta);
+
+        pop()
+    }
+}

+ 9 - 0
pandora/resources/Texture.js

@@ -0,0 +1,9 @@
+class Texture
+{
+    constructor(image = null, width = 0, height = 0)
+    {
+        this.image = image;
+        this.width = width;
+        this.height = height;
+    }
+}