Skip to content

Commit 10f7710

Browse files
committed
Add new getObjects method
1 parent 7d09375 commit 10f7710

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Version 0.5.8
2+
3+
* add new `getObjects` method to `GameWorld`
4+
15
# Version 0.5.7
26

37
* deleted `GameInputAdaptr` - use `GestureListener` via libgdx instead

Diff for: core/src/main/java/de/bitbrain/braingdx/world/GameWorld.java

+23
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,33 @@ public void update(float delta) {
212212
}
213213
}
214214

215+
/**
216+
* Gets an object by id
217+
*/
215218
public GameObject getObjectById(String id) {
216219
return identityMap.get(id);
217220
}
218221

222+
/**
223+
* Returns a list of all objects within this world.
224+
*/
225+
public List<GameObject> getObjects() {
226+
return getObjects(null);
227+
}
228+
229+
230+
/**
231+
* Returns a list of all objects within this world, ordered by the comparator provided. The comparator
232+
* can be null.
233+
*/
234+
public List<GameObject> getObjects(Comparator<GameObject> comparator) {
235+
List<GameObject> result = new ArrayList<GameObject>(objects);
236+
if (comparator != null) {
237+
Collections.sort(result, comparator);
238+
}
239+
return result;
240+
}
241+
219242
/**
220243
* Number of active objects in the world
221244
*

Diff for: core/src/test/java/de/bitbrain/braingdx/world/GameWorldTest.java

+58
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import com.badlogic.gdx.graphics.OrthographicCamera;
44
import de.bitbrain.braingdx.util.GdxUtils;
5+
import de.bitbrain.braingdx.util.Mutator;
56
import org.junit.Before;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
89
import org.mockito.Mock;
910
import org.mockito.runners.MockitoJUnitRunner;
1011

12+
import java.util.Comparator;
13+
import java.util.List;
14+
1115
import static org.assertj.core.api.Assertions.assertThat;
1216

1317
@RunWith(MockitoJUnitRunner.class)
@@ -74,4 +78,58 @@ public void testUpdateWithListener() {
7478
world.update(0f);
7579
assertThat(object.getId()).isEqualTo(fakeIdSupplier.getCurrentId());
7680
}
81+
82+
@Test
83+
public void testGetObjects() {
84+
GameObject object1 = world.addObject(new Mutator<GameObject>() {
85+
@Override
86+
public void mutate(GameObject target) {
87+
target.setLeft(10);
88+
}
89+
});
90+
GameObject object2 = world.addObject(new Mutator<GameObject>() {
91+
@Override
92+
public void mutate(GameObject target) {
93+
target.setLeft(30);
94+
}
95+
});
96+
GameObject object3 = world.addObject(new Mutator<GameObject>() {
97+
@Override
98+
public void mutate(GameObject target) {
99+
target.setLeft(20);
100+
}
101+
});
102+
List<GameObject> sorted = world.getObjects();
103+
assertThat(sorted).containsExactly(object1, object2, object3);
104+
}
105+
106+
@Test
107+
public void testGetObjectsSorted() {
108+
GameObject object1 = world.addObject(new Mutator<GameObject>() {
109+
@Override
110+
public void mutate(GameObject target) {
111+
target.setLeft(10);
112+
}
113+
});
114+
GameObject object2 = world.addObject(new Mutator<GameObject>() {
115+
@Override
116+
public void mutate(GameObject target) {
117+
target.setLeft(30);
118+
}
119+
});
120+
GameObject object3 = world.addObject(new Mutator<GameObject>() {
121+
@Override
122+
public void mutate(GameObject target) {
123+
target.setLeft(20);
124+
}
125+
});
126+
List<GameObject> sorted = world.getObjects(new Comparator<GameObject>() {
127+
128+
@Override
129+
public int compare(GameObject o1, GameObject o2) {
130+
return (int) (o1.getLeft() - o2.getLeft());
131+
}
132+
});
133+
assertThat(sorted).containsExactly(object1, object3, object2);
134+
}
77135
}

0 commit comments

Comments
 (0)