Item equipment and potion consumption logic - UQdeco2800/2022-studio-2 GitHub Wiki
This wiki page will explain the implementation of the item equipment which allows the player to equip an item or use a potion.
The equipItem() method handles weapon equipment and armour equipment.
equipables
contains two slots. Index 0 is reserved for Weapon
and 1 for Armour
.
After an item is correctly equipped. The corresponding effects will be applied to the player's stat by calling applyWeaponEffect() or applyArmourEffect().
public void equipItem(Entity item) {
boolean equipped = false;
if (inventory.contains(item)) {
if (item.checkEntityType(EntityTypes.WEAPON) && equipables[0] == null) {
equipables[0] = item;
//Slot 1 - Reserved for combat items
applyWeaponEffect(item);
equipped = true;
} else if (item.checkEntityType(EntityTypes.ARMOUR) && equipables[1] == null) {
equipables[1] = item;
//Slot 2 - Reserved for armour
applyArmourEffect(item);
equipped = true;
}
if (equipped) removeItem(item);
}
}
The code below works by using the stats of the armour from the config files and turning that into an effect which directly impacts the player in some way, whether it be through speed, damage reduction, or stamina. This works by using the createModifier() function, then inputting the stat the player wants to change and the the value of the stat on the item.
private void applyArmourEffect(Entity armour) {
ArmourStatsComponent armourStats = armour.getComponent(ArmourStatsComponent.class);
PlayerModifier pmComponent = entity.getComponent(PlayerModifier.class);
//Applying the weight of the armour to player
pmComponent.createModifier(PlayerModifier.MOVESPEED, (float)armourStats.getWeight(), true, 0);
//Applying the physical resistance of the armour to player
pmComponent.createModifier(PlayerModifier.DMGREDUCTION, (float)armourStats.getPhyResistance(), true, 0);
pmComponent.createModifier(PlayerModifier.STAMINAMAX, (float)armourStats.getVitality(), true, 0);
}
@Credit to team 4.
The applyWeaponEffect is called when the equip method is called. The corresponding weapon stat is fetched and applied to the player.
private void applyWeaponEffect(Entity weapon) {
WeaponStatsComponent weaponStats;
if ((weaponStats = weapon.getComponent(MeleeStatsComponent.class)) != null) {
MeleeStatsComponent meleeStats = (MeleeStatsComponent) weaponStats;
PlayerModifier pmComponent = entity.getComponent(PlayerModifier.class);
pmComponent.createModifier(PlayerModifier.MOVESPEED, (float) (-meleeStats.getWeight()/15), true, 0);
}
}
First, the listeners in PlayerActions are added to actively check for any triggering event with the following key bind.
entity.getEvents().addListener("consumePotionSlot1", this::consumePotionSlot1);
entity.getEvents().addListener("consumePotionSlot2", this::consumePotionSlot2);
entity.getEvents().addListener("consumePotionSlot3", this::consumePotionSlot3);
- Press
1
to use the potion in slot 1.
- Press
2
to use the potion in slot 2.
- Press
3
to use the potion in slot 3.
Then consumePotion() method handles potion consumption based on the player's input and consume the corresponding potion at the given index.
public void consumePotion(int inputIndex) {
if (quickBarItems.size() >= inputIndex) {
quickBarItems.get(inputIndex).getComponent(PotionEffectComponent.class).applyEffect(entity);
if (quickBarQuantity[inputIndex] == 1) {
removePotion(inputIndex);
} else if (quickBarQuantity[inputIndex] > 1) {
--quickBarQuantity[quickBarItems.indexOf(inputIndex)];
}
}
}
- Li-Sung Ou
- GitHub: @PeterOu8
- Discord: Secret Agent Randy Beans#6754
- Slack: Li-Sung Ou
- Lachlan McDonald
- GitHub: @Lukeyone
- Discord: merty009 #1029