diff --git a/src/com/blarg/gdx/entities/EntityManager.java b/src/com/blarg/gdx/entities/EntityManager.java index fbc9e98..03d20c1 100644 --- a/src/com/blarg/gdx/entities/EntityManager.java +++ b/src/com/blarg/gdx/entities/EntityManager.java @@ -1,6 +1,7 @@ package com.blarg.gdx.entities; import com.badlogic.gdx.utils.*; +import com.blarg.gdx.entities.systemcomponents.EntityPresetComponent; import com.blarg.gdx.entities.systemcomponents.InactiveComponent; import com.blarg.gdx.events.EventManager; import com.blarg.gdx.ReflectionUtils; @@ -13,6 +14,7 @@ public class EntityManager implements Disposable { ObjectMap, ObjectMap> componentStore; ObjectMap, Component> globalComponents; Array componentSystems; + ObjectMap, EntityPreset> presets; Pool entityPool = new Pool() { @Override @@ -32,6 +34,7 @@ public class EntityManager implements Disposable { componentStore = new ObjectMap, ObjectMap>(); globalComponents = new ObjectMap, Component>(); componentSystems = new Array(); + presets = new ObjectMap, EntityPreset>(); // possibly ugliness, but this allows us to return empty.keys() in getAllWith() // when there are no entities with a given component, preventing the calling code @@ -80,6 +83,32 @@ public class EntityManager implements Disposable { componentSystems.clear(); } + /*** public EntityPreset management */ + + public void addPreset(Class presetType) { + if (presets.containsKey(presetType)) + throw new UnsupportedOperationException("EntityPreset of that type is already registered."); + + T preset; + try { + preset = ReflectionUtils.instantiateObject(presetType, + new Class[] { EntityManager.class }, + new Object[] { this }); + } catch (Exception e) { + throw new IllegalArgumentException("Could not instantiate this type of EntityPreset.", e); + } + + presets.put(presetType, preset); + } + + public void removePreset(Class presetType) { + presets.remove(presetType); + } + + public void removeAllPresets() { + presets.clear(); + } + /*** public Entity management ***/ public Entity add() { @@ -88,6 +117,21 @@ public class EntityManager implements Disposable { return entity; } + public Entity addUsingPreset(Class presetType) { + return addusingPreset(presetType, null); + } + + public Entity addusingPreset(Class presetType, EntityPreset.CreationArgs args) { + EntityPreset preset = presets.get(presetType); + if (preset == null) + throw new IllegalArgumentException("Cannot add entity using an unregistered EntityPreset."); + + Entity entity = preset.create(args); + entity.add(EntityPresetComponent.class).presetType = presetType; + + return entity; + } + public Entity getFirstWith(Class componentType) { ObjectMap componentEntities = componentStore.get(componentType); if (componentEntities == null) @@ -278,6 +322,7 @@ public class EntityManager implements Disposable { public void dispose() { removeAll(); removeAllGlobals(); + removeAllPresets(); removeAllSubsystems(); } } diff --git a/src/com/blarg/gdx/entities/EntityPreset.java b/src/com/blarg/gdx/entities/EntityPreset.java new file mode 100644 index 0000000..d717d05 --- /dev/null +++ b/src/com/blarg/gdx/entities/EntityPreset.java @@ -0,0 +1,17 @@ +package com.blarg.gdx.entities; + +public abstract class EntityPreset { + public interface CreationArgs { + } + + public final EntityManager entityManager; + + public EntityPreset(EntityManager entityManager) { + if (entityManager == null) + throw new IllegalArgumentException("entityManager can not be null."); + + this.entityManager = entityManager; + } + + public abstract Entity create(CreationArgs args); +} diff --git a/src/com/blarg/gdx/entities/systemcomponents/EntityPresetComponent.java b/src/com/blarg/gdx/entities/systemcomponents/EntityPresetComponent.java new file mode 100644 index 0000000..fe889ef --- /dev/null +++ b/src/com/blarg/gdx/entities/systemcomponents/EntityPresetComponent.java @@ -0,0 +1,13 @@ +package com.blarg.gdx.entities.systemcomponents; + +import com.blarg.gdx.entities.Component; +import com.blarg.gdx.entities.EntityPreset; + +public class EntityPresetComponent extends Component { + public Class presetType; + + @Override + public void reset() { + presetType = null; + } +}