Skip to content

Commit

Permalink
Family creation from families libgdx#267
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiitch committed Jan 2, 2022
1 parent 07b3ce3 commit 66f5684
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* **Bug fix**: Poolable Component returned to their ComponentPools even if EntityPool is full. Issue #302.
* **Update**: Uses libgdx 1.10.0. Commit afa68fc165119a2c79c1709c642e6b620a973ecc.
* **API addition***: Adds 'concat()' method to `Family`, you can create family from a family class. Issue #267
* **API modification***: FamilyBuilder one/all/exclude can be chain multiple times.(Before doing Family.one(ComponentA.class).one(ComponentB.class) remove ComponentA)

### Ashley 1.7.4

Expand Down
39 changes: 29 additions & 10 deletions ashley/src/com/badlogic/ashley/core/Family.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,28 @@ public static final Builder exclude (Class<? extends Component>... componentType
return builder.reset().exclude(componentTypes);
}

@SafeVarargs
public static final Builder concat (Family... families) {
return builder.reset().concat(families);
}

public static class Builder {
private Bits all = zeroBits;
private Bits one = zeroBits;
private Bits exclude = zeroBits;
private Bits all;
private Bits one ;
private Bits exclude;

Builder() {

reset();
}

/**
* Resets the builder instance
* @return A Builder singleton instance to get a family
*/
public Builder reset () {
all = zeroBits;
one = zeroBits;
exclude = zeroBits;
all = new Bits();
one = new Bits();
exclude = new Bits();
return this;
}

Expand All @@ -122,7 +127,7 @@ public Builder reset () {
*/
@SafeVarargs
public final Builder all (Class<? extends Component>... componentTypes) {
all = ComponentType.getBitsFor(componentTypes);
all.or(ComponentType.getBitsFor(componentTypes));
return this;
}

Expand All @@ -132,7 +137,7 @@ public final Builder all (Class<? extends Component>... componentTypes) {
*/
@SafeVarargs
public final Builder one (Class<? extends Component>... componentTypes) {
one = ComponentType.getBitsFor(componentTypes);
one.or(ComponentType.getBitsFor(componentTypes));
return this;
}

Expand All @@ -142,10 +147,24 @@ public final Builder one (Class<? extends Component>... componentTypes) {
*/
@SafeVarargs
public final Builder exclude (Class<? extends Component>... componentTypes) {
exclude = ComponentType.getBitsFor(componentTypes);
exclude.or(ComponentType.getBitsFor(componentTypes));
return this;
}

/**
* @param families are concat with Family in builder (one, all and exclude)
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public final Builder concat (Family... families) {
for(Family family : families){
one.or(family.one);
all.or(family.all);
exclude.or(family.exclude);
}
return this;
}

/** @return A family for the configured component types */
public Family get () {
String hash = getFamilyHash(all, one, exclude);
Expand Down
42 changes: 42 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/FamilyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public void processEntity (Entity e, float d) {
}
}

@Test
public void recallOperationTest () {
Family family = Family.one(ComponentA.class).one(ComponentB.class).get();

Entity entity = new Entity();
assertFalse(family.matches(entity));

entity.add(new ComponentA());
assertTrue(family.matches(entity));

entity.remove(ComponentA.class);
entity.add(new ComponentB());
assertTrue(family.matches(entity));
}

@Test
public void validFamily () {
assertNotNull(Family.all().get());
Expand Down Expand Up @@ -299,6 +314,33 @@ public void familyFiltering () {
assertTrue(family2.matches(entity));
}

@Test
public void familyConcat() {
Family family1 = Family.one(ComponentA.class).exclude(ComponentD.class, ComponentE.class).get();
Family family2 = Family.one(ComponentF.class).concat(family1).get();

Entity entity = new Entity(); //empty
assertFalse(family1.matches(entity));
assertFalse(family2.matches(entity));

entity.add(new ComponentA());//family one match so second match
assertTrue(family1.matches(entity));
assertTrue(family2.matches(entity));

entity.add(new ComponentF());
assertTrue(family1.matches(entity));
assertTrue(family2.matches(entity));

entity.remove(ComponentA.class);
assertFalse(family1.matches(entity));
assertTrue(family2.matches(entity));//match cause componentF

entity.add(new ComponentD());//no match, D is excluded by Family1
assertFalse(family1.matches(entity));
assertFalse(family2.matches(entity));
}


@Test
public void matchWithPooledEngine () {
PooledEngine engine = new PooledEngine();
Expand Down

0 comments on commit 66f5684

Please sign in to comment.