Browse Source

Added 'jbox' @ v1.0 (Technically dev version)

jbox is currently under active development
david 7 months ago
parent
commit
455509e35a
2 changed files with 65 additions and 0 deletions
  1. 1 0
      README.md
  2. 64 0
      jbox.js

+ 1 - 0
README.md

@@ -7,3 +7,4 @@ A Collection of Kaplay.js components
 ## Components Listing
 
 - mana, Similar to the built-in [health](https://kaplayjs.com/doc/ctx/health/) component
+- jbox, A JSON component

+ 64 - 0
jbox.js

@@ -0,0 +1,64 @@
+/*
+jbox
+A JSON Component
+Version: 1.0
+
+Docs:
+- jbox(tags=[])
+    - pack() json
+
+> jbox renders it's version in debug.inspect mode
+
+Currently Supported Components:
+- pos (Obtains X and Y)
+- health (Obtains current HP and maxHP)
+- scale (Obtains scale.X and scale.Y)
+- rotate (Obtains angle, in degrees)
+*/
+
+function jbox(tags=[]) {
+    let config = {
+        tags: tags,
+    };
+    return {
+        id: "jbox",
+        require: tags,
+        inspect() {
+            return "[[email protected]]"
+        },
+        pack() {
+            let j = {
+                jbox: "[[email protected]]",
+                comps: {},
+            };
+            for (let tag in config.tags) {
+                switch (tag) {
+                    case "pos": {
+                        j.comps["pos"] = {
+                            x: this.pos.x,
+                            y: this.pos.y,
+                        }
+                    }
+                    case "health": {
+                        j.comps["health"] = {
+                            health: this.hp(),
+                            max_health: this.maxHP(),
+                        }
+                    }
+                    case "scale": {
+                        j.comps["scale"] = {
+                            x: this.scale.x,
+                            y: this.scale.y,
+                        }
+                    }
+                    case "rotate": {
+                        j.comps["rotate"] = {
+                            angle: this.angle,
+                        }
+                    }
+                }
+            }
+            return j;
+        }
+    }
+}