Switched from using class literal to class

This commit is contained in:
Brychan Dempsey 2021-05-22 19:17:37 +12:00
parent 358b303c35
commit 39c3de7dcb
6 changed files with 60 additions and 57 deletions

View File

@ -17,44 +17,44 @@ public final class App {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
ECS gameEngine = new ECS(); ECS gameEngine = new ECS();
System.out.println("Hello World!"); System.out.println("Hello World!");
gameEngine.registerComponent(Vec2D.class.getName()); gameEngine.registerComponent(Vec2D.class);
gameEngine.registerComponent(RidgidBody.class.getName()); gameEngine.registerComponent(RidgidBody.class);
gameEngine.registerComponent(Gravity.class.getName()); gameEngine.registerComponent(Gravity.class);
PhysicsSystem physicsSystem = new PhysicsSystem(gameEngine); PhysicsSystem physicsSystem = new PhysicsSystem(gameEngine);
gameEngine.registerSystem(PhysicsSystem.class.getName(), physicsSystem); gameEngine.registerSystem(PhysicsSystem.class, physicsSystem);
{ {
BitSet signature = new BitSet(); BitSet signature = new BitSet();
signature.set(gameEngine.getComponentIndex(Gravity.class.getName())); signature.set(gameEngine.getComponentIndex(Gravity.class));
signature.set(gameEngine.getComponentIndex(Vec2D.class.getName())); signature.set(gameEngine.getComponentIndex(Vec2D.class));
signature.set(gameEngine.getComponentIndex(RidgidBody.class.getName())); signature.set(gameEngine.getComponentIndex(RidgidBody.class));
gameEngine.setSystemSignature(PhysicsSystem.class.getName(), signature); gameEngine.setSystemSignature(PhysicsSystem.class, signature);
} }
physicsSystem.init(); physicsSystem.init();
LogVec2DSystem logVec2DSystem = new LogVec2DSystem(gameEngine); LogVec2DSystem logVec2DSystem = new LogVec2DSystem(gameEngine);
gameEngine.registerSystem(LogVec2DSystem.class.getName(), logVec2DSystem); gameEngine.registerSystem(LogVec2DSystem.class, logVec2DSystem);
{ {
BitSet signature = new BitSet(); BitSet signature = new BitSet();
signature.set(gameEngine.getComponentIndex(Gravity.class.getName())); signature.set(gameEngine.getComponentIndex(Gravity.class));
signature.set(gameEngine.getComponentIndex(Vec2D.class.getName())); signature.set(gameEngine.getComponentIndex(Vec2D.class));
signature.set(gameEngine.getComponentIndex(RidgidBody.class.getName())); signature.set(gameEngine.getComponentIndex(RidgidBody.class));
gameEngine.setSystemSignature(LogVec2DSystem.class.getName(), signature); gameEngine.setSystemSignature(LogVec2DSystem.class, signature);
} }
logVec2DSystem.init(); logVec2DSystem.init();
FrameRateSystem frameRateSystem = new FrameRateSystem(); FrameRateSystem frameRateSystem = new FrameRateSystem();
gameEngine.registerSystem(FrameRateSystem.class.getName(), frameRateSystem); gameEngine.registerSystem(FrameRateSystem.class, frameRateSystem);
// Create components: // Create components:
int entity = gameEngine.createEntity(); int entity = gameEngine.createEntity();
gameEngine.addComponent(entity, Gravity.class.getName(), new Gravity(0.0, -9.80665, -1.0, 100.0)); gameEngine.addComponent(entity, Gravity.class, new Gravity(0.0, -9.80665, -1.0, 100.0));
gameEngine.addComponent(entity, Vec2D.class.getName(), new Vec2D(1.5, 0.0, 1)); gameEngine.addComponent(entity, Vec2D.class, new Vec2D(1.5, 0.0, 1));
gameEngine.addComponent(entity, RidgidBody.class.getName(), new RidgidBody()); gameEngine.addComponent(entity, RidgidBody.class, new RidgidBody());

View File

@ -9,42 +9,43 @@ package nz.ac.massey.programming_project_159333_s1_2021;
* *
*/ */
import java.util.Map; import java.util.Map;
import java.lang.reflect.Type;
import java.util.HashMap; import java.util.HashMap;
class ComponentManager{ class ComponentManager{
Map<String, ComponentArray> componentArrays = new HashMap<>(); Map<Type, ComponentArray> componentArrays = new HashMap<>();
Map<String, Integer> componentPosIndex = new HashMap<>(); Map<Type, Integer> componentPosIndex = new HashMap<>();
int componentPos = 0; int componentPos = 0;
public void registerComponent(String name){ public void registerComponent(Type type){
if (componentArrays.containsKey(name)){ if (componentArrays.containsKey(type)){
System.err.println("Component " + name + " is already registered"); System.err.println("Component " + type.getTypeName() + " is already registered");
return; return;
} }
componentArrays.put(name, new ComponentArray()); componentArrays.put(type, new ComponentArray());
componentPosIndex.put(name, componentPos++); componentPosIndex.put(type, componentPos++);
} }
public void addComponentToEntity(String componentName, Object componentData, int entity){ public void addComponentToEntity(Type componentName, Object componentData, int entity){
componentArrays.get(componentName).insertData(entity, componentData); componentArrays.get(componentName).insertData(entity, componentData);
} }
public void removeComponentFromEntity(String componentName, int entity){ public void removeComponentFromEntity(Type componentName, int entity){
componentArrays.get(componentName).removeData(entity); componentArrays.get(componentName).removeData(entity);
} }
// Java does not allow reflective typing, so must cast this in the retrieving function. // Java does not allow reflective typing, so must cast this in the retrieving function.
public Object getComponent(String componentType, int entity){ public Object getComponent(Type componentType, int entity){
return componentArrays.get(componentType).getData(entity); return componentArrays.get(componentType).getData(entity);
} }
public void entityDestroyed(int entity){ public void entityDestroyed(int entity){
for (String key : componentArrays.keySet()) { for (Type key : componentArrays.keySet()) {
componentArrays.get(key).entityDestroyed(entity); componentArrays.get(key).entityDestroyed(entity);
} }
} }
public Integer getComponentIndex(String name){ public Integer getComponentIndex(Type type){
return componentPosIndex.get(name); return componentPosIndex.get(type);
} }
} }

View File

@ -12,6 +12,7 @@ package nz.ac.massey.programming_project_159333_s1_2021;
* *
*/ */
import java.lang.reflect.Type;
import java.util.BitSet; import java.util.BitSet;
/** /**
* The ECS manager. * The ECS manager.
@ -66,12 +67,12 @@ public class ECS {
* Registers the specified name in the component manager * Registers the specified name in the component manager
* @param name the name to register. Should be the component class name or a suitable name for primitive types * @param name the name to register. Should be the component class name or a suitable name for primitive types
*/ */
void registerComponent(String name){ void registerComponent(Type type){
componentManager.registerComponent(name); componentManager.registerComponent(type);
} }
Integer getComponentIndex(String name){ Integer getComponentIndex(Type type){
return componentManager.getComponentIndex(name); return componentManager.getComponentIndex(type);
} }
/** /**
@ -80,7 +81,7 @@ public class ECS {
* @param componentName the class name of the component to add * @param componentName the class name of the component to add
* @param component the actual component data * @param component the actual component data
*/ */
void addComponent(int entity, String componentName, Object component){ void addComponent(int entity, Type componentName, Object component){
componentManager.addComponentToEntity(componentName, component, entity); componentManager.addComponentToEntity(componentName, component, entity);
entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity); entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity);
systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity)); systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity));
@ -91,9 +92,9 @@ public class ECS {
* @param entity the entity to remove the component from * @param entity the entity to remove the component from
* @param componentName the class name of the component * @param componentName the class name of the component
*/ */
void removeComponent(int entity, String componentName){ void removeComponent(int entity, Type componentType){
componentManager.removeComponentFromEntity(componentName, entity); componentManager.removeComponentFromEntity(componentType, entity);
entityManager.unregisterComponent(componentManager.getComponentIndex(componentName), entity); entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity);
systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity)); systemManager.entitySignatureChanged(entity, entityManager.getRegistrations(entity));
} }
@ -101,11 +102,11 @@ public class ECS {
* Gets the actual data of the component associated to the entity. * Gets the actual data of the component associated to the entity.
* May require casting from Object to the known data type * May require casting from Object to the known data type
* @param entity the entity to retrieve the data for * @param entity the entity to retrieve the data for
* @param componentName the class name of the component * @param componentType the class type of the component
* @return the component data Object associated with the entity * @return the component data Object associated with the entity
*/ */
Object getComponentData(int entity, String componentName){ Object getComponentData(int entity, Type componentType){
return componentManager.getComponent(componentName, entity); return componentManager.getComponent(componentType, entity);
} }
/** /**
@ -113,8 +114,8 @@ public class ECS {
* @param systemName * @param systemName
* @param action * @param action
*/ */
void registerSystem(String systemName, ECSSystem action){ void registerSystem(Type systemType, ECSSystem action){
systemManager.registerSystem(systemName, action); systemManager.registerSystem(systemType, action);
} }
/** /**
@ -122,7 +123,7 @@ public class ECS {
* @param system the class name of the system to set the signature of * @param system the class name of the system to set the signature of
* @param signature the new signature data * @param signature the new signature data
*/ */
void setSystemSignature(String system, BitSet signature){ void setSystemSignature(Type system, BitSet signature){
systemManager.setSignature(system, signature); systemManager.setSignature(system, signature);
} }
} }

View File

@ -10,7 +10,7 @@ public class LogVec2DSystem extends ECSSystem{
void update(double dt){ void update(double dt){
for (Integer entity : entities) { for (Integer entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class.getName()); Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y)); System.out.println(String.format("X: %.6g, Y: %.6g", pos.x, pos.y));
} }
} }

View File

@ -10,9 +10,9 @@ public class PhysicsSystem extends ECSSystem{
void update(double dt){ void update(double dt){
for (Integer entity : entities) { for (Integer entity : entities) {
Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class.getName()); Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class.getName()); RidgidBody ridgidBody = (RidgidBody)gameEngine.getComponentData(entity, RidgidBody.class);
Gravity gravity = (Gravity)gameEngine.getComponentData(entity, Gravity.class.getName()); Gravity gravity = (Gravity)gameEngine.getComponentData(entity, Gravity.class);
// Firstly, add the result of the accelerative forces to the ridgidbody // Firstly, add the result of the accelerative forces to the ridgidbody
ridgidBody.xdot += ridgidBody.xAcc * dt; ridgidBody.xdot += ridgidBody.xAcc * dt;
ridgidBody.ydot += ridgidBody.yAcc * dt; ridgidBody.ydot += ridgidBody.yAcc * dt;

View File

@ -9,13 +9,14 @@ package nz.ac.massey.programming_project_159333_s1_2021;
* *
*/ */
import java.lang.reflect.Type;
import java.util.BitSet; import java.util.BitSet;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
class SystemManager{ class SystemManager{
Map<String, BitSet> signatures = new HashMap<>(); Map<Type, BitSet> signatures = new HashMap<>();
Map<String, ECSSystem> systems = new HashMap<>(); Map<Type, ECSSystem> systems = new HashMap<>();
// Registering the system adds it to the array of systems. // Registering the system adds it to the array of systems.
// In Austin Morlan's implementation, this also creates an instance of the // In Austin Morlan's implementation, this also creates an instance of the
@ -26,28 +27,28 @@ class SystemManager{
// instance. // instance.
// I.e., create an object that represents the system class; then store that class in the system // I.e., create an object that represents the system class; then store that class in the system
// table. // table.
public boolean registerSystem(String systemName, ECSSystem system){ public boolean registerSystem(Type systemType, ECSSystem system){
if (systems.containsKey(systemName)){ if (systems.containsKey(systemType)){
System.err.println("System already registered"); System.err.println("System already registered");
return false; return false;
} }
systems.put(systemName, system); systems.put(systemType, system);
signatures.put(systemName, new BitSet()); signatures.put(systemType, new BitSet());
return true; return true;
} }
public void setSignature(String system, BitSet registrations){ public void setSignature(Type system, BitSet registrations){
signatures.put(system, registrations); signatures.put(system, registrations);
} }
public void entityDestroyed(int entity){ public void entityDestroyed(int entity){
for (String key : systems.keySet()) { for (Type key : systems.keySet()) {
systems.get(key).entities.remove(entity); systems.get(key).entities.remove(entity);
} }
} }
public void entitySignatureChanged(int entity, BitSet entitySignature){ public void entitySignatureChanged(int entity, BitSet entitySignature){
for (String key : systems.keySet()) { for (Type key : systems.keySet()) {
// Check if the signature is null // Check if the signature is null
if (!entitySignature.equals(null)){ if (!entitySignature.equals(null)){
BitSet srcCpy = (BitSet)entitySignature.clone(); BitSet srcCpy = (BitSet)entitySignature.clone();