Code Sketch


Bixuu
By: Mhalsakant School
Category: Programming

import java.awt._
import java.awt.event._
import java.awt.geom._
import javax.swing._
import scala.collection.mutable.ArrayBuffer

case class DObstacle(
  x: Double,
  kind: String,
  z: Double = 0.0,
  w: Double = 1.1,
  len: Double = 1.2,
  h: Double = 1.2
)

case class DPortal(x: Double, mode: String)

case class Dust(
  var x: Double,
  var y: Double,
  var life: Int
)

case class DLevel(
  name: String,
  color: Color,
  hazards: Vector[DObstacle],
  portals: Vector[DPortal],
  finish: Double
)

object DashLevels {

  def makeLevel(n: Int): DLevel = {

    val a = ArrayBuffer[DObstacle]()

    def floorSpikes(xs: Double*): Unit = {
      xs.foreach(x => a += DObstacle(x, "spike"))
    }

    def ceilingSpikes(xs: Double*): Unit = {
      xs.foreach(x => a += DObstacle(x, "ceilingSpike"))
    }

    def block(x: Double, h: Double): Unit = {
      a += DObstacle(x, "block", 0.0, 1.2, 1.2, h)
    }

    def topBlock(x: Double, h: Double): Unit = {
      a += DObstacle(x, "ceilingBlock", 0.0, 1.2, 1.2, h)
    }

    floorSpikes(24, 48, 75)

    block(130, 2.0)
    block(168, 2.7)
    block(198, 1.5)

    ceilingSpikes(145, 183, 215)

    floorSpikes(260, 295, 330)
    ceilingSpikes(277, 313, 347)

    floorSpikes(395, 432, 468)
    ceilingSpikes(415, 450)

    block(535, 1.6)
    topBlock(551, 1.6)
    block(567, 1.6)
    topBlock(583, 1.6)
    block(599, 1.6)
    topBlock(615, 1.6)

    floorSpikes(670, 700, 730, 753)

    floorSpikes(800, 832, 864)
    ceilingSpikes(815, 847, 880)

    floorSpikes(945, 970, 1000, 1027, 1055)

    if (n >= 1) {
      floorSpikes(58, 690, 716, 986, 1040)
      ceilingSpikes(160, 305, 860)
    }

    if (n >= 2) {
      floorSpikes(35, 63, 290, 455, 685, 720, 850, 960, 1035)
      ceilingSpikes(150, 200, 305, 570, 840, 1030)
    }

    val portals = Vector(
      DPortal(105, "SHIP"),
      DPortal(240, "BALL"),
      DPortal(375, "UFO"),
      DPortal(510, "WAVE"),
      DPortal(645, "ROBOT"),
      DPortal(780, "SPIDER"),
      DPortal(915, "CUBE")
    )

    val colors = Vector(
      new Color(0, 230, 255),
      new Color(255, 65, 210),
      new Color(255, 190, 35)
    )

    val names = Vector(
      "NEON WORLD",
      "CYBER RUN",
      "DASH EXTREME"
    )

    DLevel(
      names(n),
      colors(n),
      a.toVector,
      portals,
      1080.0
    )
  }

  val all = Vector(
    makeLevel(0),
    makeLevel(1),
    makeLevel(2)
  )
}

class Dash3DGame extends JPanel {

  val SW = 1150
  val SH = 650

  val HORIZON = 150
  val FOCAL = 520.0
  val CAMERA_HEIGHT = 7.5

  val CEILING = 4.8
  val PLAYER_DISTANCE = 5.0
  val PLAYER_HEIGHT = 0.8

  val SPEED = 0.18

  var levelIndex = 0

  def level: DLevel = DashLevels.all(levelIndex)

  var state = "MENU"
  var mode = "CUBE"

  var worldX = 0.0

  var playerY = 0.0
  var velocityY = 0.0

  var rotation = 0.0
  var gravityDir = 1

  var onGround = true
  var spaceDown = false
  var holdFrames = 0
  var ufoCooldown = 0
  var portalIndex = 0

