Skip to content

Commit

Permalink
#30 - Implemented Scene2D class using SpriteBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
sriharshachilakapati committed Jun 6, 2015
1 parent 9eb31cf commit a976dc8
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,8 +56,8 @@ public class SceneCollider2D
// The collision map, used to store registered classes
private Map<Class<? extends Entity2D>, List<Class<? extends Entity2D>>> 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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -114,28 +113,23 @@ public void register(Class<? extends Entity2D> type1, Class<? extends Entity2D>
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++;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/shc/silenceengine/graphics/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
109 changes: 109 additions & 0 deletions src/main/java/com/shc/silenceengine/scene/Scene2D.java
Original file line number Diff line number Diff line change
@@ -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<Entity2D> 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<Entity2D> getEntities()
{
return entities;
}
}
14 changes: 0 additions & 14 deletions src/main/java/com/shc/silenceengine/scene/SceneNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a976dc8

Please sign in to comment.