Code Sketch


Bixuu AstralNexusCrystalProphecyEvent08
By: Mhalsakant School
Category: Programming
import java.awt.{BasicStroke, BorderLayout, Color, Dimension, Font, GradientPaint, Graphics, Graphics2D, Polygon, RenderingHints}
import java.awt.event.{ActionEvent, ActionListener, KeyAdapter, KeyEvent, MouseAdapter, MouseEvent, WindowAdapter, WindowEvent}
import javax.swing.{BorderFactory, JButton, JFrame, JLabel, JPanel, JTextField, SwingConstants, SwingUtilities, Timer, WindowConstants}
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.HashSet

object AstralNexusCrystalProphecyEvent08 {
  def main(args: Array[String]): Unit = {
    SwingUtilities.invokeLater(new Runnable {
      override def run(): Unit = {
        val frame = new AstralNexusFrame()
        frame.setVisible(true)
      }
    })
  }
}

class AstralNexusFrame extends JFrame("TITANVERSE: AGE OF LEGENDS - ASTRAL NEXUS: CRYSTAL PROPHECY") {
  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
  setMinimumSize(new Dimension(960, 640))
  setSize(1180, 790)
  setLocationRelativeTo(null)

  var activeGame: AstralNexusGame = null

  def showLogin(): Unit = {
    val login = new AstralNexusLogin((playerName: String) => startAdventure(playerName))
    setContentPane(login)
    revalidate()
    repaint()
  }

  def startAdventure(playerName: String): Unit = {
    if (activeGame != null) activeGame.stopGame()
    val nextGame = new AstralNexusGame(playerName, () => {
      if (activeGame != null) activeGame.stopGame()
      activeGame = null
      showLogin()
    })
    activeGame = nextGame
    setContentPane(nextGame)
    revalidate()
    repaint()
    nextGame.startGame()
  }

  addWindowListener(new WindowAdapter {
    override def windowClosing(e: WindowEvent): Unit = {
      if (activeGame != null) activeGame.stopGame()
    }
  })

  showLogin()
}

class AstralNexusLogin(onStart: String => Unit) extends JPanel {
  setPreferredSize(new Dimension(1120, 740))
  setLayout(null)
  setBackground(new Color(5, 7, 24))

  val heading = new JLabel("ASTRAL NEXUS", SwingConstants.CENTER)
  heading.setFont(new Font("Serif", Font.BOLD, 56))
  heading.setForeground(new Color(224, 246, 255))
  heading.setBounds(180, 105, 760, 72)
  add(heading)

  val subheading = new JLabel("CRYSTAL PROPHECY  |  TITANVERSE EVENT 08", SwingConstants.CENTER)
  subheading.setFont(new Font("SansSerif", Font.BOLD, 18))
  subheading.setForeground(new Color(100, 231, 255))
  subheading.setBounds(180, 180, 760, 36)
  add(subheading)

  val story = new JLabel("Explore cosmic realms, collect Star Crystals and awaken the Astral Core.", SwingConstants.CENTER)
  story.setFont(new Font("SansSerif", Font.PLAIN, 16))
  story.setForeground(new Color(196, 215, 239))
  story.setBounds(120, 245, 880, 32)
  add(story)

  val nameLabel = new JLabel("ENTER YOUR EXPLORER NAME", SwingConstants.CENTER)
  nameLabel.setFont(new Font("SansSerif", Font.BOLD, 16))
  nameLabel.setForeground(new Color(211, 241, 255))
  nameLabel.setBounds(350, 340, 420, 30)
  add(nameLabel)

  val nameField = new JTextField("Star Explorer")
  nameField.setFont(new Font("SansSerif", Font.PLAIN, 20))
  nameField.setHorizontalAlignment(SwingConstants.CENTER)
  nameField.setBounds(360, 378, 400, 48)
  add(nameField)

  val startButton = new JButton("ENTER THE ASTRAL NEXUS")
  startButton.setFont(new Font("SansSerif", Font.BOLD, 17))
  startButton.setForeground(Color.WHITE)
  startButton.setBackground(new Color(24, 130, 171))
  startButton.setFocusPainted(false)
  startButton.setBounds(360, 448, 400, 54)
  startButton.addActionListener(new ActionListener {
    override def actionPerformed(e: ActionEvent): Unit = {
      val chosenName = nameField.getText.trim
      if (chosenName.length > 0) onStart(chosenName)
    }
  })
  add(startButton)
  nameField.addActionListener(new ActionListener {
    override def actionPerformed(e: ActionEvent): Unit = startButton.doClick()
  })

  val controls = new JLabel("WASD MOVE  |  ARROWS TURN  |  SPACE DASH  |  F NOVA PULSE  |  E INTERACT", SwingConstants.CENTER)
  controls.setFont(new Font("SansSerif", Font.BOLD, 13))
  controls.setForeground(new Color(150, 190, 219))
  controls.setBounds(100, 585, 920, 30)
  add(controls)