  val best = Array(0, 0, 0)

  // FIXED: Correctly declared Dust particle collection
  val dust = ArrayBuffer[Dust]()

  val timer = new Timer(16, new ActionListener {
    override def actionPerformed(e: ActionEvent): Unit = {
      updateGame()
      repaint()
    }
  })

  setPreferredSize(new Dimension(SW, SH))
  setBackground(Color.BLACK)
  setFocusable(true)

  installControls()

  addMouseListener(new MouseAdapter {

    override def mousePressed(e: MouseEvent): Unit = {
      requestFocusInWindow()
      pressControl()
    }

    override def mouseReleased(e: MouseEvent): Unit = {
      releaseControl()
    }
  })

  // KEYBOARD CONTROLS
  def bind(key: String, id: String)(action: => Unit): Unit = {

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
      .put(KeyStroke.getKeyStroke(key), id)

    getActionMap.put(id, new AbstractAction {

      override def actionPerformed(e: ActionEvent): Unit = {
        action
      }
    })
  }

  def installControls(): Unit = {

    bind("pressed SPACE", "spacePress") {
      pressControl()
    }

    bind("released SPACE", "spaceRelease") {
      releaseControl()
    }

    bind("pressed UP", "upPress") {
      pressControl()
    }

    bind("released UP", "upRelease") {
      releaseControl()
    }

    bind("pressed W", "wPress") {
      pressControl()
    }

    bind("released W", "wRelease") {
      releaseControl()
    }

    bind("pressed P", "pause") {
      if (state == "PLAY") state = "PAUSE"
      else if (state == "PAUSE") state = "PLAY"
    }

    bind("pressed R", "restart") {
      resetGame()
    }

    bind("pressed ESCAPE", "menu") {
      state = "MENU"
      repaint()
    }

    bind("pressed 1", "levelOne") {
      if (state == "MENU") levelIndex = 0
      repaint()
    }

    bind("pressed 2", "levelTwo") {
      if (state == "MENU") levelIndex = 1
      repaint()
    }

    bind("pressed 3", "levelThree") {
      if (state == "MENU") levelIndex = 2
      repaint()
    }
  }

  // RESET
  def resetGame(): Unit = {

    worldX = 0.0

    playerY = 0.0
    velocityY = 0.0

    rotation = 0.0
    gravityDir = 1

    mode = "CUBE"

    onGround = true
    portalIndex = 0

    holdFrames = 0
    ufoCooldown = 0

    spaceDown = false

    dust.clear()

    state = "PLAY"
    repaint()
  }

  // HOLD / RELEASE
  def pressControl(): Unit = {

    if (!spaceDown) {
      spaceDown = true
      tapAction()
    }
  }

  def releaseControl(): Unit = {
    spaceDown = false
  }

  def tapAction(): Unit = {

    if (state == "MENU" || state == "DEAD") {
      resetGame()
      return
    }

    if (state == "WIN") {

      if (levelIndex < 2) {
        levelIndex += 1
        resetGame()
      } else {
        state = "MENU"
      }

      return
    }

    if (state != "PLAY") return

    mode match {

      case "CUBE" =>
        if (onGround) {
          velocityY = 0.34
          onGround = false
          holdFrames = 0
        }

      case "ROBOT" =>
        if (onGround) {
          velocityY = 0.43
          onGround = false
          holdFrames = 0
        }

      case "BALL" =>
        gravityDir = -gravityDir
        velocityY = 0.0

      case "SPIDER" =>
        gravityDir = -gravityDir
        velocityY = 0.0

      case "UFO" =>
        if (ufoCooldown <= 0) {
          velocityY = 0.31
          ufoCooldown = 12
        }

      case _ =>
    }
  }

  // MODE PORTALS
  def changeMode(next: String): Unit = {

    mode = next

    velocityY = 0.0
    rotation = 0.0
    gravityDir = 1

    holdFrames = 0

    if (next == "SHIP" || next == "WAVE") {
      playerY = 2.3
      onGround = false
    } else {
      playerY = 0.0
      onGround = true
    }
  }

