Пример #1
0
  public void resetRessources() {
    ComponentMapper<RessourceComponent> rm = ComponentMapper.getFor(RessourceComponent.class);
    RessourceComponent rc = rm.get(this);

    rc.fuel = 100;
    rc.health = 100;
  }
  @Override
  public void set(byte index, Inventory inventory) {
    SlotElement slot = m_slots[index];

    Entity item = inventory.item(index);
    ItemComponent itemComponent = itemMapper.get(item);
    m_slots[index].itemCountLabel.setText(Integer.toString(itemComponent.stackSize));

    TextureRegion region;
    SpriteComponent spriteComponent = spriteMapper.get(item);
    if (blockMapper.get(item) != null) {
      // hack
      region =
          m_client.m_world.m_tileRenderer.m_tilesAtlas.findRegion(
              spriteComponent.textureName.concat("-00"));
    } else {
      region = m_client.m_world.m_atlas.findRegion(spriteComponent.textureName);
    }

    Image slotImage = slot.itemImage;
    //        //m_blockAtlas.findRegion("stone"));
    slotImage.setDrawable(new TextureRegionDrawable(region));
    slotImage.setSize(region.getRegionWidth(), region.getRegionHeight());
    slotImage.setScaling(Scaling.fit);

    setHotbarSlotVisible(index, true);

    // do not exceed the max size/resort to horrible upscaling. prefer native size of each inventory
    // sprite.
    // .maxSize(region.getRegionWidth(), region.getRegionHeight()).expand().center();

  }
Пример #3
0
public class SpineSystem extends IteratingSystem {

  private ComponentMapper<SpineObjectComponent> spineObjectComponentMapper =
      ComponentMapper.getFor(SpineObjectComponent.class);
  private ComponentMapper<TransformComponent> transformComponentMapper =
      ComponentMapper.getFor(TransformComponent.class);

  public SpineSystem() {
    super(Family.all(SpineDataComponent.class).get());
  }

  @Override
  protected void processEntity(Entity entity, float deltaTime) {

    TransformComponent transformComponent = transformComponentMapper.get(entity);
    SpineObjectComponent spineObjectComponent = spineObjectComponentMapper.get(entity);

    spineObjectComponent.skeleton.updateWorldTransform(); //
    spineObjectComponent.state.update(deltaTime); // Update the animation time.
    spineObjectComponent.state.apply(
        spineObjectComponent
            .skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
    spineObjectComponent.skeleton.setPosition(
        transformComponent.x - spineObjectComponent.minX,
        transformComponent.y - spineObjectComponent.minY);
  }
}
  public void update(float delta) {
    Entity playerEntity = player.get(0);

    PlayerSteerableComponent psc = cmpsc.get(playerEntity);

    for (int i = 0; i < entities.size(); i++) {
      Entity entity = entities.get(i);

      TalkComponent tc = cmtc.get(entity);
      NonControllablePersonSteerableComponent ncpstc = cmstc.get(entity);
      if (ncpstc.x - 160 < psc.getPosition().x
          && psc.getPosition().x < ncpstc.x + 160
          && ncpstc.y - 210 < psc.getPosition().y
          && psc.getPosition().y < ncpstc.y + 210) {
        layout.setText(font, tc.textToSay);
        float x = ncpstc.x - (layout.width / 2);
        float y = ncpstc.y + layout.height + 15;
        batch.end();
        shapeRenderer.setProjectionMatrix(TwoD.getMain().camera.combined);
        shapeRenderer.begin(ShapeType.Filled);
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.rect(x - 13, y + 2, 26 + layout.width, 26 + layout.height);
        shapeRenderer.setColor(Color.BLACK);
        shapeRenderer.rect(x - 10, y + 5, 20 + layout.width, 20 + layout.height);
        shapeRenderer.end();
        batch.begin();
        font.draw(batch, layout, x, y + 25);
      }
    }
  }
Пример #5
0
  public AnimationSystem() {
    super(Family.all(TextureComponent.class, AnimationComponent.class, StateComponent.class).get());

    tm = ComponentMapper.getFor(TextureComponent.class);
    am = ComponentMapper.getFor(AnimationComponent.class);
    sm = ComponentMapper.getFor(StateComponent.class);
  }
Пример #6
0
  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    TransformComponent tfm = tm.get(entity);
    BoundsComponent bounds = bm.get(entity);

    bounds.bounds.x = tfm.position.x - bounds.bounds.width * 0.5f;
    bounds.bounds.y = tfm.position.y - bounds.bounds.height * 0.5f;
  }
