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:
Matt Low 2020-01-26 15:47:53 +04:00
parent bd37d4fc65
commit 82828ff05d
6 changed files with 30 additions and 23 deletions

View File

@ -17,6 +17,10 @@ public class ComponentBag {
return items[index];
}
public boolean contains(int index) {
return index < size && items[index] != null;
}
public void insert(int index, Component item) {
if (index >= size) {
grow((int) (index * 1.5f));

View File

@ -16,4 +16,8 @@ public class ComponentMapper<T extends Component> {
return typeClass.cast(engine.getEntityComponent(entity, type));
}
public boolean has(Entity entity) {
return type.isTypeInMask(entity.componentBits);
}
}

View File

@ -3,7 +3,7 @@ package com.me.common.ecs;
import java.util.HashMap;
import java.util.Map;
public class ComponentType {
final class ComponentType {
private static final Map<Class<? extends Component>, ComponentType> types = new HashMap<>();
private static final ComponentType[] typeById = new ComponentType[Long.SIZE];
@ -11,8 +11,8 @@ public class ComponentType {
private static long nextBit = 1l;
private static int nextId = 0;
private long bits;
private int id;
protected long bits;
protected int id;
private ComponentType() {
this.bits = nextBit;
@ -26,7 +26,7 @@ public class ComponentType {
}
protected int getId() {
return this.id;
return id;
}
protected static long getMaskBits(Class<? extends Component>... components) {
@ -38,11 +38,11 @@ public class ComponentType {
}
protected static long getTypeBits(Class<? extends Component> component) {
return getComponentType(component).getBits();
return getComponentType(component).bits;
}
protected static int getTypeId(Class<? extends Component> component) {
return getComponentType(component).getId();
return getComponentType(component).id;
}
protected static ComponentType getById(int id) {
@ -60,7 +60,7 @@ public class ComponentType {
}
type = new ComponentType();
types.put(component, type);
typeById[type.getId()] = type;
typeById[type.id] = type;
}
protected static ComponentType getComponentType(Class<? extends Component> component) {

View File

@ -122,7 +122,7 @@ public class Engine {
}
for (Entity entity : toRemove) {
removeAllEntityComponents(entity.getId());
removeAllEntityComponents(entity.id);
entities.removeValue(entity, true);
removeEntityFromSystems(entity);
removedEntities.add(entity);
@ -173,24 +173,27 @@ public class Engine {
protected void addEntityComponent(Entity entity, Component component) {
ComponentType type = ComponentType.getComponentType(component.getClass());
components[type.getId()].insert(entity.getId(), component);
components[type.id].insert(entity.id, component);
entity.addComponentType(type);
}
protected void removeEntityComponent(Entity entity, Component component) {
ComponentType type = ComponentType.getComponentType(component.getClass());
components[type.getId()].remove(entity.getId());
components[type.id].remove(entity.id);
entity.removeComponentType(type);
}
protected <T extends Component> T getEntityComponent(Entity entity, Class<T> clazz) {
ComponentType type = ComponentType.getComponentType(clazz);
return clazz.cast(components[type.getId()].get(entity.getId()));
return clazz.cast(components[ComponentType.getTypeId(clazz)].get(entity.id));
}
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")

View File

@ -7,12 +7,12 @@ public final class Entity {
private Engine engine;
private int id;
protected int id;
protected long componentBits;
private long uniqueId;
private boolean active;
private long componentBits;
protected Entity(Engine engine) {
this.engine = engine;
this.active = false;
@ -66,15 +66,11 @@ public final class Entity {
}
protected void addComponentType(ComponentType type) {
componentBits |= type.getBits();
componentBits |= type.bits;
}
protected void removeComponentType(ComponentType type) {
componentBits &= ~type.getBits();
}
protected long getComponentBits() {
return componentBits;
componentBits &= ~type.bits;
}
protected void reset() {

View File

@ -27,7 +27,7 @@ public abstract class EntitySystem extends BaseSystem {
public abstract void processEntity(Entity entity, float dt);
public boolean interestedIn(Entity entity) {
return (entity.getComponentBits() & typeBits) == typeBits;
return (entity.componentBits & typeBits) == typeBits;
}
protected void addEntity(Entity entity) {