-
Notifications
You must be signed in to change notification settings - Fork 1
Weapon Pickup Function
This wiki page will explain the code implementation which allows the player to pick up weapons from the map.
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.
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);
Java
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 Java
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.
Java
if (other == f) {
If they are the same, the weapon is removed from the map using the removeWeaponOnMap()
function in ForestGameArea
Java
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.
Java
ServiceLocator.getGameArea().getPlayer().getComponent(InventoryComponent.class).addItem(entityOfComponent);
Back to Combat Items Contents Page
- Eugene Chong
- GitHub: @eugene2341
- Discord: gene#0005
- Slack: Eugene Chong