  def updatePortals(): Unit = {

    while (
      portalIndex < level.portals.length &&
      worldX + PLAYER_DISTANCE >= level.portals(portalIndex).x
    ) {

      changeMode(level.portals(portalIndex).mode)
      portalIndex += 1
    }
  }

  // PHYSICS
  def updatePhysics(): Unit = {

    mode match {

      case "CUBE" | "ROBOT" =>

        // Hold for extra jump height
        if (spaceDown && velocityY > 0.0 && holdFrames < 11) {
          velocityY += 0.012
          holdFrames += 1
        }

        velocityY -= 0.026
        playerY += velocityY

        if (playerY <= 0.0) {
          playerY = 0.0
          velocityY = 0.0
          onGround = true

          rotation =
            math.round(rotation / (math.Pi / 2.0)) *
              (math.Pi / 2.0)

        } else {
          onGround = false
          rotation += 0.13
        }

      case "SHIP" =>

        // Holding = UP, releasing = DOWN
        velocityY += (if (spaceDown) 0.018 else -0.018)

        velocityY = math.max(
          -0.18,
          math.min(0.18, velocityY)
        )

        playerY += velocityY

        if (playerY < 0.35 || playerY > 4.5) {
          die()
        }

      case "WAVE" =>

        playerY += (if (spaceDown) 0.105 else -0.105)

        rotation = if (spaceDown) -0.35 else 0.35

        if (playerY < 0.35 || playerY > 4.5) {
          die()
        }

      case "BALL" | "SPIDER" =>

        if (gravityDir == 1) {
          velocityY -= 0.025
        } else {
          velocityY += 0.025
        }

        playerY += velocityY

        if (gravityDir == 1 && playerY <= 0.0) {
          playerY = 0.0
          velocityY = 0.0
        } else if (gravityDir == -1 && playerY >= 4.0) {
          playerY = 4.0
          velocityY = 0.0
        }

        rotation += 0.10 * gravityDir

      case "UFO" =>

        velocityY -= 0.027
        playerY += velocityY

        if (playerY <= 0.0) {
          playerY = 0.0
          velocityY = 0.0
        }

        if (ufoCooldown > 0) {
          ufoCooldown -= 1
        }
    }
  }

  // 3D PERSPECTIVE
  def project(d: Double, y: Double, z: Double): Point = {

    val depth = math.max(1.0, d + 8.0)
    val scale = FOCAL / depth

    val sx = (SW / 2.0 + z * scale).toInt

    val sy = (
      HORIZON + (CAMERA_HEIGHT - y) * scale
    ).toInt

    new Point(sx, sy)
  }

  def poly(g: Graphics2D, pts: Seq[Point], c: Color): Unit = {

    val xs = pts.map(_.x).toArray
    val ys = pts.map(_.y).toArray

    g.setColor(c)
    g.fillPolygon(new Polygon(xs, ys, pts.length))
  }

  // 3D BLOCK
  def drawBox(
    g: Graphics2D,
    d: Double,
    y: Double,
    z: Double,
    len: Double,
    width: Double,
    height: Double,
    c: Color
  ): Unit = {

    val a = project(d, y, z - width / 2)
    val b = project(d + len, y, z - width / 2)
    val cc = project(d + len, y, z + width / 2)
    val dd = project(d, y, z + width / 2)

    val at = project(d, y + height, z - width / 2)
    val bt = project(d + len, y + height, z - width / 2)
    val ct = project(d + len, y + height, z + width / 2)
    val dt = project(d, y + height, z + width / 2)

    poly(g, Seq(a, b, cc, dd), c.darker())
    poly(g, Seq(a, dd, dt, at), c.brighter())
    poly(g, Seq(a, b, bt, at), c)

    poly(g, Seq(at, bt, ct, dt), c.brighter().brighter())

    g.setColor(Color.WHITE)
    g.setStroke(new BasicStroke(1.2f))

    g.drawPolygon(new Polygon(
      Array(at.x, bt.x, ct.x, dt.x),
      Array(at.y, bt.y, ct.y, dt.y),
      4
    ))
  }