  val safety = new JLabel("OFFLINE SWING ADVENTURE  |  NO EXTRA ASSETS OR INTERNET REQUIRED", SwingConstants.CENTER)
  safety.setFont(new Font("SansSerif", Font.PLAIN, 12))
  safety.setForeground(new Color(108, 129, 157))
  safety.setBounds(150, 620, 820, 26)
  add(safety)

  override def paintComponent(g: Graphics): Unit = {
    super.paintComponent(g)
    val g2 = g.asInstanceOf[Graphics2D]
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
    g2.setPaint(new GradientPaint(0, 0, new Color(4, 8, 29), 0, getHeight, new Color(13, 26, 61)))
    g2.fillRect(0, 0, getWidth, getHeight)
    var i = 0
    while (i < 115) {
      val sx = (i * 149 + 37) % math.max(1, getWidth)
      val sy = (i * 83 + 19) % math.max(1, getHeight)
      val radius = 1 + (i % 3)
      g2.setColor(new Color(130, 224, 255, 80 + (i * 17) % 150))
      g2.fillOval(sx, sy, radius, radius)
      i += 1
    }
    g2.setColor(new Color(35, 173, 224, 32))
    g2.fillOval(getWidth / 2 - 220, 10, 440, 300)
    g2.setColor(new Color(105, 239, 255, 135))
    g2.setStroke(new BasicStroke(3.0f))
    g2.drawOval(getWidth / 2 - 115, 48, 230, 230)
    g2.setColor(new Color(196, 252, 255, 190))
    val cx = getWidth / 2
    val cy = 165
    val crystal = new Polygon(Array(cx, cx + 44, cx + 27, cx, cx - 27, cx - 44), Array(cy - 98, cy - 22, cy + 68, cy + 112, cy + 68, cy - 22), 6)
    g2.fillPolygon(crystal)
    g2.setColor(new Color(255, 255, 255, 160))
    g2.drawLine(cx, cy - 98, cx, cy + 112)
  }
}

case class AstralPoint(x: Double, y: Double, z: Double)
case class AstralFace(points: Vector[AstralPoint], color: Color, depth: Double)
case class AstralScreenPoint(x: Int, y: Int, depth: Double)

class AstralCrystal(val id: Int, val realm: Int, val x: Double, val z: Double, val phase: Double) {
  var collected = false
}

class AstralWisp(var x: Double, var z: Double, val phase: Double) {
  var stunnedTicks = 0
  var active = true
}

class AstralNexusGame(playerName: String, onExit: () => Unit) extends JPanel {
  setLayout(new BorderLayout())
  setBackground(Color.BLACK)

  var playerX = 0.0
  var playerZ = 2.0
  var cameraYaw = 0.0
  var health = 100
  var energy = 100
  var score = 0
  var totalCrystals = 0
  var currentRealm = 0
  var bossHealth = 4
  var gamePaused = false
  var hasWon = false
  var hasLost = false
  var mapVisible = false
  var tickCount = 0
  var dashCooldown = 0
  var pulseFlash = 0
  var shieldTicks = 0
  var damageCooldown = 0
  var messageTicks = 180
  var messageText = "Find the Star Crystals. The Astral Core is waiting."
  val pressedKeys = HashSet[Int]()

  val realmNames = Vector("NEBULA EXPANSE", "PRISM CAVERNS", "ASTRAL NEXUS")
  val realmStart = Vector(0.0, 31.0, 62.0)
  val realmEnd = Vector(29.0, 60.0, 91.0)
  val realmTints = Vector(new Color(75, 207, 255), new Color(189, 119, 255), new Color(255, 207, 105))
  val crystalList = ArrayBuffer[AstralCrystal]()
  val wispList = ArrayBuffer[AstralWisp]()

  def createWorldItems(): Unit = {
    crystalList.clear()
    var id = 0
    var realm = 0
    while (realm < 3) {
      val base = realmStart(realm)
      val places = Vector((-5.0, 7.0), (4.5, 11.0), (-4.0, 16.0), (5.0, 21.0), (0.0, 25.0))
      var i = 0
      while (i < places.length) {
        val p = if (realm == 2 && i == 4) (5.0, 22.0) else places(i)
        crystalList += new AstralCrystal(id, realm, p._1, base + p._2, i.toDouble * 0.83 + realm.toDouble)
        id += 1
        i += 1
      }
      realm += 1
    }
    wispList.clear()
    wispList += new AstralWisp(7.0, 13.0, 0.5)
    wispList += new AstralWisp(-7.0, 43.0, 1.5)
    wispList += new AstralWisp(6.0, 73.0, 2.5)
    wispList += new AstralWisp(-5.0, 81.0, 3.5)
  }
  createWorldItems()

  val scene = new JPanel() {
    setFocusable(true)
    setBackground(new Color(3, 6, 22))
    override def paintComponent(g: Graphics): Unit = {
      super.paintComponent(g)
      renderScene(g.asInstanceOf[Graphics2D], getWidth, getHeight)
    }
  }
  add(scene, BorderLayout.CENTER)