public class NonControllablePersonTalkSystem extends EntitySystem {
  SpriteBatch batch;
  BitmapFont font;
  GlyphLayout layout;
  ShapeRenderer shapeRenderer;

  private ImmutableArray<Entity> entities, player;

  private ComponentMapper<TalkComponent> cmtc = ComponentMapper.getFor(TalkComponent.class);
  private ComponentMapper<NonControllablePersonSteerableComponent> cmstc =
      ComponentMapper.getFor(NonControllablePersonSteerableComponent.class);

  private ComponentMapper<PlayerSteerableComponent> cmpsc =
      ComponentMapper.getFor(PlayerSteerableComponent.class);

  public NonControllablePersonTalkSystem(SpriteBatch batch) {
    this.batch = batch;
    font = new BitmapFont();
    layout = new GlyphLayout();
    shapeRenderer = new ShapeRenderer();
  }

  public void addedToEngine(Engine engine) {
    player = engine.getEntitiesFor(Family.all(PlayerSteerableComponent.class).get());
    entities = engine.getEntitiesFor(Family.all(TalkComponent.class).get());
  }

  public void update(float delta) {
    Entity playerEntity = player.get(0);

    PlayerSteerableComponent psc = cmpsc.get(playerEntity);

    for (int i = 0; i < entities.size(); i++) {
      Entity entity = entities.get(i);

      TalkComponent tc = cmtc.get(entity);
      NonControllablePersonSteerableComponent ncpstc = cmstc.get(entity);
      if (ncpstc.x - 160 < psc.getPosition().x
          && psc.getPosition().x < ncpstc.x + 160
          && ncpstc.y - 210 < psc.getPosition().y
          && psc.getPosition().y < ncpstc.y + 210) {
        layout.setText(font, tc.textToSay);
        float x = ncpstc.x - (layout.width / 2);
        float y = ncpstc.y + layout.height + 15;
        batch.end();
        shapeRenderer.setProjectionMatrix(TwoD.getMain().camera.combined);
        shapeRenderer.begin(ShapeType.Filled);
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.rect(x - 13, y + 2, 26 + layout.width, 26 + layout.height);
        shapeRenderer.setColor(Color.BLACK);
        shapeRenderer.rect(x - 10, y + 5, 20 + layout.width, 20 + layout.height);
        shapeRenderer.end();
        batch.begin();
        font.draw(batch, layout, x, y + 25);
      }
    }
  }
}
Пример #8
0
/** Created by Harry on 2016-02-29. */
public class Mappers {
  public static final ComponentMapper<HealthComponent> health =
      ComponentMapper.getFor(HealthComponent.class);
  public static final ComponentMapper<PositionComponent> position =
      ComponentMapper.getFor(PositionComponent.class);
  public static final ComponentMapper<VelocityComponent> velocity =
      ComponentMapper.getFor(VelocityComponent.class);
  public static final ComponentMapper<SpriteComponent> sprite =
      ComponentMapper.getFor(SpriteComponent.class);
}
Пример #9
0
  @SuppressWarnings("unchecked")
  public HudSystem() {
    super(Family.all(PlayerComponent.class, ShapeComponent.class).get());

    this.batch = new SpriteBatch();
    this.font = new BitmapFont();
    this.font.setColor(Color.RED);

    this.playerM = ComponentMapper.getFor(PlayerComponent.class);
    this.shapeM = ComponentMapper.getFor(ShapeComponent.class);
  }
Пример #10
0
/** Created by Vihan on 1/22/2016. */
public class PhysicsSystem extends IteratingSystem {

  private static final float MAX_STEP_TIME = 1 / 45f;
  private static float accumulator = 0f;

  private World world;
  private Array<Entity> bodiesQueue;

  // TODO: Remove all instances of debugRenderer on release
  private Box2DDebugRenderer debugRenderer;
  private OrthographicCamera camera;

  private ComponentMapper<BodyComponent> bm = ComponentMapper.getFor(BodyComponent.class);
  private ComponentMapper<TransformComponent> tm = ComponentMapper.getFor(TransformComponent.class);

  public PhysicsSystem(World world, OrthographicCamera camera) {
    super(Family.all(BodyComponent.class, TransformComponent.class).get());

    debugRenderer = new Box2DDebugRenderer();
    this.world = world;
    this.camera = camera;
    this.bodiesQueue = new Array<Entity>();
  }

