@Test public void addAndRemoveComponent() { Entity entity = new Entity(); entity.add(new ComponentA()); assertEquals(1, entity.getComponents().size()); Bits componentBits = entity.getComponentBits(); int componentAIndex = ComponentType.getIndexFor(ComponentA.class); for (int i = 0; i < componentBits.length(); ++i) { assertEquals(i == componentAIndex, componentBits.get(i)); } assertNotNull(am.get(entity)); assertNull(bm.get(entity)); assertTrue(am.has(entity)); assertFalse(bm.has(entity)); entity.remove(ComponentA.class); assertEquals(0, entity.getComponents().size()); for (int i = 0; i < componentBits.length(); ++i) { assertFalse(componentBits.get(i)); } assertNull(am.get(entity)); assertNull(bm.get(entity)); assertFalse(am.has(entity)); assertFalse(bm.has(entity)); }
/** Gets a list of all the components of the specified type. */ public <T> List<T> getComponents(Class<T> type) { ArrayList<T> components = new ArrayList<T>(); for (Entity e : list) { components.addAll(e.getComponents(type)); } return components; }
@Test public void noComponents() { Entity entity = new Entity(); assertEquals(0, entity.getComponents().size()); assertTrue(entity.getComponentBits().isEmpty()); assertNull(am.get(entity)); assertNull(bm.get(entity)); assertFalse(am.has(entity)); assertFalse(bm.has(entity)); }
@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 addSameComponent() { Entity entity = new Entity(); ComponentA a1 = new ComponentA(); ComponentA a2 = new ComponentA(); entity.add(a1); entity.add(a2); assertEquals(1, entity.getComponents().size()); assertTrue(am.has(entity)); assertNotEquals(a1, am.get(entity)); assertEquals(a2, am.get(entity)); }