Parcourir la source

✨ Start work on the pandora engine

- Implement parenting of game objects,  _update and _draw callbacks;
- Add Vector2 and Rect components;
- Add GameObject base class;
- Add Object2D and Shape2D game objects;
- Add GameHandler handler;
- Add Enums singleton;
- Change Sketch test out the new features of the engine.
Pedro Schneider il y a 3 ans
Parent
commit
1f0911fea6

+ 23 - 2
index.html

@@ -4,14 +4,35 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 
-    <title>Sketch</title>
+    <title>Project Edu</title>
 
     <link rel="stylesheet" type="text/css" href="style.css">
 
+    <!--******************************
+      p5Js Modules
+    *******************************-->
     <script src="libraries/p5.min.js"></script>
     <script src="libraries/p5.sound.min.js"></script>
-  </head>
+    
+    <!--******************************
+      Pandora Engine
+    *******************************-->
+    <!-- Components -->
+    <script src="pandora/components/Rect.js"></script>
+    <script src="pandora/components/Vector2.js"></script>
+    
+    <!-- Game Objects -->
+    <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>
 
+    <!-- Handlers -->
+    <script src="pandora/handlers/GameHandler.js"></script>
+    
+    <!-- Singletons -->
+    <script src="pandora/Enums.js"></script>
+  </head>
+  
   <body>
     <script src="src/sketch.js"></script>
   </body>

+ 4 - 0
pandora/Enums.js

@@ -0,0 +1,4 @@
+const SHAPES = {
+    RECT: 1,
+    ELLIPSE: 2,
+}

+ 10 - 0
pandora/components/Rect.js

@@ -0,0 +1,10 @@
+class Rect
+{
+    constructor(x, y, w, h)
+    {
+        this.x = x;
+        this.y = y;
+        this.w = w;
+        this.h = h;
+    }
+}

+ 65 - 0
pandora/components/Vector2.js

@@ -0,0 +1,65 @@
+class Vector2
+{
+    static ZERO()
+    {
+        return new Vector2(0, 0);
+    }
+    static ONE()
+    {
+        return new Vector2(1, 1);
+    }
+    static RIGHT()
+    {
+        return new Vector2(1, 0);
+    }
+    static LEFT()
+    {
+        return new Vector2(-1, 0);
+    }
+    static UP()
+    {
+        return new Vector2(0, -1);
+    }
+    static DOWN()
+    {
+        return new Vector2(0, 1);
+    }
+
+    constructor(x, y)
+    {
+        this.x = x;
+        this.y = y;
+    }
+
+    // Methods
+    abs()
+    {
+        return new Vector2(abs(this.x), abs(this.y));
+    }
+
+    angle()
+    {
+        return atan2(this.y, this.x);
+    }
+
+    lengthSquared()
+    {
+        return this.x * this.x + this.y * this.y;
+    }
+
+    length()
+    {
+        return sqrt(this.lengthSquared());
+    }
+
+    normalized()
+    {
+        let len = this.length();
+        return new Vector2(this.x / len, this.y / len);
+    }
+
+    distanceSquaredTo(v)
+    {
+        return new Vector2(v.x - this.x, v.y - this.y).length();
+    }
+}

+ 25 - 0
pandora/game_objects/2d_objects/Object2D.js

@@ -0,0 +1,25 @@
+class Object2D extends GameObject
+{
+    constructor(name)
+    {
+        super(name);
+
+        this.position = Vector2.ZERO();
+        this.rotationDegrees = 0;
+        this.scale = Vector2.ONE();
+    }
+
+    draw(delta)
+    {
+        push();
+        translate(this.position.x, this.position.y);
+        rotate(this.rotationDegrees);
+        scale(this.scale.x, this.scale.y);
+        this._draw(delta);
+
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].draw(delta);
+
+        pop()
+    }
+}

+ 10 - 0
pandora/game_objects/2d_objects/Shape2D.js