  @Override
  public void update(float deltaTime) {
    super.update(deltaTime);
    debugRenderer.render(world, camera.combined);
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    if (accumulator >= MAX_STEP_TIME) {
      world.step(MAX_STEP_TIME, 6, 2);
      accumulator -= MAX_STEP_TIME;

      // Entity Queue
      for (Entity entity : bodiesQueue) {
        TransformComponent t = tm.get(entity);
        BodyComponent body = bm.get(entity);
        Vector2 pos = body.body.getPosition();
        pos.x = t.pos.x;
        pos.y = t.pos.y;
        body.body.setTransform(pos, t.rotation * MathUtils.degreesToRadians);
      }
    }
    bodiesQueue.clear();
  }

  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    bodiesQueue.add(entity);
  }
}
Пример #11
0
  public ControllerSystem() {
    super(
        Family.all(
                TransformComponent.class,
                BodyComponent.class,
                PlayerComponent.class,
                ControllerComponent.class)
            .get());

    tm = ComponentMapper.getFor(TransformComponent.class);
    bm = ComponentMapper.getFor(BodyComponent.class);
    cm = ComponentMapper.getFor(ControllerComponent.class);
  }
Пример #12
0
  @Override
  public void processEntity(Entity entity, float deltaTime) {
    TextureComponent tex = tm.get(entity);
    AnimationComponent anim = am.get(entity);
    StateComponent state = sm.get(entity);

    Animation animation = anim.animations.get(state.get());

    if (animation != null) {
      tex.region = animation.getKeyFrame(state.time);
    }

    state.time += deltaTime;
  }
Пример #13
0
  @Override
  protected void processEntity(Entity entity, float deltaTime) {

    TransformComponent transformComponent = transformComponentMapper.get(entity);
    SpineObjectComponent spineObjectComponent = spineObjectComponentMapper.get(entity);

    spineObjectComponent.skeleton.updateWorldTransform(); //
    spineObjectComponent.state.update(deltaTime); // Update the animation time.
    spineObjectComponent.state.apply(
        spineObjectComponent
            .skeleton); // Poses skeleton using current animations. This sets the bones' local SRT.
    spineObjectComponent.skeleton.setPosition(
        transformComponent.x - spineObjectComponent.minX,
        transformComponent.y - spineObjectComponent.minY);
  }
  public void endContact(float delta, Contact contact) {
    for (Entity e : entities) {

      if (contact.getFixtureA() == bm.get(e).body.getFixtureList().first()) {
        for (Entity player : players) {
          if (bm.has(player) && contact.getFixtureB() == sensorm.get(player).sensor) {

            e.remove(CollisionComponent.class);
            player.remove(CollisionComponent.class);
            System.out.println("No longer a touch");
            return;
          }
        }
      }
    }
  }
  public void addContact(float delta, Contact contact) {
    for (Entity e : entities) {

      if (contact.getFixtureA() == bm.get(e).body.getFixtureList().first()) {
        for (Entity player : players) {
          if (contact.getFixtureB() == sensorm.get(player).sensor) {
            e.add(new CollisionComponent(player.getId(), contact.getWorldManifold()));
            player.add(new CollisionComponent(e.getId(), contact.getWorldManifold()));
            System.out.println("hit a player");

            return;
          }
        }
      }
    }
  }
  /**
   * Apply a force to an entity with physics component.
   *
   * @param force The world force vector, usually in Newtons (N)
   * @param relativePoint The point where the force is applied relative to the body origin
   * @param linkedComponent The force is applied as long as the corresponding entity has this
   *     component
   * @return The com.uwsoft.editor.renderer.systems.action.data.ForceData object
   */
  public static ForceData force(
      Vector2 force, Vector2 relativePoint, Class<? extends Component> linkedComponent) {
    ForceData forceData = force(force, relativePoint);

    forceData.linkedComponentMapper = ComponentMapper.getFor(linkedComponent);
    return forceData;
  }
Пример #17
0
 @Override
 protected void processEntity(Entity entity, float deltaTime) {
   Health health = healthComponentMapper.get(entity);
   if (health.value > 0) {
     aliveSettlements++;
   }
 }
Пример #18
0
public class OutOfBoundsSystem extends IteratingSystem {

  private FitViewport viewport;
  private LightsManager lightsManager;
  private ComponentMapper<PositionComponent> positionMapper =
      ComponentMapper.getFor(PositionComponent.class);
  private ComponentMapper<OutOfBoundsComponent> outOfBoundsMapper =
      ComponentMapper.getFor(OutOfBoundsComponent.class);
  private ComponentMapper<BodyComponent> bodyMapper = ComponentMapper.getFor(BodyComponent.class);