  // 3D SPIKE
  def drawPyramid(
    g: Graphics2D,
    d: Double,
    baseY: Double,
    z: Double,
    width: Double,
    len: Double,
    height: Double,
    c: Color,
    pointsUp: Boolean
  ): Unit = {

    val sign = if (pointsUp) 1.0 else -1.0

    val a = project(d, baseY, z - width / 2)
    val b = project(d + len, baseY, z - width / 2)
    val cc = project(d + len, baseY, z + width / 2)
    val dd = project(d, baseY, z + width / 2)

    val tip = project(
      d + len / 2,
      baseY + sign * height,
      z
    )

    poly(g, Seq(a, b, tip), c.brighter())
    poly(g, Seq(b, cc, tip), c)
    poly(g, Seq(cc, dd, tip), c.darker())
    poly(g, Seq(dd, a, tip), c)

    g.setColor(Color.WHITE)
    g.drawLine(a.x, a.y, tip.x, tip.y)
    g.drawLine(b.x, b.y, tip.x, tip.y)
  }

  // 3D ROAD
  def drawRoad(g: Graphics2D): Unit = {

    val farL = project(140.0, 0.0, -5.5)
    val farR = project(140.0, 0.0, 5.5)

    val nearL = project(2.0, 0.0, -5.5)
    val nearR = project(2.0, 0.0, 5.5)

    poly(
      g,
      Seq(farL, farR, nearR, nearL),
      new Color(20, 27, 52)
    )

    g.setColor(level.color)
    g.setStroke(new BasicStroke(3.5f))

    g.drawLine(farL.x, farL.y, nearL.x, nearL.y)
    g.drawLine(farR.x, farR.y, nearR.x, nearR.y)

    g.setColor(new Color(110, 140, 210, 120))
    g.setStroke(new BasicStroke(1.2f))

    var d = 6.0

    while (d < 140.0) {

      val l = project(d, 0.0, -5.5)
      val r = project(d, 0.0, 5.5)

      g.drawLine(l.x, l.y, r.x, r.y)

      d += 8.0
    }

    for (z <- Vector(-2.7, 0.0, 2.7)) {

      val p1 = project(140.0, 0.0, z)
      val p2 = project(2.0, 0.0, z)

      g.setColor(new Color(100, 120, 185, 80))
      g.drawLine(p1.x, p1.y, p2.x, p2.y)
    }
  }

  // DRAW OBSTACLES
  def drawHazards(g: Graphics2D): Unit = {

    level.hazards.sortBy(h => -h.x).foreach { h =>

      val d = h.x - worldX

      if (d > 0.2 && d < 140.0) {

        if (h.kind == "block") {

          drawBox(
            g, d, 0.0, h.z,
            h.len, h.w, h.h,
            new Color(30, 145, 230)
          )

        } else if (h.kind == "ceilingBlock") {

          drawBox(
            g, d, CEILING - h.h, h.z,
            h.len, h.w, h.h,
            new Color(180, 50, 210)
          )

        } else if (h.kind == "spike") {

          drawPyramid(
            g, d, 0.0, h.z,
            h.w, h.len, h.h,
            new Color(0, 230, 255),
            true
          )

        } else if (h.kind == "ceilingSpike") {

          drawPyramid(
            g, d, CEILING, h.z,
            h.w, h.len, h.h,
            new Color(255, 70, 160),
            false
          )
        }
      }
    }
  }

  // PORTALS
  def modeColor(m: String): Color = {

    m match {
      case "SHIP" => Color.CYAN
      case "BALL" => Color.YELLOW
      case "UFO" => Color.PINK
      case "WAVE" => Color.MAGENTA
      case "ROBOT" => Color.ORANGE
      case "SPIDER" => Color.GREEN
      case _ => new Color(0, 230, 255)
    }
  }

