@Test
  public void old_entities_are_recycled() {
    Set<Integer> ids = new HashSet<Integer>();

    Entity e1 = world.createEntity();
    Entity e2 = world.createEntity();
    Entity e3 = world.createEntity();

    ids.add(System.identityHashCode(e1));
    ids.add(System.identityHashCode(e2));
    ids.add(System.identityHashCode(e3));

    assertEquals(3, ids.size());

    e1.deleteFromWorld();
    e2.deleteFromWorld();
    e3.deleteFromWorld();

    world.process();

    Entity e1b = world.createEntity();
    Entity e2b = world.createEntity();
    Entity e3b = world.createEntity();

    ids.add(System.identityHashCode(e1b));
    ids.add(System.identityHashCode(e2b));
    ids.add(System.identityHashCode(e3b));

    assertEquals(3, ids.size());
  }
  @Test
  public void recycled_entities_behave_nicely_with_components() {
    ComponentMapper<ComponentX> mapper = world.getMapper(ComponentX.class);

    Entity e1 = world.createEntity();
    e1.edit().add(new ComponentX());
    assertTrue(mapper.has(e1));

    int id1 = e1.getId();
    e1.deleteFromWorld();

    Entity e2 = world.createEntity();

    assertNotEquals(id1, e2.getId());
    assertFalse("Error:" + mapper.getSafe(e2), mapper.has(e2));
  }
  @Test
  public void should_recycle_entities_after_one_round() {
    ComponentMapper<ComponentX> mapper = world.getMapper(ComponentX.class);

    Entity e1 = world.createEntity();
    e1.edit().add(new ComponentX());
    assertTrue(mapper.has(e1));

    int id1 = e1.getId();
    e1.deleteFromWorld();
    world.process();
    Entity e2 = world.createEntity();

    assertEquals(id1, e2.getId());
    assertFalse("Error:" + mapper.getSafe(e2), mapper.has(e2));
  }
 @Test
 public void is_active_check_never_throws() {
   EntityManager em = world.getEntityManager();
   for (int i = 0; 1024 > i; i++) {
     Entity e = world.createEntity();
     assertFalse(em.isActive(e.getId()));
   }
 }
  @SuppressWarnings("unchecked")
  private Entity createEntity(Class<?>... components) {
    Entity e = world.createEntity();
    for (Class<?> c : components) {
      e.edit().create((Class<Component>) c);
    }

    return e;
  }
 protected void createAndProcessWorld(BaseSystem system) {
   final World world =
       new World(
           new WorldConfigurationBuilder()
               .with(system)
               .with(new ExtendedComponentMapperPlugin())
               .build());
   world.createEntity().edit().create(TestMarker.class);
   world.process();
 }
  @Before
  public void setUp() throws Exception {
    world =
        new World(
            new WorldConfiguration()
                .setSystem(EnemyChangeSystem.class)
                .setSystem(EntityFactorySystem.class));

    world.createEntity().edit().create(InteractionType.class);
    world.process();
  }
Esempio n. 8
0
 public static void createTutorialCollision(
     World world, CollisionAction action, Entity parent, Vector2 center) {
   Entity tCollider = world.createEntity();
   HasParent hp = new HasParent(parent);
   PhysicsBody pb = new PhysicsBody(tutorialBounds.x, tutorialBounds.y);
   RenderBody rd = new RenderBody(tutorialBounds);
   Position pos =
       new Position(center.x - (tutorialBounds.x / 2f), center.y - (tutorialBounds.y / 2f));
   Deletable d = new Deletable(false);
   CollidableComponent cc = new CollidableComponent(CollisionType.TUT, action);
   DrawLineAroundBody dlab = new DrawLineAroundBody();
   tCollider.edit().add(pos).add(pos).add(rd).add(cc).add(dlab).add(d).add(pb).add(d).add(hp);
 }
  @Test
  public void active_entities_never_negative() {
    EntityManager em = world.getEntityManager();
    assertEquals(0, em.getActiveEntityCount());

    Entity e = world.createEntity();
    assertEquals(0, em.getActiveEntityCount());

    e.deleteFromWorld();
    assertEquals(0, em.getActiveEntityCount());

    world.process();
    assertEquals(0, em.getActiveEntityCount());

    e = world.createEntity();
    world.process();
    assertEquals(1, em.getActiveEntityCount());
    e.deleteFromWorld();
    assertEquals(1, em.getActiveEntityCount());

    world.process();
    assertEquals(0, em.getActiveEntityCount());
  }
