Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Family creation from families #267 #305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +147,21 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this formatting be altered to be consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :D

exclude.or(family.exclude);
}
return this;
}

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