add entity preset support
This commit is contained in:
parent
79b027b024
commit
c0baa42ff6
|
@ -1,6 +1,7 @@
|
||||||
package com.blarg.gdx.entities;
|
package com.blarg.gdx.entities;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.*;
|
import com.badlogic.gdx.utils.*;
|
||||||
|
import com.blarg.gdx.entities.systemcomponents.EntityPresetComponent;
|
||||||
import com.blarg.gdx.entities.systemcomponents.InactiveComponent;
|
import com.blarg.gdx.entities.systemcomponents.InactiveComponent;
|
||||||
import com.blarg.gdx.events.EventManager;
|
import com.blarg.gdx.events.EventManager;
|
||||||
import com.blarg.gdx.ReflectionUtils;
|
import com.blarg.gdx.ReflectionUtils;
|
||||||
|
@ -13,6 +14,7 @@ public class EntityManager implements Disposable {
|
||||||
ObjectMap<Class<? extends Component>, ObjectMap<Entity, Component>> componentStore;
|
ObjectMap<Class<? extends Component>, ObjectMap<Entity, Component>> componentStore;
|
||||||
ObjectMap<Class<? extends Component>, Component> globalComponents;
|
ObjectMap<Class<? extends Component>, Component> globalComponents;
|
||||||
Array<ComponentSystem> componentSystems;
|
Array<ComponentSystem> componentSystems;
|
||||||
|
ObjectMap<Class<? extends EntityPreset>, EntityPreset> presets;
|
||||||
|
|
||||||
Pool<Entity> entityPool = new Pool<Entity>() {
|
Pool<Entity> entityPool = new Pool<Entity>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,6 +34,7 @@ public class EntityManager implements Disposable {
|
||||||
componentStore = new ObjectMap<Class<? extends Component>, ObjectMap<Entity, Component>>();
|
componentStore = new ObjectMap<Class<? extends Component>, ObjectMap<Entity, Component>>();
|
||||||
globalComponents = new ObjectMap<Class<? extends Component>, Component>();
|
globalComponents = new ObjectMap<Class<? extends Component>, Component>();
|
||||||
componentSystems = new Array<ComponentSystem>();
|
componentSystems = new Array<ComponentSystem>();
|
||||||
|
presets = new ObjectMap<Class<? extends EntityPreset>, EntityPreset>();
|
||||||
|
|
||||||
// possibly ugliness, but this allows us to return empty.keys() in getAllWith()
|
// 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
|
// when there are no entities with a given component, preventing the calling code
|
||||||
|
@ -80,6 +83,32 @@ public class EntityManager implements Disposable {
|
||||||
componentSystems.clear();
|
componentSystems.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*** public EntityPreset management */
|
||||||
|
|
||||||
|
public <T extends EntityPreset> void addPreset(Class<T> 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 <T extends EntityPreset> void removePreset(Class<T> presetType) {
|
||||||
|
presets.remove(presetType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAllPresets() {
|
||||||
|
presets.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/*** public Entity management ***/
|
/*** public Entity management ***/
|
||||||
|
|
||||||
public Entity add() {
|
public Entity add() {
|
||||||
|
@ -88,6 +117,21 @@ public class EntityManager implements Disposable {
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T extends EntityPreset> Entity addUsingPreset(Class<T> presetType) {
|
||||||
|
return addusingPreset(presetType, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends EntityPreset> Entity addusingPreset(Class<T> 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 <T extends Component> Entity getFirstWith(Class<T> componentType) {
|
public <T extends Component> Entity getFirstWith(Class<T> componentType) {
|
||||||
ObjectMap<Entity, Component> componentEntities = componentStore.get(componentType);
|
ObjectMap<Entity, Component> componentEntities = componentStore.get(componentType);
|
||||||
if (componentEntities == null)
|
if (componentEntities == null)
|
||||||
|
@ -278,6 +322,7 @@ public class EntityManager implements Disposable {
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
removeAll();
|
removeAll();
|
||||||
removeAllGlobals();
|
removeAllGlobals();
|
||||||
|
removeAllPresets();
|
||||||
removeAllSubsystems();
|
removeAllSubsystems();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/com/blarg/gdx/entities/EntityPreset.java
Normal file
17
src/com/blarg/gdx/entities/EntityPreset.java
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -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<? extends EntityPreset> presetType;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
presetType = null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue