From 423148a2719873efb50d4f2b3982c247df4fceca Mon Sep 17 00:00:00 2001 From: Daniel Laube Date: Tue, 24 May 2016 21:22:44 +0200 Subject: [PATCH] Added addAndReturn Sometimes it is useful to return the Component instead of the Entity. Especially when using the Pooled Engine --- ashley/src/com/badlogic/ashley/core/Entity.java | 9 +++++++++ .../tests/com/badlogic/ashley/core/EntityTests.java | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ashley/src/com/badlogic/ashley/core/Entity.java b/ashley/src/com/badlogic/ashley/core/Entity.java index 29c63f47..df133e96 100644 --- a/ashley/src/com/badlogic/ashley/core/Entity.java +++ b/ashley/src/com/badlogic/ashley/core/Entity.java @@ -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. diff --git a/ashley/tests/com/badlogic/ashley/core/EntityTests.java b/ashley/tests/com/badlogic/ashley/core/EntityTests.java index 9da9bef6..f9350c7e 100644 --- a/ashley/tests/com/badlogic/ashley/core/EntityTests.java +++ b/ashley/tests/com/badlogic/ashley/core/EntityTests.java @@ -52,6 +52,18 @@ public void receive (Signal signal, Entity object) { private ComponentMapper am = ComponentMapper.getFor(ComponentA.class); private ComponentMapper 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();