Class BattleScene

java.lang.Object
application.BattleScene

public class BattleScene extends Object
Battle phase screen displaying sprites for both sides, HP bars, and action buttons. GameController calls methods here to play animations and update the UI after each turn.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private javafx.scene.layout.HBox
    HBox holding the action buttons at the bottom
    private javafx.scene.media.AudioClip
    Sound effect played on attack
    private javafx.scene.control.Button
    Attack Button
    private javafx.scene.text.Text
    Battle log text displayed at the center of the screen
    private javafx.scene.image.Image
    background battle scene
    private javafx.scene.media.MediaPlayer
    BGM media player for the battle
    private javafx.scene.control.Button
    Defend button
    private final double
    X position of fighter 1's sprite
    private final double
    Y position of fighter 1's sprite
    private int
    Attack power of fighter 1
    private String
    Class of fighter 1, e.g.
    private int
    Defense of fighter 1
    private double
    Current HP of fighter 1
    private javafx.scene.control.ProgressBar
    HP bar for fighter 1.
    private javafx.scene.image.ImageView
    Sprite for fighter 1
    private double
    Maximum HP of fighter 1
    private String
    fighter 1 name
    private javafx.scene.text.Text
    Stat text for fighter 1
    private final double
    X position of fighter 2's sprite
    private final double
    Y position of fighter 2's sprite
    private int
    Attack power of fighter 2
    private String
    Class of fighter 2, e.g.
    private int
    Defense of fighter 2
    private double
    Current HP of fighter 2
    private javafx.scene.control.ProgressBar
    HP bar for fighter 2.
    private javafx.scene.image.ImageView
    Sprite for fighter 2
    private double
    Maximum HP of fighter 2
    private String
    Fighter 2 name
    private javafx.scene.text.Text
    Stat text for fighter 2
    private javafx.scene.image.Image
    Inventory slot image
    private javafx.scene.control.Button
    Use Item button
    private javafx.scene.layout.StackPane
    Root container.
    private javafx.scene.media.AudioClip
    Sound effect played when drinking a potion
    private javafx.scene.layout.BorderPane
    Main layout.
    private javafx.scene.Scene
    Main JavaFX Scene for this screen.
    private javafx.scene.control.Button
    Use Skill button
    private javafx.scene.text.Text
    Text indicating whose turn it is
    private javafx.scene.image.Image
    VS image displayed at the center of the screen
  • Constructor Summary

    Constructors
    Constructor
    Description
    BattleScene(String f1Name, String f1Class, double f1Hp, double f1MaxHp, int f1Atk, int f1Def, String f2Name, String f2Class, double f2Hp, double f2MaxHp, int f2Atk, int f2Def)
    Creates the Battle Scene with data for both fighters.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Builds all UI components for the battle screen.
    private String
    charImg(String cls, String action)
    Resolves the resource path for a character sprite based on class name and action.
    private javafx.scene.layout.HBox
    Creates the action menu container with all 4 buttons (attack, defend, item, skill).
    private javafx.scene.layout.StackPane
    Creates the battle log box that displays the result of the latest turn.
    private javafx.scene.layout.Pane
    Creates the center pane containing sprites for both fighters facing each other.
    private javafx.scene.layout.HBox
    createFighterProfile(String name, String cls, double hp, double maxHp, int atk, int def, boolean isLeft)
    Creates a profile card for one fighter in the top panel.
    private javafx.scene.control.Button
    Creates an image-based button with a hover effect.
    private javafx.scene.layout.BorderPane
    Creates the top panel showing HP bars and stats for both fighters.
    void
    Hides all action buttons while an animation is playing.
    javafx.scene.control.Button
     
    javafx.scene.media.MediaPlayer
     
    javafx.scene.control.Button
     
    javafx.scene.control.Button
     
    javafx.scene.Scene
     
    javafx.scene.control.Button
     
    private void
    Loads background and UI images.
    private void
    Loads sound effects and BGM.
    void
    openItemSelectionMenu(String[] items, Consumer<Integer> onItemClicked)
    Switches the action menu to display item selection slots from the inventory.
    void
    playActionAnimation(boolean isF1, String actionType, String attackerName, String defenderName, int dmgAmount, Runnable onFinished)
    Plays a normal attack animation: swaps the sprite, shakes the target, then invokes the callback when done.
    void
    Displays a message indicating the fighter is taking a defensive stance.
    void
    playItemAnimation(boolean isF1, String itemName, Runnable onFinished)
    Plays an item-use animation: switches to the potion-drinking pose, then invokes the callback.
    void
    playSkillAnimation(boolean isF1, String skillName, String attackerName, String defenderName, int dmgAmount, Runnable onFinished)
    Plays a skill animation, handling attack skills and heal skills separately.
    void
    Sets the battle log text.
    void
    setTurnIndicator(boolean isF1, String name)
    Updates the turn indicator text with the appropriate color for the active side.
    void
    Shows all 4 main action buttons.
    void
    updateHP(boolean isF1, double hp, double maxHp, int atk, int def)
    Updates the HP bar and stat text for the specified fighter.

    Methods inherited from class java.lang.Object

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

    • F1_POS_X

      private final double F1_POS_X
      X position of fighter 1's sprite
      See Also:
    • F1_POS_Y

      private final double F1_POS_Y
      Y position of fighter 1's sprite
      See Also:
    • F2_POS_X

      private final double F2_POS_X
      X position of fighter 2's sprite
      See Also:
    • F2_POS_Y

      private final double F2_POS_Y
      Y position of fighter 2's sprite
      See Also:
    • scene

      private javafx.scene.Scene scene
      Main JavaFX Scene for this screen.
    • mainRoot

      private javafx.scene.layout.StackPane mainRoot
      Root container.
    • rootPane

      private javafx.scene.layout.BorderPane rootPane
      Main layout.
    • actionMenuBox

      private javafx.scene.layout.HBox actionMenuBox
      HBox holding the action buttons at the bottom
    • f1HpBar

      private javafx.scene.control.ProgressBar f1HpBar
      HP bar for fighter 1.
    • f2HpBar

      private javafx.scene.control.ProgressBar f2HpBar
      HP bar for fighter 2.
    • f1StatText

      private javafx.scene.text.Text f1StatText
      Stat text for fighter 1
    • f2StatText

      private javafx.scene.text.Text f2StatText
      Stat text for fighter 2
    • f1ImgView

      private javafx.scene.image.ImageView f1ImgView
      Sprite for fighter 1
    • f2ImgView

      private javafx.scene.image.ImageView f2ImgView
      Sprite for fighter 2
    • battleLogText

      private javafx.scene.text.Text battleLogText
      Battle log text displayed at the center of the screen
    • turnIndicatorText

      private javafx.scene.text.Text turnIndicatorText
      Text indicating whose turn it is
    • attackBtn

      private javafx.scene.control.Button attackBtn
      Attack Button
    • skillBtn

      private javafx.scene.control.Button skillBtn
      Use Skill button
    • itemBtn

      private javafx.scene.control.Button itemBtn
      Use Item button
    • defendBtn

      private javafx.scene.control.Button defendBtn
      Defend button
    • f1Name

      private String f1Name
      fighter 1 name
    • f1Class

      private String f1Class
      Class of fighter 1, e.g. "knight", "archer"
    • f1Hp

      private double f1Hp
      Current HP of fighter 1
    • f1MaxHp

      private double f1MaxHp
      Maximum HP of fighter 1
    • f1Atk

      private int f1Atk
      Attack power of fighter 1
    • f1Def

      private int f1Def
      Defense of fighter 1
    • f2Name

      private String f2Name
      Fighter 2 name
    • f2Class

      private String f2Class
      Class of fighter 2, e.g. "goblin", "alien"
    • f2Hp

      private double f2Hp
      Current HP of fighter 2
    • f2MaxHp

      private double f2MaxHp
      Maximum HP of fighter 2
    • f2Atk

      private int f2Atk
      Attack power of fighter 2
    • f2Def

      private int f2Def
      Defense of fighter 2
    • bgImg

      private javafx.scene.image.Image bgImg
      background battle scene
    • vsImg

      private javafx.scene.image.Image vsImg
      VS image displayed at the center of the screen
    • inventoryImg

      private javafx.scene.image.Image inventoryImg
      Inventory slot image
    • atkSound

      private javafx.scene.media.AudioClip atkSound
      Sound effect played on attack
    • potionSound

      private javafx.scene.media.AudioClip potionSound
      Sound effect played when drinking a potion
    • bgmPlayer

      private javafx.scene.media.MediaPlayer bgmPlayer
      BGM media player for the battle
  • Constructor Details

    • BattleScene

      public BattleScene(String f1Name, String f1Class, double f1Hp, double f1MaxHp, int f1Atk, int f1Def, String f2Name, String f2Class, double f2Hp, double f2MaxHp, int f2Atk, int f2Def)
      Creates the Battle Scene with data for both fighters.
      Parameters:
      f1Name - Name of fighter 1
      f1Class - Class of fighter 1
      f1Hp - Current HP of fighter 1
      f1MaxHp - Maximum HP of fighter 1
      f1Atk - ATK of fighter 1
      f1Def - DEF of fighter 1
      f2Name - Name of fighter 2
      f2Class - Class of fighter 2
      f2Hp - Current HP of fighter 2
      f2MaxHp - Maximum HP of fighter 2
      f2Atk - ATK of fighter 2
      f2Def - DEF of fighter 2
  • Method Details

    • buildUI

      private void buildUI()
      Builds all UI components for the battle screen.
    • loadImages

      private void loadImages()
      Loads background and UI images.
    • loadSounds

      private void loadSounds()
      Loads sound effects and BGM.
    • charImg

      private String charImg(String cls, String action)
      Resolves the resource path for a character sprite based on class name and action.
      Parameters:
      cls - Class name
      action - Action name, e.g. "stand", "atk", "skill"
      Returns:
      Resource path for the image
    • createTopPanel

      private javafx.scene.layout.BorderPane createTopPanel()
      Creates the top panel showing HP bars and stats for both fighters.
      Returns:
      The top panel as a BorderPane
    • createFighterProfile

      private javafx.scene.layout.HBox createFighterProfile(String name, String cls, double hp, double maxHp, int atk, int def, boolean isLeft)
      Creates a profile card for one fighter in the top panel.
      Parameters:
      name - Display name
      cls - Character class
      hp - Current HP
      maxHp - Maximum HP
      atk - Attack power
      def - Defense
      isLeft - true if this is fighter 1 (left side)
      Returns:
      The profile card as an HBox
    • createCenterBattle

      private javafx.scene.layout.Pane createCenterBattle()
      Creates the center pane containing sprites for both fighters facing each other.
      Returns:
      The center pane with both sprites
    • createBattleLog

      private javafx.scene.layout.StackPane createBattleLog()
      Creates the battle log box that displays the result of the latest turn.
      Returns:
      The battle log as a StackPane
    • createActionMenuContainer

      private javafx.scene.layout.HBox createActionMenuContainer()
      Creates the action menu container with all 4 buttons (attack, defend, item, skill).
      Returns:
      The action menu as an HBox
    • createImgBtn

      private javafx.scene.control.Button createImgBtn(String path)
      Creates an image-based button with a hover effect.
      Parameters:
      path - Resource path for the button image
      Returns:
      A Button using an image instead of text
    • getScene

      public javafx.scene.Scene getScene()
      Returns:
      The Scene for the battle screen
    • getBgmPlayer

      public javafx.scene.media.MediaPlayer getBgmPlayer()
      Returns:
      The BGM MediaPlayer for the battle
    • getAttackBtn

      public javafx.scene.control.Button getAttackBtn()
      Returns:
      The Attack button
    • getDefendBtn

      public javafx.scene.control.Button getDefendBtn()
      Returns:
      Defend button
    • getItemBtn

      public javafx.scene.control.Button getItemBtn()
      Returns:
      Use Item button
    • getSkillBtn

      public javafx.scene.control.Button getSkillBtn()
      Returns:
      Use Skill button
    • showMainActionMenu

      public void showMainActionMenu()
      Shows all 4 main action buttons.
    • disableAllActionButtons

      public void disableAllActionButtons()
      Hides all action buttons while an animation is playing.
    • setTurnIndicator

      public void setTurnIndicator(boolean isF1, String name)
      Updates the turn indicator text with the appropriate color for the active side.
      Parameters:
      isF1 - true if it is fighter 1's turn
      name - Name of the active fighter
    • setLogText

      public void setLogText(String msg)
      Sets the battle log text.
      Parameters:
      msg - Message to display
    • updateHP

      public void updateHP(boolean isF1, double hp, double maxHp, int atk, int def)
      Updates the HP bar and stat text for the specified fighter.
      Parameters:
      isF1 - true to update fighter 1
      hp - Current HP
      maxHp - Maximum HP
      atk - Attack power
      def - Defense
    • playDefendEffect

      public void playDefendEffect(String name)
      Displays a message indicating the fighter is taking a defensive stance.
      Parameters:
      name - Name of the defending fighter
    • playActionAnimation

      public void playActionAnimation(boolean isF1, String actionType, String attackerName, String defenderName, int dmgAmount, Runnable onFinished)
      Plays a normal attack animation: swaps the sprite, shakes the target, then invokes the callback when done.
      Parameters:
      isF1 - true if fighter 1 is attacking
      actionType - Action name shown in the log
      attackerName - Name of the attacker
      defenderName - Name of the defender
      dmgAmount - Damage dealt
      onFinished - Callback invoked after the animation finishes
    • playSkillAnimation

      public void playSkillAnimation(boolean isF1, String skillName, String attackerName, String defenderName, int dmgAmount, Runnable onFinished)
      Plays a skill animation, handling attack skills and heal skills separately.
      Parameters:
      isF1 - true if fighter 1 is using the skill
      skillName - Name of the skill
      attackerName - Name of the skill user
      defenderName - Name of the target
      dmgAmount - Damage dealt (ignored for heal skills)
      onFinished - Callback invoked after the animation finishes
    • playItemAnimation

      public void playItemAnimation(boolean isF1, String itemName, Runnable onFinished)
      Plays an item-use animation: switches to the potion-drinking pose, then invokes the callback.
      Parameters:
      isF1 - true if fighter 1 is using the item
      itemName - Name of the item
      onFinished - Callback invoked after the animation finishes
    • openItemSelectionMenu

      public void openItemSelectionMenu(String[] items, Consumer<Integer> onItemClicked)
      Switches the action menu to display item selection slots from the inventory. If the inventory is empty, a warning message is shown instead.
      Parameters:
      items - Array of item names in the inventory
      onItemClicked - Callback that receives the index of the selected item