예제 #1
0
  @Test
  public void recycleComponent() {
    int maxEntities = 10;
    int maxComponents = 10;
    PooledEngine engine = new PooledEngine(maxEntities, maxEntities, maxComponents, maxComponents);

    for (int i = 0; i < maxComponents; ++i) {
      Entity e = engine.createEntity();
      PooledComponentSpy c = engine.createComponent(PooledComponentSpy.class);

      assertEquals(false, c.recycled);

      e.add(c);

      engine.addEntity(e);
    }

    engine.removeAllEntities();

    for (int i = 0; i < maxComponents; ++i) {
      Entity e = engine.createEntity();
      PooledComponentSpy c = engine.createComponent(PooledComponentSpy.class);

      assertEquals(true, c.recycled);

      e.add(c);
    }

    engine.removeAllEntities();
  }
예제 #2
0
  @Test
  public void resetEntityCorrectly() {
    PooledEngine engine = new PooledEngine();

    ComponentCounterListener addedListener = new ComponentCounterListener();
    ComponentCounterListener removedListener = new ComponentCounterListener();

    // force the engine to create a Family so family bits get set
    ImmutableArray<Entity> familyEntities =
        engine.getEntitiesFor(Family.all(PositionComponent.class).get());

    Entity[] entities = new Entity[10];
    final int totalEntities = 10;

    for (int i = 0; i < totalEntities; i++) {
      entities[i] = engine.createEntity();

      entities[i].flags = 5;

      entities[i].componentAdded.add(addedListener);
      entities[i].componentRemoved.add(removedListener);

      entities[i].add(engine.createComponent(PositionComponent.class));
      engine.addEntity(entities[i]);

      assertNotNull(entities[i].componentOperationHandler);
      assertEquals(1, entities[i].getComponents().size());
      assertFalse(entities[i].getFamilyBits().isEmpty());
      assertTrue(familyEntities.contains(entities[i], true));
    }

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(0, removedListener.totalCalls);

    engine.removeAllEntities();

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(totalEntities, removedListener.totalCalls);

    for (int i = 0; i < totalEntities; i++) {
      assertEquals(0, entities[i].flags);
      assertNull(entities[i].componentOperationHandler);
      assertEquals(0, entities[i].getComponents().size());
      assertTrue(entities[i].getFamilyBits().isEmpty());
      assertFalse(familyEntities.contains(entities[i], true));

      entities[i].componentAdded.dispatch(entities[i]);
      entities[i].componentRemoved.dispatch(entities[i]);
    }

    assertEquals(totalEntities, addedListener.totalCalls);
    assertEquals(totalEntities, removedListener.totalCalls);
  }
예제 #3
0
 @Override
 public void update(float deltaTime) {
   Entity entity;
   PooledEngine engine = (PooledEngine) getEngine();
   for (int i = 0; i < 10; i++) {
     entity = engine.createEntity();
     assertEquals(0, entity.flags);
     entity.flags = 1;
     entity.add(engine.createComponent(PositionComponent.class));
     engine.addEntity(entity);
   }
   for (int i = 0; i < entities.size(); ++i) {
     entity = entities.get(i);
     engine.removeEntity(entity);
     engine.removeEntity(entity);
   }
 }
예제 #4
0
  @Test
  public void entityRemovalListenerOrder() {
    PooledEngine engine = new PooledEngine();

    CombinedSystem combinedSystem = new CombinedSystem();

    engine.addSystem(combinedSystem);
    engine.addEntityListener(Family.all(PositionComponent.class).get(), new MyPositionListener());

    for (int i = 0; i < 10; i++) {
      Entity entity = engine.createEntity();
      entity.add(engine.createComponent(PositionComponent.class));
      engine.addEntity(entity);
    }

    assertEquals(10, combinedSystem.allEntities.size());

    for (int i = 0; i < 10; i++) {
      engine.update(deltaTime);
    }

    engine.removeAllEntities();
  }