Just did some changes to the code. The entities
list does no longer exist now as it was never used. The IEntityComponent
has now also a new method to be able to get the entity to which this component belongs.
The next change I did was to change the example systems and entities to completly new examples as these examples give a better overview of the system and the interaction between different components than the code from before had done.
The EntityManager
:
public class EntityManager {
private ArrayList<ISystem> componentSystems = new ArrayList<>();
public void addEntity(ISystem... system) {
this.componentSystems.addAll(Arrays.asList(system));
}
public void init() {
for(ISystem system : this.componentSystems)
system.init();
}
public void update() {
for(ISystem system : this.componentSystems)
system.update();
}
public void render(double currentTime) {
for(ISystem system : this.componentSystems)
system.render(currentTime);
}
public void dispose() {
for(ISystem system : this.componentSystems)
system.dispose();
}
}
The IEntityComponent
:
public interface IEntityComponent {
public int getEntityID();
}
The ISystem
:
public interface ISystem {
public void init();
public void update();
public void render(double currentTime);
public void dispose();
public IEntityComponent[] get();
}
The ComponentPosition
:
public class ComponentPosition implements IEntityComponent{
public float posX;
public float posY;
public float posZ;
private int entityID;
// z axis exists for rendering system to determine if the object should be drawn on top
// of something else with a smaller posZ value
public ComponentPosition(int entityID, float posX, float posY, float posZ) {
this.entityID = entityID;
this.posX = posX;
this.posY = posY;
this.posZ = posZ;
}
@Override
public int getEntityID() {
return this.entityID;
}
}
The ComponentVelocity
:
public class ComponentVelocity implements IEntityComponent {
public float velX;
public float velY;
private int entityID;
// movement on the z axis is not allowed!
public ComponentVelocity(int entityID, float velX, float velY) {
this.entityID = entityID;
this.velX = velX;
this.velY = velY;
}
@Override
public int getEntityID() {
return this.entityID;
}
}
The SystemMovement
:
public class SystemMovement implements ISystem {
private ComponentVelocity vecocity;
private ComponentPosition position;
public SystemMovement(ComponentPosition pos, ComponentVelocity vel) {
this.position = pos;
this.vecocity = vel;
}
@Override
public void init() {
// can be called on initialisiation of the scene
}
@Override
public void update() {
// each tick (currently 60t/s)
this.position.posX += this.vecocity.velX;
this.position.posY += this.vecocity.velY;
}
@Override
public void render(double currentTime) {
// fps varies
}
@Override
public void dispose() {
// on deletion to clear for example OpenGL stuff when used for rendering
}
@Override
public IEntityComponent[] get() {
return new IEntityComponent[] {this.position, this.vecocity};
}
}
The SystemRenderer
:
public class SystemRenderer implements ISystem {
private ComponentPosition position;
public SystemRenderer(ComponentPosition pos) {
this.position = pos;
}
@Override
public void init() {
// can be called on initialisiation of the scene
}
@Override
public void update() {
// each tick (currently 60t/s)
}
@Override
public void render(double currentTime) {
// fps varies
if(this.position.getEntityID() == 0) // just for testing to see the change in player pos
return;
System.out.println("______________________________________");
System.out.println("Entity: " + this.position.getEntityID());
System.out.println(this.position.posX);
System.out.println(this.position.posY);
}
@Override
public void dispose() {
// on deletion to clear for example OpenGL stuff when used for rendering
}
@Override
public IEntityComponent[] get() {
return new IEntityComponent[] {this.position};
}
}
The SystemPosition
:
public class SystemPosition implements ISystem {
private ComponentPosition position;
public SystemPosition(ComponentPosition pos) {
this.position = pos;
}
@Override
public void init() {
// can be called on initialisiation of the scene
}
@Override
public void update() {
// each tick (currently 60t/s)
}
@Override
public void render(double currentTime) {
// fps varies
}
@Override
public void dispose() {
// on deletion to clear for example OpenGL stuff when used for rendering
}
@Override
public IEntityComponent[] get() {
return new IEntityComponent[] {this.position};
}
}
The TestScene
:
public class TestScene implements Scene {
private EntityManager manager = new EntityManager();
@Override
public boolean init() {
InputMultiplexer.getInput().addComponent(new InputComponent.InputKeyComponent() {
@Override
public boolean onKeyPress(int key, int action) {
if(key == GLFW_KEY_ESCAPE && action == GLFW_REPEAT)
Game.windowShouldClose();
return true;
}
});
ComponentPosition posPlayer = new ComponentPosition(1, 14, 15, 0);
this.manager.addEntity(new SystemPosition(new ComponentPosition(0, 12, 13, 1)), new SystemRenderer(new ComponentPosition(0, 12, 13, 1)));
this.manager.addEntity(new SystemMovement(posPlayer, new ComponentVelocity(1, 1, 1)),
new SystemRenderer(posPlayer));
this.manager.init();
return true;
}
@Override
public void update() {
this.manager.update();
}
@Override
public void render(double currentTime) {
this.manager.render(currentTime);
}
@Override
public void dispose() {
this.manager.dispose();
}
}
Note:
- the Entity with the ID 0 could be for example a tree
- the Entity with the ID 1 could be a player or a moving entity (thought AI is missing)