Changed access modifiers to public
Changed access so data may be manually changed, if needed
This commit is contained in:
parent
ccabef5226
commit
2c96e92a6a
@ -17,21 +17,20 @@ import java.util.HashMap;
|
|||||||
* Manages the addition, sorting and retrieving of components and component data
|
* Manages the addition, sorting and retrieving of components and component data
|
||||||
*/
|
*/
|
||||||
public class ComponentManager{
|
public class ComponentManager{
|
||||||
private Map<Type, Map<Entity, Object>> componentArrays = new HashMap<>();
|
public Map<Type, Map<Entity, Object>> componentArrays = new HashMap<>();
|
||||||
// Need to be able to map bit indices and component types
|
// Need to be able to map bit indices and component types
|
||||||
private Map<Integer, Type> indexComponentType = new HashMap<>();
|
public Map<Integer, Type> indexComponentType = new HashMap<>();
|
||||||
private Map<Type, Integer> componentTypeIndex = new HashMap<>();
|
public Map<Type, Integer> componentTypeIndex = new HashMap<>();
|
||||||
//
|
//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the specified component to the provided entity.
|
* Adds the specified component to the provided entity.
|
||||||
* <p>
|
* Does not ensure synchronisation with the other managers; ensure this is done!
|
||||||
* Providing a null Object defaults the value to boolean false
|
|
||||||
* @param componentType the class type of the component to add
|
* @param componentType the class type of the component to add
|
||||||
* @param componentData the component data to associate
|
* @param componentData the component data to associate
|
||||||
* @param entity the entity to associate data to
|
* @param entity the entity to associate data to
|
||||||
*/
|
*/
|
||||||
protected boolean addComponentToEntity(Type componentType, Object componentData, Entity entity){
|
public boolean addComponentToEntity(Type componentType, Object componentData, Entity entity){
|
||||||
componentArrays.get(componentType).put(entity, componentData);
|
componentArrays.get(componentType).put(entity, componentData);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,8 @@ import java.util.HashSet;
|
|||||||
* Additional functions can be implemented as required.
|
* Additional functions can be implemented as required.
|
||||||
*/
|
*/
|
||||||
public abstract class ECSSystem{
|
public abstract class ECSSystem{
|
||||||
protected Set<Entity> entities = new HashSet<>();
|
public Set<Entity> entities = new HashSet<>();
|
||||||
protected BitSet registrationSet;
|
public BitSet registrationSet;
|
||||||
|
|
||||||
public ECSSystem(){
|
public ECSSystem(){
|
||||||
registrationSet = new BitSet();
|
registrationSet = new BitSet();
|
||||||
|
@ -19,15 +19,17 @@ import java.util.ArrayList;
|
|||||||
* Manages data from the perspective of the entity.
|
* Manages data from the perspective of the entity.
|
||||||
* <p>
|
* <p>
|
||||||
* I.e. Controls adding and removing entities, and registration and deregistration of components.
|
* I.e. Controls adding and removing entities, and registration and deregistration of components.
|
||||||
|
* <p>
|
||||||
|
* Do not update values in this class unless you have reviewed the JavaECS source!
|
||||||
*/
|
*/
|
||||||
public class EntityManager{
|
public class EntityManager{
|
||||||
// According to https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack
|
// According to https://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack
|
||||||
// ArrayDeque is likely faster than a LinkedList, when used in place of one.
|
// ArrayDeque is likely faster than a LinkedList, when used in place of one.
|
||||||
// We can also supply a size to the constructor of ArrayDeque, which avoids resizing the collection
|
// We can also supply a size to the constructor of ArrayDeque, which avoids resizing the collection
|
||||||
// at initialisation time (took 1.4s vs 1.8s for 1M)
|
// at initialisation time (took 1.4s vs 1.8s for 1M)
|
||||||
private Deque<Entity> unusedEntities;
|
public Deque<Entity> unusedEntities;
|
||||||
private List<BitSet> entityRegistrations;
|
public List<BitSet> entityRegistrations;
|
||||||
private int maxSize = 1024;
|
public int maxSize = 1024;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialise the EntityManager with the default max size of 1024
|
* Initialise the EntityManager with the default max size of 1024
|
||||||
@ -63,7 +65,7 @@ public class EntityManager{
|
|||||||
* @return the index of the new entity
|
* @return the index of the new entity
|
||||||
* @throws NoSuchElementException an exception if there are no more unused entities
|
* @throws NoSuchElementException an exception if there are no more unused entities
|
||||||
*/
|
*/
|
||||||
protected Entity addEntity() throws NoSuchElementException{
|
public Entity addEntity() throws NoSuchElementException{
|
||||||
Entity result = unusedEntities.remove();
|
Entity result = unusedEntities.remove();
|
||||||
entityRegistrations.set(result.getValue(), new BitSet());
|
entityRegistrations.set(result.getValue(), new BitSet());
|
||||||
return result;
|
return result;
|
||||||
@ -105,7 +107,7 @@ public class EntityManager{
|
|||||||
* @param entity the entity to register
|
* @param entity the entity to register
|
||||||
* @return true if the operation was successful
|
* @return true if the operation was successful
|
||||||
*/
|
*/
|
||||||
protected boolean registerComponent(int component, Entity entity){
|
public boolean registerComponent(int component, Entity entity){
|
||||||
if (entity.getValue() >= maxSize){
|
if (entity.getValue() >= maxSize){
|
||||||
Engine.writeErr("Attempted to assign a component to non-existent entity: " + entity.getValue());
|
Engine.writeErr("Attempted to assign a component to non-existent entity: " + entity.getValue());
|
||||||
return false;
|
return false;
|
||||||
@ -124,32 +126,36 @@ public class EntityManager{
|
|||||||
/**
|
/**
|
||||||
* Adds the entity index back into unusedEntities, and sets the registrations to null
|
* Adds the entity index back into unusedEntities, and sets the registrations to null
|
||||||
* <p>
|
* <p>
|
||||||
* <b>Does not handle associated data</b> Use the method in ECS to remove entities cleanly
|
* <b>Does not handle associated data</b> Use the method in ECS to remove entities cleanly,
|
||||||
|
* or otherwise ensure the component data and systems are updated!
|
||||||
* @param entity the entity to remove
|
* @param entity the entity to remove
|
||||||
*/
|
*/
|
||||||
protected void removeEntity(Entity entity){
|
public void removeEntity(Entity entity){
|
||||||
unusedEntities.add(entity);
|
unusedEntities.add(entity);
|
||||||
entityRegistrations.set(entity.getValue(), null);
|
entityRegistrations.set(entity.getValue(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the entity's registrations to the provided BitSet
|
* Sets the entity's registrations to the provided BitSet.
|
||||||
|
* <p>
|
||||||
|
* Does <b>not</b> ensure the systems registrations are updated.
|
||||||
* @param entity the entity to set
|
* @param entity the entity to set
|
||||||
* @param registrations the preset registrations
|
* @param registrations the preset registrations
|
||||||
*/
|
*/
|
||||||
protected void setRegistrations(Entity entity, BitSet registrations){
|
public void setRegistrations(Entity entity, BitSet registrations){
|
||||||
entityRegistrations.set(entity.getValue(), registrations);
|
entityRegistrations.set(entity.getValue(), registrations);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters the specified component from the entity
|
* Unregisters the specified component from the entity
|
||||||
* <p>
|
* <p>
|
||||||
* <b>Does not handle component data</b> Use the method in ECS to remove components cleanly
|
* <b>Does not handle component data</b> Use the method in ECS to remove components cleanly,
|
||||||
|
* or otherwise ensure the component data and systems are updated!
|
||||||
* @param component the component index to remove
|
* @param component the component index to remove
|
||||||
* @param entity the entity to remove
|
* @param entity the entity to remove
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
protected boolean unregisterComponent(int component, Entity entity){
|
public boolean unregisterComponent(int component, Entity entity){
|
||||||
try{
|
try{
|
||||||
entityRegistrations.get(entity.getValue()).clear(component);
|
entityRegistrations.get(entity.getValue()).clear(component);
|
||||||
return true;
|
return true;
|
||||||
|
@ -17,16 +17,18 @@ import java.util.HashMap;
|
|||||||
/**
|
/**
|
||||||
* Manages system-focused aspects, such as ensuring a system has the correct list of current entities.
|
* Manages system-focused aspects, such as ensuring a system has the correct list of current entities.
|
||||||
* Manages registration of new systems
|
* Manages registration of new systems
|
||||||
|
* <p>
|
||||||
|
* Do not update data in this class unless you have reviewed the JavaECS source.
|
||||||
*/
|
*/
|
||||||
public class SystemManager{
|
public class SystemManager{
|
||||||
private Map<Type, ECSSystem> systems = new HashMap<>();
|
public Map<Type, ECSSystem> systems = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signals the SystemManager that an entity was destroyed.
|
* Signals the SystemManager that an entity was destroyed.
|
||||||
* Removes the entity from each system's tracked entities
|
* Removes the entity from each system's tracked entities
|
||||||
* @param entity the destroyed entity
|
* @param entity the destroyed entity
|
||||||
*/
|
*/
|
||||||
protected void entityDestroyed(Entity entity){
|
public void entityDestroyed(Entity entity){
|
||||||
// Unlike components, this isn't simply indexed.
|
// Unlike components, this isn't simply indexed.
|
||||||
for (Type key : systems.keySet()) {
|
for (Type key : systems.keySet()) {
|
||||||
systems.get(key).entities.remove(entity);
|
systems.get(key).entities.remove(entity);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user