@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();
  }
Exemple #2
0
  @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);
  }
  protected Entity addItem(Entity parentEntity, Integer qtyValue, Double priceValue) {
    EntityDefinition rootEntityDefn = parentEntity.getDefinition();
    EntityDefinition itemDefn = (EntityDefinition) rootEntityDefn.getChildDefinition("item");
    Entity item = (Entity) itemDefn.createNode();

    if (qtyValue != null) {
      NodeDefinition qtyDefn = itemDefn.getChildDefinition("qty");
      IntegerAttribute qty = (IntegerAttribute) qtyDefn.createNode();
      qty.setValue(new IntegerValue(qtyValue, null));
      item.add(qty);
    }
    if (priceValue != null) {
      NumericAttributeDefinition priceDefn =
          (NumericAttributeDefinition) itemDefn.getChildDefinition("price");
      RealAttribute price = (RealAttribute) priceDefn.createNode();
      price.setValue(new RealValue(priceValue, null));
      item.add(price);
    }
    NumberAttributeDefinition totalDefn =
        (NumberAttributeDefinition) itemDefn.getChildDefinition("total");
    RealAttribute total = (RealAttribute) totalDefn.createNode();
    item.add(total);

    NumberAttributeDefinition discountDefn =
        (NumberAttributeDefinition) itemDefn.getChildDefinition("discount_percent");
    IntegerAttribute discount = (IntegerAttribute) discountDefn.createNode();
    item.add(discount);

    parentEntity.add(item);
    return item;
  }
Exemple #4
0
  @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));
  }
Exemple #5
0
  @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));
  }
 @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);
   }
 }
Exemple #7
0
  @Test
  public void getComponentByClass() {
    ComponentA compA = new ComponentA();
    ComponentB compB = new ComponentB();

    Entity entity = new Entity();
    entity.add(compA).add(compB);

    ComponentA retA = entity.getComponent(ComponentA.class);
    ComponentB retB = entity.getComponent(ComponentB.class);

    assertNotNull(retA);
    assertNotNull(retB);

    assertTrue(retA == compA);
    assertTrue(retB == compB);
  }
  @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();
  }