Player interaction with item - UQcsse3200/2024-studio-1 GitHub Wiki

This page is about how players are able to 'pick up' an item, and what happens after this occurs.

About the ItemPickupComponent

The ItemPickupComponent is a Component class that acts a way for the player to 'pickup' items on the level. This component attaches to a player and handles methods for picking up an item, detecting collisions, removing the item from the map, and handles the "reroll" item functionality and buyable item purchasing functionality. entity and detects collisions with other entities in the onCollisionStart() method. This method updates a flag, contact, to be True and updates the item entity in collision (variable itemEntity) and the collectible representation (variable item). If the entity in collision is an item entity, and "E" is pressed, then the handleItemPickup() method is called. This method checks if the contact flag is true, and if so, then the 'pickup' method of that item is called, and the item is marked for removal. This may cause for the item to be stored in the inventory (for UsableItems) or may cause the effect of the item to immediately apply to the player (for BuffItems).

Key features

  • Handling item pickups: This component has important variables such as collision (a flag that is True if there is a collision or False), itemEntity (the item entity in collision) and item (the Collectible representation of the item in collision). If an item is in collision with an item, the collision flag, itemEntity and item variables are updated.
  • Key presses: If "E" is pressed, the handleItemPickup() method is called, which uses the Inventory of the player's InventoryComponent to pickup() the item, if the contact flag is true. The item is then marked for removal, and the item and itemEntity variables are set to null.
  • Purchasing items: Refer to Buyable items wiki for more details
  • Handling reroll item: Refer to Usable items wiki for more details on the Reroll item
  • Disposal of items upon pickup: The ItemPickupComponent has a method, markEntityForRemoval(), to flag item entities for removal when the player comes in contact with them. In the EntityService class these marked entities are then prevented from being updated during the game update cycle and will ensure that the entity is safely disposed.

Key methods

  • onCollisionStart(): This method is responsible for detecting whenever the player is in collision with another entity. The itemEntity and item variables are not updated, if the other entity in collision is not a collectible, or has already been picked up
  • onCollisionEnd(): This method is responsible for setting the collision flag to False
  • handleReroll(): This method handles the reroll functionality
  • checkItemPurchase(): This method is responsible for the buyable item purchasing
  • handleItemPickup(): This method is responsible for the main item picking up functionality. It is called whenever "E" is pressed by the user. This method will return nothing if either the item or itemEntity variables are null. This means that there is no Collectible item that can be picked up. This method will also return nothing if the item in collision has a BuyableComponent. A buyable item cannot be picked up.
  • markEntityForRemoval(): This method is responsible for removing item entities from the map. It uses the markEntityForRemoval() method of the EntityService class
  • randomItemGenerator(): This method is used in the reroll item functionality. It takes in a number (that was randomly generated) and gets the specification of an item, associated with this number. This specification is then used to create a collectible entity in the CollectibleFactory