Move Entity management to new EntityManager class to clean up Engine

This commit is contained in:
Matt Low 2020-01-27 01:35:51 +04:00
parent 82828ff05d
commit 83223e5dc2
2 changed files with 139 additions and 104 deletions

View File

@ -9,25 +9,16 @@ import java.util.Map;
public class Engine {
private Array<Entity> entities;
private Array<Entity> toActivate;
private Array<Entity> toDeactivate;
private Array<Entity> toRemove;
private Array<Entity> removedEntities;
private ComponentBag[] components;
private Array<BaseSystem> systems;
protected Array<BaseSystem> systems;
private EntityManager entityManager;
private ListenerRegistry listenerRegistry;
private Map<Class<? extends Component>, ComponentMapper> componentMappers;
public Engine() {
this.entities = new Array<>();
this.toActivate = new Array<>();
this.toDeactivate = new Array<>();
this.toRemove = new Array<>();
this.removedEntities = new Array<>(false, 16);
this.entityManager = new EntityManager(this);
this.systems = new Array<>();
this.listenerRegistry = new ListenerRegistry();
@ -38,7 +29,7 @@ public class Engine {
ComponentType.registerComponentType(clazz);
}
public void registerSystem(EntitySystem system) {
public void registerSystem(BaseSystem system) {
this.systems.add(system);
}
@ -53,18 +44,8 @@ public class Engine {
}
}
public Array<Entity> getEntities() {
return entities;
}
public int getEntityCount() {
return entities.size;
}
public void update(float dt) {
activatePending();
deactivatePending();
removePending();
entityManager.update();
for (BaseSystem system : systems) {
system.preProcess();
@ -73,99 +54,37 @@ public class Engine {
}
}
public Array<Entity> getEntities() {
return entityManager.entities;
}
public int getEntityCount() {
return entityManager.entities.size;
}
public Entity createEntity() {
Entity entity;
if (!removedEntities.isEmpty()) {
entity = removedEntities.removeIndex(0);
entity.reset();
} else {
entity = new Entity(this);
}
entity.updateUniqueId();
entities.add(entity);
return entity;
}
protected void removeEntity(Entity entity) {
toRemove.add(entity);
}
private void removeEntityFromSystems(Entity entity) {
for (BaseSystem system : systems) {
if (!(system instanceof EntitySystem)) {
continue;
}
EntitySystem entitySystem = (EntitySystem) system;
if (entitySystem.interestedIn(entity)) {
entitySystem.removeEntity(entity);
}
}
}
private void addEntityToSystems(Entity entity) {
for (BaseSystem system : systems) {
if (!(system instanceof EntitySystem)) {
continue;
}
EntitySystem entitySystem = (EntitySystem) system;
if (entitySystem.interestedIn(entity)) {
entitySystem.addEntity(entity);
}
}
}
private void removePending() {
if (toRemove.isEmpty()) {
return;
}
for (Entity entity : toRemove) {
removeAllEntityComponents(entity.id);
entities.removeValue(entity, true);
removeEntityFromSystems(entity);
removedEntities.add(entity);
}
toRemove.clear();
return entityManager.create();
}
protected void activateEntity(Entity entity) {
toActivate.add(entity);
entityManager.activate(entity);
}
private void activatePending() {
if (toActivate.isEmpty()) {
return;
}
for (Entity entity : toActivate) {
addEntityToSystems(entity);
}
toActivate.clear();
}
protected void deactivateEntity(Entity entity) {
toDeactivate.add(entity);
entityManager.deactivate(entity);
}
private void deactivatePending() {
if (toDeactivate.isEmpty()) {
return;
}
for (Entity entity : toDeactivate) {
removeEntityFromSystems(entity);
}
toDeactivate.clear();
protected void removeEntity(Entity entity) {
entityManager.remove(entity);
}
public void callEvent(Event event) {
listenerRegistry.callEvent(event);
}
private void removeAllEntityComponents(int entityId) {
protected void removeAllEntityComponents(int entityId) {
for (int i = 0; i < components.length; i++) {
components[i].insert(entityId, null);
}
@ -192,10 +111,6 @@ public class Engine {
return components[type.id].get(entity.id);
}
protected boolean entityHasComponent(Entity entity, ComponentType type) {
return components[type.id].contains(entity.id);
}
@SuppressWarnings("unchecked")
public <T extends Component> ComponentMapper<T> getComponentMapper(Class<T> typeClass) {
ComponentMapper<T> mapper = componentMappers.get(typeClass);

View File

@ -0,0 +1,120 @@
package com.me.common.ecs;
import com.badlogic.gdx.utils.Array;
final class EntityManager {
private Engine engine;
protected Array<Entity> entities;
protected Array<Entity> toActivate;
protected Array<Entity> toDeactivate;
protected Array<Entity> toRemove;
protected Array<Entity> removedEntities;
public EntityManager(Engine engine) {
this.engine = engine;
this.entities = new Array<>();
this.toActivate = new Array<>();
this.toDeactivate = new Array<>();
this.toRemove = new Array<>();
this.removedEntities = new Array<>(false, 16);
}
public void update() {
activatePending();
deactivatePending();
removePending();
}
public Entity create() {
Entity entity;
if (!removedEntities.isEmpty()) {
entity = removedEntities.removeIndex(0);
entity.reset();
} else {
entity = new Entity(engine);
}
entity.updateUniqueId();
entities.add(entity);
return entity;
}
public void remove(Entity entity) {
toRemove.add(entity);
}
public void activate(Entity entity) {
toActivate.add(entity);
}
public void deactivate(Entity entity) {
toDeactivate.add(entity);
}
private void removePending() {
if (toRemove.isEmpty()) {
return;
}
for (Entity entity : toRemove) {
engine.removeAllEntityComponents(entity.id);
entities.removeValue(entity, true);
removeEntityFromSystems(entity);
removedEntities.add(entity);
}
toRemove.clear();
}
private void activatePending() {
if (toActivate.isEmpty()) {
return;
}
for (Entity entity : toActivate) {
addEntityToSystems(entity);
}
toActivate.clear();
}
private void deactivatePending() {
if (toDeactivate.isEmpty()) {
return;
}
for (Entity entity : toDeactivate) {
removeEntityFromSystems(entity);
}
toDeactivate.clear();
}
private void removeEntityFromSystems(Entity entity) {
for (BaseSystem system : engine.systems) {
if (!(system instanceof EntitySystem)) {
continue;
}
EntitySystem entitySystem = (EntitySystem) system;
if (entitySystem.interestedIn(entity)) {
entitySystem.removeEntity(entity);
}
}
}
private void addEntityToSystems(Entity entity) {
for (BaseSystem system : engine.systems) {
if (!(system instanceof EntitySystem)) {
continue;
}
EntitySystem entitySystem = (EntitySystem) system;
if (entitySystem.interestedIn(entity)) {
entitySystem.addEntity(entity);
}
}
}
}