  def drawPortals(g: Graphics2D): Unit = {

    level.portals.drop(portalIndex).foreach { p =>

      val d = p.x - worldX

      if (d > 2.0 && d < 140.0) {

        val center = project(d, 2.2, 0.0)

        val scale = FOCAL / (d + 8.0)

        val rx = (scale * 0.48).toInt
        val ry = (scale * 1.4).toInt

        val c = modeColor(p.mode)

        g.setColor(new Color(
          c.getRed,
          c.getGreen,
          c.getBlue,
          65
        ))

        g.fillOval(
          center.x - rx - 5,
          center.y - ry - 5,
          rx * 2 + 10,
          ry * 2 + 10
        )

        g.setColor(c)
        g.setStroke(new BasicStroke(4))

        g.drawOval(
          center.x - rx,
          center.y - ry,
          rx * 2,
          ry * 2
        )

        if (scale > 12.0) {
          g.setFont(new Font("Arial", Font.BOLD, 11))
          g.drawString(p.mode, center.x - 25, center.y - ry - 5)
        }
      }
    }
  }

  // PLAYER MODEL
  def drawPlayer(g: Graphics2D): Unit = {

    val d = PLAYER_DISTANCE
    val y = playerY

    val c = modeColor(mode)

    val center = project(d, y + 0.4, 0.0)
    val scale = FOCAL / (d + 8.0)

    val shadow = project(d, 0.0, 0.0)

    g.setColor(new Color(0, 0, 0, 95))

    g.fillOval(
      shadow.x - 28,
      shadow.y - 5,
      56,
      11
    )

    if (mode == "CUBE" || mode == "ROBOT" || mode == "SPIDER") {

      drawBox(
        g, d, y, 0.0,
        0.78, 0.78, 0.78,
        c
      )

      if (mode == "ROBOT" || mode == "SPIDER") {

        g.setColor(Color.WHITE)
        g.setStroke(new BasicStroke(3))

        g.drawLine(
          center.x - 12, center.y,
          center.x - 19, center.y + 10
        )

        g.drawLine(
          center.x + 12, center.y,
          center.x + 19, center.y + 10
        )
      }

    } else if (mode == "SHIP") {

      val nose = project(d + 0.1, y + 0.35, 0.0)
      val left = project(d + 1.0, y, -0.60)
      val right = project(d + 1.0, y, 0.60)
      val back = project(d + 0.95, y + 0.12, 0.0)

      poly(g, Seq(nose, left, back, right), c)

      g.setColor(Color.WHITE)
      g.setStroke(new BasicStroke(2))

      g.drawLine(nose.x, nose.y, left.x, left.y)
      g.drawLine(nose.x, nose.y, right.x, right.y)

      val flame = project(d + 1.2, y + 0.1, 0.0)

      g.setColor(Color.ORANGE)
      g.fillOval(flame.x - 7, flame.y - 5, 13, 10)

    } else if (mode == "BALL") {

      val r = (scale * 0.38).toInt

      g.setColor(c)
      g.fillOval(center.x - r, center.y - r, 2 * r, 2 * r)

      g.setColor(Color.WHITE)
      g.drawOval(center.x - r, center.y - r, 2 * r, 2 * r)

      g.drawLine(
        center.x - r,
        center.y,
        center.x + r,
        center.y
      )

    } else if (mode == "UFO") {

      val rx = (scale * 0.48).toInt
      val ry = (scale * 0.24).toInt

      g.setColor(c)
      g.fillOval(center.x - rx, center.y - ry, rx * 2, ry * 2)

      g.setColor(Color.WHITE)
      g.drawOval(center.x - rx, center.y - ry, rx * 2, ry * 2)

      g.fillArc(
        center.x - rx / 2,
        center.y - ry * 2,
        rx,
        ry * 2,
        0,
        180
      )

    } else if (mode == "WAVE") {

      val a = project(d + 0.2, y + 0.35, 0.0)
      val b = project(d + 0.9, y + 0.05, 0.5)
      val cc = project(d + 1.4, y + 0.35, 0.0)

      g.setColor(c)
      g.setStroke(new BasicStroke(5))

      g.drawLine(a.x, a.y, b.x, b.y)
      g.drawLine(b.x, b.y, cc.x, cc.y)
    }
  }

