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

Added addAndReturn for easier Component Manipulation #222

Merged
merged 1 commit into from
Jul 1, 2016
Merged
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
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