CombatButtonDisplay - UQcsse3200/2024-studio-2 GitHub Wiki
CombatButtonDisplay
Class Documentation
Overview
The CombatButtonDisplay
class is a crucial component in the game's combat system. It extends the UIComponent
class and is responsible for rendering and managing interactive combat buttons (Attack
, Guard
, Sleep
, and Items
) on the screen during combat. This class not only handles the visual representation of combat options but also manages user interactions, provides combat hints, and coordinates with other game systems to execute combat actions.
Key Features
- Button Display: Renders and positions combat buttons on the screen (Attack, Guard, Sleep, Items).
- Event Handling: Triggers events for each button, allowing interaction with other game systems.
- Dialogue Integration: Monitors the visibility of the dialogue box and manages button visibility accordingly.
- Combat Hints: Provides informative tooltips when hovering over combat buttons.
- End Combat Handling: Displays dialogue at the end of combat and manages the transition after combat ends.
- Animation Integration: Coordinates with the
CombatArea
to trigger enemy animations. - Item Usage: Handles item selection and confirmation during combat.
Visuals
Usage Guide
Initialization
To use the CombatButtonDisplay
, you need to initialize it with the current game screen, service container, and combat area:
Screen gameScreen = ...; // Your current game screen
ServiceContainer serviceContainer = ...; // Your game's service container
CombatArea combatArea = ...; // Your combat area instance
CombatButtonDisplay combatButtonDisplay = new CombatButtonDisplay(gameScreen, serviceContainer, combatArea);
Creating and Adding Actors
The create()
method initializes the combat buttons and sets up event listeners. It's typically called when the combat scene is being set up:
combatButtonDisplay.create();
Handling Button Clicks
Button clicks are handled internally by the CombatButtonDisplay
class. Each button triggers a specific event when clicked:
- Attack Button: Triggers "Attack" event
- Guard Button: Triggers "Guard" event
- Sleep Button: Triggers "Sleep" event
- Items Button: Triggers "Items" event and hides the combat buttons
Combat Hints
Combat hints are displayed when the player hovers over a button. The setTextForCombatHint()
method is used to set the hint text:
private void setTextForCombatHint(String moveDescription) {
hoverTextLabel.setText(moveDescription);
// Position and show the hint
// ...
}
Handling End of Combat
When combat ends, you can display the end combat dialogue:
boolean winStatus = true; // or false
Entity enemyEntity = ...; // The defeated enemy entity
combatButtonDisplay.displayEndCombatDialogue(enemyEntity, winStatus);
For boss encounters:
boolean winStatus = true; // or false
Entity bossEntity = ...; // The boss entity
combatButtonDisplay.displayBossEndCombatDialogue(bossEntity, winStatus);
Item Usage in Combat
When an item is selected from the inventory during combat, the onItemClicked()
method is called:
private void onItemClicked(AbstractItem item, int index, ItemUsageContext context) {
// Display confirmation dialogue
// Trigger item usage if confirmed
}
Best Practices
- Event-Driven Architecture: Utilize the event system for communication between
CombatButtonDisplay
and other game components. - Separation of Concerns: Keep the
CombatButtonDisplay
focused on UI management and delegate game logic to appropriate systems. - Responsive Design: Ensure that the button layout and hint display adapt to different screen sizes.
- Error Handling: Implement proper error handling, especially when dealing with dialogue and animation triggers.
- Performance Optimization: Be mindful of performance, especially when handling animations and frequent UI updates.
Implementation Details
Combat Button Initialization
The class initializes four buttons (Attack
, Guard
, Sleep
, Items
) in the addActors()
method. Each button has an associated ChangeListener
, which triggers corresponding events (e.g., Attack
, Guard
) when clicked and provides a combat hints dialogue box to provide the user information how each combat action when mouse hovering over button.
Combat Dialogue Handling
The class listens for visibility changes in the dialogue box using dialogueBoxListener
. When the dialogue box appears, the buttons are hidden; when it disappears, the buttons are restored.
End Combat Logic
At the end of combat, based on whether the player won or lost, specific end combat dialogues are displayed using the ServiceLocator.getDialogueBoxService()
and handled in displayEndCombatDialogue()
.
Z-Index and Draw Order
The getZIndex()
method controls the layer of the UI component, ensuring that buttons appear at the correct position in the rendering order.
Sequence Diagram
The sequence diagram illustrates the flow of events when a player clicks the attack button in a game. The player's action triggers an "Attack" event in the CombatButtonDisplay. This event is then passed to the CombatSystem, which initiates a dialogue if necessary. If a dialogue is present, it is made visible, and the combat buttons are hidden. Once the dialogue ends, the CombatSystem displays an end-combat dialogue, and finally, triggers an "End Combat" event.
sequenceDiagram
actor Player
participant CombatButtonDisplay
participant Entity
participant CombatArea
participant DialogueBoxService
Player->>CombatButtonDisplay: Click Attack Button
CombatButtonDisplay->>Entity: Trigger "Attack" Event
Entity->>CombatArea: Start Enemy Animation (MOVE)
CombatArea-->>Entity: Animation Started
Entity-->>CombatButtonDisplay: Combat Action Executed
Player->>CombatButtonDisplay: Hover over Guard Button
CombatButtonDisplay->>CombatButtonDisplay: Display Combat Hint
Player->>CombatButtonDisplay: Click Items Button
CombatButtonDisplay->>Entity: Trigger "Items" Event
CombatButtonDisplay->>CombatButtonDisplay: Hide Buttons
Entity->>DialogueBoxService: Display Item Selection Dialogue
DialogueBoxService-->>CombatButtonDisplay: Dialogue Closed
CombatButtonDisplay->>CombatButtonDisplay: Add Actors (Show Buttons)
Note over CombatButtonDisplay: End of Combat
CombatButtonDisplay->>DialogueBoxService: Display End Combat Dialogue
DialogueBoxService-->>CombatButtonDisplay: Dialogue Closed
CombatButtonDisplay->>Entity: Trigger "finishedEndCombatDialogue" Event
Class Diagram
The CombatButtonDisplay class represents a user interface component that displays and manages combat buttons. It contains a table, screen, ServiceContainer, TextButton instances for attack, guard, sleep, and items, and a ChangeListener for dialogue box events. The class provides methods to create and add actors, hide buttons, display end-combat dialogues, change actor states, and dispose of the component. It inherits from UIComponent, indicating its role as a user interface element. The class also has dependencies on TextButton, Table, Screen, ServiceContainer, and Entity classes, suggesting their involvement in the component's functionality.
Class Diagram
classDiagram
UIComponent <|-- CombatButtonDisplay
CombatButtonDisplay --> "1" Table
CombatButtonDisplay --> "1" Screen
CombatButtonDisplay --> "1" ServiceContainer
CombatButtonDisplay --> "4" TextButton
CombatButtonDisplay --> "1" ChangeListener
CombatButtonDisplay --> "1" CombatArea
CombatButtonDisplay --> "1" Label
CombatButtonDisplay --> "1" Image
class CombatButtonDisplay {
-Table table
-Screen screen
-ServiceContainer container
-TextButton attackButton
-TextButton guardButton
-TextButton sleepButton
-TextButton itemsButton
-ChangeListener dialogueBoxListener
-CombatArea combatArea
-Label hoverTextLabel
-Image backgroundImage
+create()
+addActors()
+hideButtons()
+displayEndCombatDialogue(Entity, boolean)
+displayBossEndCombatDialogue(Entity, boolean)
-onItemClicked(AbstractItem, int, ItemUsageContext)
-setTextForCombatHint(String)
+draw(SpriteBatch)
+getZIndex()
+dispose()
}