I am new to Artemis Entity Systems framework, and I want to know whether there is a way to get all the entities that have a specific component or components in them? (There should be, but I cannot find.)

For example I want to find all entities that have a EnemyComponent and check if they collide with any of the entities that have BulletComponent in them. How can I do this?

有帮助吗?

解决方案

What you can do is to create a system which will be called in your collision system to get the list of all entities with chosen components.

For example:

public class FindBulletsSystem extends EntitySystem {
  private ImmutableBag<Entity> bullets;
  private boolean processingFlag = false;

  public FindBulletsSystem () {
    super(Aspect.getAspectForAll(BulletComponent.class));

  }

  @Override
  protected boolean checkProcessing() {
    if (processingFlag) {
      processingFlag = false;
      return true;
    }
    return false;
  }

  @Override
  protected void processEntities(ImmutableBag<Entity> entities) {
         bullets = entities;

  }

  public ImmutableBag<Entity> getAllBullets() {
    bullets = null;
    processingFlag = true;

    this.process();
    return bullets;
  }

}

In your collision system you can get bullets by calling this system:

world.getSystem(FindBulletsSystem.class).getAllBullets();

其他提示

Perhaps I was too strict in forbidding you from retrieving components by their type from the component manager, but I'm sure it was for a good reason at the time, enforcing a strict api.

The problem with "BulletComponent" and "EnemyComponent" is that they are flags, to indicate the type of group they belong to. Do they contain any data? What if you had a FlagSystem that processes FlagComponent, which has an array of flags. You could request from this flagSystem entities having certain flags.

But there's no one way to do this. You could create your own Manager class, or you could do this with systems.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top