Java Code Examples for com.badlogic.ashley.core.Entity#remove()

The following examples show how to use com.badlogic.ashley.core.Entity#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemHelpers.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Remove an item from inventory.
 *
 * @param entity Who's inventory
 * @param item   The item
 */
private void removeFromInventory(Entity entity, Entity item) {
  InventoryComponent inventory = ComponentMappers.inventory.get(entity);
  inventory.items.remove(item);

  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (getTotalWeight(entity) <= attributes.strength * 5
      && ComponentMappers.encumbered.has(entity)) {
    entity.remove(EncumberedComponent.class);

    if (ComponentMappers.player.has(entity)) {
      WorldManager.log.add("effects.encumbered.stopped");
    }
  }
}
 
Example 2
Source File: ItemHelpers.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Remove multiple items from inventory.
 *
 * @param entity Who's inventory
 * @param items  A list of items
 */
public void removeMultipleFromInventory(Entity entity, ArrayList<Entity> items) {
  InventoryComponent inventory = ComponentMappers.inventory.get(entity);
  inventory.items.removeAll(items);

  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (getTotalWeight(entity) <= attributes.strength * 5
      && ComponentMappers.encumbered.has(entity)) {
    entity.remove(EncumberedComponent.class);

    if (ComponentMappers.player.has(entity)) {
      WorldManager.log.add("effects.encumbered.stopped");
    }
  }
}
 
Example 3
Source File: PoisonedSystem.java    From xibalba with MIT License 6 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  PoisonedComponent poisoned = ComponentMappers.poisoned.get(entity);

  if (poisoned.counter == poisoned.life) {
    entity.remove(PoisonedComponent.class);
  } else {
    WorldManager.entityHelpers.takeDamage(entity, poisoned.damage);

    if (WorldManager.entityHelpers.canSee(WorldManager.player, entity)) {
      boolean isPlayer = ComponentMappers.player.has(entity);
      AttributesComponent attributes = ComponentMappers.attributes.get(entity);

      WorldManager.log.add(
          "effects.poisoned.tookDamage", (isPlayer ? "You" : attributes.name), poisoned.damage
      );

      if (attributes.health <= 0) {
        WorldManager.log.add("effects.poisoned.died", (isPlayer ? "You" : attributes.name));
      }
    }

    poisoned.counter += 1;
  }
}
 
Example 4
Source File: CharmedSystem.java    From xibalba with MIT License 6 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  CharmedComponent charmed = ComponentMappers.charmed.get(entity);

  if (charmed.counter == charmed.life) {
    entity.remove(CharmedComponent.class);

    boolean isPlayer = ComponentMappers.player.has(entity);
    AttributesComponent attributes = ComponentMappers.attributes.get(entity);

    WorldManager.log.add(
        "effects.charmed.stopped",
        (isPlayer ? "You" : attributes.name),
        (isPlayer ? "are" : "is")
    );
  } else {
    charmed.counter += 1;
  }
}
 
Example 5
Source File: BleedingSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  BleedingComponent bleeding = ComponentMappers.bleeding.get(entity);

  if (bleeding.counter == bleeding.life) {
    entity.remove(BleedingComponent.class);
  } else {
    WorldManager.entityHelpers.takeDamage(entity, bleeding.damage);

    PositionComponent position = ComponentMappers.position.get(entity);
    WorldManager.mapHelpers.makeFloorBloody(position.pos);

    if (WorldManager.entityHelpers.canSee(WorldManager.player, entity)) {
      AttributesComponent attributes = ComponentMappers.attributes.get(entity);

      boolean isPlayer = ComponentMappers.player.has(entity);

      WorldManager.log.add(
          "effects.bleeding.tookDamage", (isPlayer ? "You" : attributes.name),
          bleeding.damage
      );

      if (attributes.health <= 0) {
        WorldManager.log.add("effects.bleeding.died", (isPlayer ? "You" : attributes.name));
      }
    }

    bleeding.counter += 1;
  }
}
 