  private PooledEngine engine;
  private BodyGenerator bodyGenerator;

  public OutOfBoundsSystem(
      FitViewport viewport,
      LightsManager lightsManager,
      PooledEngine engine,
      BodyGenerator bodyGenerator) {
    super(Family.all(RenderableComponent.class, OutOfBoundsComponent.class).get());
    this.viewport = viewport;
    this.lightsManager = lightsManager;
    this.engine = engine;
    this.bodyGenerator = bodyGenerator;
  }

  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    PositionComponent position = positionMapper.get(entity);
    OutOfBoundsComponent outOfBounds = outOfBoundsMapper.get(entity);
    if (position.x > viewport.getWorldWidth() / 2
        || position.x < -viewport.getWorldWidth() / 2
        || position.y > viewport.getWorldHeight() / 2
        || position.y < -viewport.getWorldHeight() / 2) {
      if (outOfBounds.action == AdequateAction.RESPAWN) {
        position.x = MathUtils.random(-viewport.getWorldWidth() / 2, viewport.getWorldWidth() / 2);
        position.y =
            MathUtils.random(-viewport.getWorldHeight() / 2, viewport.getWorldHeight() / 2);
      } else if (outOfBounds.action == AdequateAction.ALERT) {
        lightsManager.ambientLight = 0.1f;
      } else if (outOfBounds.action == AdequateAction.DISPOSE) {
        BodyComponent body = bodyMapper.get(entity);
        if (body != null) bodyGenerator.destroyBody(entity);
        engine.removeEntity(entity);
      }
    }
  }
}
Пример #19
0
  @Override
  public void processEntity(Entity entity, float deltatime) {
    TransformComponent tc = mTransforms.get(entity);
    MovementComponent mc = mMovements.get(entity);
    OrbitComponent oc = mOrbits.get(entity);

    float R = mWorld.mOrbitRadius.get(oc.id);
    float radians = (float) Math.toRadians(tc.rotation);

    tc.position.x = (float) (R * Math.cos(radians)) + mWorld.mOribitCentre.get(oc.id).x;
    tc.position.y = (float) (R * Math.sin(radians)) + mWorld.mOribitCentre.get(oc.id).y;

    tc.rotation += mc.angularVelocity * deltatime;
    mc.angularVelocity += mc.angularAcceleration;

    if (tc.rotation >= 360) tc.rotation = 0;
  }
Пример #20
0
  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    PlayerComponent player = playerM.get(entity);
    ShapeComponent shape = shapeM.get(entity);

    float life = 0;
    for (int i = 0; i < player.orbs.size; i++) {
      life += player.orbs.get(i).getComponent(OrbComponent.class).life;
    }

    batch.begin();
    font.draw(batch, "Position: " + shape.body.getPosition(), 10, 80);
    font.draw(batch, "Speed: " + shape.body.getLinearVelocity(), 10, 60);
    font.draw(batch, "Orbs: " + player.orbs.size, 10, 40);
    font.draw(batch, String.format("Life:  %.1f", life), 10, 20);
    batch.end();
  }
/** Created by barry on 12/8/15 @ 10:11 PM. */
public class PhysicsSystem extends IteratingSystem {

  private static final float MAX_STEP_TIME = 1 / 45f;
  private static float accumulator = 0f;

  private World world;
  private Array<Entity> bodiesQueue;

  private ComponentMapper<BodyComponent> bm = ComponentMapper.getFor(BodyComponent.class);
  private ComponentMapper<TransformComponent> tm = ComponentMapper.getFor(TransformComponent.class);

  public PhysicsSystem(World world) {
    super(Family.all(BodyComponent.class, TransformComponent.class).get());

    this.world = world;
    this.bodiesQueue = new Array<>();
  }

  @Override
  public void update(float deltaTime) {
    super.update(deltaTime);
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    if (accumulator >= MAX_STEP_TIME) {
      world.step(MAX_STEP_TIME, 6, 2);
      accumulator -= MAX_STEP_TIME;

      // Entity Queue
      for (Entity entity : bodiesQueue) {
        TransformComponent tfm = tm.get(entity);
        BodyComponent bodyComp = bm.get(entity);
        Vector2 position = bodyComp.body.getPosition();
        tfm.position.x = position.x;
        tfm.position.y = position.y;
        tfm.rotation = bodyComp.body.getAngle() * MathUtils.radiansToDegrees;
      }
    }

    bodiesQueue.clear();
  }

  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    bodiesQueue.add(entity);
  }
}
Пример #22
0
  @Override
  protected void processEntity(Entity entity, float deltaTime) {
    ExplosionComponent explosion = _explosionComponents.get(entity);
    explosion.timeLeft -= (float) Time.time;
    if (explosion.timeLeft < 0f) EntityManager.getInstance().destroyEntity(entity);

    if (explosion.destroyedEnemy) explosion.lethal = false;
  }
