Deleting Items (CookingAndServingHandler) - UQcsse3200/2024-studio-3 GitHub Wiki
Description
The CookingAndServingHandler class is a component responsible for managing the cooking and serving logic of ingredients within the game. It handles the state transitions of ingredients as they are cooked, updates their visual representation, and manages the serving and disposal of meals. The class operates within an entity-component-system (ECS) architecture, where it interacts with other components such as IngredientComponent and TextureRenderComponent.
Core Mechanics
- Cooking management: Starts and stops the cooking process, updating the state of the ingredient based on the elapsed time.
- Texture management: Updates visual representation of the ingredient based on its state.
- Serving and Disposal: Manages the serving of the meal and its disposal once served.
Key Methods:
- create(): Initializes the handler by retrieving the 'IngredientComponent' and 'TextureRenderComponent' from the entity. It updates the texture based on the initial state of the ingredient.
-
updateTexture(): Updates the texture of the ingredient based on its state and name. Uses a switch statement to determine the correct texture path based on the ingredient's name. Adjusts the texture path depending on the item’s cooking state (raw, cooked, or burnt). Sets the updated texture on the 'TextureRenderComponent'.
-
serveMeal(): Serves the meal if the ingredient is cooked. It sets the 'isServed' flag to true and prints a message indicating that the meal has been served.
- deleteMeal(): Disposes of the meal entity if it has been served. It removes the entity from the game and prints a message indicating that the meal has been removed.
- updateState(): Updates the state of the ingredient based on elapsed cooking time. Checks if the cooking duration has passed and updates the ingredient's state to "cooked" or "burnt" as appropriate. Calls updateTexture() to update the visual representation of the ingredient. Stops cooking if the item has burnt.
Example:
Scenario: Cooking a Fish
Initialization: A CookingAndServingHandler is created with a GameTime instance and attached to a fish entity that has IngredientComponent and TextureRenderComponent.
Start Cooking: Call startCooking(). If the fish is raw, the cooking process begins and the texture is updated to reflect cooking status.
Update State: As time progresses, updateState() is called to check if the fish should be cooked or burnt.
Serve Meal: Once cooked, call serveMeal() to mark the fish as served.
Dispose: After serving, call deleteMeal() to remove the fish entity from the game.
Dependencies
IngredientComponent: Must be present on the entity to provide information about the ingredient's state and cooking time. TextureRenderComponent: Must be present on the entity to update the visual representation of the ingredient. GameTime: Provides the current time for managing cooking durations.