Example 6
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldIterateEntitiesWithCorrectFamily () {
	final Engine engine = new Engine();

	final Family family = Family.all(OrderComponent.class, ComponentB.class).get();
	final SortedIteratingSystemMock system = new SortedIteratingSystemMock(family);
	final Entity e = new Entity();

	engine.addSystem(system);
	engine.addEntity(e);

	// When entity has OrderComponent
	e.add(new OrderComponent("A", 0));
	engine.update(deltaTime);

	// When entity has OrderComponent and ComponentB
	e.add(new ComponentB());
	system.expectedNames.addLast("A");
	engine.update(deltaTime);

	// When entity has OrderComponent, ComponentB and ComponentC
	e.add(new ComponentC());
	system.expectedNames.addLast("A");
	engine.update(deltaTime);

	// When entity has ComponentB and ComponentC
	e.remove(OrderComponent.class);
	e.add(new ComponentC());
	engine.update(deltaTime);
}
 
Example 7
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Override
public void processEntity (Entity entity, float deltaTime) {
	int index = im.get(entity).index;
	if (index % 2 == 0) {
		entity.remove(SpyComponent.class);
		entity.remove(IndexComponent.class);
	} else {
		sm.get(entity).updates++;
	}
}
 
Example 8
Source File: IteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldIterateEntitiesWithCorrectFamily () {
	final Engine engine = new Engine();

	final Family family = Family.all(ComponentA.class, ComponentB.class).get();
	final IteratingSystemMock system = new IteratingSystemMock(family);
	final Entity e = new Entity();

	engine.addSystem(system);
	engine.addEntity(e);

	// When entity has ComponentA
	e.add(new ComponentA());
	engine.update(deltaTime);

	assertEquals(0, system.numUpdates);

	// When entity has ComponentA and ComponentB
	system.numUpdates = 0;
	e.add(new ComponentB());
	engine.update(deltaTime);

	assertEquals(1, system.numUpdates);

	// When entity has ComponentA, ComponentB and ComponentC
	system.numUpdates = 0;
	e.add(new ComponentC());
	engine.update(deltaTime);

	assertEquals(1, system.numUpdates);

	// When entity has ComponentB and ComponentC
	system.numUpdates = 0;
	e.remove(ComponentA.class);
	e.add(new ComponentC());
	engine.update(deltaTime);

	assertEquals(0, system.numUpdates);
}
 
Example 9
Source File: IteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Override
public void processEntity (Entity entity, float deltaTime) {
	int index = im.get(entity).index;
	if (index % 2 == 0) {
		entity.remove(SpyComponent.class);
		entity.remove(IndexComponent.class);
	} else {
		sm.get(entity).updates++;
	}
}
 
Example 10
Source File: MouseMovementSystem.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Get next step in moving path, add a movement component with that position, remove step.
 *
 * @param deltaTime Time between now and previous frame
 */
public void update(float deltaTime) {
  for (Entity entity : entities) {
    PlayerComponent playerDetails = ComponentMappers.player.get(WorldManager.player);
    AttributesComponent attributes = ComponentMappers.attributes.get(WorldManager.player);

    // Remove mouse movement component once path is empty
    if (playerDetails.path == null || playerDetails.path.isEmpty()) {
      attributes.energy -= MovementComponent.COST;

      entity.remove(MouseMovementComponent.class);
      WorldManager.state = WorldManager.State.PLAYING;
    } else {
      if (attributes.energy >= MovementComponent.COST) {
        // Start walking
        GridCell cell = playerDetails.path.get(0);

        entity.add(new MovementComponent(new Vector2(cell.getX(), cell.getY())));

        List<GridCell> newPath = new ArrayList<>(playerDetails.path);
        newPath.remove(cell);

        playerDetails.path = newPath;
      }
    }
  }
}
 
Example 11
Source File: MeleeSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  MeleeComponent melee = ComponentMappers.melee.get(entity);
  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (melee.target != null && !entity.isScheduledForRemoval()) {
    WorldManager.combatHelpers.melee(entity, melee.target, melee.bodyPart, melee.isFocused);
  }

  attributes.energy -= MeleeComponent.COST;
  entity.remove(MeleeComponent.class);
}
 