  // FIXED DUST SYSTEM
  def updateDust(): Unit = {

    dust.foreach { p =>
      p.x -= 1.0
      p.y += 0.3
      p.life -= 1
    }

    var i = dust.length - 1

    while (i >= 0) {

      if (dust(i).life <= 0) {
        dust.remove(i)
      }

      i -= 1
    }
  }

  def addExplosion(): Unit = {

    val center = project(
      PLAYER_DISTANCE,
      playerY + 0.4,
      0.0
    )

    for (i <- 0 until 25) {

      val a = i * math.Pi * 2.0 / 25.0

      dust += Dust(
        center.x + math.cos(a) * 15,
        center.y + math.sin(a) * 15,
        35
      )
    }
  }

  def drawDust(g: Graphics2D): Unit = {

    dust.foreach { p =>

      val alpha = math.max(
        0,
        math.min(255, p.life * 7)
      )

      g.setColor(new Color(
        level.color.getRed,
        level.color.getGreen,
        level.color.getBlue,
        alpha
      ))

      g.fillOval(
        p.x.toInt,
        p.y.toInt,
        7,
        7
      )
    }
  }

  def die(): Unit = {

    if (state != "DEAD") {

      state = "DEAD"
      addExplosion()
    }
  }

  // COLLISION
  def checkCollision(): Boolean = {

    level.hazards.exists { h =>

      val d = h.x - worldX

      if (math.abs(d - PLAYER_DISTANCE) > 0.85) {

        false

      } else {

        h.kind match {

          case "ceilingSpike" | "ceilingBlock" =>
            playerY + PLAYER_HEIGHT > CEILING - h.h

          case "spike" | "block" =>
            playerY < h.h &&
              playerY + PLAYER_HEIGHT > 0.0

          case _ =>
            false
        }
      }
    }
  }

  // MAIN UPDATE
  def updateGame(): Unit = {

    updateDust()

    if (state != "PLAY") return

    worldX += SPEED

    updatePortals()
    updatePhysics()

    if (state != "PLAY") return

    if (checkCollision()) {
      die()
    }

    val percent = math.min(
      100,
      (worldX / level.finish * 100).toInt
    )

    if (percent > best(levelIndex)) {
      best(levelIndex) = percent
    }

    if (worldX + PLAYER_DISTANCE >= level.finish) {
      state = "WIN"
    }
  }

  // BACKGROUND
  def drawBackground(g: Graphics2D): Unit = {

    g.setPaint(new GradientPaint(
      0.0f, 0.0f,
      new Color(4, 7, 24),
      0.0f, SH.toFloat,
      new Color(30, 12, 61)
    ))

    g.fillRect(0, 0, SW, SH)

    for (i <- 0 until 65) {

      val sx =
        (i * 97 - (worldX * 1.3).toInt % SW + SW) % SW

      val sy = (i * 47) % 400

      g.setColor(new Color(210, 225, 255, 115))
      g.fillOval(sx, sy, 3, 3)
    }

    val glow = new GradientPaint(
      0.0f, (HORIZON - 20).toFloat,
      new Color(
        level.color.getRed,
        level.color.getGreen,
        level.color.getBlue,
        55
      ),
      0.0f, (HORIZON + 100).toFloat,
      new Color(5, 5, 25, 0)
    )

    g.setPaint(glow)
    g.fillRect(0, HORIZON - 20, SW, 130)
  }

  // HUD
  def drawHUD(g: Graphics2D): Unit = {

    val percent = math.min(
      100,
      (worldX / level.finish * 100).toInt
    )

    g.setColor(Color.WHITE)
    g.setFont(new Font("Arial", Font.BOLD, 17))

    g.drawString(level.name, 25, 32)
    g.drawString("MODE: " + mode, 25, 60)
    g.drawString("BEST: " + best(levelIndex) + "%", 25, 88)

    g.setColor(new Color(55, 60, 90))
    g.fillRoundRect(375, 20, 400, 14, 12, 12)

    g.setColor(level.color)

    g.fillRoundRect(
      375,
      20,
      (400 * percent / 100.0).toInt,
      14,
      12,
      12
    )

    g.setColor(Color.WHITE)
    g.drawString(percent + "%", 790, 33)

    g.setFont(new Font("Arial", Font.BOLD, 14))
    g.drawString("HOLD = UP | RELEASE = DOWN", 900, 34)
  }