Esempio n. 10
0
  @Test
  public void entityNotRemovedFromGroupWhenDeleted() {
    String group = "EntityGroup";

    World world = new World();
    Entity e = world.createEntity();

    e.setGroup(group);
    e.refresh();

    world.setDelta(10);
    world.loopStart();

    ImmutableBag<Entity> entities = world.getGroupManager().getEntities(group);
    assertThat(entities.size(), IsEqual.equalTo(1));

    world.deleteEntity(e);

    world.setDelta(10);
    world.loopStart();

    entities = world.getGroupManager().getEntities(group);
    assertThat(entities.size(), IsEqual.equalTo(0));
  }
Esempio n. 11
0
  GameScreen(TestGame game) {
    stage = new Stage();
    this.game = game;

    camera = new OrthographicCamera();
    stage.setCamera(camera);

    world = new World();

    world.setManager(new GroupManager());
    world.setManager(new TagManager());

    Random rand = new Random();

    // Game world.
    Entity root = world.createEntity();
    root.addComponent(new GameWorld());
    root.addToWorld();

    // Four-Direction controller /w keyboard bindings.
    ActionTrigger up = new ActionTrigger("up");
    ActionTrigger down = new ActionTrigger("down");
    ActionTrigger left = new ActionTrigger("left");
    ActionTrigger right = new ActionTrigger("right");
    FourDirectionController fourDirController =
        new FourDirectionController(1500, up, down, left, right);
    KeyboardBindingMap kbMap = new KeyboardBindingMap();
    kbMap.addBinding(Keys.W, up);
    kbMap.addBinding(Keys.S, down);
    kbMap.addBinding(Keys.A, left);
    kbMap.addBinding(Keys.D, right);

    // Create a bunch of villagers.
    for (int i = 0; i < 12; i++) {
      Vector2 pos =
          new Vector2(rand.nextFloat() * stage.getWidth(), rand.nextFloat() * stage.getHeight());
      Entity villager = EntityFactory.createVillager(world, pos);
    }

    // Create a bunch of FREAKING GOBLINS.
    for (int i = 0; i < 4; i++) {
      Vector2 pos =
          new Vector2(rand.nextFloat() * stage.getWidth(), rand.nextFloat() * stage.getHeight());
      Entity goblin = EntityFactory.createGoblin(world, pos);
    }

    Entity player = EntityFactory.createHero(world, new Vector2(250, 250));
    player.addComponent(fourDirController);
    player.addComponent(kbMap);
    world.getManager(TagManager.class).register("player", player);

    world.setSystem(new GoblinRespawnerSystem());

    world.setSystem(new PhysicsMotionSystem());
    world.setSystem(new PhysicsScreenBounceSystem());

    world.setSystem(new WanderBehaviourSystem());
    world.setSystem(new HuntBehaviourSystem());

    world.setSystem(new RectangleRenderer(new ShapeRenderer()));

    world.setSystem(new KeyboardInputSystem());
    world.setSystem(new FourDirectionControllerSystem());

    world.initialize();
  }
 public static Entity create(World world) {
   Entity e = world.createEntity();
   return e;
 }
 private void archetypeEntity(Archetype arch, int s) {
   for (int i = 0; s > i; i++) {
     world.createEntity(arch);
   }
 }