diff --git a/core/src/com/me/common/ecs/ComponentBag.java b/core/src/com/me/common/ecs/ComponentBag.java index 39b3c0f..4e635b9 100644 --- a/core/src/com/me/common/ecs/ComponentBag.java +++ b/core/src/com/me/common/ecs/ComponentBag.java @@ -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)); diff --git a/core/src/com/me/common/ecs/ComponentMapper.java b/core/src/com/me/common/ecs/ComponentMapper.java index 2baac3d..09efc6f 100644 --- a/core/src/com/me/common/ecs/ComponentMapper.java +++ b/core/src/com/me/common/ecs/ComponentMapper.java @@ -16,4 +16,8 @@ public class ComponentMapper { return typeClass.cast(engine.getEntityComponent(entity, type)); } + public boolean has(Entity entity) { + return type.isTypeInMask(entity.componentBits); + } + } diff --git a/core/src/com/me/common/ecs/ComponentType.java b/core/src/com/me/common/ecs/ComponentType.java index 0fcabbd..c14c8c2 100644 --- a/core/src/com/me/common/ecs/ComponentType.java +++ b/core/src/com/me/common/ecs/ComponentType.java @@ -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, 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... components) { @@ -38,11 +38,11 @@ public class ComponentType { } protected static long getTypeBits(Class component) { - return getComponentType(component).getBits(); + return getComponentType(component).bits; } protected static int getTypeId(Class 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 component) { diff --git a/core/src/com/me/common/ecs/Engine.java b/core/src/com/me/common/ecs/Engine.java index d9dd968..8d6dd51 100644 --- a/core/src/com/me/common/ecs/Engine.java +++ b/core/src/com/me/common/ecs/Engine.java @@ -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 getEntityComponent(Entity entity, Class 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") diff --git a/core/src/com/me/common/ecs/Entity.java b/core/src/com/me/common/ecs/Entity.java index a4aa5e0..808c8a6 100644 --- a/core/src/com/me/common/ecs/Entity.java +++ b/core/src/com/me/common/ecs/Entity.java @@ -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() { diff --git a/core/src/com/me/common/ecs/EntitySystem.java b/core/src/com/me/common/ecs/EntitySystem.java index 89e0b8c..378ee09 100644 --- a/core/src/com/me/common/ecs/EntitySystem.java +++ b/core/src/com/me/common/ecs/EntitySystem.java @@ -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) {