  def centerText(
    g: Graphics2D,
    text: String,
    y: Int
  ): Unit = {

    val fm = g.getFontMetrics

    g.drawString(
      text,
      (SW - fm.stringWidth(text)) / 2,
      y
    )
  }

  // MENU
  def drawMenu(g: Graphics2D): Unit = {

    g.setColor(new Color(0, 0, 15, 185))
    g.fillRect(0, 0, SW, SH)

    g.setFont(new Font("Arial", Font.BOLD, 76))
    g.setColor(level.color)

    centerText(g, "NEON DASH 3D", 170)

    g.setFont(new Font("Arial", Font.BOLD, 23))
    g.setColor(Color.WHITE)

    centerText(g, "ULTIMATE PERSPECTIVE RUNNER", 215)

    g.setFont(new Font("Arial", Font.BOLD, 20))
    g.setColor(level.color)

    centerText(g, "SELECT LEVEL:  1     2     3", 310)

    g.setColor(Color.WHITE)
    centerText(g, level.name, 350)

    g.setFont(new Font("Arial", Font.BOLD, 22))
    g.setColor(Color.YELLOW)

    centerText(g, "PRESS SPACE TO START", 425)

    g.setFont(new Font("Arial", Font.PLAIN, 17))
    g.setColor(Color.LIGHT_GRAY)

    centerText(
      g,
      "CUBE  SHIP  BALL  UFO  WAVE  ROBOT  SPIDER",
      485
    )

    centerText(
      g,
      "SPACE = JUMP / FLY    P = PAUSE    R = RESTART",
      525
    )

    centerText(
      g,
      "HOLD SPACE TO RISE - RELEASE TO FALL",
      560
    )
  }

  // OVERLAYS
  def overlay(
    g: Graphics2D,
    title: String,
    sub: String,
    c: Color
  ): Unit = {

    g.setColor(new Color(0, 0, 12, 205))
    g.fillRect(0, 0, SW, SH)

    g.setFont(new Font("Arial", Font.BOLD, 58))
    g.setColor(c)

    centerText(g, title, 250)

    g.setFont(new Font("Arial", Font.BOLD, 22))
    g.setColor(Color.WHITE)

    centerText(g, sub, 315)
  }

  // MAIN DRAW
  override def paintComponent(graphics: Graphics): Unit = {

    super.paintComponent(graphics)

    val g = graphics.create().asInstanceOf[Graphics2D]

    g.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON
    )

    drawBackground(g)
    drawRoad(g)
    drawPortals(g)
    drawHazards(g)
    drawPlayer(g)
    drawDust(g)

    if (state == "PLAY") {

      drawHUD(g)

    } else if (state == "MENU") {

      drawMenu(g)

    } else if (state == "PAUSE") {

      overlay(g, "PAUSED", "PRESS P TO RESUME", Color.YELLOW)

    } else if (state == "DEAD") {

      overlay(g, "CRASHED!", "PRESS SPACE TO RETRY", Color.RED)

    } else if (state == "WIN") {

      overlay(g, "LEVEL COMPLETE!", "PRESS SPACE FOR NEXT LEVEL", Color.GREEN)
    }

    g.dispose()
  }
}

// =================================
// DIRECT SWING WINDOW LAUNCH
// =================================

SwingUtilities.invokeLater(new Runnable {

  override def run(): Unit = {

    val game = new Dash3DGame()

    val window = new JFrame("NEON DASH 3D")

    window.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)

    window.setContentPane(game)
    window.pack()
    window.setResizable(false)
    window.setLocationRelativeTo(null)

    window.setVisible(true)
    window.toFront()

    game.timer.start()
    game.requestFocusInWindow()
  }
})