Skip to content

Commit

Permalink
Merge pull request #222 from laubed/master
Browse files Browse the repository at this point in the history
Added addAndReturn for easier Component Manipulation
  • Loading branch information
dsaltares authored Jul 1, 2016
2 parents fe3ed12 + 423148a commit 969dc72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ashley/src/com/badlogic/ashley/core/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ public Entity add (Component component) {
return this;
}

/**
* Adds a {@link Component} to this Entity. If a {@link Component} of the same type already exists, it'll be replaced.
* @return The Component for direct component manipulation (e.g. PooledComponent)
*/
public Component addAndReturn(Component component) {
add(component);
return component;
}

/**
* Removes the {@link Component} of the specified type. Since there is only ever one component of one type, we don't need an
* instance reference.
Expand Down
12 changes: 12 additions & 0 deletions ashley/tests/com/badlogic/ashley/core/EntityTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void receive (Signal<Entity> signal, Entity object) {
private ComponentMapper<ComponentA> am = ComponentMapper.getFor(ComponentA.class);
private ComponentMapper<ComponentB> bm = ComponentMapper.getFor(ComponentB.class);

@Test
public void addAndReturnComponent(){
Entity entity = new Entity();
ComponentA componentA = new ComponentA();
ComponentB componentB = new ComponentB();

assertEquals(componentA, entity.addAndReturn(componentA));
assertEquals(componentB, entity.addAndReturn(componentB));

assertEquals(2, entity.getComponents().size());
}

@Test
public void noComponents () {
Entity entity = new Entity();
Expand Down

0 comments on commit 969dc72

Please sign in to comment.