@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 should_be_possible_to_re_add_texturecomponent_via_listener() throws Exception {
    final Entity enemy = createEnemy(Type.GREEN);
    createEnemyChangeEvent();

    world
        .getAspectSubscriptionManager()
        .get(Aspect.all(TextureRef.class).exclude(TextureComponent.class))
        .addSubscriptionListener(
            new EntitySubscription.SubscriptionListener() {
              @Override
              public void inserted(IntBag entities) {
                final int[] data = entities.getData();
                for (int i = 0; i < entities.size(); i++) {
                  world.getEntity(data[i]).edit().create(TextureComponent.class);
                }
              }

              @Override
              public void removed(IntBag entities) {}
            });

    world.process();
    assertThat(enemy.getComponent(TextureComponent.class)).isNotNull();
  }
 @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()));
   }
 }
예제 #4
0
  protected Entity(World world, int id) {
    this.world = world;
    this.id = id;
    this.entityManager = world.getEntityManager();
    this.componentManager = world.getComponentManager();
    systemBits = new Bits();
    componentBits = new Bits();

    reset();
  }
예제 #5
0
  public <T extends Component> T create(Class<T> componentKlazz) {
    ComponentManager componentManager = world.getComponentManager();
    T component = componentManager.create(entity, componentKlazz);

    ComponentTypeFactory tf = world.getComponentManager().typeFactory;
    ComponentType componentType = tf.getTypeFor(componentKlazz);
    componentBits.set(componentType.getIndex());

    return component;
  }
 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();
 }
예제 #7
0
 @Override
 public void render(float delta) {
   Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
   Gdx.gl.glClearColor(0, 0, 0, 1);
   camera.update();
   world.setDelta(delta);
   world.process();
   menuRenderSystem.process();
   Gdx.graphics.setTitle("Shape Breaker | Menu");
 }
  @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();
  }
예제 #9
0
  @Override
  public void render(float delta) {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.draw();
    stage.act();

    world.setDelta(delta);
    world.process();
  }
예제 #10
0
  @Before
  public void init() {
    world =
        new World(
            new WorldConfiguration()
                .setSystem(new EntityFactory())
                .setSystem(new TiledMapSystem()));

    world.inject(this);

    world.process();
  }
  @Test
  public void test_adding_to_systems() {
    archetypeEntity(arch1, 2); // never inserted
    archetypeEntity(arch2, 4); // es1
    archetypeEntity(arch3, 8); // es1 + 2

    world.process();

    assertEquals(12, es1.getSubscription().getEntities().size());
    assertEquals(8, es2.getSubscription().getEntities().size());

    world.process();
  }
예제 #12
0
  @Override
  public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    camera.update();

    world.setDelta(delta);
    world.process();

    mapRenderSystem.process();
    spriteRenderSystem.process();
    hudRenderSystem.process();
  }
 @Test
 public void should_change_type_of_enemy_when_an_enemychange_event_is_created() throws Exception {
   final Entity enemy = createEnemy(Type.GREEN);
   createEnemyChangeEvent();
   world.process();
   assertThat(enemy.getComponent(Enemy.class).type).isEqualTo(Type.BLUE);
 }
예제 #14
0
 /**
  * Faster removal of components from a entity.
  *
  * @param type the type of component to remove from this entity
  * @return this EntityEdit for chaining
  */
 public EntityEdit remove(ComponentType type) {
   if (componentBits.get(type.getIndex())) {
     world.getComponentManager().removeComponent(entity, type);
     componentBits.clear(type.getIndex());
   }
   return this;
 }
  @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 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));
  }
예제 #17
0
  public ArchetypeMapper(World world, IntBag toSave) {
    int[] ids = toSave.getData();
    Bag<Component> components = new Bag<Component>();
    Bag<Class<? extends Component>> types = new Bag<Class<? extends Component>>();

    for (int i = 0, s = toSave.size(); s > i; i++) {
      int compositionId = world.getEntity(ids[i]).getCompositionId();
      if (!compositionIdMapper.containsKey(compositionId)) {
        components.clear();
        types.clear();

        world.getComponentManager().getComponentsFor(ids[i], components);
        compositionIdMapper.put(compositionId, new TransmuterEntry(toClasses(components, types)));
      }
    }
  }
 @Test
 public void should_remove_update_texture_refs_when_type_is_changed() throws Exception {
   final Entity enemy = createEnemy(Type.GREEN);
   createEnemyChangeEvent();
   world.process();
   assertThat(enemy.getComponent(TextureRef.class).id).isEqualTo(Type.BLUE.image + ".png");
   assertThat(enemy.getComponent(NormalRef.class).id).isEqualTo(Type.BLUE.image + "_n.png");
 }
 @Test
 public void should_remove_textures_when_type_is_changed() throws Exception {
   final Entity enemy = createEnemy(Type.GREEN);
   createEnemyChangeEvent();
   world.process();
   assertThat(enemy.getComponent(TextureComponent.class)).isNull();
   assertThat(enemy.getComponent(NormalTextureComponent.class)).isNull();
 }
