diff --git a/src/main/java/com/shc/silenceengine/collision/colliders/SceneCollider2D.java b/src/main/java/com/shc/silenceengine/collision/colliders/SceneCollider2D.java index 155b48ea..95a227d8 100644 --- a/src/main/java/com/shc/silenceengine/collision/colliders/SceneCollider2D.java +++ b/src/main/java/com/shc/silenceengine/collision/colliders/SceneCollider2D.java @@ -26,8 +26,7 @@ import com.shc.silenceengine.collision.broadphase.IBroadphase2D; import com.shc.silenceengine.math.Vector2; -import com.shc.silenceengine.scene.Scene; -import com.shc.silenceengine.scene.SceneNode; +import com.shc.silenceengine.scene.Scene2D; import com.shc.silenceengine.scene.entity.Entity2D; import java.util.ArrayList; @@ -57,8 +56,8 @@ public class SceneCollider2D // The collision map, used to store registered classes private Map, List>> collisionMap = new HashMap<>(); - // The Scene and the grid - private Scene scene; + // The Scene and the broadphase + private Scene2D scene; private IBroadphase2D broadphase; // Number of children in the scene @@ -76,7 +75,7 @@ public SceneCollider2D(IBroadphase2D broadphase) /** * @return The scene that this ISceneCollider2D is using to resolve collisions. */ - public Scene getScene() + public Scene2D getScene() { return scene; } @@ -86,7 +85,7 @@ public Scene getScene() * * @param scene The scene to be used. */ - public void setScene(Scene scene) + public void setScene(Scene2D scene) { this.scene = scene; } @@ -114,28 +113,23 @@ public void register(Class type1, Class public void checkCollisions() { // If there are no children in the scene, simply return - if (scene.getChildren() == null || scene.getChildren().size() == 0) + if (scene.getEntities().size() == 0) { childrenInScene = 0; return; } // Update the list of entities from the list of children in the scene - if (scene.getChildren().size() != childrenInScene) + if (scene.getEntities().size() != childrenInScene) { entities.clear(); broadphase.clear(); childrenInScene = 0; - for (SceneNode child : scene.getChildren()) + for (Entity2D entity : scene.getEntities()) { - if (child instanceof Entity2D) - { - Entity2D entity = (Entity2D) child; - - broadphase.insert(entity); - entities.add(entity); - } + broadphase.insert(entity); + entities.add(entity); childrenInScene++; } diff --git a/src/main/java/com/shc/silenceengine/graphics/Sprite.java b/src/main/java/com/shc/silenceengine/graphics/Sprite.java index 4efbbf98..3ae1f66d 100644 --- a/src/main/java/com/shc/silenceengine/graphics/Sprite.java +++ b/src/main/java/com/shc/silenceengine/graphics/Sprite.java @@ -93,6 +93,11 @@ public void update(float delta) animation.update(delta); } + public Sprite copy() + { + return new Sprite(animation, scaleX, scaleY, rotation); + } + public Texture getTexture() { return animation.getCurrentFrame(); diff --git a/src/main/java/com/shc/silenceengine/scene/Scene2D.java b/src/main/java/com/shc/silenceengine/scene/Scene2D.java new file mode 100644 index 00000000..19924dcb --- /dev/null +++ b/src/main/java/com/shc/silenceengine/scene/Scene2D.java @@ -0,0 +1,109 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2015 Sri Harsha Chilakapati + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.shc.silenceengine.scene; + +import com.shc.silenceengine.core.SilenceEngine; +import com.shc.silenceengine.graphics.SpriteBatch; +import com.shc.silenceengine.scene.entity.Entity2D; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Sri Harsha Chilakapati + */ +public class Scene2D +{ + private List entities; + + public Scene2D() + { + entities = new ArrayList<>(); + } + + public void addChild(Entity2D e) + { + entities.add(e); + } + + public void removeChild(Entity2D e) + { + entities.remove(e); + } + + public void update(float delta) + { + for (int i = 0; i < entities.size(); i++) + { + Entity2D entity = entities.get(i); + entity.preUpdate(delta); + + if (entity.isDestroyed()) + { + removeChild(entity); + i--; + } + } + } + + public void render(float delta) + { + // Sort the entities based on depth +// entities.sort((e1, e2) -> Integer.compare(e2.getDepth(), e1.getDepth())); + + // Render the entities in batches of depths + int depth = entities.get(0).getDepth(); + + SpriteBatch batch = SilenceEngine.graphics.getSpriteBatch(); + batch.begin(); + { + for (Entity2D entity : entities) + { + if (entity.getDepth() != depth) + { + batch.end(); + depth = entity.getDepth(); + batch.begin(); + } + + entity.render(delta, batch); + } + } + batch.end(); + } + + public void destroy() + { + for (Entity2D entity : entities) + entity.destroy(); + + entities.clear(); + } + + public List getEntities() + { + return entities; + } +} diff --git a/src/main/java/com/shc/silenceengine/scene/SceneNode.java b/src/main/java/com/shc/silenceengine/scene/SceneNode.java index c68935e0..bf5796e5 100644 --- a/src/main/java/com/shc/silenceengine/scene/SceneNode.java +++ b/src/main/java/com/shc/silenceengine/scene/SceneNode.java @@ -28,11 +28,9 @@ import com.shc.silenceengine.graphics.Batcher; import com.shc.silenceengine.graphics.opengl.GL3Context; import com.shc.silenceengine.math.Transform; -import com.shc.silenceengine.scene.entity.Entity2D; import org.lwjgl.opengl.GL11; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -88,18 +86,6 @@ public void addChild(SceneNode child) children.add(child); child.setParent(this); child.init(); - - if (child instanceof Entity2D) - { - // Sort the Entity2D's in children based on their depth - Collections.sort(children, (SceneNode c1, SceneNode c2) -> - { - if (c1 instanceof Entity2D && c2 instanceof Entity2D) - return ((Integer) ((Entity2D) c2).getDepth()).compareTo(((Entity2D) c1).getDepth()); - - return 0; - }); - } } public SceneNode getParent() diff --git a/src/main/java/com/shc/silenceengine/scene/entity/Entity2D.java b/src/main/java/com/shc/silenceengine/scene/entity/Entity2D.java index e635d543..b621b87f 100644 --- a/src/main/java/com/shc/silenceengine/scene/entity/Entity2D.java +++ b/src/main/java/com/shc/silenceengine/scene/entity/Entity2D.java @@ -25,12 +25,12 @@ package com.shc.silenceengine.scene.entity; import com.shc.silenceengine.collision.Collision2D; -import com.shc.silenceengine.graphics.Batcher; +import com.shc.silenceengine.graphics.Sprite; +import com.shc.silenceengine.graphics.SpriteBatch; import com.shc.silenceengine.math.Vector2; -import com.shc.silenceengine.math.Vector3; import com.shc.silenceengine.math.geom2d.Polygon; import com.shc.silenceengine.math.geom2d.Rectangle; -import com.shc.silenceengine.scene.SceneNode; +import com.shc.silenceengine.utils.IDGenerator; /** *

