Class GameController
java.lang.Object
application.GameController
- All Implemented Interfaces:
GameEventListener
Mediator between the Model (GameManager) and the View (Scenes).
Implements GameEventListener to receive events from GameManager and instructs Scenes to update the UI.
Manages keyboard input for the map phase and action buttons in the battle phase.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BattleSceneCurrent battle phase screen (nullif not in battle).private BoardSceneCurrent map phase screen (nullif not yet created).private GameManagerGameManager that controls all game logic.private booleanWhether the current battle is PvP, used to determine if P2's input must be waited for.private StringLast direction P1 moved.private StringLast direction P2 moved.private MainMenuScenecurrent Main menu sceneprivate booleanLock that prevents repeated key presses while an animation is playing.private StringClass of P1 used to load sprites in the View.private booleanWhether it is P1's turn to move.private StringClass of P2 used to load sprites in the View.private final javafx.stage.StageThe main JavaFX Stage of the application. -
Constructor Summary
ConstructorsConstructorDescriptionGameController(javafx.stage.Stage stage) Creates a GameController and registers a listener with the GameManager. -
Method Summary
Modifier and TypeMethodDescriptionprivate StringConverts an entity to its class name string for sprite loading in the View.voidInitialises the application, configures the Stage, and shows the Main Menu.private String[]Converts a player's inventory to an array of item name strings for display in the View.voidonBattleResult(BattleResult result) Receives the result of each battle turn, plays the appropriate animation, then updates the HP bars after the animation finishes.voidonBattleStart(Entity entityA, Entity entityB) Receives a battle-start event, creates the BattleScene, and shows action buttons.private voidConfigures the map phase screen when entering MAP_PHASE.voidonGameOver(Player winner) Receives a game-over event, shows the GameOverScene, and binds its buttons.voidonItemPickedUp(Player player) Receives an item-pickup event and updates the inventory panel and map.voidonPlayerMoved(Player player, int row, int col) Receives a player-moved event, updates the animation on the BoardScene, and switches the turn.voidonRoundEnd(int round) Receives a round-end event on the map, updates stats and the round counter.voidonStateChanged(GameState state) Receives a state-change event and switches to the appropriate screen.voidonTomeApplied(Player player, String effectDesc) Receives a tome-applied event after the player defeats a Goblin, updates stats, and shows a notification.voidonZoneShrunk(int newRadius) Receives a zone-shrink event, redraws the map, and shows a notification.private voidWaits for the specified delay then runs the given Runnable.private voidUpdates the HP bars and inventories of both players on the BoardScene.private voidBinds battle action buttons for side A (Player 1, or the side-A entity in PvP).private voidBinds battle action buttons for side B (PvP only).private voidSets up keyboard listeners on the BoardScene.private voidShows the character selection screen and sets the callback for when selection is complete.private voidShows the Main Menu screen and binds the Start button.private voidstopBgm(javafx.scene.media.MediaPlayer player) Safely stops BGM playback without throwing an exception if the player is null.private voidsubmitA(BattleAction action) Submits side A's action to the GameManager.private CharacterTypeConverts a class name string to the corresponding CharacterType enum value.
-
Field Details
-
stage
private final javafx.stage.Stage stageThe main JavaFX Stage of the application. -
gm
GameManager that controls all game logic. -
mainMenu
current Main menu scene -
board
Current map phase screen (nullif not yet created). -
battle
Current battle phase screen (nullif not in battle). -
p1Class
Class of P1 used to load sprites in the View. -
p2Class
Class of P2 used to load sprites in the View. -
p1TurnToMove
private boolean p1TurnToMoveWhether it is P1's turn to move. -
lastP1Dir
Last direction P1 moved. -
lastP2Dir
Last direction P2 moved. -
isPvP
private boolean isPvPWhether the current battle is PvP, used to determine if P2's input must be waited for. -
moveLock
private boolean moveLockLock that prevents repeated key presses while an animation is playing.
-
-
Constructor Details
-
GameController
public GameController(javafx.stage.Stage stage) Creates a GameController and registers a listener with the GameManager.- Parameters:
stage- The main Stage of the application
-
-
Method Details
-
initialize
public void initialize()Initialises the application, configures the Stage, and shows the Main Menu. -
showMainMenu
private void showMainMenu()Shows the Main Menu screen and binds the Start button. -
onStateChanged
Receives a state-change event and switches to the appropriate screen.- Specified by:
onStateChangedin interfaceGameEventListener- Parameters:
state- The new game state
-
onPlayerMoved
Receives a player-moved event, updates the animation on the BoardScene, and switches the turn.- Specified by:
onPlayerMovedin interfaceGameEventListener- Parameters:
player- The player who movedrow- New rowcol- New column
-
onItemPickedUp
Receives an item-pickup event and updates the inventory panel and map.- Specified by:
onItemPickedUpin interfaceGameEventListener- Parameters:
player- The player who picked up the item
-
onRoundEnd
public void onRoundEnd(int round) Receives a round-end event on the map, updates stats and the round counter.- Specified by:
onRoundEndin interfaceGameEventListener- Parameters:
round- The round that just ended
-
onZoneShrunk
public void onZoneShrunk(int newRadius) Receives a zone-shrink event, redraws the map, and shows a notification.- Specified by:
onZoneShrunkin interfaceGameEventListener- Parameters:
newRadius- New radius of the safe zone
-
onTomeApplied
Receives a tome-applied event after the player defeats a Goblin, updates stats, and shows a notification.- Specified by:
onTomeAppliedin interfaceGameEventListener- Parameters:
player- The player who received the tomeeffectDesc- Description of the stat boost
-
onBattleStart
Receives a battle-start event, creates the BattleScene, and shows action buttons.- Specified by:
onBattleStartin interfaceGameEventListener- Parameters:
entityA- Entity on side AentityB- Entity on side B
-
onBattleResult
Receives the result of each battle turn, plays the appropriate animation, then updates the HP bars after the animation finishes.- Specified by:
onBattleResultin interfaceGameEventListener- Parameters:
result- Result of this turn
-
onGameOver
Receives a game-over event, shows the GameOverScene, and binds its buttons.- Specified by:
onGameOverin interfaceGameEventListener- Parameters:
winner- The winner, ornullin case of a draw
-
showCharacterSelect
private void showCharacterSelect()Shows the character selection screen and sets the callback for when selection is complete. -
onEnterMapPhase
private void onEnterMapPhase()Configures the map phase screen when entering MAP_PHASE. If no BoardScene exists yet, it is created and keyboard listeners are set up. -
setupKeyboard
private void setupKeyboard()Sets up keyboard listeners on the BoardScene. P1 uses WASD (plus QEZX for Alien diagonals); P2 uses UHJK (plus YINM). -
setupBattleButtonsForA
private void setupBattleButtonsForA()Binds battle action buttons for side A (Player 1, or the side-A entity in PvP). -
submitA
Submits side A's action to the GameManager. In PvP mode, sets up side B's buttons if the turn has not been resolved yet.- Parameters:
action- The chosen action
-
setupBattleButtonsForB
private void setupBattleButtonsForB()Binds battle action buttons for side B (PvP only). -
refreshStats
private void refreshStats()Updates the HP bars and inventories of both players on the BoardScene. -
classOf
-
itemNames
-
toType
Converts a class name string to the corresponding CharacterType enum value.- Parameters:
name- Class name, e.g. "Knight", "archer"- Returns:
- Matching CharacterType (default: KNIGHT)
-
pause
Waits for the specified delay then runs the given Runnable.- Parameters:
seconds- Delay duration in secondsonFinished- Action to run after the delay
-
stopBgm
private void stopBgm(javafx.scene.media.MediaPlayer player) Safely stops BGM playback without throwing an exception if the player is null.- Parameters:
player- MediaPlayer to stop
-