Code Sketch


Bixuu nfernoCoreElementalAwakeningEvent06
By: Mhalsakant School
Category: Programming
import java.awt.BorderLayout
import java.awt.CardLayout
import java.awt.Color
import java.awt.Dimension
import java.awt.Font
import java.awt.GradientPaint
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.RenderingHints
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.awt.geom.Ellipse2D
import java.awt.geom.RoundRectangle2D
import javax.swing.AbstractAction
import javax.swing.BorderFactory
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField
import javax.swing.KeyStroke
import javax.swing.SwingConstants
import javax.swing.SwingUtilities
import javax.swing.Timer
import javax.swing.WindowConstants
import java.util.Random
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object InfernoCoreElementalAwakeningEvent06 {
  def main(args: Array[String]): Unit = {
    SwingUtilities.invokeLater(new Runnable {
      override def run(): Unit = {
        val app = new InfernoCoreApp()
        app.showWindow()
      }
    })
  }

  private class InfernoCoreApp {
    private val frame = new JFrame("TITANVERSE: AGE OF LEGENDS - INFERNO CORE")
    private val cards = new CardLayout()
    private val root = new JPanel(cards)
    private val nameField = new JTextField("Element Hero", 18)
    private val loginStatus = new JLabel("ENTER YOUR NAME TO AWAKEN THE CORE", SwingConstants.CENTER)
    private var playerName = "Element Hero"
    private var selectedElement = "FIRE"
    private var gamePanel: InfernoGamePanel = null

    def showWindow(): Unit = {
      root.add(buildLoginPage(), "LOGIN")
      root.add(buildMenuPage(), "MENU")
      root.add(buildElementPage(), "ELEMENTS")
      root.add(buildControlsPage(), "CONTROLS")
      frame.setContentPane(root)
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
      frame.addWindowListener(new WindowAdapter {
        override def windowClosing(e: WindowEvent): Unit = {
          if (gamePanel != null) gamePanel.stopGame()
        }
      })
      frame.setMinimumSize(new Dimension(960, 680))
      frame.setSize(1040, 730)
      frame.setLocationRelativeTo(null)
      frame.setVisible(true)
      cards.show(root, "LOGIN")
    }

    private def makeButton(text: String): JButton = {
      val b = new JButton(text)
      b.setFont(new Font("SansSerif", Font.BOLD, 15))
      b.setForeground(new Color(255, 247, 226))
      b.setBackground(new Color(77, 42, 34))
      b.setFocusPainted(false)
      b.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createLineBorder(new Color(255, 156, 70), 2),
        BorderFactory.createEmptyBorder(10, 18, 10, 18)
      ))
      b.setAlignmentX(0.5f)
      b
    }

    private def heading(text: String, size: Int): JLabel = {
      val label = new JLabel(text, SwingConstants.CENTER)
      label.setFont(new Font("SansSerif", Font.BOLD, size))
      label.setForeground(new Color(255, 225, 185))
      label.setAlignmentX(0.5f)
      label
    }

    private def pagePanel(): JPanel = {
      val panel = new JPanel()
      panel.setBackground(new Color(25, 17, 24))
      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS))
      panel.setBorder(BorderFactory.createEmptyBorder(26, 40, 28, 40))
      panel
    }

    private def buildLoginPage(): JPanel = {
      val panel = pagePanel()
      panel.add(Box.createVerticalGlue())
      panel.add(heading("TITANVERSE: AGE OF LEGENDS", 27))
      panel.add(Box.createVerticalStrut(12))
      panel.add(heading("INFERNO CORE: ELEMENTAL AWAKENING", 22))
      panel.add(Box.createVerticalStrut(12))
      val desc = new JLabel("MASTER FIRE, WATER, EARTH AND STORM", SwingConstants.CENTER)
      desc.setForeground(new Color(255, 184, 119))
      desc.setFont(new Font("SansSerif", Font.BOLD, 14))
      desc.setAlignmentX(0.5f)
      panel.add(desc)
      panel.add(Box.createVerticalStrut(35))
      nameField.setMaximumSize(new Dimension(330, 42))
      nameField.setFont(new Font("SansSerif", Font.PLAIN, 18))
      nameField.setHorizontalAlignment(SwingConstants.CENTER)
      panel.add(nameField)
      panel.add(Box.createVerticalStrut(16))
      val enter = makeButton("AWAKEN YOUR JOURNEY")
      enter.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = {
          val typed = nameField.getText.trim
          if (typed.length == 0) loginStatus.setText("PLEASE ENTER A HERO NAME")
          else {
            playerName = typed.take(22)
            loginStatus.setText("WELCOME, " + playerName.toUpperCase)
            cards.show(root, "MENU")
          }
        }
      })
      panel.add(enter)
      panel.add(Box.createVerticalStrut(14))
      loginStatus.setForeground(new Color(255, 203, 145))
      loginStatus.setAlignmentX(0.5f)
      panel.add(loginStatus)
      panel.add(Box.createVerticalGlue())
      panel
    }

    private def buildMenuPage(): JPanel = {
      val panel = pagePanel()
      panel.add(Box.createVerticalGlue())
      panel.add(heading("INFERNO CORE", 34))
      panel.add(Box.createVerticalStrut(8))
      panel.add(heading("ELEMENTAL AWAKENING", 22))
      panel.add(Box.createVerticalStrut(12))
      val welcome = new JLabel("HERO: " + playerName, SwingConstants.CENTER)
      welcome.setForeground(new Color(255, 190, 130))
      welcome.setAlignmentX(0.5f)
      panel.add(welcome)
      panel.add(Box.createVerticalStrut(28))
      val play = makeButton("START ELEMENTAL EXPEDITION")
      play.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = launchGame()
      })
      val choose = makeButton("CHOOSE YOUR ELEMENT")
      choose.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = cards.show(root, "ELEMENTS")
      })
      val controls = makeButton("HOW TO PLAY")
      controls.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = cards.show(root, "CONTROLS")
      })
      val logout = makeButton("CHANGE HERO NAME")
      logout.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = cards.show(root, "LOGIN")
      })
      panel.add(play)
      panel.add(Box.createVerticalStrut(12))
      panel.add(choose)
      panel.add(Box.createVerticalStrut(12))
      panel.add(controls)
      panel.add(Box.createVerticalStrut(12))
      panel.add(logout)
      panel.add(Box.createVerticalGlue())
      panel
    }

    private def buildElementPage(): JPanel = {
      val panel = pagePanel()
      panel.add(heading("CHOOSE YOUR ELEMENTAL POWER", 25))
      panel.add(Box.createVerticalStrut(18))
      val options = new JPanel(new java.awt.GridLayout(2, 2, 14, 14))
      options.setOpaque(false)
      val choices = Seq(
        ("FIRE - INFERNO BURST", "FIRE"),
        ("WATER - TIDAL NOVA", "WATER"),
        ("EARTH - TITAN AEGIS", "EARTH"),
        ("STORM - THUNDER ARC", "STORM")
      )
      choices.foreach { item =>
        val button = makeButton(item._1)
        button.addActionListener(new ActionListener {
          override def actionPerformed(e: ActionEvent): Unit = {
            selectedElement = item._2
            cards.show(root, "MENU")
          }
        })
        options.add(button)
      }
      panel.add(options)
      panel.add(Box.createVerticalStrut(18))
      val back = makeButton("BACK TO MENU")
      back.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = cards.show(root, "MENU")
      })
      panel.add(back)
      panel.add(Box.createVerticalGlue())
      panel
    }

    private def buildControlsPage(): JPanel = {
      val panel = pagePanel()
      panel.add(heading("HOW TO PLAY", 28))
      panel.add(Box.createVerticalStrut(22))
      val lines = Seq(
        "W A S D / ARROW KEYS  -  MOVE YOUR HERO",
        "MOUSE CLICK  -  MOVE TO A DESTINATION",
        "F  -  CAST YOUR SELECTED ELEMENTAL POWER",
        "SPACE  -  ELEMENTAL DASH",
        "Q  -  TITAN SHIELD",
        "E  -  COLLECT CORES OR ACTIVATE A GATE",
        "P  -  PAUSE / RESUME",
        "R  -  RESTART AFTER A LOST TRIAL",
        "B  -  RETURN TO THE MENU"
      )
      lines.foreach { line =>
        val label = new JLabel(line, SwingConstants.CENTER)
        label.setForeground(new Color(255, 218, 183))
        label.setFont(new Font("Monospaced", Font.BOLD, 15))
        label.setAlignmentX(0.5f)
        panel.add(label)
        panel.add(Box.createVerticalStrut(12))
      }
      val back = makeButton("BACK TO MENU")
      back.addActionListener(new ActionListener {
        override def actionPerformed(e: ActionEvent): Unit = cards.show(root, "MENU")
      })
      panel.add(Box.createVerticalStrut(12))
      panel.add(back)
      panel.add(Box.createVerticalGlue())
      panel
    }

    private def launchGame(): Unit = {
      if (gamePanel != null) {
        gamePanel.stopGame()
        root.remove(gamePanel)
      }
      gamePanel = new InfernoGamePanel(playerName, selectedElement, new Runnable {
        override def run(): Unit = cards.show(root, "MENU")
      })
      root.add(gamePanel, "GAME")
      cards.show(root, "GAME")
      gamePanel.startGame()
    }
  }

  private class InfernoGamePanel(heroName: String, powerName: String, backAction: Runnable) extends JPanel(new BorderLayout()) {
    private case class Core(var x: Double, var y: Double, var taken: Boolean)
    private case class Wisp(var x: Double, var y: Double, var hp: Int, var speed: Double)

    private val rng = new Random()
    private val keys = mutable.Set[Int]()
    private val cores = ArrayBuffer[Core]()
    private val wisps = ArrayBuffer[Wisp]()
    private val world = new WorldCanvas()
    private val status = new JLabel("PREPARE FOR THE ELEMENTAL TRIAL", SwingConstants.CENTER)
    private val timer = new Timer(30, new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = updateGame()
    })

    private var element = powerName
    private var heroX = 105.0
    private var heroY = 330.0
    private var targetX = heroX
    private var targetY = heroY
    private var hasTarget = false
    private var stage = 1
    private var collected = 0
    private var totalCores = 0
    private var credits = 0
    private var health = 100
    private var energy = 100
    private var lives = 3
    private var tick = 0
    private var powerCooldown = 0
    private var dashTicks = 0
    private var shieldTicks = 0
    private var damageCooldown = 0
    private var bossActive = false
    private var bossHealth = 5
    private var gateOpen = false
    private var paused = false
    private var finished = false
    private var lost = false
    private var message = "COLLECT 4 ELEMENTAL CORES TO OPEN THE GATE"

    private val realmNames = Seq("EMBER CRATER", "TIDAL GROTTO", "TERRA GROVE", "STORM CITADEL")

    setBackground(new Color(18, 14, 22))
    add(world, BorderLayout.CENTER)
    status.setFont(new Font("SansSerif", Font.BOLD, 13))
    status.setForeground(new Color(255, 218, 170))
    status.setOpaque(true)
    status.setBackground(new Color(33, 21, 27))
    status.setBorder(BorderFactory.createEmptyBorder(7, 8, 7, 8))
    add(status, BorderLayout.NORTH)

    private val actions = new JPanel(new java.awt.GridLayout(1, 6, 8, 0))
    actions.setBackground(new Color(29, 20, 25))
    actions.setBorder(BorderFactory.createEmptyBorder(8, 10, 8, 10))
    private val powerButton = gameButton("F - POWER")
    private val dashButton = gameButton("SPACE - DASH")
    private val shieldButton = gameButton("Q - SHIELD")
    private val pauseButton = gameButton("P - PAUSE")
    private val restartButton = gameButton("R - RETRY")
    private val backButton = gameButton("B - MENU")
    actions.add(powerButton)
    actions.add(dashButton)
    actions.add(shieldButton)
    actions.add(pauseButton)
    actions.add(restartButton)
    actions.add(backButton)
    add(actions, BorderLayout.SOUTH)

    powerButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = castPower() })
    dashButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = dash() })
    shieldButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = useShield() })
    pauseButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = togglePause() })
    restartButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = restartGame() })
    backButton.addActionListener(new ActionListener { override def actionPerformed(e: ActionEvent): Unit = goBack() })

    private def gameButton(text: String): JButton = {
      val b = new JButton(text)
      b.setFont(new Font("SansSerif", Font.BOLD, 12))
      b.setForeground(new Color(255, 240, 218))
      b.setBackground(new Color(92, 48, 35))
      b.setFocusPainted(false)
      b.setBorder(BorderFactory.createLineBorder(new Color(246, 157, 74), 2))
      b
    }

    def startGame(): Unit = {
      spawnStage()
      timer.start()
      world.requestFocusInWindow()
    }

    def stopGame(): Unit = timer.stop()

    private def goBack(): Unit = {
      timer.stop()
      backAction.run()
    }

    private def keyCodeFor(keyText: String): Int = {
      keyText match {
        case "W" => KeyEvent.VK_W
        case "A" => KeyEvent.VK_A
        case "S" => KeyEvent.VK_S
        case "D" => KeyEvent.VK_D
        case "UP" => KeyEvent.VK_UP
        case "DOWN" => KeyEvent.VK_DOWN
        case "LEFT" => KeyEvent.VK_LEFT
        case "RIGHT" => KeyEvent.VK_RIGHT
        case "SPACE" => KeyEvent.VK_SPACE
        case "F" => KeyEvent.VK_F
        case "Q" => KeyEvent.VK_Q
        case "E" => KeyEvent.VK_E
        case "P" => KeyEvent.VK_P
        case "B" => KeyEvent.VK_B
        case "R" => KeyEvent.VK_R
        case _ => 0
      }
    }

    private def bindKey(keyText: String, actionName: String, pressed: Boolean): Unit = {
      val stroke = KeyStroke.getKeyStroke((if (pressed) "pressed " else "released ") + keyText)
      world.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, actionName)
      world.getActionMap().put(actionName, new AbstractAction {
        override def actionPerformed(e: ActionEvent): Unit = {
          val code = keyCodeFor(keyText)
          if (pressed && (code == KeyEvent.VK_W || code == KeyEvent.VK_A || code == KeyEvent.VK_S || code == KeyEvent.VK_D || code == KeyEvent.VK_UP || code == KeyEvent.VK_DOWN || code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT)) keys += code
          else if (!pressed) keys -= code
          if (keyText == "SPACE" && pressed) dash()
          if (keyText == "F" && pressed) castPower()
          if (keyText == "Q" && pressed) useShield()
          if (keyText == "E" && pressed) interact()
          if (keyText == "P" && pressed) togglePause()
          if (keyText == "B" && pressed) goBack()
          if (keyText == "R" && pressed && (finished || lost)) restartGame()
        }
      })
    }

    private def installKeys(): Unit = {
      val names = Seq("W", "A", "S", "D", "UP", "DOWN", "LEFT", "RIGHT", "SPACE", "F", "Q", "E", "P", "B", "R")
      names.foreach { key =>
        bindKey(key, "down_" + key, true)
        bindKey(key, "up_" + key, false)
      }
    }

    installKeys()
    world.addMouseListener(new MouseAdapter {
      override def mousePressed(e: MouseEvent): Unit = {
        if (!paused && !finished && !lost) {
          targetX = e.getX.toDouble
          targetY = e.getY.toDouble
          hasTarget = true
        }
      }
    })

    private def spawnStage(): Unit = {
      cores.clear()
      wisps.clear()
      collected = 0
      gateOpen = false
      bossActive = false
      bossHealth = 5
      heroX = 90.0
      heroY = 360.0
      targetX = heroX
      targetY = heroY
      hasTarget = false
      for (i <- 0 until 7) {
        val x = 155 + rng.nextInt(650)
        val y = 135 + rng.nextInt(345)
        cores += Core(x.toDouble, y.toDouble, false)
      }
      for (i <- 0 until (3 + stage)) {
        val x = 290 + rng.nextInt(560)
        val y = 120 + rng.nextInt(390)
        wisps += Wisp(x.toDouble, y.toDouble, 2, 0.85 + stage * 0.12)
      }
      message = "COLLECT 4 ELEMENTAL CORES - THEN REACH THE GATE"
      status.setText("HERO: " + heroName.toUpperCase + "   |   ELEMENT: " + element + "   |   " + realmNames(stage - 1))
    }

    private def togglePause(): Unit = {
      if (!finished && !lost) {
        paused = !paused
        pauseButton.setText(if (paused) "RESUME" else "P - PAUSE")
        message = if (paused) "EXPEDITION PAUSED" else "THE ELEMENTS AWAIT"
        world.repaint()
      }
    }

    private def dash(): Unit = {
      if (!paused && !finished && !lost && energy >= 12 && dashTicks <= 0) {
        energy -= 12
        dashTicks = 10
        message = "ELEMENTAL DASH!"
      }
    }

    private def useShield(): Unit = {
      if (!paused && !finished && !lost && energy >= 25 && shieldTicks <= 0) {
        energy -= 25
        shieldTicks = 115
        message = "TITAN AEGIS ACTIVE!"
      }
    }

    private def distance(ax: Double, ay: Double, bx: Double, by: Double): Double = {
      val dx = ax - bx
      val dy = ay - by
      math.sqrt(dx * dx + dy * dy)
    }

    private def castPower(): Unit = {
      if (paused || finished || lost) return
      if (powerCooldown > 0 || energy < 22) {
        message = "POWER RECHARGING - COLLECT CORES"
        return
      }
      energy -= 22
      powerCooldown = 38
      val range = if (element == "STORM") 235.0 else 180.0
      var hits = 0
      if (bossActive && distance(heroX, heroY, 815.0, 300.0) < range + 60.0) {
        bossHealth -= (if (element == "STORM") 2 else 1)
        hits += 1
        if (bossHealth <= 0) {
          bossActive = false
          gateOpen = true
          message = "INFINITY GUARDIAN DEFEATED - REACH THE CORE GATE"
        } else message = element + " POWER HIT THE GUARDIAN!"
      }
      val removeList = ArrayBuffer[Wisp]()
      wisps.foreach { w =>
        if (distance(heroX, heroY, w.x, w.y) < range) {
          w.hp -= (if (element == "FIRE") 2 else 1)
          if (element == "WATER") {
            val dx = w.x - heroX
            val dy = w.y - heroY
            w.x += dx * 0.45
            w.y += dy * 0.45
          }
          if (w.hp <= 0) removeList += w
          hits += 1
        }
      }
      removeList.foreach(w => wisps -= w)
      if (element == "EARTH") shieldTicks = math.max(shieldTicks, 55)
      if (element == "WATER") health = math.min(100, health + 8)
      if (element == "STORM") energy = math.min(100, energy + 5)
      if (hits == 0) message = element + " POWER RELEASED!"
      else message = element + " POWER HIT " + hits + " TARGET(S)!"
    }

    private def interact(): Unit = {
      if (paused || finished || lost) return
      val gateX = world.getWidth - 95.0
      val gateY = world.getHeight * 0.52
      if (distance(heroX, heroY, gateX, gateY) < 95.0) {
        if (!gateOpen) message = "THE GATE NEEDS 4 CORES"
        else if (stage < 4) {
          stage += 1
          spawnStage()
          message = "WELCOME TO " + realmNames(stage - 1)
        } else {
          finished = true
          message = "INFERNO CORE AWAKENED - LEGENDARY VICTORY!"
        }
      } else {
        message = "MOVE CLOSER TO THE GLOWING GATE"
      }
    }

    private def updateGame(): Unit = {
      if (paused || finished || lost) {
        world.repaint()
        return
      }
      tick += 1
      if (powerCooldown > 0) powerCooldown -= 1
      if (dashTicks > 0) dashTicks -= 1
      if (shieldTicks > 0) shieldTicks -= 1
      if (damageCooldown > 0) damageCooldown -= 1
      

      var dx = 0.0
      var dy = 0.0
      if (keys.contains(KeyEvent.VK_W) || keys.contains(KeyEvent.VK_UP)) dy -= 1.0
      if (keys.contains(KeyEvent.VK_S) || keys.contains(KeyEvent.VK_DOWN)) dy += 1.0
      if (keys.contains(KeyEvent.VK_A) || keys.contains(KeyEvent.VK_LEFT)) dx -= 1.0
      if (keys.contains(KeyEvent.VK_D) || keys.contains(KeyEvent.VK_RIGHT)) dx += 1.0
      if (dx == 0.0 && dy == 0.0 && hasTarget) {
        dx = targetX - heroX
        dy = targetY - heroY
        if (math.sqrt(dx * dx + dy * dy) < 8.0) {
          dx = 0.0
          dy = 0.0
          hasTarget = false
        }
      }
      val length = math.sqrt(dx * dx + dy * dy)
      if (length > 0.0) {
        val baseSpeed = 2.65 + (if (element == "STORM") 0.35 else 0.0)
        val speed = baseSpeed * (if (dashTicks > 0) 2.8 else 1.0)
        heroX += (dx / length) * speed
        heroY += (dy / length) * speed
      }
      heroX = math.max(28.0, math.min(world.getWidth - 30.0, heroX))
      heroY = math.max(95.0, math.min(world.getHeight - 35.0, heroY))

      cores.foreach { c =>
        if (!c.taken && distance(heroX, heroY, c.x, c.y) < 29.0) {
          c.taken = true
          collected += 1
          totalCores += 1
          credits += 20
          energy = math.min(100, energy + 12)
          message = "ELEMENTAL CORE COLLECTED! +20 CREDITS"
        }
      }
      if (collected >= 4 && stage < 4) {
        gateOpen = true
        message = "REALM GATE OPEN - REACH THE PORTAL AND PRESS E"
      }
      if (collected >= 4 && stage == 4 && !bossActive && !gateOpen) {
        bossActive = true
        bossHealth = 5
        message = "INFINITY GUARDIAN AWAKENED - USE YOUR POWER!"
      }

      wisps.foreach { w =>
        val ex = heroX - w.x
        val ey = heroY - w.y
        val len = math.sqrt(ex * ex + ey * ey)
        if (len > 1.0) {
          w.x += (ex / len) * w.speed
          w.y += (ey / len) * w.speed
        }
        if (len < 29.0 && damageCooldown <= 0) {
          if (shieldTicks <= 0) health -= 12
          damageCooldown = 28
          message = if (shieldTicks > 0) "SHIELD BLOCKED A WISP!" else "WISP TOUCH! FIND CORES TO RESTORE ENERGY"
          if (health <= 0) {
            lives -= 1
            if (lives <= 0) {
              lost = true
              message = "THE ELEMENTAL TRIAL IS OVER - PRESS R TO RETRY"
            } else {
              health = 55
              heroX = 90.0
              heroY = 360.0
              energy = math.max(25, energy - 20)
              message = "RETURNED TO THE BASE - LIVES LEFT: " + lives
            }
          }
        }
      }

      if (bossActive) {
        val bx = heroX - 815.0
        val by = heroY - 300.0
        val len = math.sqrt(bx * bx + by * by)
        if (len > 1.0) {
          val bossX = 815.0 + (bx / len) * 0.75
          val bossY = 300.0 + (by / len) * 0.75
          if (distance(heroX, heroY, bossX, bossY) < 52.0 && damageCooldown <= 0) {
            if (shieldTicks <= 0) health -= 16
            damageCooldown = 34
            if (health <= 0) {
              lives -= 1
              if (lives <= 0) {
                lost = true
                message = "THE GUARDIAN STOPPED YOUR QUEST - PRESS R TO RETRY"
              } else {
                health = 55
                heroX = 90.0
                heroY = 360.0
              }
            }
          }
        }
      }
      status.setText("HERO: " + heroName + "  |  REALM " + stage + "/4  |  CORES " + collected + "/4  |  CREDITS " + credits + "  |  LIVES " + lives)
      world.repaint()
    }

    private def stageColor(): Color = {
      if (stage == 1) new Color(190, 74, 38)
      else if (stage == 2) new Color(40, 145, 190)
      else if (stage == 3) new Color(75, 150, 88)
      else new Color(116, 86, 196)
    }

    private def elementColor(): Color = {
      if (element == "FIRE") new Color(255, 116, 55)
      else if (element == "WATER") new Color(75, 206, 245)
      else if (element == "EARTH") new Color(118, 211, 116)
      else new Color(185, 148, 255)
    }

    private def drawBar(g: Graphics2D, x: Int, y: Int, width: Int, value: Int, maximum: Int, color: Color, label: String): Unit = {
      g.setColor(new Color(20, 15, 22, 210))
      g.fillRoundRect(x, y, width, 15, 8, 8)
      val fill = math.max(0, math.min(width, (width.toDouble * value.toDouble / maximum.toDouble).toInt))
      g.setColor(color)
      g.fillRoundRect(x, y, fill, 15, 8, 8)
      g.setColor(Color.WHITE)
      g.setFont(new Font("SansSerif", Font.BOLD, 10))
      g.drawString(label, x, y - 4)
    }

    private class WorldCanvas extends JPanel {
      setPreferredSize(new Dimension(980, 570))
      setFocusable(true)
      setBackground(new Color(30, 18, 24))

      override protected def paintComponent(graphics: Graphics): Unit = {
        super.paintComponent(graphics)
        val g = graphics.create().asInstanceOf[Graphics2D]
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
        val w = getWidth
        val h = getHeight
        val bg = stageColor()
        g.setPaint(new GradientPaint(0, 0, new Color(25, 15, 25), w, h, bg.darker().darker()))
        g.fillRect(0, 0, w, h)
        drawWorld(g, w, h)
        g.dispose()
      }

      private def drawWorld(g: Graphics2D, w: Int, h: Int): Unit = {
        for (i <- 0 until 55) {
          val sx = (i * 173 + 41) % math.max(1, w)
          val sy = (i * 97 + 29) % math.max(1, h)
          g.setColor(new Color(255, 222, 178, 65 + (i % 4) * 35))
          g.fillOval(sx, sy, 2 + (i % 3), 2 + (i % 3))
        }
        g.setColor(new Color(15, 10, 17, 150))
        g.fillOval(80, h - 105, w - 160, 135)
        g.setColor(new Color(255, 151, 69, 55))
        g.fillOval(120, h - 83, w - 240, 78)
        g.setColor(new Color(255, 215, 168))
        g.setFont(new Font("SansSerif", Font.BOLD, 17))
        g.drawString(realmNames(stage - 1), 24, 30)
        g.setFont(new Font("SansSerif", Font.PLAIN, 12))
        g.drawString("MOVE: WASD / ARROWS / CLICK     POWER: F     DASH: SPACE     SHIELD: Q     GATE: E", 24, 52)

        drawBar(g, 25, 79, 150, health, 100, new Color(236, 76, 66), "HEALTH")
        drawBar(g, 195, 79, 150, energy, 100, new Color(75, 191, 235), "ENERGY")
        drawBar(g, 365, 79, 150, collected, 4, new Color(255, 197, 91), "REALM CORES")
        drawBar(g, 535, 79, 150, if (bossActive) bossHealth else 0, 5, new Color(185, 90, 220), "GUARDIAN")

        val gateX = w - 95
        val gateY = h / 2
        g.setColor(if (gateOpen) new Color(255, 205, 100, 105) else new Color(80, 72, 78, 90))
        g.fillOval(gateX - 38, gateY - 60, 76, 120)
        g.setColor(if (gateOpen) new Color(255, 219, 135) else new Color(112, 99, 110))
        g.setStroke(new java.awt.BasicStroke(5.0f))
        g.drawOval(gateX - 29, gateY - 52, 58, 104)
        g.setStroke(new java.awt.BasicStroke(1.0f))
        g.setFont(new Font("SansSerif", Font.BOLD, 11))
        g.drawString(if (gateOpen) "OPEN GATE" else "LOCKED", gateX - 32, gateY + 76)

        cores.foreach { c =>
          if (!c.taken) {
            val diamond = new java.awt.Polygon()
            diamond.addPoint(c.x.toInt, (c.y - 17).toInt)
            diamond.addPoint((c.x + 12).toInt, c.y.toInt)
            diamond.addPoint(c.x.toInt, (c.y + 17).toInt)
            diamond.addPoint((c.x - 12).toInt, c.y.toInt)
            g.setColor(new Color(255, 175, 82, 70))
            g.fillOval((c.x - 22).toInt, (c.y - 22).toInt, 44, 44)
            g.setColor(new Color(255, 221, 132))
            g.fillPolygon(diamond)
            g.setColor(new Color(255, 250, 220))
            g.drawPolygon(diamond)
          }
        }

        wisps.foreach { enemy =>
          g.setColor(new Color(185, 72, 72, 48))
          g.fillOval((enemy.x - 23).toInt, (enemy.y - 23).toInt, 46, 46)
          g.setColor(new Color(198, 88, 104))
          g.fillOval((enemy.x - 13).toInt, (enemy.y - 15).toInt, 26, 30)
          g.setColor(new Color(255, 226, 197))
          g.fillOval((enemy.x - 7).toInt, (enemy.y - 6).toInt, 4, 4)
          g.fillOval((enemy.x + 3).toInt, (enemy.y - 6).toInt, 4, 4)
        }

        if (bossActive) {
          g.setColor(new Color(174, 57, 191, 65))
          g.fillOval(755, 238, 120, 120)
          g.setColor(new Color(85, 48, 112))
          g.fillOval(775, 255, 80, 88)
          g.setColor(new Color(255, 185, 110))
          g.fillOval(791, 279, 12, 9)
          g.fillOval(827, 279, 12, 9)
          g.setColor(new Color(248, 220, 255))
          g.setFont(new Font("SansSerif", Font.BOLD, 13))
          g.drawString("INFINITY GUARDIAN", 750, 225)
        }

        val heroColor = elementColor()
        if (shieldTicks > 0) {
          g.setColor(new Color(110, 225, 255, 65))
          g.fillOval((heroX - 31).toInt, (heroY - 31).toInt, 62, 62)
          g.setColor(new Color(151, 232, 255, 180))
          g.drawOval((heroX - 30).toInt, (heroY - 30).toInt, 60, 60)
        }
        if (dashTicks > 0) {
          g.setColor(new Color(heroColor.getRed, heroColor.getGreen, heroColor.getBlue, 85))
          g.fillOval((heroX - 34).toInt, (heroY + 3).toInt, 44, 16)
        }
        g.setColor(new Color(255, 220, 183))
        g.fillOval((heroX - 11).toInt, (heroY - 23).toInt, 22, 22)
        g.setColor(heroColor)
        g.fillRoundRect((heroX - 13).toInt, (heroY - 5).toInt, 26, 29, 10, 10)
        g.setColor(new Color(40, 26, 25))
        g.fillRect((heroX - 8).toInt, (heroY + 20).toInt, 6, 13)
        g.fillRect((heroX + 3).toInt, (heroY + 20).toInt, 6, 13)
        g.setColor(Color.WHITE)
        g.setFont(new Font("SansSerif", Font.BOLD, 11))
        g.drawString(heroName.take(14), (heroX - 28).toInt, (heroY - 30).toInt)

        g.setColor(new Color(255, 235, 210))
        g.setFont(new Font("SansSerif", Font.BOLD, 13))
        g.drawString(message, 24, h - 19)
        g.setFont(new Font("SansSerif", Font.PLAIN, 11))
        g.drawString("CREDITS: " + credits + "    TOTAL CORES: " + totalCores + "    POWER: " + element, w - 325, h - 19)

        if (paused || finished || lost) {
          g.setColor(new Color(10, 8, 14, 190))
          g.fillRect(0, 0, w, h)
          g.setColor(new Color(255, 224, 180))
          g.setFont(new Font("SansSerif", Font.BOLD, 30))
          val title = if (paused) "EXPEDITION PAUSED" else if (finished) "INFERNO CORE AWAKENED!" else "TRIAL FAILED"
          val fm = g.getFontMetrics
          g.drawString(title, (w - fm.stringWidth(title)) / 2, h / 2 - 10)
          g.setFont(new Font("SansSerif", Font.PLAIN, 16))
          val sub = if (paused) "PRESS P OR CLICK RESUME" else if (finished) "YOU MASTERED THE FOUR ELEMENTS" else "CLICK RESTART TO TRY AGAIN"
          g.drawString(sub, (w - g.getFontMetrics.stringWidth(sub)) / 2, h / 2 + 25)
        }
      }
    }

    private def restartGame(): Unit = {
      timer.stop()
      stage = 1
      collected = 0
      totalCores = 0
      credits = 0
      health = 100
      energy = 100
      lives = 3
      tick = 0
      finished = false
      lost = false
      paused = false
      powerCooldown = 0
      dashTicks = 0
      shieldTicks = 0
      damageCooldown = 0
      spawnStage()
      timer.start()
    }
  }
}

InfernoCoreElementalAwakeningEvent06.main(Array[String]())