Weapon Pickup Function - UQdeco2800/2022-studio-2 GitHub Wiki

Summary

This wiki page will explain the code implementation which allows the player to pick up weapons from the map.

WeaponPickupComponent

Located in: source/core/src/main/com/deco2800/game/components/CombatItemsComponent/WeaponPickupComponent.java

WeaponPickupComponent is a new class created that extends Component which allows the player entity to pickup weapons from the map and inserts it into inventory. This component will most probably be generalised in the next sprints to allow not just weapon to be picked up upon collision, but other items like materials, buffs etc.

Firstly, the pickup listeners are created in create() which check for collisions with the weapon entities that implements this component.

entity.getEvents().addListener("collisionStart", this::pickUp);

pickUp method

private void pickUp(Fixture me, Fixture other) {
    hitboxComponent = entity.getComponent(HitboxComponent.class);
    Fixture f = ServiceLocator.getGameArea().getPlayer().getComponent(HitboxComponent.class).getFixture();

    if (other == f) {
        Entity entityOfComponent = getEntity();
        ForestGameArea.removeWeaponOnMap(entityOfComponent);

        //insert into inventory
        ServiceLocator.getGameArea().getPlayer().getComponent(InventoryComponent.class).addItem(entityOfComponent);
        logger.info("Weapon picked up");
    }
}

Upon collision with an entity, the pickUp() method gets called and the if statement checks if the entity colliding with the weapon entity on the map is the player entity. This is done by getting the fixture of the player entity, as below

Fixture f = ServiceLocator.getGameArea().getPlayer().getComponent(HitboxComponent.class).getFixture();

and comparing it to the other fixture (the fixture that is colliding the weapon) to see if they are the same.

if (other == f) {

If they are the same, the weapon is removed from the map using the removeWeaponOnMap() function in ForestGameArea

public static void removeWeaponOnMap(Entity entityToRemove) {

    entityToRemove.setEnabled(false);
    weaponOnMap.remove(entityToRemove);

    Gdx.app.postRunnable(() -> entityToRemove.dispose());
  }

The weapon is also inserted into the inventory with the addItem() function in InventoryComponent.

ServiceLocator.getGameArea().getPlayer().getComponent(InventoryComponent.class).addItem(entityOfComponent);

Back to Combat Items Contents Page

Author

  • Eugene Chong
  • GitHub: @eugene2341
  • Discord: gene#0005
  • Slack: Eugene Chong