Skip to content

Commit

Permalink
Fixes #390: limit ComponentMappers to one per component type per world.
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Nov 8, 2015
1 parent f07e415 commit ef04f7a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions artemis/src/main/java/com/artemis/ComponentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ComponentManager extends BaseSystem {
private final ComponentPool pooledComponents;

private int highestSeenEntityId;

private Bag<ComponentMapper> componentMappers = new Bag<ComponentMapper>();

protected final ComponentTypeFactory typeFactory;

Expand Down Expand Up @@ -150,8 +152,7 @@ private void ensurePackedComponentCapacity(int entityId) {
}
}

protected BitSet getPackedComponentOwners(ComponentType type)
{
protected BitSet getPackedComponentOwners(ComponentType type) {
BitSet owners = packedComponentOwners.safeGet(type.getIndex());
if (owners == null) {
owners = new BitSet();
Expand All @@ -160,6 +161,15 @@ protected BitSet getPackedComponentOwners(ComponentType type)
return owners;
}

<T extends Component> ComponentMapper<T> getMapper(Class<T> mapper) {
int index = typeFactory.getIndexFor(mapper);
if (componentMappers.safeGet(index) == null) {
componentMappers.set(index, ComponentMapper.getFor(mapper, world));
}

return componentMappers.get(index);
}

@SuppressWarnings("unchecked")
<T extends Component> T newInstance(Class<T> componentClass, boolean constructorHasWorldParameter) {
try {
Expand Down
2 changes: 1 addition & 1 deletion artemis/src/main/java/com/artemis/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ void updateEntityStates() {
* @return mapper for specified component type
*/
public <T extends Component> ComponentMapper<T> getMapper(Class<T> type) {
return BasicComponentMapper.getFor(type, this);
return cm.getMapper(type);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions artemis/src/test/java/com/artemis/ComponentMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ protected void process(Entity e) {
createAndProcessWorld(new TestSystemA());
}

@Test
public void mappers_are_per_type_per_world() {
World w1 = new World();
World w2 = new World();

assertNotSame(w1.getMapper(ComponentX.class), w2.getMapper(ComponentX.class));
assertNotSame(w1.getMapper(ComponentX.class), w1.getMapper(ComponentY.class));
assertSame(w1.getMapper(ComponentX.class), w1.getMapper(ComponentX.class));
}

@Test
public void create_if_exists_should_recycle_existing_component() {

Expand Down

0 comments on commit ef04f7a

Please sign in to comment.