Example 12
Source File: RangeSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  RangeComponent range = ComponentMappers.range.get(entity);
  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (range.position != null && !entity.isScheduledForRemoval()) {
    Entity target = WorldManager.mapHelpers.getEnemyAt(range.position);

    if (target != null) {
      WorldManager.combatHelpers.range(
          entity, target, range.bodyPart, range.item, range.skill, range.isFocused
      );
    }

    if (Objects.equals(range.skill, "throwing")) {
      ComponentMappers.item.get(range.item).throwing = false;
    }

    if (target == null) {
      doThrowAnimation(entity, range.item, range.position, false);
    } else {
      doThrowAnimation(entity, range.item, range.position, true);
    }
  }

  attributes.energy -= RangeComponent.COST;
  entity.remove(RangeComponent.class);
}
 
Example 13
Source File: BurningSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  BurningComponent burning = ComponentMappers.burning.get(entity);

  if (burning.counter == burning.life) {
    entity.remove(BurningComponent.class);
  } else {
    WorldManager.entityHelpers.takeDamage(entity, burning.damage);

    PositionComponent position = ComponentMappers.position.get(entity);
    WorldManager.mapHelpers.makeFloorBloody(position.pos);

    if (WorldManager.entityHelpers.canSee(WorldManager.player, entity)) {
      AttributesComponent attributes = ComponentMappers.attributes.get(entity);

      boolean isPlayer = ComponentMappers.player.has(entity);

      WorldManager.log.add(
          "effects.burning.tookDamage", (isPlayer ? "You" : attributes.name),
          burning.damage
      );

      if (attributes.health <= 0) {
        WorldManager.log.add("effects.burning.died", (isPlayer ? "You" : attributes.name));
      }
    }

    burning.counter += 1;
  }
}
 
Example 14
Source File: World.java    From xibalba with MIT License 5 votes vote down vote up
private void changeDepth(int change) {
  Main.playScreen.dispose();
  main.setScreen(new DepthScreen());

  entities.get(currentMapIndex).removeValue(WorldManager.player, true);
  entities.get(currentMapIndex + change).add(WorldManager.player);

  currentMapIndex += change;

  PlayerComponent playerDetails = ComponentMappers.player.get(WorldManager.player);
  if (currentMapIndex > playerDetails.lowestDepth) {
    playerDetails.lowestDepth = currentMapIndex;
  }

  WorldManager.engine.removeAllEntities();

  for (Entity entity : entities.get(currentMapIndex)) {
    WorldManager.engine.addEntity(entity);

    if (ComponentMappers.attributes.has(entity)) {
      if (ComponentMappers.player.has(entity)) {
        entity.remove(MouseMovementComponent.class);

        Vector2 position = change > 0
            ? WorldManager.world.getCurrentMap().entrance
            : WorldManager.world.getCurrentMap().exit;

        WorldManager.entityHelpers.updatePosition(entity, position.x, position.y);
        WorldManager.entityHelpers.updateSprite(entity, position.x, position.y);
      }

      WorldManager.entityHelpers.updateSenses(entity);
    }
  }

  Main.playScreen = new PlayScreen(main);
  main.setScreen(Main.playScreen);
}
 
Example 15
Source File: StuckSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  StuckComponent stuck = ComponentMappers.stuck.get(entity);

  if (stuck.counter == stuck.life) {
    entity.remove(StuckComponent.class);
  } else {
    stuck.counter += 1;
  }
}
 
Example 16
Source File: SickSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  SickComponent sick = ComponentMappers.sick.get(entity);

  if (sick.counter == sick.life) {
    entity.remove(SickComponent.class);
  } else {
    if (MathUtils.random() > 0.5) {
      WorldManager.entityHelpers.vomit(entity, sick.damage);

      if (WorldManager.entityHelpers.canSee(WorldManager.player, entity)) {
        boolean isPlayer = ComponentMappers.player.has(entity);
        AttributesComponent attributes = ComponentMappers.attributes.get(entity);

        WorldManager.log.add(
            "effects.sick.tookDamage", (isPlayer ? "You" : attributes.name), sick.damage
        );

        if (attributes.health <= 0) {
          WorldManager.log.add("effects.sick.died", (isPlayer ? "You" : attributes.name));
        }
      }

      sick.counter += 1;
    }
  }
}
 
