Added abstraction to ECSSystem
Allows the library to signal the required implementations of inti() and update()
This commit is contained in:
parent
4354eac0cf
commit
7524a0096a
@ -97,7 +97,7 @@ public class ECS {
|
||||
void addComponent(int entity, Type componentName, Object component){
|
||||
componentManager.addComponentToEntity(componentName, component, entity);
|
||||
entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity);
|
||||
systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity));
|
||||
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +108,7 @@ public class ECS {
|
||||
void removeComponent(int entity, Type componentType){
|
||||
componentManager.removeComponentFromEntity(componentType, entity);
|
||||
entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity);
|
||||
systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity));
|
||||
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -137,7 +137,7 @@ public class ECS {
|
||||
* @param signature the new signature data
|
||||
*/
|
||||
void setSystemSignature(Type system, BitSet signature){
|
||||
systemManager.setSignature(system, signature);
|
||||
systemManager.setRegistrationSignature(system, signature);
|
||||
}
|
||||
Integer getMaxEntities(){
|
||||
return entityManager.currentSize;
|
||||
|
@ -15,6 +15,10 @@ package nz.ac.massey.javaecs;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
|
||||
class ECSSystem{
|
||||
abstract class ECSSystem{
|
||||
Set<Integer> entities = new HashSet<>();
|
||||
|
||||
// Abstractions are included to remind the library user these two implementations (and/or variations) should be implemented
|
||||
abstract void init();
|
||||
abstract void update();
|
||||
}
|
@ -110,7 +110,7 @@ class EntityManager{
|
||||
setRegistrations(newPos, getRegistrations(integer));
|
||||
// Invoke an update in the SystemManager
|
||||
systemManager.entityDestroyed(integer);
|
||||
systemManager.entitySignatureChanged(newPos, getRegistrations(newPos));
|
||||
systemManager.entityRegistrationsChanged(newPos, getRegistrations(newPos));
|
||||
// Invoke the change in the components
|
||||
componentManager.moveComponentData(integer, newPos);
|
||||
componentManager.entityDestroyed(integer);
|
||||
|
@ -1,9 +1,15 @@
|
||||
package nz.ac.massey.javaecs;
|
||||
|
||||
public class FrameRateSystem extends ECSSystem{
|
||||
@Override
|
||||
void init() {}
|
||||
|
||||
@Override
|
||||
void update() {}
|
||||
|
||||
void update(double dt, double idleTime){
|
||||
System.out.print(String.format("dt: %.3g (%.3g idle) ", dt, idleTime));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,12 +6,18 @@ public class LogVec2DSystem extends ECSSystem{
|
||||
this.gameEngine = gameEngine;
|
||||
}
|
||||
|
||||
@Override
|
||||
void init() {}
|
||||
|
||||
@Override
|
||||
void update() {}
|
||||
|
||||
void update(double dt){
|
||||
for (Integer entity : entities) {
|
||||
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
|
||||
System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,9 +6,13 @@ public class PhysicsSystem extends ECSSystem{
|
||||
this.gameEngine = gameEngine;
|
||||
}
|
||||
|
||||
@Override
|
||||
void init() {}
|
||||
|
||||
void update(double dt){
|
||||
@Override
|
||||
void update() {}
|
||||
|
||||
void update(Double dt){
|
||||
for (Integer entity : entities) {
|
||||
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
|
||||
RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class);
|
||||
|
@ -15,7 +15,7 @@ import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
class SystemManager{
|
||||
Map<Type, BitSet> signatures = new HashMap<>();
|
||||
Map<Type, BitSet> registrationSignatures = new HashMap<>();
|
||||
Map<Type, ECSSystem> systems = new HashMap<>();
|
||||
|
||||
// Registering the system adds it to the array of systems.
|
||||
@ -33,12 +33,12 @@ class SystemManager{
|
||||
return false;
|
||||
}
|
||||
systems.put(systemType, system);
|
||||
signatures.put(systemType, new BitSet());
|
||||
registrationSignatures.put(systemType, new BitSet());
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setSignature(Type system, BitSet registrations){
|
||||
signatures.put(system, registrations);
|
||||
public void setRegistrationSignature(Type systemType, BitSet registrations){
|
||||
registrationSignatures.put(systemType, registrations);
|
||||
}
|
||||
|
||||
public void entityDestroyed(int entity){
|
||||
@ -47,13 +47,13 @@ class SystemManager{
|
||||
}
|
||||
}
|
||||
|
||||
public void entitySignatureChanged(int entity, BitSet entitySignature){
|
||||
public void entityRegistrationsChanged(int entity, BitSet entityRegistrations){
|
||||
for (Type key : systems.keySet()) {
|
||||
// Check if the signature is null
|
||||
if (!entitySignature.equals(null)){
|
||||
BitSet srcCpy = (BitSet)entitySignature.clone();
|
||||
srcCpy.and(signatures.get(key));
|
||||
if (srcCpy.equals(signatures.get(key))){ // Bitwise check if the entity is subscribed to this system
|
||||
if (!entityRegistrations.equals(null)){
|
||||
BitSet srcCpy = (BitSet)entityRegistrations.clone();
|
||||
srcCpy.and(registrationSignatures.get(key));
|
||||
if (srcCpy.equals(registrationSignatures.get(key))){ // Bitwise check if the entity is subscribed to this system
|
||||
systems.get(key).entities.add(entity);
|
||||
}
|
||||
else{
|
||||
|
Loading…
x
Reference in New Issue
Block a user