  val bottomBar = new JPanel()
  bottomBar.setBackground(new Color(7, 15, 35))
  bottomBar.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6))

  def addControlButton(label: String, action: () => Unit): JButton = {
    val button = new JButton(label)
    button.setFont(new Font("SansSerif", Font.BOLD, 12))
    button.setForeground(Color.WHITE)
    button.setBackground(new Color(19, 78, 112))
    button.setFocusPainted(false)
    button.setFocusable(false)
    button.addActionListener(new ActionListener {
      override def actionPerformed(e: ActionEvent): Unit = {
        action()
        scene.requestFocusInWindow()
        scene.repaint()
      }
    })
    bottomBar.add(button)
    button
  }

  addControlButton("NOVA PULSE [F]", () => castNovaPulse())
  addControlButton("GRAVITY DASH [SPACE]", () => performDash())
  addControlButton("PRISM SHIELD [Q]", () => activateShield())
  addControlButton("INTERACT [E]", () => interact())
  addControlButton("STAR MAP [M]", () => { mapVisible = !mapVisible })
  addControlButton("PAUSE [P]", () => { gamePaused = !gamePaused })
  addControlButton("EXIT TO LOGIN", () => onExit())
  add(bottomBar, BorderLayout.SOUTH)

  val gameTimer = new Timer(33, new ActionListener {
    override def actionPerformed(e: ActionEvent): Unit = updateGame()
  })

  scene.addKeyListener(new KeyAdapter {
    override def keyPressed(e: KeyEvent): Unit = {
      pressedKeys += e.getKeyCode
      if (e.getKeyCode == KeyEvent.VK_SPACE) performDash()
      if (e.getKeyCode == KeyEvent.VK_F) castNovaPulse()
      if (e.getKeyCode == KeyEvent.VK_Q) activateShield()
      if (e.getKeyCode == KeyEvent.VK_E) interact()
      if (e.getKeyCode == KeyEvent.VK_M) mapVisible = !mapVisible
      if (e.getKeyCode == KeyEvent.VK_P || e.getKeyCode == KeyEvent.VK_ESCAPE) gamePaused = !gamePaused
      if (e.getKeyCode == KeyEvent.VK_B) onExit()
      if (e.getKeyCode == KeyEvent.VK_R && (hasWon || hasLost)) restartExpedition()
      scene.repaint()
    }
    override def keyReleased(e: KeyEvent): Unit = {
      pressedKeys -= e.getKeyCode
    }
  })

  scene.addMouseListener(new MouseAdapter {
    override def mouseClicked(e: MouseEvent): Unit = {
      if (e.getButton == MouseEvent.BUTTON1) castNovaPulse()
      scene.requestFocusInWindow()
    }
  })

  def startGame(): Unit = {
    gameTimer.start()
    scene.requestFocusInWindow()
  }

  def restartExpedition(): Unit = {
    playerX = 0.0
    playerZ = 2.0
    cameraYaw = 0.0
    health = 100
    energy = 100
    score = 0
    totalCrystals = 0
    currentRealm = 0
    bossHealth = 4
    gamePaused = false
    hasWon = false
    hasLost = false
    mapVisible = false
    tickCount = 0
    dashCooldown = 0
    pulseFlash = 0
    shieldTicks = 0
    damageCooldown = 0
    createWorldItems()
    setMessage("A new Astral expedition begins. Find the 15 Star Crystals!")
    scene.requestFocusInWindow()
  }

  def stopGame(): Unit = {
    gameTimer.stop()
    pressedKeys.clear()
  }

  def setMessage(text: String): Unit = {
    messageText = text
    messageTicks = 165
  }

  def realmCrystalCount(realm: Int): Int = {
    var count = 0
    var i = 0
    while (i < crystalList.length) {
      if (crystalList(i).realm == realm && crystalList(i).collected) count += 1
      i += 1
    }
    count
  }

  def performDash(): Unit = {
    if (!gamePaused && !hasWon && !hasLost && dashCooldown <= 0) {
      playerX += math.sin(cameraYaw) * 3.6
      playerZ += math.cos(cameraYaw) * 3.6
      playerX = math.max(-14.0, math.min(14.0, playerX))
      playerZ = math.max(1.0, math.min(92.0, playerZ))
      dashCooldown = 82
      setMessage("GRAVITY DASH! You crossed the star field.")
    }
  }

  def activateShield(): Unit = {
    if (!gamePaused && !hasWon && !hasLost && energy >= 14) {
      energy -= 14
      shieldTicks = 150
      setMessage("PRISM SHIELD ACTIVE!")
    } else {
      setMessage("Not enough energy for the Prism Shield.")
    }
  }

  def castNovaPulse(): Unit = {
    if (gamePaused || hasWon || hasLost) return
    if (energy < 18) {
      setMessage("Not enough energy. Collect Star Crystals to recharge.")
      return
    }
    energy -= 18
    pulseFlash = 24
    var hitWisp = false
    var i = 0
    while (i < wispList.length) {
      val w = wispList(i)
      if (w.active) {
        val dx = w.x - playerX
        val dz = w.z - playerZ
        val distance = math.sqrt(dx * dx + dz * dz)
        val angleToWisp = math.atan2(dx, dz)
        val angleDifference = math.abs(math.atan2(math.sin(angleToWisp - cameraYaw), math.cos(angleToWisp - cameraYaw)))
        if (distance < 12.0 && angleDifference < 0.9) {
          w.stunnedTicks = 135
          hitWisp = true
        }
      }
      i += 1
    }
    if (currentRealm == 2 && totalCrystals >= 10) {
      val bossDistanceX = playerX
      val bossDistanceZ = 87.0 - playerZ
      val distanceToBoss = math.sqrt(bossDistanceX * bossDistanceX + bossDistanceZ * bossDistanceZ)
      if (distanceToBoss < 15.0 && bossHealth > 0) {
        bossHealth -= 1
        hitWisp = true
        if (bossHealth <= 0) setMessage("ASTRAL ORACLE DEFEATED! Reach the final Star Gate.")
        else setMessage("NOVA PULSE HIT! Oracle shield strength: " + bossHealth)
      }
    }
    if (hitWisp && bossHealth > 0) setMessage("NOVA PULSE! Nearby shadow wisps are stunned.")
    if (!hitWisp) setMessage("NOVA PULSE released into the cosmic sky.")
  }

  def interact(): Unit = {
    if (gamePaused || hasWon || hasLost) return
    var closest: AstralCrystal = null
    var closestDistance = 9999.0
    var i = 0
    while (i < crystalList.length) {
      val crystal = crystalList(i)
      if (!crystal.collected && crystal.realm == currentRealm) {
        val dx = crystal.x - playerX
        val dz = crystal.z - playerZ
        val d = math.sqrt(dx * dx + dz * dz)
        if (d < closestDistance) {
          closestDistance = d
          closest = crystal
        }
      }
      i += 1
    }
    if (closest != null && closestDistance < 3.4) {
      closest.collected = true
      totalCrystals += 1
      score += 100
      energy = math.min(100, energy + 16)
      health = math.min(100, health + 3)
      setMessage("STAR CRYSTAL COLLECTED! " + totalCrystals + "/15  |  +100 POINTS")
      return
    }

    val gateZ = realmEnd(currentRealm) - 1.0
    val gateDistance = math.sqrt(playerX * playerX + (gateZ - playerZ) * (gateZ - playerZ))
    if (gateDistance < 5.2) {
      if (realmCrystalCount(currentRealm) >= 5) {
        if (currentRealm < 2) {
          currentRealm += 1
          playerX = 0.0
          playerZ = realmStart(currentRealm) + 1.7
          cameraYaw = 0.0
          setMessage("REALM UNLOCKED: " + realmNames(currentRealm))
        } else if (bossHealth <= 0) {
          hasWon = true
          setMessage("THE ASTRAL CORE IS AWAKENED! PROPHECY COMPLETE!")
        } else {
          setMessage("The Astral Oracle guards the final gate. Use Nova Pulse near it!")
        }
      } else {
        setMessage("Gate sealed. Collect all 5 crystals in this realm first.")
      }
    } else {
      setMessage("Move close to a floating Star Crystal or the glowing Realm Gate, then press E.")
    }
  }

  def updateGame(): Unit = {
    if (!gamePaused && !hasWon && !hasLost) {
      tickCount += 1
      if (dashCooldown > 0) dashCooldown -= 1
      if (pulseFlash > 0) pulseFlash -= 1
      if (shieldTicks > 0) shieldTicks -= 1
      if (damageCooldown > 0) damageCooldown -= 1
      if (messageTicks > 0) messageTicks -= 1
      if (energy < 100 && tickCount % 5 == 0) energy = math.min(100, energy + 1)

      if (pressedKeys.contains(KeyEvent.VK_LEFT)) cameraYaw -= 0.045
      if (pressedKeys.contains(KeyEvent.VK_RIGHT)) cameraYaw += 0.045
      var forward = 0.0
      var side = 0.0
      if (pressedKeys.contains(KeyEvent.VK_W) || pressedKeys.contains(KeyEvent.VK_UP)) forward += 0.17
      if (pressedKeys.contains(KeyEvent.VK_S) || pressedKeys.contains(KeyEvent.VK_DOWN)) forward -= 0.14
      if (pressedKeys.contains(KeyEvent.VK_A)) side -= 0.15
      if (pressedKeys.contains(KeyEvent.VK_D)) side += 0.15
      val movementSpeed = if (shieldTicks > 0) 1.06 else 1.0
      playerX += (math.sin(cameraYaw) * forward + math.cos(cameraYaw) * side) * movementSpeed
      playerZ += (math.cos(cameraYaw) * forward - math.sin(cameraYaw) * side) * movementSpeed
      playerX = math.max(-14.0, math.min(14.0, playerX))
      playerZ = math.max(1.0, math.min(92.0, playerZ))

      var w = 0
      while (w < wispList.length) {
        val enemy = wispList(w)
        if (enemy.stunnedTicks > 0) enemy.stunnedTicks -= 1
        else if (enemy.active) {
          val dx = playerX - enemy.x
          val dz = playerZ - enemy.z
          val distance = math.sqrt(dx * dx + dz * dz)
          if (distance > 1.9 && distance < 19.0) {
            enemy.x += (dx / math.max(0.1, distance)) * 0.035
            enemy.z += (dz / math.max(0.1, distance)) * 0.035
          }
          if (distance < 1.55 && shieldTicks <= 0 && damageCooldown <= 0) {
            health = math.max(0, health - 9)
            damageCooldown = 55
            setMessage("A Void Wisp touched you! Use a Shield or Nova Pulse.")
            if (health <= 0) hasLost = true
          }
        }
        w += 1
      }
    }
    scene.repaint()
  }

  def forwardDepth(point: AstralPoint): Double = {
    val dx = point.x - playerX
    val dz = point.z - playerZ
    dx * math.sin(cameraYaw) + dz * math.cos(cameraYaw)
  }

  def sideDepth(point: AstralPoint): Double = {
    val dx = point.x - playerX
    val dz = point.z - playerZ
    dx * math.cos(cameraYaw) - dz * math.sin(cameraYaw)
  }

  def project(point: AstralPoint, width: Int, height: Int): AstralScreenPoint = {
    val depth = forwardDepth(point)
    val safeDepth = math.max(0.35, depth)
    val focal = math.min(width, height).toDouble * 0.92
    val sx = (width.toDouble / 2.0 + sideDepth(point) * focal / safeDepth).toInt
    val sy = (height.toDouble * 0.43 - (point.y - 1.62) * focal / safeDepth).toInt
    AstralScreenPoint(sx, sy, (depth * 100.0).toInt)
  }

  def shadeColor(color: Color, amount: Double): Color = {
    val factor = math.max(0.22, math.min(1.45, amount))
    new Color(
      math.max(0, math.min(255, (color.getRed.toDouble * factor).toInt)),
      math.max(0, math.min(255, (color.getGreen.toDouble * factor).toInt)),
      math.max(0, math.min(255, (color.getBlue.toDouble * factor).toInt)),
      color.getAlpha
    )
  }

  def addFace(faces: ArrayBuffer[AstralFace], points: Vector[AstralPoint], color: Color): Unit = {
    var totalDepth = 0.0
    var i = 0
    while (i < points.length) {
      totalDepth += forwardDepth(points(i))
      i += 1
    }
    faces += AstralFace(points, color, totalDepth / math.max(1, points.length).toDouble)
  }

  def addCube(faces: ArrayBuffer[AstralFace], x: Double, y: Double, z: Double, width: Double, height: Double, depth: Double, color: Color): Unit = {
    val x0 = x - width / 2.0
    val x1 = x + width / 2.0
    val y0 = y - height / 2.0
    val y1 = y + height / 2.0
    val z0 = z - depth / 2.0
    val z1 = z + depth / 2.0
    val p0 = AstralPoint(x0, y0, z0)
    val p1 = AstralPoint(x1, y0, z0)
    val p2 = AstralPoint(x1, y1, z0)
    val p3 = AstralPoint(x0, y1, z0)
    val p4 = AstralPoint(x0, y0, z1)
    val p5 = AstralPoint(x1, y0, z1)
    val p6 = AstralPoint(x1, y1, z1)
    val p7 = AstralPoint(x0, y1, z1)
    addFace(faces, Vector(p0, p1, p2, p3), shadeColor(color, 0.62))
    addFace(faces, Vector(p4, p5, p6, p7), shadeColor(color, 1.18))
    addFace(faces, Vector(p0, p4, p7, p3), shadeColor(color, 0.76))
    addFace(faces, Vector(p1, p5, p6, p2), shadeColor(color, 0.93))
    addFace(faces, Vector(p3, p2, p6, p7), shadeColor(color, 1.36))
    addFace(faces, Vector(p0, p1, p5, p4), shadeColor(color, 0.44))
  }

  def addPyramid(faces: ArrayBuffer[AstralFace], x: Double, y: Double, z: Double, size: Double, height: Double, color: Color): Unit = {
    val a = AstralPoint(x - size / 2.0, y, z - size / 2.0)
    val b = AstralPoint(x + size / 2.0, y, z - size / 2.0)
    val c = AstralPoint(x + size / 2.0, y, z + size / 2.0)
    val d = AstralPoint(x - size / 2.0, y, z + size / 2.0)
    val top = AstralPoint(x, y + height, z)
    addFace(faces, Vector(a, b, top), shadeColor(color, 0.92))
    addFace(faces, Vector(b, c, top), shadeColor(color, 1.24))
    addFace(faces, Vector(c, d, top), shadeColor(color, 0.76))
    addFace(faces, Vector(d, a, top), shadeColor(color, 1.04))
    addFace(faces, Vector(a, d, c, b), shadeColor(color, 0.45))
  }

  def addOctahedron(faces: ArrayBuffer[AstralFace], x: Double, y: Double, z: Double, size: Double, color: Color): Unit = {
    val top = AstralPoint(x, y + size, z)
    val bottom = AstralPoint(x, y - size, z)
    val left = AstralPoint(x - size * 0.72, y, z)
    val back = AstralPoint(x, y, z - size * 0.72)
    val right = AstralPoint(x + size * 0.72, y, z)
    val front = AstralPoint(x, y, z + size * 0.72)
    addFace(faces, Vector(top, left, back), shadeColor(color, 1.34))
    addFace(faces, Vector(top, back, right), shadeColor(color, 0.92))
    addFace(faces, Vector(top, right, front), shadeColor(color, 1.20))
    addFace(faces, Vector(top, front, left), shadeColor(color, 1.05))
    addFace(faces, Vector(bottom, back, left), shadeColor(color, 0.53))
    addFace(faces, Vector(bottom, right, back), shadeColor(color, 0.67))
    addFace(faces, Vector(bottom, front, right), shadeColor(color, 0.72))
    addFace(faces, Vector(bottom, left, front), shadeColor(color, 0.61))
  }

  def addPortal(faces: ArrayBuffer[AstralFace], z: Double, tint: Color): Unit = {
    var i = 0
    while (i < 18) {
      val angle = i.toDouble * math.Pi * 2.0 / 18.0
      val x = math.cos(angle) * 2.35
      val y = 3.2 + math.sin(angle) * 2.75
      addCube(faces, x, y, z, 0.52, 0.58, 0.5, tint)
      i += 1
    }
    addFace(faces, Vector(
      AstralPoint(-1.85, 0.7, z - 0.08), AstralPoint(1.85, 0.7, z - 0.08),
      AstralPoint(1.85, 5.6, z - 0.08), AstralPoint(-1.85, 5.6, z - 0.08)
    ), new Color(tint.getRed, tint.getGreen, tint.getBlue, 80))
  }

  def buildSceneFaces(): ArrayBuffer[AstralFace] = {
    val faces = ArrayBuffer[AstralFace]()
    var z = 1.0
    while (z < 94.0) {
      val tileColor = if ((z.toInt / 5) % 2 == 0) new Color(15, 28, 48) else new Color(19, 35, 58)
      addFace(faces, Vector(AstralPoint(-22.0, 0.0, z), AstralPoint(22.0, 0.0, z), AstralPoint(22.0, 0.0, z + 5.0), AstralPoint(-22.0, 0.0, z + 5.0)), tileColor)
      addFace(faces, Vector(AstralPoint(-1.6, 0.025, z), AstralPoint(1.6, 0.025, z), AstralPoint(1.6, 0.025, z + 5.0), AstralPoint(-1.6, 0.025, z + 5.0)), new Color(40, 88, 117))
      z += 5.0
    }

    var i = 0
    while (i < 15) {
      val side = if (i % 2 == 0) -1.0 else 1.0
      val zPos = 5.0 + i.toDouble * 5.7
      val tint = realmTints((i / 5) % 3)
      addCube(faces, side * (8.5 + (i % 3).toDouble), 1.8, zPos, 1.7, 3.6 + (i % 3).toDouble, 1.7, new Color(27, 45, 76))
      addPyramid(faces, side * (8.5 + (i % 3).toDouble), 3.7 + (i % 3).toDouble, zPos, 2.7, 2.5, tint)
      i += 1
    }

    addPortal(faces, 28.5, new Color(61, 211, 255))
    addPortal(faces, 59.5, new Color(190, 106, 255))
    addPortal(faces, 90.0, new Color(255, 210, 105))

    var c = 0
    while (c < crystalList.length) {
      val crystal = crystalList(c)
      if (!crystal.collected) {
        val tint = realmTints(crystal.realm)
        val floatY = 1.45 + math.sin(tickCount.toDouble * 0.055 + crystal.phase) * 0.22
        addOctahedron(faces, crystal.x, floatY, crystal.z, 0.82, tint)
      }
      c += 1
    }

    var w = 0
    while (w < wispList.length) {
      val enemy = wispList(w)
      if (enemy.active) {
        val tint = if (enemy.stunnedTicks > 0) new Color(101, 231, 255) else new Color(157, 88, 255)
        val floatY = 1.5 + math.sin(tickCount.toDouble * 0.07 + enemy.phase) * 0.32
        addOctahedron(faces, enemy.x, floatY, enemy.z, 0.78, tint)
      }
      w += 1
    }

    if (currentRealm == 2 && totalCrystals >= 10 && bossHealth > 0) {
      val bossTint = new Color(255, 188, 94)
      addOctahedron(faces, 0.0, 3.2 + math.sin(tickCount.toDouble * 0.04) * 0.25, 87.0, 2.65, bossTint)
      addOctahedron(faces, 0.0, 3.2, 87.0, 1.35, new Color(255, 245, 196))
    }
    faces
  }

  def renderFace(g2: Graphics2D, face: AstralFace, width: Int, height: Int): Unit = {
    if (face.depth > 0.35) {
      val polygon = new Polygon()
      var valid = true
      var i = 0
      while (i < face.points.length) {
        val projected = project(face.points(i), width, height)
        if (projected.depth <= 35) valid = false
        polygon.addPoint(projected.x, projected.y)
        i += 1
      }
      if (valid && polygon.npoints >= 3) {
        g2.setColor(face.color)
        g2.fillPolygon(polygon)
        g2.setColor(new Color(210, 242, 255, 45))
        g2.setStroke(new BasicStroke(1.0f))
        g2.drawPolygon(polygon)
      }
    }
  }

  def drawCosmicSky(g2: Graphics2D, width: Int, height: Int): Unit = {
    val sky = if (currentRealm == 0) new Color(5, 12, 40) else if (currentRealm == 1) new Color(19, 8, 44) else new Color(28, 14, 35)
    g2.setPaint(new GradientPaint(0, 0, sky, 0, height, new Color(5, 8, 24)))
    g2.fillRect(0, 0, width, height)
    var i = 0
    while (i < 125) {
      val sx = (i * 173 + 31) % math.max(1, width)
      val sy = (i * 97 + 13) % math.max(1, (height * 0.62).toInt)
      val radius = 1 + (i % 3)
      val alpha = 85 + ((i * 23 + tickCount) % 150)
      g2.setColor(new Color(174, 225, 255, alpha))
      g2.fillOval(sx, sy, radius, radius)
      i += 1
    }
    val tint = realmTints(currentRealm)
    g2.setColor(new Color(tint.getRed, tint.getGreen, tint.getBlue, 35))
    g2.fillOval(width / 2 - 210, 12, 420, 300)
    g2.setColor(new Color(tint.getRed, tint.getGreen, tint.getBlue, 180))
    g2.setStroke(new BasicStroke(5.0f))
    g2.drawOval(width / 2 - 92, 48, 184, 184)
    g2.setColor(new Color(215, 249, 255, 210))
    g2.fillOval(width / 2 - 59, 81, 118, 118)
    g2.setColor(new Color(14, 27, 57, 225))
    g2.fillOval(width / 2 - 43, 70, 112, 112)
    g2.setColor(new Color(tint.getRed, tint.getGreen, tint.getBlue, 100))
    g2.fillRect(0, (height * 0.43).toInt, width, (height * 0.08).toInt)
  }

  def drawHud(g2: Graphics2D, width: Int, height: Int): Unit = {
    g2.setFont(new Font("SansSerif", Font.BOLD, 14))
    g2.setColor(new Color(5, 12, 29, 220))
    g2.fillRoundRect(14, 14, 330, 118, 18, 18)
    g2.setColor(new Color(160, 229, 255))
    g2.drawString("EXPLORER: " + playerName, 28, 38)
    g2.setColor(Color.WHITE)
    g2.drawString("REALM: " + realmNames(currentRealm), 28, 60)
    g2.drawString("STAR CRYSTALS: " + totalCrystals + "/15   SCORE: " + score, 28, 82)
    drawBar(g2, 28, 96, 135, 12, health, new Color(255, 91, 115), "HP")
    drawBar(g2, 179, 96, 135, 12, energy, new Color(73, 217, 255), "ENERGY")

    g2.setColor(new Color(4, 12, 29, 210))
    g2.fillRoundRect(width - 220, 15, 205, 80, 16, 16)
    g2.setColor(new Color(179, 229, 255))
    g2.drawString("REALM PROGRESS", width - 203, 38)
    g2.setColor(Color.WHITE)
    g2.drawString(realmCrystalCount(currentRealm) + "/5 CRYSTALS", width - 203, 61)
    g2.drawString("DASH: " + (if (dashCooldown <= 0) "READY" else "CHARGING"), width - 203, 82)

    if (currentRealm == 2 && totalCrystals >= 10 && bossHealth > 0) {
      g2.setColor(new Color(18, 10, 30, 220))
      g2.fillRoundRect(width / 2 - 160, 18, 320, 54, 16, 16)
      g2.setColor(new Color(255, 220, 145))
      g2.drawString("ASTRAL ORACLE  |  SHIELDS: " + bossHealth, width / 2 - 133, 50)
    }

    if (messageTicks > 0) {
      val messageWidth = math.min(width - 50, math.max(360, messageText.length * 9))
      g2.setColor(new Color(4, 12, 28, 215))
      g2.fillRoundRect(width / 2 - messageWidth / 2, height - 86, messageWidth, 38, 14, 14)
      g2.setColor(new Color(220, 245, 255))
      g2.setFont(new Font("SansSerif", Font.BOLD, 13))
      g2.drawString(messageText, width / 2 - messageWidth / 2 + 14, height - 62)
    }

    g2.setColor(new Color(220, 245, 255, 170))
    g2.setStroke(new BasicStroke(2.0f))
    val cx = width / 2
    val cy = (height * 0.43).toInt
    g2.drawLine(cx - 9, cy, cx - 3, cy)
    g2.drawLine(cx + 3, cy, cx + 9, cy)
    g2.drawLine(cx, cy - 9, cx, cy - 3)
    g2.drawLine(cx, cy + 3, cx, cy + 9)

    if (shieldTicks > 0) {
      g2.setColor(new Color(73, 211, 255, 100))
      g2.setStroke(new BasicStroke(5.0f))
      g2.drawOval(width / 2 - 115, height / 2 - 150, 230, 300)
    }
    if (pulseFlash > 0) {
      g2.setColor(new Color(118, 235, 255, math.min(130, pulseFlash * 5)))
      g2.setStroke(new BasicStroke(8.0f))
      g2.drawOval(width / 2 - 45 - pulseFlash * 2, height / 2 - 45 - pulseFlash * 2, 90 + pulseFlash * 4, 90 + pulseFlash * 4)
    }
  }

  def drawBar(g2: Graphics2D, x: Int, y: Int, width: Int, height: Int, value: Int, tint: Color, label: String): Unit = {
    g2.setColor(new Color(45, 57, 78))
    g2.fillRoundRect(x, y, width, height, 8, 8)
    val fillWidth = math.max(0, math.min(width, width * value / 100))
    g2.setColor(tint)
    g2.fillRoundRect(x, y, fillWidth, height, 8, 8)
    g2.setFont(new Font("SansSerif", Font.BOLD, 9))
    g2.setColor(Color.WHITE)
    g2.drawString(label, x + 4, y + 10)
  }

  def drawMiniMap(g2: Graphics2D, width: Int): Unit = {
    g2.setColor(new Color(3, 10, 24, 225))
    g2.fillRoundRect(width - 205, 112, 188, 190, 14, 14)
    g2.setColor(new Color(115, 210, 255))
    g2.drawString("ASTRAL MAP", width - 185, 134)
    g2.setColor(new Color(62, 91, 123))
    g2.fillRoundRect(width - 176, 148, 130, 130, 8, 8)
    val playerMapX = width - 111 + (playerX * 3.3).toInt
    val playerMapY = 270 - (playerZ * 1.25).toInt
    g2.setColor(new Color(255, 255, 255))
    g2.fillOval(playerMapX - 4, playerMapY - 4, 8, 8)
    g2.setColor(new Color(77, 231, 255))
    var i = 0
    while (i < crystalList.length) {
      val crystal = crystalList(i)
      if (!crystal.collected) {
        val mx = width - 111 + (crystal.x * 3.3).toInt
        val my = 270 - (crystal.z * 1.25).toInt
        g2.fillOval(mx - 2, my - 2, 4, 4)
      }
      i += 1
    }
  }

  def renderScene(g2: Graphics2D, width: Int, height: Int): Unit = {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
    drawCosmicSky(g2, width, height)
    val faces = buildSceneFaces()
    val sortedFaces = faces.sortWith((a, b) => a.depth > b.depth)
    var i = 0
    while (i < sortedFaces.length) {
      renderFace(g2, sortedFaces(i), width, height)
      i += 1
    }
    drawHud(g2, width, height)
    if (mapVisible) drawMiniMap(g2, width)

    if (gamePaused || hasWon || hasLost) {
      g2.setColor(new Color(0, 4, 15, 190))
      g2.fillRect(0, 0, width, height)
      val title = if (hasWon) "ASTRAL CORE AWAKENED" else if (hasLost) "THE VOID CLAIMED THE EXPLORER" else "GAME PAUSED"
      g2.setFont(new Font("Serif", Font.BOLD, 37))
      g2.setColor(if (hasWon) new Color(140, 255, 224) else new Color(231, 225, 255))
      val titleWidth = g2.getFontMetrics.stringWidth(title)
      g2.drawString(title, (width - titleWidth) / 2, height / 2 - 12)
      g2.setFont(new Font("SansSerif", Font.BOLD, 16))
      g2.setColor(Color.WHITE)
      val sub = if (hasWon || hasLost) "Press R to restart the expedition or B to return to login." else "Press P or ESC to resume."
      val subWidth = g2.getFontMetrics.stringWidth(sub)
      g2.drawString(sub, (width - subWidth) / 2, height / 2 + 30)
    }
  }
}

AstralNexusCrystalProphecyEvent08.main(Array[String]())