Skip to content

Commit

Permalink
Entity uses long for id
Browse files Browse the repository at this point in the history
PooledEntity correctly sets its id to 0 on reset
Changed getIndex() to getId()
  • Loading branch information
dsaltares committed Oct 2, 2014
1 parent 54ba4d6 commit 6f9d2b7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
16 changes: 8 additions & 8 deletions ashley/src/com/badlogic/ashley/core/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @author Stefan Bachmann
*/
public class Entity {
private static int nextIndex;
private static long nextId;

/** A flag that can be used to bit mask this entity. Up to the user to manage. */
public int flags;
Expand All @@ -40,7 +40,7 @@ public class Entity {
public final Signal<Entity> componentRemoved;

/** Unique entity index for fast retrieval */
private int index;
protected Long id;
/** A collection that holds all the components indexed by their {@link ComponentType} index */
private Bag<Component> components;
/** An auxiliary array for user access to all the components of an entity */
Expand All @@ -65,17 +65,17 @@ public Entity(){
familyBits = new Bits();
flags = 0;

index = nextIndex++;
id = new Long(nextId++);

componentAdded = new Signal<Entity>();
componentRemoved = new Signal<Entity>();
}

/**
* @return The Entity's unique index.
* @return The Entity's unique id.
*/
public int getIndex(){
return index;
public long getId(){
return id;
}

/**
Expand Down Expand Up @@ -223,7 +223,7 @@ Component removeInternal(Class<? extends Component> componentClass){

@Override
public int hashCode() {
return index;
return id.hashCode();
}

@Override
Expand All @@ -235,6 +235,6 @@ public boolean equals(Object obj) {
if (!(obj instanceof Entity))
return false;
Entity other = (Entity) obj;
return index == other.index;
return id.equals(other.id);
}
}
1 change: 1 addition & 0 deletions ashley/src/com/badlogic/ashley/core/PooledEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Component removeInternal(Class<? extends Component> componentType){
@Override
public void reset() {
removeAll();
id = 0L;
flags = 0;
componentAdded.removeAllListeners();
componentRemoved.removeAllListeners();
Expand Down
6 changes: 3 additions & 3 deletions ashley/tests/com/badlogic/ashley/core/EntityTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public void receive(Signal<Entity> signal, Entity object) {
public void uniqueIndex() {
int numEntities = 10000;
Array<Entity> entities = new Array<Entity>();
Set<Integer> ids = new HashSet<Integer>();
Set<Long> ids = new HashSet<Long>();

for (int i = 0; i < numEntities; ++i) {
Entity entity = new Entity();
assertFalse(ids.contains(entity.getIndex()));
ids.add(entity.getIndex());
assertFalse(ids.contains(entity.getId()));
ids.add(entity.getId());
entities.add(entity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void resetEntityCorrectly() {
assertEquals(0, entities[i].getComponents().size());
assertTrue(entities[i].getFamilyBits().isEmpty());
assertFalse(familyEntities.contains(entities[i], true));
assertEquals(0L, entities[i].getId());

entities[i].componentAdded.dispatch(entities[i]);
entities[i].componentRemoved.dispatch(entities[i]);
Expand Down

0 comments on commit 6f9d2b7

Please sign in to comment.