Add has() method to ComponentMapper
Some other minor changes: * Made ComponentType final + package-private * Made Entity and ComponentType id protected to allow direct access from Engine * Made ComponentType bits protected to allow direct access from Entity * Made Entity componentBIts protected to allow direct access from ComponentMapper and EntitySystem
This commit is contained in:
parent
bd37d4fc65
commit
82828ff05d
@ -17,6 +17,10 @@ public class ComponentBag {
|
|||||||
return items[index];
|
return items[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean contains(int index) {
|
||||||
|
return index < size && items[index] != null;
|
||||||
|
}
|
||||||
|
|
||||||
public void insert(int index, Component item) {
|
public void insert(int index, Component item) {
|
||||||
if (index >= size) {
|
if (index >= size) {
|
||||||
grow((int) (index * 1.5f));
|
grow((int) (index * 1.5f));
|
||||||
|
@ -16,4 +16,8 @@ public class ComponentMapper<T extends Component> {
|
|||||||
return typeClass.cast(engine.getEntityComponent(entity, type));
|
return typeClass.cast(engine.getEntityComponent(entity, type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean has(Entity entity) {
|
||||||
|
return type.isTypeInMask(entity.componentBits);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.me.common.ecs;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ComponentType {
|
final class ComponentType {
|
||||||
|
|
||||||
private static final Map<Class<? extends Component>, ComponentType> types = new HashMap<>();
|
private static final Map<Class<? extends Component>, ComponentType> types = new HashMap<>();
|
||||||
private static final ComponentType[] typeById = new ComponentType[Long.SIZE];
|
private static final ComponentType[] typeById = new ComponentType[Long.SIZE];
|
||||||
@ -11,8 +11,8 @@ public class ComponentType {
|
|||||||
private static long nextBit = 1l;
|
private static long nextBit = 1l;
|
||||||
private static int nextId = 0;
|
private static int nextId = 0;
|
||||||
|
|
||||||
private long bits;
|
protected long bits;
|
||||||
private int id;
|
protected int id;
|
||||||
|
|
||||||
private ComponentType() {
|
private ComponentType() {
|
||||||
this.bits = nextBit;
|
this.bits = nextBit;
|
||||||
@ -26,7 +26,7 @@ public class ComponentType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected int getId() {
|
protected int getId() {
|
||||||
return this.id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static long getMaskBits(Class<? extends Component>... components) {
|
protected static long getMaskBits(Class<? extends Component>... components) {
|
||||||
@ -38,11 +38,11 @@ public class ComponentType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static long getTypeBits(Class<? extends Component> component) {
|
protected static long getTypeBits(Class<? extends Component> component) {
|
||||||
return getComponentType(component).getBits();
|
return getComponentType(component).bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static int getTypeId(Class<? extends Component> component) {
|
protected static int getTypeId(Class<? extends Component> component) {
|
||||||
return getComponentType(component).getId();
|
return getComponentType(component).id;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ComponentType getById(int id) {
|
protected static ComponentType getById(int id) {
|
||||||
@ -60,7 +60,7 @@ public class ComponentType {
|
|||||||
}
|
}
|
||||||
type = new ComponentType();
|
type = new ComponentType();
|
||||||
types.put(component, type);
|
types.put(component, type);
|
||||||
typeById[type.getId()] = type;
|
typeById[type.id] = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static ComponentType getComponentType(Class<? extends Component> component) {
|
protected static ComponentType getComponentType(Class<? extends Component> component) {
|
||||||
|
@ -122,7 +122,7 @@ public class Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Entity entity : toRemove) {
|
for (Entity entity : toRemove) {
|
||||||
removeAllEntityComponents(entity.getId());
|
removeAllEntityComponents(entity.id);
|
||||||
entities.removeValue(entity, true);
|
entities.removeValue(entity, true);
|
||||||
removeEntityFromSystems(entity);
|
removeEntityFromSystems(entity);
|
||||||
removedEntities.add(entity);
|
removedEntities.add(entity);
|
||||||
@ -173,24 +173,27 @@ public class Engine {
|
|||||||
|
|
||||||
protected void addEntityComponent(Entity entity, Component component) {
|
protected void addEntityComponent(Entity entity, Component component) {
|
||||||
ComponentType type = ComponentType.getComponentType(component.getClass());
|
ComponentType type = ComponentType.getComponentType(component.getClass());
|
||||||
components[type.getId()].insert(entity.getId(), component);
|
components[type.id].insert(entity.id, component);
|
||||||
entity.addComponentType(type);
|
entity.addComponentType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void removeEntityComponent(Entity entity, Component component) {
|
protected void removeEntityComponent(Entity entity, Component component) {
|
||||||
ComponentType type = ComponentType.getComponentType(component.getClass());
|
ComponentType type = ComponentType.getComponentType(component.getClass());
|
||||||
components[type.getId()].remove(entity.getId());
|
components[type.id].remove(entity.id);
|
||||||
entity.removeComponentType(type);
|
entity.removeComponentType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Component> T getEntityComponent(Entity entity, Class<T> clazz) {
|
protected <T extends Component> T getEntityComponent(Entity entity, Class<T> clazz) {
|
||||||
ComponentType type = ComponentType.getComponentType(clazz);
|
return clazz.cast(components[ComponentType.getTypeId(clazz)].get(entity.id));
|
||||||
return clazz.cast(components[type.getId()].get(entity.getId()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Component getEntityComponent(Entity entity, ComponentType type) {
|
protected Component getEntityComponent(Entity entity, ComponentType type) {
|
||||||
return components[type.getId()].get(entity.getId());
|
return components[type.id].get(entity.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean entityHasComponent(Entity entity, ComponentType type) {
|
||||||
|
return components[type.id].contains(entity.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
@ -7,12 +7,12 @@ public final class Entity {
|
|||||||
|
|
||||||
private Engine engine;
|
private Engine engine;
|
||||||
|
|
||||||
private int id;
|
protected int id;
|
||||||
|
protected long componentBits;
|
||||||
|
|
||||||
private long uniqueId;
|
private long uniqueId;
|
||||||
private boolean active;
|
private boolean active;
|
||||||
|
|
||||||
private long componentBits;
|
|
||||||
|
|
||||||
protected Entity(Engine engine) {
|
protected Entity(Engine engine) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.active = false;
|
this.active = false;
|
||||||
@ -66,15 +66,11 @@ public final class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void addComponentType(ComponentType type) {
|
protected void addComponentType(ComponentType type) {
|
||||||
componentBits |= type.getBits();
|
componentBits |= type.bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void removeComponentType(ComponentType type) {
|
protected void removeComponentType(ComponentType type) {
|
||||||
componentBits &= ~type.getBits();
|
componentBits &= ~type.bits;
|
||||||
}
|
|
||||||
|
|
||||||
protected long getComponentBits() {
|
|
||||||
return componentBits;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void reset() {
|
protected void reset() {
|
||||||
|
@ -27,7 +27,7 @@ public abstract class EntitySystem extends BaseSystem {
|
|||||||
public abstract void processEntity(Entity entity, float dt);
|
public abstract void processEntity(Entity entity, float dt);
|
||||||
|
|
||||||
public boolean interestedIn(Entity entity) {
|
public boolean interestedIn(Entity entity) {
|
||||||
return (entity.getComponentBits() & typeBits) == typeBits;
|
return (entity.componentBits & typeBits) == typeBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addEntity(Entity entity) {
|
protected void addEntity(Entity entity) {
|
||||||
|
Loading…
Reference in New Issue
Block a user