65 lines
2.3 KiB
Java
65 lines
2.3 KiB
Java
package nz.ac.massey.javaecs;
|
|
/**
|
|
* System Manager
|
|
* This class manages systems registrations, and keeps a current list
|
|
* of all entities the system should operate on
|
|
*
|
|
* Contributors:
|
|
* Brychan Dempsey - brychand@hotmail.com
|
|
*
|
|
*/
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.BitSet;
|
|
import java.util.Map;
|
|
import java.util.HashMap;
|
|
|
|
class SystemManager{
|
|
Map<Type, BitSet> registrationSignatures = new HashMap<>();
|
|
Map<Type, ECSSystem> systems = new HashMap<>();
|
|
|
|
// Registering the system adds it to the array of systems.
|
|
// In Austin Morlan's implementation, this also creates an instance of the
|
|
// system that can be called from the main thread.
|
|
// It returns this object.
|
|
// In Java, we need to initialise the object first (before this is called),
|
|
// then register that object. Enactment of the system is performed on that
|
|
// instance.
|
|
// I.e., create an object that represents the system class; then store that class in the system
|
|
// table.
|
|
public boolean registerSystem(Type systemType, ECSSystem system){
|
|
if (systems.containsKey(systemType)){
|
|
ECS.writeErr("System already registered");
|
|
return false;
|
|
}
|
|
systems.put(systemType, system);
|
|
registrationSignatures.put(systemType, new BitSet());
|
|
return true;
|
|
}
|
|
|
|
public void setRegistrationSignature(Type systemType, BitSet registrations){
|
|
registrationSignatures.put(systemType, registrations);
|
|
}
|
|
|
|
public void entityDestroyed(int entity){
|
|
for (Type key : systems.keySet()) {
|
|
systems.get(key).entities.remove(entity);
|
|
}
|
|
}
|
|
|
|
public void entityRegistrationsChanged(int entity, BitSet entityRegistrations){
|
|
for (Type key : systems.keySet()) {
|
|
// Check if the signature is null
|
|
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{
|
|
systems.get(key).entities.remove(entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |