@Test public void componentListener() { EntityListenerMock addedListener = new EntityListenerMock(); EntityListenerMock removedListener = new EntityListenerMock(); Entity entity = new Entity(); entity.componentAdded.add(addedListener); entity.componentRemoved.add(removedListener); assertEquals(0, addedListener.counter); assertEquals(0, removedListener.counter); entity.add(new ComponentA()); assertEquals(1, addedListener.counter); assertEquals(0, removedListener.counter); entity.remove(ComponentA.class); assertEquals(1, addedListener.counter); assertEquals(1, removedListener.counter); entity.add(new ComponentB()); assertEquals(2, addedListener.counter); entity.remove(ComponentB.class); assertEquals(2, removedListener.counter); }
@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)); }
@Test public void testRemoveDependent() { rootEntityDef( entityDef("details", attributeDef("accessibility")), entityDef("tree", attributeDef("dbh").relevant("parent()/details/accessibility"))); createTestRecord(); Entity details = entity(rootEntity, "details"); Attribute<?, ?> accessibility = attribute(details, "accessibility"); Entity tree = entity(rootEntity, "tree"); Attribute<?, ?> dbh = attribute(tree, "dbh"); NodePointer dbhPointer = toPointer(dbh); assertDependentNodePointers(accessibility, dbhPointer); tree.remove("dbh", 0); assertDependentNodePointers(accessibility, dbhPointer); rootEntity.remove("tree", 0); assertDependentNodePointers(accessibility); }
protected void collide(Entity entity) { if (entity instanceof Bullet) { Bullet bullet = (Bullet) entity; if (bullet.owner.getClass() == this.getClass()) { return; } if (hurtTime > 0) return; entity.remove(); hurt(entity.xa, entity.za); } if (entity instanceof Player) { ((Player) entity).hurt(this, 1); } }