Skip to content

Commit

Permalink
Merge pull request #162 from Antag99/master
Browse files Browse the repository at this point in the history
Fix various Family issues
  • Loading branch information
dsaltares committed May 16, 2015
2 parents bb42f16 + d780e7f commit 99928a1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
19 changes: 4 additions & 15 deletions ashley/src/com/badlogic/ashley/core/Family.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ public int getIndex () {
public boolean matches (Entity entity) {
Bits entityComponentBits = entity.getComponentBits();

if (entityComponentBits.isEmpty()) return false;

for (int i = all.nextSetBit(0); i >= 0; i = all.nextSetBit(i + 1)) {
if (!entityComponentBits.get(i)) return false;
if (!entityComponentBits.containsAll(all)) {
return false;
}

if (!one.isEmpty() && !one.intersects(entityComponentBits)) {
Expand Down Expand Up @@ -162,21 +160,12 @@ public Family get () {

@Override
public int hashCode () {
final int prime = 31;
int result = 1;
result = prime * result + all.hashCode();
result = prime * result + one.hashCode();
result = prime * result + exclude.hashCode();
result = prime * result + index;
return result;
return index;
}

@Override
public boolean equals (Object obj) {
if (this == obj) return true;
if (!(obj instanceof Family)) return false;
Family other = (Family)obj;
return index == other.index && all.equals(other.all) && one.equals(other.one) && exclude.equals(other.exclude);
return this == obj;
}

private static String getFamilyHash (Bits all, Bits one, Bits exclude) {
Expand Down
16 changes: 16 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/FamilyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void processEntity (Entity e, float d) {

@Test
public void validFamily () {
assertNotNull(Family.all().get());
assertNotNull(Family.all(ComponentA.class).get());
assertNotNull(Family.all(ComponentB.class).get());
assertNotNull(Family.all(ComponentC.class).get());
Expand All @@ -94,6 +95,8 @@ public void sameFamily () {
.exclude(ComponentE.class, ComponentF.class).get();
Family family8 = Family.all(ComponentA.class, ComponentB.class).one(ComponentC.class, ComponentD.class)
.exclude(ComponentE.class, ComponentF.class).get();
Family family9 = Family.all().get();
Family family10 = Family.all().get();

assertTrue(family1.equals(family2));
assertTrue(family2.equals(family1));
Expand All @@ -103,11 +106,13 @@ public void sameFamily () {
assertTrue(family6.equals(family5));
assertTrue(family7.equals(family8));
assertTrue(family8.equals(family7));
assertTrue(family9.equals(family10));

assertEquals(family1.getIndex(), family2.getIndex());
assertEquals(family3.getIndex(), family4.getIndex());
assertEquals(family5.getIndex(), family6.getIndex());
assertEquals(family7.getIndex(), family8.getIndex());
assertEquals(family9.getIndex(), family10.getIndex());
}

@Test
Expand All @@ -126,6 +131,7 @@ public void differentFamily () {
.exclude(ComponentE.class, ComponentF.class).get();
Family family12 = Family.all(ComponentC.class, ComponentD.class).one(ComponentE.class, ComponentF.class)
.exclude(ComponentA.class, ComponentB.class).get();
Family family13 = Family.all().get();

assertFalse(family1.equals(family2));
assertFalse(family1.equals(family3));
Expand All @@ -138,6 +144,7 @@ public void differentFamily () {
assertFalse(family1.equals(family10));
assertFalse(family1.equals(family11));
assertFalse(family1.equals(family12));
assertFalse(family1.equals(family13));

assertFalse(family10.equals(family1));
assertFalse(family10.equals(family2));
Expand All @@ -149,6 +156,7 @@ public void differentFamily () {
assertFalse(family10.equals(family8));
assertFalse(family10.equals(family9));
assertFalse(family11.equals(family12));
assertFalse(family10.equals(family13));

assertNotEquals(family1.getIndex(), family2.getIndex());
assertNotEquals(family1.getIndex(), family3.getIndex());
Expand All @@ -160,6 +168,7 @@ public void differentFamily () {
assertNotEquals(family1.getIndex(), family9.getIndex());
assertNotEquals(family1.getIndex(), family10.getIndex());
assertNotEquals(family11.getIndex(), family12.getIndex());
assertNotEquals(family1.getIndex(), family13.getIndex());
}

@Test
Expand Down Expand Up @@ -238,6 +247,13 @@ public void entityMismatchThenMatch () {
assertTrue(family.matches(entity));
}

@Test
public void testEmptyFamily() {
Family family = Family.all().get();
Entity entity = new Entity();
assertTrue(family.matches(entity));
}

@Test
public void familyFiltering () {
Family family1 = Family.all(ComponentA.class, ComponentB.class).one(ComponentC.class, ComponentD.class)
Expand Down

0 comments on commit 99928a1

Please sign in to comment.