Skip to content

Entity Drops

27abdullah edited this page Oct 21, 2023 · 8 revisions

Animals in the game can drop items. This is handled by the MultiDropComponent. This component that enables animals to drop different items on different triggers. Each item can be dropped at a different rate.

Usage

The following example code demonstrates how to make chickens drop periodically and on death. During entity creation in NPCFactory. Create a list of SingleDropHandlers, and add desired items and drops. The constructor takes in

  • createItem Method that creates item to be dropped
  • triggersToNextDrop Number of triggers until item is dropped
  • listener Method to add new listener
  • trigger Trigger that will prompt item drop
    List<SingleDropHandler> singleDropHandlers = new ArrayList<>();
    MultiDropComponent multiDropComponent = new MultiDropComponent(singleDropHandlers, true);
    //Chickens untamed drop eggs
    singleDropHandlers.add(new SingleDropHandler(ItemFactory::createEgg, 24,
            ServiceLocator.getTimeService().getEvents()::addListener, "hourUpdate", false));
    //Once tamed, chickens drop one extra egg
    singleDropHandlers.add(new SingleDropHandler(ItemFactory::createEgg, 24,
            ServiceLocator.getTimeService().getEvents()::addListener, "hourUpdate", true));
    //Once tamed, chickens can be fed to drop golden eggs
    singleDropHandlers.add(new SingleDropHandler(ItemFactory::createGoldenEgg, 3,
            chicken.getEvents()::addListener, "feed", true));
    //Drop chicken on death
    singleDropHandlers.add(new SingleDropHandler(ItemFactory::createChickenMeat, 1,
            chicken.getEvents()::addListener, "death", false));

Add this component to the entity.

   chicken.addComponnent(multiDropComponent)

The chicken will now drop eggs, can be fed to drop golden eggs, and will drop meat on death.

System Overview

The entity drops feature is handled by the following classes

image
Clone this wiki locally