Пример #23
0
  @Override
  public void processEntity(Entity entity, float deltaTime) {
    Sprite thisSprite = spriteMap.get(entity).sprites.first();
    PositionComponent position = positionMap.get(entity);

    //        thisSprite.setX((float) ((attachedSprite.getX() + attachedSprite.getWidth() / 2) -
    // (thisSprite.getWidth() / 2) + Math.sin(-Math.toRadians(angle)) * distance));
    //        thisSprite.setY((float) ((attachedSprite.getY() + attachedSprite.getHeight() / 2) -
    // (thisSprite.getHeight() / 2) + Math.cos(Math.toRadians(angle)) * distance));

    position.x += GameInput.MouseForce.x * 0.7f;
    position.y += GameInput.MouseForce.y * 0.7f;

    if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
      SoundManager.playSoundAtPosition(
          "Sounds/laser.wav", 0.5f, new Vector3(position.x, position.y, 0));
    }
  }
Пример #24
0
  public void setPointOfInterest(PointOfInterestEntity e) {
    SeekComponent sc = ComponentMapper.getFor(SeekComponent.class).get(this);

    if (sc == null) {
      sc = new SeekComponent();
      this.add(sc);
    }

    sc.entityTarget = e;
    sc.target = getPositionComponentMapper().get(e).position;
  }
Пример #25
0
 @Override
 protected void processEntity(Entity entity, float deltaTime) {
   PositionComponent position = positionMapper.get(entity);
   OutOfBoundsComponent outOfBounds = outOfBoundsMapper.get(entity);
   if (position.x > viewport.getWorldWidth() / 2
       || position.x < -viewport.getWorldWidth() / 2
       || position.y > viewport.getWorldHeight() / 2
       || position.y < -viewport.getWorldHeight() / 2) {
     if (outOfBounds.action == AdequateAction.RESPAWN) {
       position.x = MathUtils.random(-viewport.getWorldWidth() / 2, viewport.getWorldWidth() / 2);
       position.y =
           MathUtils.random(-viewport.getWorldHeight() / 2, viewport.getWorldHeight() / 2);
     } else if (outOfBounds.action == AdequateAction.ALERT) {
       lightsManager.ambientLight = 0.1f;
     } else if (outOfBounds.action == AdequateAction.DISPOSE) {
       BodyComponent body = bodyMapper.get(entity);
       if (body != null) bodyGenerator.destroyBody(entity);
       engine.removeEntity(entity);
     }
   }
 }
Пример #26
0
/** Created by paradox on 12/2/15. */
public class OrbitSystem extends IteratingSystem {
  private ComponentMapper<TransformComponent> mTransforms =
      ComponentMapper.getFor(TransformComponent.class);
  private ComponentMapper<MovementComponent> mMovements =
      ComponentMapper.getFor(MovementComponent.class);
  private ComponentMapper<OrbitComponent> mOrbits = ComponentMapper.getFor(OrbitComponent.class);

  private World mWorld;

  public OrbitSystem() {
    super(
        Family.all(TransformComponent.class, MovementComponent.class, OrbitComponent.class).get());
  }

  public void setWorld(World world) {
    mWorld = world;
  }

  public void update(float deltatime) {
    super.update(deltatime);
  }

  @Override
  public void processEntity(Entity entity, float deltatime) {
    TransformComponent tc = mTransforms.get(entity);
    MovementComponent mc = mMovements.get(entity);
    OrbitComponent oc = mOrbits.get(entity);

    float R = mWorld.mOrbitRadius.get(oc.id);
    float radians = (float) Math.toRadians(tc.rotation);

    tc.position.x = (float) (R * Math.cos(radians)) + mWorld.mOribitCentre.get(oc.id).x;
    tc.position.y = (float) (R * Math.sin(radians)) + mWorld.mOribitCentre.get(oc.id).y;

    tc.rotation += mc.angularVelocity * deltatime;
    mc.angularVelocity += mc.angularAcceleration;

    if (tc.rotation >= 360) tc.rotation = 0;
  }
}
Пример #27
0
  // Sets target for pursuit or evade behaviors
  public boolean setTarget(Entity target, String action) {
    if (target == null) {
      return false;

    } else {

      if (action.contains("Evade")) {
        ComponentMapper<EvadeComponent> em = ComponentMapper.getFor(EvadeComponent.class);
        EvadeComponent ec = em.get(this);
        if (ec == null) {
          ec = new EvadeComponent();
        }
        ec.target = target;
        this.add(ec);
        return true;
      } else if (action.contains("Pursuit")) {
        ComponentMapper<PursuitComponent> pm = ComponentMapper.getFor(PursuitComponent.class);
        PursuitComponent pc = pm.get(this);

        if (pc == null) {
          pc = new PursuitComponent();
        }
        pc.target = target;
        this.add(pc);
        return true;
      }
      return false;
    }
  }
