I worked through the API a little more and refactored it to a system-first approach, in which the manager associates the components of an entity to a particular system, In this way, I will be able to retrieve system-relevant components easily.
The general memory layout will be Controller-Associated System(java.lang.String) []
> Entities (IDs)
> Components[]
> Component
.
The Controller
interface provides the standards for different types of interactions.
I still need to count the pitfalls of using ConcurrentHashMaps
instead of regular arrays.
This would be a dummy example of the new work:
final StandardGameComponent position = new PositionComponent(new Component.Id(2223));
final StandardGameComponent velocity = new VelocityComponent(new Component.Id(344));
final Entity entity = EntityFactory.PLAYER.getEntity();
final EntityComponentManager<Float> ecsManager = new EntityComponentManager<>();
final SystemEntitiesUpdater<Float> updater = new SystemEntitiesUpdater<Float>() {
@Override
public void update(Type.EntityMap entityMap, EntityComponentManager<Float> entityComponentManager, Float input) {
try {
System.out.println(entityMap.get(entity.getId().intValue())
.get(position.getId().intValue())
.getData("positionVector").toString());
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public String getAssociatedSystem() {
return "Demo System";
}
};
ecsManager.attachAssociatedSystem(updater);
ecsManager.attachEntity(entity, updater);
ecsManager.register(position, entity, updater);
ecsManager.register(velocity, entity, updater);
// updating entities in a specific system
ecsManager.updateSystemEntities(updater, 0.005f);
// other update strategies
// ecsManager.updateSystemComponents();
// ecsManager.updateSystems();
A component example:
public class PositionComponent extends StandardGameComponent {
private String positionVector = "(3, 2, 1)";
/**
* Instantiates a new game entity component object.
*
* @param componentId the game component identifier
*/
public PositionComponent(Id componentId) {
super(componentId);
}
}
I used the resources linked by Paul and Data-oriented design by Richard Fabian, you can find a website version here.
After working on the main API, I will focus on optimizing the searching and matching algorithms, I would like to hear your remarks, Do you recommend any optimizations or things to put on my mind?
EDIT:
By extending the SystemController
and the EntityComponentManager
additional filters could be applied.
This is the source-code.