예제 #20
0
  public MenuScreen(Game game) {
    camera = new OrthographicCamera();
    this.game = game;
    camera.setToOrtho(false, 480, 640);

    world = new World();
    world.setManager(new GroupManager());

    menuRenderSystem = world.setSystem(new MenuRenderSystem(camera), true);
    world.setSystem(new MenuInputSystem(camera, game));

    world.initialize();

    if (MenuScreen.playerName.isEmpty()) {
      Gdx.input.getTextInput(
          new TextInputListener() {

            @Override
            public void input(String text) {
              if (text.length() > 10) {
                playerName = text.substring(0, 10);
              } else if (text.length() == 10) {
                playerName = text;
              } else {
                playerName = text + "          ".substring(text.length());
              }
            }

            @Override
            public void canceled() {}
          },
          "Enter your name:",
          "");

      try {
        BufferedReader in =
            new BufferedReader(new FileReader(Gdx.files.local("scores.txt").file()));
        while (in.ready()) {
          String[] line = in.readLine().split(",");
          Highscore.scoreTable.add(line);
        }
        in.close();
      } catch (IOException ioe) {
      }
    }
  }
예제 #21
0
    public Aspect build(World world) {
      ComponentTypeFactory tf = world.getComponentManager().typeFactory;
      Aspect aspect = new Aspect();
      associate(tf, allTypes, aspect.allSet);
      associate(tf, exclusionTypes, aspect.exclusionSet);
      associate(tf, oneTypes, aspect.oneSet);

      return aspect;
    }
예제 #22
0
  @SuppressWarnings("unchecked")
  private Entity createEntity(Class<?>... components) {
    Entity e = world.createEntity();
    for (Class<?> c : components) {
      e.edit().create((Class<Component>) c);
    }

    return e;
  }
예제 #23
0
  @Test
  public void packed_component_mappers_return_new_instance_on_request() {
    world.initialize();

    Entity e1 = createEntity(Packed.class);
    Entity e2 = createEntity(Packed.class);
    Packed packed1 = packedMapper.get(e1, true);
    Packed packed2 = packedMapper.get(e2, true);

    assertNotEquals(packed1.entityId, packed2.entityId);
  }
  @Before
  public void init() {
    world =
        new World(
            new WorldConfiguration()
                .setSystem(new EntityFactory())
                .setSystem(new Es1())
                .setSystem(new Es2()));

    world.inject(this);
  }
예제 #25
0
  /**
   * Faster adding of components into the entity.
   *
   * <p>Not necessary to use this, but in some cases you might need the extra performance.
   *
   * @param component the component to add
   * @param type the type of the component
   * @return this EntityEdit for chaining
   * @see #createComponent(Class)
   */
  public EntityEdit add(Component component, ComponentType type) {
    if (type.getTaxonomy() != Taxonomy.BASIC) {
      throw new InvalidComponentException(
          component.getClass(), "Use Entity#createComponent for adding non-basic component types");
    }
    world.getComponentManager().addComponent(entity, type, component);

    componentBits.set(type.getIndex());

    return this;
  }
예제 #26
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);
 }
예제 #27
0
  public GameXYZ(Game game) {
    this.game = game;

    batch = new SpriteBatch();
    camera = new OrthographicCamera();

    world = new World();
    spriteRenderSystem = world.setSystem(new SpriteRenderSystem(camera), true);
    mapRenderSystem = world.setSystem(new MapRenderSystem(camera), true);
    hudRenderSystem = world.setSystem(new HudRenderSystem(camera), true);

    world.setSystem(new SpriteAnimationSystem());
    world.setSystem(new PlayerInputSystem(camera));
    world.initialize();

    EntityFactory.createMap(world).addToWorld();
    EntityFactory.createPlayer(world, 11, 14).addToWorld();
    for (int i = 0; i < 100; i++)
      EntityFactory.createWarrior(
              world,
              MathUtils.random(MapTools.width() - 1),
              MathUtils.random(MapTools.height() - 1))
          .addToWorld();
  }
예제 #28
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));
  }
예제 #29
0
  @Test
  public void packed_components_are_known_to_mapper() {
    world.initialize();

    List<Entity> packed = new ArrayList<Entity>();
    packed.add(createEntity(Packed.class));
    packed.add(createEntity(Packed.class));
    packed.add(createEntity(Packed.class));
    Entity notPresent = createEntity();
    packed.add(createEntity(Packed.class));
    packed.add(createEntity(Packed.class));

    for (Entity e : packed) {
      assertTrue(packedMapper.has(e));
    }
    assertFalse(packedMapper.has(notPresent));

    packed.get(1).edit().remove(Packed.class);
    for (int i = 0; packed.size() > i; i++) {
      if (i != 1) assertTrue(packedMapper.has(packed.get(i)));
    }
    assertFalse(packedMapper.has(packed.get(1)));
  }
  @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());
  }