Skip to content

Commit

Permalink
Fixes issue #148, makes Builder constructor package protected to avoi…
Browse files Browse the repository at this point in the history
…d partial family construction
  • Loading branch information
dsaltares committed Mar 14, 2015
1 parent 2761213 commit 0c662a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
38 changes: 17 additions & 21 deletions ashley/src/com/badlogic/ashley/core/Family.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
public class Family {
private static ObjectMap<String, Family> families = new ObjectMap<String, Family>();
private static int familyIndex = 0;
// zero bits should goes first for builder
private static final Bits zeroBits = new Bits();
private static final Builder builder = new Builder();
private static final Bits zeroBits = new Bits();

private final Bits all;
private final Bits one;
Expand Down Expand Up @@ -77,33 +76,37 @@ public boolean matches (Entity entity) {
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public static final Builder all (Class<? extends Component>... componentTypes) {
return builder.all(componentTypes);
public static Builder all (Class<? extends Component>... componentTypes) {
return builder.reset().all(componentTypes);
}

/**
* @param componentTypes entities will have to contain at least one of the specified components.
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public static final Builder one (Class<? extends Component>... componentTypes) {
return builder.one(componentTypes);
public static Builder one (Class<? extends Component>... componentTypes) {
return builder.reset().one(componentTypes);
}

/**
* @param componentTypes entities cannot contain any of the specified components.
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public static final Builder exclude (Class<? extends Component>... componentTypes) {
return builder.exclude(componentTypes);
public static Builder exclude (Class<? extends Component>... componentTypes) {
return builder.reset().exclude(componentTypes);
}

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

Builder() {

}

/**
* Resets the builder instance
* @return A Builder singleton instance to get a family
Expand All @@ -120,10 +123,8 @@ public Builder reset () {
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public final Builder all (Class<? extends Component>... componentTypes) {
Bits bits = ComponentType.getBitsFor(componentTypes);
bits.or(all);
all = bits;
public Builder all (Class<? extends Component>... componentTypes) {
all = ComponentType.getBitsFor(componentTypes);
return this;
}

Expand All @@ -132,10 +133,8 @@ public final Builder all (Class<? extends Component>... componentTypes) {
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public final Builder one (Class<? extends Component>... componentTypes) {
Bits bits = ComponentType.getBitsFor(componentTypes);
bits.or(one);
one = bits;
public Builder one (Class<? extends Component>... componentTypes) {
one = ComponentType.getBitsFor(componentTypes);
return this;
}

Expand All @@ -144,10 +143,8 @@ public final Builder one (Class<? extends Component>... componentTypes) {
* @return A Builder singleton instance to get a family
*/
@SafeVarargs
public final Builder exclude (Class<? extends Component>... componentTypes) {
Bits bits = ComponentType.getBitsFor(componentTypes);
bits.or(exclude);
exclude = bits;
public Builder exclude (Class<? extends Component>... componentTypes) {
exclude = ComponentType.getBitsFor(componentTypes);
return this;
}

Expand All @@ -159,7 +156,6 @@ public Family get () {
family = new Family(all, one, exclude);
families.put(hash, family);
}
reset();
return family;
}
}
Expand Down
17 changes: 0 additions & 17 deletions ashley/tests/com/badlogic/ashley/core/FamilyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,23 +335,6 @@ public void matchWithoutSystems () {
engine.clearPools();
}

@Test
public void matchWithPartialBuilding () {
Component[] components = {new ComponentA(), new ComponentB(), new ComponentC()};
Entity entity = new Entity();
for (Component component : components) {
entity.add(component);
}

Builder builder = new Builder();
for (Component component : components) {
builder.all(component.getClass());
}
Family family = builder.get();
assertTrue(family.matches(entity));
assertSame(family, Family.all(ComponentA.class, ComponentB.class, ComponentC.class).get());
}

@Test
public void matchWithComplexBuilding () {
Family family = Family.all(ComponentB.class).one(ComponentA.class).exclude(ComponentC.class).get();
Expand Down

0 comments on commit 0c662a8

Please sign in to comment.