Search code examples
entityentity-systemartemis

Artemis Entity System Framework


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?


Solution

  • 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();