Пример #28
0
  @Override
  public void update(float deltaTime) {
    super.update(deltaTime);
    debugRenderer.render(world, camera.combined);
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    if (accumulator >= MAX_STEP_TIME) {
      world.step(MAX_STEP_TIME, 6, 2);
      accumulator -= MAX_STEP_TIME;

      // Entity Queue
      for (Entity entity : bodiesQueue) {
        TransformComponent t = tm.get(entity);
        BodyComponent body = bm.get(entity);
        Vector2 pos = body.body.getPosition();
        pos.x = t.pos.x;
        pos.y = t.pos.y;
        body.body.setTransform(pos, t.rotation * MathUtils.degreesToRadians);
      }
    }
    bodiesQueue.clear();
  }
  @Override
  public void update(float deltaTime) {
    super.update(deltaTime);
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    if (accumulator >= MAX_STEP_TIME) {
      world.step(MAX_STEP_TIME, 6, 2);
      accumulator -= MAX_STEP_TIME;

      // Entity Queue
      for (Entity entity : bodiesQueue) {
        TransformComponent tfm = tm.get(entity);
        BodyComponent bodyComp = bm.get(entity);
        Vector2 position = bodyComp.body.getPosition();
        tfm.position.x = position.x;
        tfm.position.y = position.y;
        tfm.rotation = bodyComp.body.getAngle() * MathUtils.radiansToDegrees;
      }
    }

    bodiesQueue.clear();
  }
Пример #30
0
  // Gibt Gegner Position zur�ueck, die am naechersten ist, falls keiner im
  // Sichtfeld = null
  public Entity searchTarget() {
    ComponentMapper<PositionComponent> pm = ComponentMapper.getFor(PositionComponent.class);
    Entity result = null;
    ImmutableArray<Entity> entities = engine.getEntities();
    float smallestDistance = sightRadius * sightRadius;
    float collisionDistance = 30f;

    for (Entity entity : entities) {

      // check if entity is a point of interest
      if (entity.getClass() == PointOfInterestEntity.class) {
        PointOfInterestEntity pie = (PointOfInterestEntity) entity;
        PositionComponent piePosition = pm.get(pie);

        // if the POI is a target and the distance < collision distance => collision
        if (team == Team.GREEN
            && pie.toString() == "target"
            && piePosition.position.dst2(pm.get(this).position)
                <= (collisionDistance * collisionDistance)) {
          // add a new green boid entity
          BoidEntity boidR = new BoidEntity(BoidEntity.Team.GREEN, engine, new EvadeState());
          boidR.add(new PositionComponent());
          pm.get(boidR).position = piePosition.position.cpy();
          boidR.add(new VelocityComponent());
          boidR.add(new RenderComponent());
          boidR.add(new BoidCenterComponent());
          boidR.add(new BoidDistanceComponent());
          boidR.add(new BoidMatchVelocityComponent());
          boidR.add(new RessourceComponent());
          boidR.setPointOfInterest(pie);
          engine.addEntity(boidR);
          // heal the boid that collected the cross
          this.resetRessources();
          // set new target position
          piePosition.position.set(
              MathUtils.random(0, Gdx.graphics.getWidth()),
              MathUtils.random(0, Gdx.graphics.getHeight()));
        }
      }
      if (entity.getComponent(BoidCenterComponent.class) == null) continue;

      BoidEntity currentBoid = (BoidEntity) entity;

      float newDistance = pm.get(this).position.dst2(pm.get(currentBoid).position);
      // Checke falls Gegner in Sicht => pursue Gegner
      if (this.team != currentBoid.team && newDistance < smallestDistance) {
        smallestDistance = newDistance;
        result = currentBoid;
      }
    }

    return result;
  }