Example 17
Source File: WetSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  WetComponent wet = ComponentMappers.wet.get(entity);
  PositionComponent position = ComponentMappers.position.get(entity);

  if (!WorldManager.mapHelpers.getCell(position.pos.x, position.pos.y).isWater()) {
    if (wet.counter == wet.life) {
      entity.remove(WetComponent.class);
    } else {
      WorldManager.mapHelpers.makeFloorWet(position.pos);

      wet.counter += 1;
    }
  }
}
 
Example 18
Source File: CrippledSystem.java    From xibalba with MIT License 5 votes vote down vote up
@Override
protected void processEntity(Entity entity, float deltaTime) {
  CrippledComponent crippled = ComponentMappers.crippled.get(entity);

  if (crippled.counter == crippled.life) {
    entity.remove(CrippledComponent.class);
  } else {
    if (crippled.turnCounter == 2) {
      crippled.turnCounter = 0;
      crippled.counter += 1;
    } else {
      crippled.turnCounter += 1;
    }
  }
}
 
Example 19
Source File: ExploreSystem.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Update all entities with the Explore component.
 *
 * @param deltaTime Time since last frame.
 */
public void update(float deltaTime) {
  for (Entity entity : entities) {
    ExploreComponent explore = ComponentMappers.explore.get(entity);

    // If the player sees an enemy, stop
    if (WorldManager.entityHelpers.enemyInSight(entity)) {
      entity.remove(ExploreComponent.class);
      WorldManager.state = WorldManager.State.PLAYING;

      return;
    }

    // If there is nowhere else to go, stop
    if (WorldManager.world.getCurrentMap().dijkstra.exploreGoals.size == 0) {
      entity.remove(ExploreComponent.class);
      WorldManager.state = WorldManager.State.PLAYING;

      return;
    }

    // Get new path if we're done
    if (explore.path.size == 0) {
      WorldManager.world.getCurrentMap().dijkstra.updatePlayerExplore();

      explore.path = WorldManager.world.getCurrentMap().dijkstra.findExplorePath(
          ComponentMappers.position.get(WorldManager.player).pos
      );
    }

    AttributesComponent attributes = ComponentMappers.attributes.get(entity);

    // Walk it out!
    if (attributes.energy >= MovementComponent.COST) {
      // Start walking
      Vector2 cell = explore.path.get(0);

      entity.add(new MovementComponent(cell));
      explore.path.removeIndex(0);
    }
  }
}
 
Example 20
Source File: ItemHelpers.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Add an item to an entity's inventory.
 *
 * @param entity Entity we wanna to give shit to
 * @param item   The item itself
 * @param log    Whether or not to log this action
 */
public void addToInventory(Entity entity, Entity item, boolean log) {
  InventoryComponent inventory = ComponentMappers.inventory.get(entity);
  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (inventory != null) {
    item.remove(PositionComponent.class);
    inventory.items.add(item);

    EquipmentComponent equipment = ComponentMappers.equipment.get(entity);
    ItemComponent itemDetails = ComponentMappers.item.get(item);

    if (log) {
      WorldManager.log.add(
          "inventory.pickedUp", WorldManager.itemHelpers.getName(entity, item)
      );
    }

    if (itemDetails.twoHanded && WorldManager.entityHelpers.hasDefect(entity, "One arm")) {
      return;
    }

    if (Objects.equals(itemDetails.type, "weapon")) {
      if (!itemDetails.twoHanded || !WorldManager.entityHelpers.hasDefect(entity, "One arm")) {
        if (equipment.slots.get("right hand") == null) {
          hold(entity, item);

          if (log) {
            WorldManager.log.add(
                "inventory.holding", WorldManager.itemHelpers.getName(entity, item)
            );
          }
        }
      }
    }

    if (Objects.equals(itemDetails.type, "armor")) {
      if (equipment.slots.get(itemDetails.location) == null) {
        wear(entity, item);
      }

      if (log) {
        WorldManager.log.add(
            "inventory.wearing", WorldManager.itemHelpers.getName(entity, item)
        );
      }
    }

    if (getTotalWeight(entity) > attributes.strength * 5) {
      entity.add(new EncumberedComponent());

      if (ComponentMappers.player.has(entity)) {
        WorldManager.log.add("effects.encumbered.started");
      }
    }
  }
}