Class GameController

java.lang.Object
application.GameController
All Implemented Interfaces:
GameEventListener

public class GameController extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    private BattleScene
    Current battle phase screen (null if not in battle).
    private BoardScene
    Current map phase screen (null if not yet created).
    private GameManager
    GameManager that controls all game logic.
    private boolean
    Whether the current battle is PvP, used to determine if P2's input must be waited for.
    private String
    Last direction P1 moved.
    private String
    Last direction P2 moved.
    current Main menu scene
    private boolean
    Lock that prevents repeated key presses while an animation is playing.
    private String
    Class of P1 used to load sprites in the View.
    private boolean
    Whether it is P1's turn to move.
    private String
    Class of P2 used to load sprites in the View.
    private final javafx.stage.Stage
    The main JavaFX Stage of the application.
  • Constructor Summary

    Constructors
    Constructor
    Description
    GameController(javafx.stage.Stage stage)
    Creates a GameController and registers a listener with the GameManager.
  • Method Summary

    Modifier and Type
    Method
    Description
    private String
    Converts an entity to its class name string for sprite loading in the View.
    void
    Initialises the application, configures the Stage, and shows the Main Menu.
    private String[]
    itemNames(Player player)
    Converts a player's inventory to an array of item name strings for display in the View.
    void
    Receives the result of each battle turn, plays the appropriate animation, then updates the HP bars after the animation finishes.
    void
    onBattleStart(Entity entityA, Entity entityB)
    Receives a battle-start event, creates the BattleScene, and shows action buttons.
    private void
    Configures the map phase screen when entering MAP_PHASE.
    void
    Receives a game-over event, shows the GameOverScene, and binds its buttons.
    void
    Receives an item-pickup event and updates the inventory panel and map.
    void
    onPlayerMoved(Player player, int row, int col)
    Receives a player-moved event, updates the animation on the BoardScene, and switches the turn.
    void
    onRoundEnd(int round)
    Receives a round-end event on the map, updates stats and the round counter.
    void
    Receives a state-change event and switches to the appropriate screen.
    void
    onTomeApplied(Player player, String effectDesc)
    Receives a tome-applied event after the player defeats a Goblin, updates stats, and shows a notification.
    void
    onZoneShrunk(int newRadius)
    Receives a zone-shrink event, redraws the map, and shows a notification.
    private void
    pause(double seconds, Runnable onFinished)
    Waits for the specified delay then runs the given Runnable.
    private void
    Updates the HP bars and inventories of both players on the BoardScene.
    private void
    Binds battle action buttons for side A (Player 1, or the side-A entity in PvP).
    private void
    Binds battle action buttons for side B (PvP only).
    private void
    Sets up keyboard listeners on the BoardScene.
    private void
    Shows the character selection screen and sets the callback for when selection is complete.
    private void
    Shows the Main Menu screen and binds the Start button.
    private void
    stopBgm(javafx.scene.media.MediaPlayer player)
    Safely stops BGM playback without throwing an exception if the player is null.
    private void
    Submits side A's action to the GameManager.
    toType(String name)
    Converts a class name string to the corresponding CharacterType enum value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • stage

      private final javafx.stage.Stage stage
      The main JavaFX Stage of the application.
    • gm

      private GameManager gm
      GameManager that controls all game logic.
    • board

      private BoardScene board
      Current map phase screen (null if not yet created).
    • battle

      private BattleScene battle
      Current battle phase screen (null if not in battle).
    • p1Class

      private String p1Class
      Class of P1 used to load sprites in the View.
    • p2Class

      private String p2Class
      Class of P2 used to load sprites in the View.
    • p1TurnToMove

      private boolean p1TurnToMove
      Whether it is P1's turn to move.
    • lastP1Dir

      private String lastP1Dir
      Last direction P1 moved.
    • lastP2Dir

      private String lastP2Dir
      Last direction P2 moved.
    • isPvP

      private boolean isPvP
      Whether the current battle is PvP, used to determine if P2's input must be waited for.
    • moveLock

      private boolean moveLock
      Lock 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

      public void onStateChanged(GameState state)
      Receives a state-change event and switches to the appropriate screen.
      Specified by:
      onStateChanged in interface GameEventListener
      Parameters:
      state - The new game state
    • onPlayerMoved

      public void onPlayerMoved(Player player, int row, int col)
      Receives a player-moved event, updates the animation on the BoardScene, and switches the turn.
      Specified by:
      onPlayerMoved in interface GameEventListener
      Parameters:
      player - The player who moved
      row - New row
      col - New column
    • onItemPickedUp

      public void onItemPickedUp(Player player)
      Receives an item-pickup event and updates the inventory panel and map.
      Specified by:
      onItemPickedUp in interface GameEventListener
      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:
      onRoundEnd in interface GameEventListener
      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:
      onZoneShrunk in interface GameEventListener
      Parameters:
      newRadius - New radius of the safe zone
    • onTomeApplied

      public void onTomeApplied(Player player, String effectDesc)
      Receives a tome-applied event after the player defeats a Goblin, updates stats, and shows a notification.
      Specified by:
      onTomeApplied in interface GameEventListener
      Parameters:
      player - The player who received the tome
      effectDesc - Description of the stat boost
    • onBattleStart

      public void onBattleStart(Entity entityA, Entity entityB)
      Receives a battle-start event, creates the BattleScene, and shows action buttons.
      Specified by:
      onBattleStart in interface GameEventListener
      Parameters:
      entityA - Entity on side A
      entityB - Entity on side B
    • onBattleResult

      public void onBattleResult(BattleResult result)
      Receives the result of each battle turn, plays the appropriate animation, then updates the HP bars after the animation finishes.
      Specified by:
      onBattleResult in interface GameEventListener
      Parameters:
      result - Result of this turn
    • onGameOver

      public void onGameOver(Player winner)
      Receives a game-over event, shows the GameOverScene, and binds its buttons.
      Specified by:
      onGameOver in interface GameEventListener
      Parameters:
      winner - The winner, or null in 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

      private void submitA(BattleAction action)
      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

      private String classOf(Entity e)
      Converts an entity to its class name string for sprite loading in the View.
      Parameters:
      e - Entity to convert
      Returns:
      Class name as a String, e.g. "knight", "goblin"
    • itemNames

      private String[] itemNames(Player player)
      Converts a player's inventory to an array of item name strings for display in the View.
      Parameters:
      player - Player whose inventory to convert
      Returns:
      Array of item names (length equals maxInventorySize; empty slots are "")
    • toType

      private CharacterType toType(String name)
      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

      private void pause(double seconds, Runnable onFinished)
      Waits for the specified delay then runs the given Runnable.
      Parameters:
      seconds - Delay duration in seconds
      onFinished - 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