@@ -0,0 +1,10 @@
+class Shape2D extends Object2D
+{
+    constructor(name, shape = null, data = null)
+    {
+        super(name);
+
+        this.shape = shape;
+        this.data = data;
+    }
+}

+ 79 - 0
pandora/game_objects/GameObject.js

@@ -0,0 +1,79 @@
+class GameObject
+{
+    constructor(name)
+    {
+        this.id = 0;
+
+        this.name = name;
+
+        this.children = [];
+        this.parented = false;
+        this.parent = null;
+
+        GameHandler.instanceGameObject(this);
+
+    }
+
+
+
+
+
+    // Getters
+    getChildren()
+    {
+        return this.children;
+    }
+
+    getChildByIndex(idx)
+    {
+        if (idx >= 0 && idx < this.children.length)
+            return this.children[idx];
+        return null;
+    }
+
+    getChildByName(name)
+    {
+        for (let i = 0; i < this.children.length; i++)
+            if (this.children[i].name == name) return this.children[i];
+        return null;
+    }
+
+    getParent()
+    {
+        if (!this.parented) return null;
+        return this.parent;
+    }
+
+    // Methods
+    addChild(child)
+    {
+        child.parent = this;
+        child.parented = true;
+        this.children.push(child);
+    }
+
+    update(delta)
+    {
+        this._update(delta);
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].update(delta);
+    }
+
+    draw(delta)
+    {
+        this._draw(delta);
+        for (let i = 0; i < this.children.length; i++)
+            this.children[i].draw(delta);
+    }
+
+    // Callbacks
+    _update(delta)
+    {
+
+    }
+
+    _draw(delta)
+    {
+
+    }
+}

+ 41 - 0
pandora/handlers/GameHandler.js

@@ -0,0 +1,41 @@
+class GameHandler
+{
+    static rootObjects = []
+    static nextId = 0;
+
+    static bDrawDebugFPS = false;
+    static drawDebugFPS(val)
+    {
+        this.bDrawDebugFPS = val;
+    }
+
+    static instanceGameObject(obj)
+    {
+        obj.id = this.nextId;
+        this.nextId++;
+    }
+
+    static addRootObject(obj)
+    {
+        this.rootObjects.push(obj);
+    }
+
+    static update()
+    {
+        for (let i = 0; i < this.rootObjects.length; i++)
+            this.rootObjects[i].update(1 / frameRate());
+    }
+
+    static draw()
+    {
+        if (this.bDrawDebugFPS)
+        {
+            textSize(12);
+            stroke(0);
+            text("FPS: " + frameRate(), 10, 10, windowWidth, windowHeight);
+        }
+
+        for (let i = 0; i < this.rootObjects.length; i++)
+            this.rootObjects[i].draw(1 / frameRate());
+    }
+}

+ 50 - 4
src/sketch.js

@@ -1,7 +1,53 @@
-function setup() {
-  createCanvas(400, 400);
+class TestObject extends Object2D
+{
+    constructor(name)
+    {
+        super(name);
+
+        this.position.x = windowWidth / 2;
+        this.position.y = windowHeight / 2;
+    }
+
+    _update(delta)
+    {
+        if (this.name == "myTest")
+        {
+            this.position.x -= 1;
+        }
+    }
+
+    _draw(delta)
+    {
+        ellipse(0, 0, 100, 100)
+    }
+}
+
+let test;
+
+function setup()
+{
+    createCanvas(windowWidth, windowHeight);
+    smooth();
+    GameHandler.drawDebugFPS(true);
+
+    test = new TestObject("myTest");
+    GameHandler.addRootObject(test);
+
+    test.position = new Vector2(windowWidth / 2, windowHeight / 2);
+    test.scale = Vector2.ONE();
+    test.rotationDegrees = 0;
+
+    test.addChild(new TestObject("myChild"));
 }
 
-function draw() {
-  background(220);
+function draw()
+{
+    background(220);
+    GameHandler.update();
+    GameHandler.draw();
 }
+
+function windowResized()
+{
+    resizeCanvas(windowWidth, windowHeight);
+}