This class represents all the 2D Entities in a Scene. Any entity which is 2D and wants to be in a Scene must @@ -50,11 +50,6 @@ * // Update the entity here * } * - * public void render(float delta, Batcher batcher) - * { - * // Render the entity to the screen - * } - * * public void collision(Entity2D other) * { * // Process collisions here @@ -67,8 +62,11 @@ * * @author Sri Harsha Chilakapati */ -public class Entity2D extends SceneNode +public class Entity2D { + // The sprite + private Sprite sprite; + // The position, velocity and the polygon private Vector2 position; private Vector2 velocity; @@ -78,26 +76,25 @@ public class Entity2D extends SceneNode // The higher the depth, the first the object is rendered. private int depth; + private int id; + + private boolean destroyed; + /** * Constructs a Entity2D to use a Polygon that can be used to perform collisions. * * @param polygon The collision mask. */ - public Entity2D(Polygon polygon) + public Entity2D(Sprite sprite, Polygon polygon) { - this(); + setSprite(sprite); this.polygon = polygon; - } - /** - * The default constructor. - */ - public Entity2D() - { position = new Vector2(); velocity = new Vector2(); depth = 0; + id = IDGenerator.generate(); } /** @@ -106,7 +103,6 @@ public Entity2D() * * @param delta The delta time. */ - @Override public void preUpdate(float delta) { if (isDestroyed()) @@ -122,28 +118,14 @@ public void preUpdate(float delta) // Calculate the new position setPosition(tempVec2.set(position).addSelf(velocity)); - // Setup the local transform - getLocalTransform().reset().translateSelf(tempVec2.set(getPosition()).subtractSelf(getCenter())) - .rotateSelf(Vector3.AXIS_Z, polygon.getRotation()) - .translateSelf(getCenter()); - Vector2.REUSABLE_STACK.push(tempVec2); + + sprite.update(delta); + sprite.setRotation(getRotation()); } - /** - * Prepares this Entity2D for a new frame. This method is not meant to be called by the user and is called by the - * SceneGraph. - * - * @param delta The delta time. - * @param batcher The Batcher to batch rendering. - */ - @Override - public void preRender(float delta, Batcher batcher) + public void update(float delta) { - if (isDestroyed()) - return; - - super.preRender(delta, batcher); } /** @@ -163,14 +145,6 @@ public void setPosition(Vector2 position) { this.position.set(position); polygon.setPosition(position); - - Vector2 tempVec2 = Vector2.REUSABLE_STACK.pop(); - - getLocalTransform().reset().translateSelf(tempVec2.set(getPosition()).subtractSelf(getCenter())) - .rotateSelf(Vector3.AXIS_Z, polygon.getRotation()) - .translateSelf(getCenter()); - - Vector2.REUSABLE_STACK.push(tempVec2); } /** @@ -194,15 +168,11 @@ public void setCenter(Vector2 center) Vector2 tempVec2 = Vector2.REUSABLE_STACK.pop(); - getLocalTransform().reset().translateSelf(tempVec2.set(getPosition()).subtractSelf(getCenter())) - .rotateSelf(Vector3.AXIS_Z, polygon.getRotation()) - .translateSelf(getCenter()); - Vector2.REUSABLE_STACK.push(tempVec2); } /** - * Called by the ISceneCollider2D instance to notify that a collision event has occurred. + * Called by the SceneCollider2D instance to notify that a collision event has occurred. * * @param other The other entity that collided with this entity. */ @@ -293,6 +263,20 @@ public void bounce(Entity2D other) alignNextTo(other); } + Vector2 temp = new Vector2(); + Vector2 temp2 = new Vector2(); + + public void render(float delta, SpriteBatch batch) + { + temp.set(getPosition()); + temp2.set(getVelocity()); + temp2.normalizeSelf(); + temp2.scaleSelf(delta); + temp.addSelf(temp2); + + batch.addSprite(sprite, temp); + } + /** * @return The width of the bounding rectangle of this entity */ @@ -355,14 +339,6 @@ public void setPolygon(Polygon polygon) public void rotate(float angle) { polygon.rotate(angle); - - Vector2 tempVec2 = Vector2.REUSABLE_STACK.pop(); - - getLocalTransform().reset().translateSelf(tempVec2.set(getPosition()).subtractSelf(getCenter())) - .rotateSelf(Vector3.AXIS_Z, polygon.getRotation()) - .translateSelf(getCenter()); - - Vector2.REUSABLE_STACK.push(tempVec2); } public int getDepth() @@ -392,14 +368,12 @@ public float getRotation() public void setRotation(float rotation) { polygon.setRotation(rotation); + sprite.setRotation(rotation); + } - Vector2 tempVec2 = Vector2.REUSABLE_STACK.pop(); - - getLocalTransform().reset().translateSelf(tempVec2.set(getPosition()).subtractSelf(getCenter())) - .rotateSelf(Vector3.AXIS_Z, polygon.getRotation()) - .translateSelf(getCenter()); - - Vector2.REUSABLE_STACK.push(tempVec2); + public int getID() + { + return id; } /** @@ -468,6 +442,11 @@ public Vector2 getVelocity() return velocity; } + public boolean isDestroyed() + { + return destroyed; + } + /** * Sets the velocity of this entity * @@ -478,6 +457,21 @@ public void setVelocity(Vector2 velocity) this.velocity.set(velocity); } + public void destroy() + { + destroyed = true; + } + + public Sprite getSprite() + { + return sprite; + } + + public void setSprite(Sprite sprite) + { + this.sprite = sprite.copy(); + } + @Override public int hashCode() { diff --git a/src/main/java/com/shc/silenceengine/tests/DynamicTreeSceneColliderTest2D.java b/src/main/java/com/shc/silenceengine/tests/DynamicTreeSceneColliderTest2D.java index 9f0e5fb6..15374072 100644 --- a/src/main/java/com/shc/silenceengine/tests/DynamicTreeSceneColliderTest2D.java +++ b/src/main/java/com/shc/silenceengine/tests/DynamicTreeSceneColliderTest2D.java @@ -31,24 +31,28 @@ import com.shc.silenceengine.core.SilenceEngine; import com.shc.silenceengine.graphics.Batcher; import com.shc.silenceengine.graphics.Color; +import com.shc.silenceengine.graphics.Sprite; import com.shc.silenceengine.graphics.cameras.OrthoCam; import com.shc.silenceengine.graphics.opengl.GL3Context; +import com.shc.silenceengine.graphics.opengl.Texture; import com.shc.silenceengine.input.Keyboard; import com.shc.silenceengine.math.Vector2; import com.shc.silenceengine.math.geom2d.Rectangle; -import com.shc.silenceengine.scene.Scene; +import com.shc.silenceengine.scene.Scene2D; import com.shc.silenceengine.scene.entity.Entity2D; -import com.shc.silenceengine.utils.RenderUtils; /** * @author Sri Harsha Chilakapati */ public class DynamicTreeSceneColliderTest2D extends Game { - private Scene scene; + private Scene2D scene; private SceneCollider2D collider; private OrthoCam cam; + private Texture playerTexture; + private Texture boxTexture; + public static void main(String[] args) { new DynamicTreeSceneColliderTest2D().start(); @@ -58,12 +62,15 @@ public void init() { Display.setTitle("DynamicTree Collider Test 2D"); + playerTexture = Texture.fromColor(Color.DARK_RED, 48, 48); + boxTexture = Texture.fromColor(Color.CORN_FLOWER_BLUE, 48, 48); + GL3Context.clearColor(Color.DARK_SLATE_GRAY); cam = new OrthoCam().initProjection(Display.getWidth(), Display.getHeight()); // Create and initialize the scene - scene = new Scene(); + scene = new Scene2D(); for (int i = 0; i < 20; i++) { scene.addChild(new Box(new Vector2(48 * i, 0))); @@ -73,7 +80,6 @@ public void init() scene.addChild(new Box(new Vector2(48 * 19, 48 * i))); } scene.addChild(new Player(new Vector2(Display.getWidth() / 2 - 24, Display.getHeight() / 2 - 24))); - scene.init(); // Create the SceneCollider and set the scene collider = new SceneCollider2D(new DynamicTree2D()); @@ -82,7 +88,7 @@ public void init() // Register entities for collisions collider.register(Player.class, Box.class); - System.out.println(scene.getChildren().size()); + System.out.println(scene.getEntities().size()); } public void resize() @@ -108,40 +114,31 @@ public void update(float delta) public void render(float delta, Batcher batcher) { cam.apply(); - scene.render(delta, batcher); + scene.render(delta); } public void dispose() { scene.destroy(); + playerTexture.dispose(); + boxTexture.dispose(); } public class Box extends Entity2D { public Box(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(boxTexture), new Rectangle(48, 48)); setPosition(position); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), Color.CORN_FLOWER_BLUE); - RenderUtils.tracePolygon(batcher, getPolygon(), Color.RED); - } } public class Player extends Entity2D { - private Color color; - private Vector2 temp = new Vector2(); - public Player(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(playerTexture), new Rectangle(48, 48)); setPosition(position); - - color = Color.random(); } public void update(float delta) @@ -167,16 +164,8 @@ public void update(float delta) public void collision(Entity2D other) { - color = Color.random(); - alignNextTo(other); bounce(other); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), temp.set(getVelocity()), color); - RenderUtils.tracePolygon(batcher, getPolygon(), temp.set(getVelocity()), Color.GREEN); - } } } diff --git a/src/main/java/com/shc/silenceengine/tests/GridSceneColliderTest.java b/src/main/java/com/shc/silenceengine/tests/GridSceneColliderTest.java index 4bd1bc81..f0defb76 100644 --- a/src/main/java/com/shc/silenceengine/tests/GridSceneColliderTest.java +++ b/src/main/java/com/shc/silenceengine/tests/GridSceneColliderTest.java @@ -28,25 +28,31 @@ import com.shc.silenceengine.collision.colliders.SceneCollider2D; import com.shc.silenceengine.core.Display; import com.shc.silenceengine.core.Game; +import com.shc.silenceengine.core.SilenceEngine; import com.shc.silenceengine.graphics.Batcher; import com.shc.silenceengine.graphics.Color; +import com.shc.silenceengine.graphics.Sprite; import com.shc.silenceengine.graphics.cameras.OrthoCam; +import com.shc.silenceengine.graphics.opengl.GL3Context; +import com.shc.silenceengine.graphics.opengl.Texture; import com.shc.silenceengine.input.Keyboard; import com.shc.silenceengine.math.Vector2; import com.shc.silenceengine.math.geom2d.Rectangle; -import com.shc.silenceengine.scene.Scene; +import com.shc.silenceengine.scene.Scene2D; import com.shc.silenceengine.scene.entity.Entity2D; -import com.shc.silenceengine.utils.RenderUtils; /** * @author Sri Harsha Chilakapati */ public class GridSceneColliderTest extends Game { - private Scene scene; + private Scene2D scene; private SceneCollider2D collider; private OrthoCam cam; + private Texture playerTexture; + private Texture boxTexture; + public static void main(String[] args) { new GridSceneColliderTest().start(); @@ -54,13 +60,17 @@ public static void main(String[] args) public void init() { - Display.setTitle("GridSceneCollider Test"); - Display.setFullScreen(true); + Display.setTitle("Grid Collider Test 2D"); + + playerTexture = Texture.fromColor(Color.DARK_RED, 48, 48); + boxTexture = Texture.fromColor(Color.CORN_FLOWER_BLUE, 48, 48); + + GL3Context.clearColor(Color.DARK_SLATE_GRAY); cam = new OrthoCam().initProjection(Display.getWidth(), Display.getHeight()); // Create and initialize the scene - scene = new Scene(); + scene = new Scene2D(); for (int i = 0; i < 20; i++) { scene.addChild(new Box(new Vector2(48 * i, 0))); @@ -70,14 +80,15 @@ public void init() scene.addChild(new Box(new Vector2(48 * 19, 48 * i))); } scene.addChild(new Player(new Vector2(Display.getWidth() / 2 - 24, Display.getHeight() / 2 - 24))); - scene.init(); // Create the SceneCollider and set the scene - collider = new SceneCollider2D(new Grid(Display.getWidth(), Display.getHeight(), 48, 48)); + collider = new SceneCollider2D(new Grid(48 * 20, 48 * 20, 48, 48)); collider.setScene(scene); // Register entities for collisions collider.register(Player.class, Box.class); + + System.out.println(scene.getEntities().size()); } public void resize() @@ -93,46 +104,41 @@ public void update(float delta) // Update the scene and check for collisions scene.update(delta); collider.checkCollisions(); + + Display.setTitle("Total Memory: " + (getTotalMemory() / 1048576) + + "MB / Free Memory: " + (getFreeMemory() / 1048576) + + "MB / Used Memory: " + (getUsedMemory() / 1048576) + "MB" + + "/ RC: " + SilenceEngine.graphics.renderCallsPerFrame); } public void render(float delta, Batcher batcher) { cam.apply(); - scene.render(delta, batcher); + scene.render(delta); } public void dispose() { scene.destroy(); + playerTexture.dispose(); + boxTexture.dispose(); } public class Box extends Entity2D { public Box(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(boxTexture), new Rectangle(48, 48)); setPosition(position); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), Color.CORN_FLOWER_BLUE); - RenderUtils.tracePolygon(batcher, getPolygon(), Color.RED); - } } public class Player extends Entity2D { - private Color color; - public Player(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(playerTexture), new Rectangle(48, 48)); setPosition(position); - - color = Color.random(); - - rotate(45); } public void update(float delta) @@ -152,20 +158,14 @@ public void update(float delta) getVelocity().x = +speed; cam.center(getPolygon().getCenter()); + + rotate(90 * delta); } public void collision(Entity2D other) { - color = Color.random(); - alignNextTo(other); bounce(other); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), getVelocity().scale(delta), color); - RenderUtils.tracePolygon(batcher, getPolygon(), getVelocity().scale(delta), Color.GREEN); - } } } diff --git a/src/main/java/com/shc/silenceengine/tests/QuadTreeSceneColliderTest.java b/src/main/java/com/shc/silenceengine/tests/QuadTreeSceneColliderTest.java index da081ba0..8ddf31d5 100644 --- a/src/main/java/com/shc/silenceengine/tests/QuadTreeSceneColliderTest.java +++ b/src/main/java/com/shc/silenceengine/tests/QuadTreeSceneColliderTest.java @@ -28,26 +28,31 @@ import com.shc.silenceengine.collision.colliders.SceneCollider2D; import com.shc.silenceengine.core.Display; import com.shc.silenceengine.core.Game; +import com.shc.silenceengine.core.SilenceEngine; import com.shc.silenceengine.graphics.Batcher; import com.shc.silenceengine.graphics.Color; +import com.shc.silenceengine.graphics.Sprite; import com.shc.silenceengine.graphics.cameras.OrthoCam; import com.shc.silenceengine.graphics.opengl.GL3Context; +import com.shc.silenceengine.graphics.opengl.Texture; import com.shc.silenceengine.input.Keyboard; import com.shc.silenceengine.math.Vector2; import com.shc.silenceengine.math.geom2d.Rectangle; -import com.shc.silenceengine.scene.Scene; +import com.shc.silenceengine.scene.Scene2D; import com.shc.silenceengine.scene.entity.Entity2D; -import com.shc.silenceengine.utils.RenderUtils; /** * @author Sri Harsha Chilakapati */ public class QuadTreeSceneColliderTest extends Game { - private Scene scene; + private Scene2D scene; private SceneCollider2D collider; private OrthoCam cam; + private Texture playerTexture; + private Texture boxTexture; + public static void main(String[] args) { new QuadTreeSceneColliderTest().start(); @@ -55,14 +60,17 @@ public static void main(String[] args) public void init() { - Display.setTitle("QuadTreeCollider Test"); + Display.setTitle("QuadTree Collider Test 2D"); + + playerTexture = Texture.fromColor(Color.DARK_RED, 48, 48); + boxTexture = Texture.fromColor(Color.CORN_FLOWER_BLUE, 48, 48); GL3Context.clearColor(Color.DARK_SLATE_GRAY); cam = new OrthoCam().initProjection(Display.getWidth(), Display.getHeight()); // Create and initialize the scene - scene = new Scene(); + scene = new Scene2D(); for (int i = 0; i < 20; i++) { scene.addChild(new Box(new Vector2(48 * i, 0))); @@ -72,14 +80,15 @@ public void init() scene.addChild(new Box(new Vector2(48 * 19, 48 * i))); } scene.addChild(new Player(new Vector2(Display.getWidth() / 2 - 24, Display.getHeight() / 2 - 24))); - scene.init(); // Create the SceneCollider and set the scene - collider = new SceneCollider2D(new QuadTree(Display.getWidth(), Display.getHeight())); + collider = new SceneCollider2D(new QuadTree(48 * 20, 48 * 20)); collider.setScene(scene); // Register entities for collisions collider.register(Player.class, Box.class); + + System.out.println(scene.getEntities().size()); } public void resize() @@ -96,45 +105,40 @@ public void update(float delta) scene.update(delta); collider.checkCollisions(); - Display.setTitle("Total Memory: " + (getTotalMemory() / 1048576) + "MB / Free Memory: " + (getFreeMemory() / 1048576) + "MB / Used Memory: " + (getUsedMemory() / 1048576) + "MB"); + Display.setTitle("Total Memory: " + (getTotalMemory() / 1048576) + + "MB / Free Memory: " + (getFreeMemory() / 1048576) + + "MB / Used Memory: " + (getUsedMemory() / 1048576) + "MB" + + "/ RC: " + SilenceEngine.graphics.renderCallsPerFrame); } public void render(float delta, Batcher batcher) { cam.apply(); - scene.render(delta, batcher); + scene.render(delta); } public void dispose() { scene.destroy(); + playerTexture.dispose(); + boxTexture.dispose(); } public class Box extends Entity2D { public Box(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(boxTexture), new Rectangle(48, 48)); setPosition(position); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), Color.CORN_FLOWER_BLUE); - RenderUtils.tracePolygon(batcher, getPolygon(), Color.RED); - } } public class Player extends Entity2D { - private Color color; - public Player(Vector2 position) { - setPolygon(new Rectangle(0, 0, 48, 48)); + super(new Sprite(playerTexture), new Rectangle(48, 48)); setPosition(position); - - color = Color.random(); } public void update(float delta) @@ -160,16 +164,8 @@ public void update(float delta) public void collision(Entity2D other) { - color = Color.random(); - alignNextTo(other); bounce(other); } - - public void render(float delta, Batcher batcher) - { - RenderUtils.fillPolygon(batcher, getPolygon(), getVelocity().scale(delta), color); - RenderUtils.tracePolygon(batcher, getPolygon(), getVelocity().scale(delta), Color.GREEN); - } } }