-
Notifications
You must be signed in to change notification settings - Fork 7
Taming Animals
Mr Goh edited this page Oct 3, 2023
·
26 revisions
To begin to tame the animal, an event listener that would listen out for the "feed event was added to the game. When this event has been triggered, it will enter the main logic for taming. More information about the addition of events and events listener can be found at Event Systems
@Override
public void create() {
entity.getEvents().addListener("feed", this::feedAnimal);
}
private void feedAnimal() {
if (isTamed) {
return;
}
//Ensures player is holding an item
if (this.playerInventory.getHeldItem() == null) {
return;
}
//If true, ensures that the player's held item has the ItemComponent class.
if (this.playerInventory.getHeldItem().getComponent(ItemComponent.class) == null) {
return;
}
// If so, we can check if player is holding the right item
if (this.playerInventory.getHeldItem().getComponent(ItemComponent.class).getItemName().equals(favouriteFood)) {
// Generate RNG number for taming
double randomDecimal = generateRandomDecimal();
// Try and tame the animal
// Check how many times the player has tried to tame the animal
// If player has already tried enough times, tame the animal (prevents frustration).
// Use RNG to try and tame the animal
if (numTimesFed == tamingThreshold || randomDecimal > tamingProbability) {
isTamed = true;
ServiceLocator.getMissionManager().getEvents().trigger(MissionManager.MissionEvent.ANIMAL_TAMED.name());
}
else {
numTimesFed++;
}
// this.playerInventory.removeItem(this.playerInventory.getHeldItem()); TODO: once inventory works comment it in
// Remove the food from the players inventory
}
}
This is feedAnimal is the logic for taming. It uses pseudo random number generator to determine if the animal is tamed. Each animal species will have different tame levels, making it easier/harder to tame a specific animal.