Skip to content

Commit

Permalink
GameViewport is an opaque type (#778)
Browse files Browse the repository at this point in the history
* feat: deprecate GameViewport.scala

* Revert "feat: deprecate GameViewport.scala"

This reverts commit f6560cc.

* GameViewport is an opaque type

* GameViewport is an opaque type

- fix formating

* Remove type bounds from GameViewport

* OOPS: Scalafmt run

---------

Co-authored-by: davesmith00000 <[email protected]>
  • Loading branch information
david-lebl-adastra and davesmith00000 authored Nov 8, 2024
1 parent b9ac0ed commit f358c45
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 63 deletions.
2 changes: 2 additions & 0 deletions indigo/indigo/src/main/scala/indigo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ object syntax:
extension [A](l: List[Option[A]]) def sequence: Option[List[A]] = NonEmptyList.sequenceListOption(l)
extension [A](l: NonEmptyList[Option[A]]) def sequence: Option[NonEmptyList[A]] = NonEmptyList.sequenceOption(l)

extension (s: Size) def toGameViewport: GameViewport = GameViewport(s)

// Timeline animations
object animations:
import indigo.shared.animation.timeline.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ final case class GameConfig(

def withViewport(width: Int, height: Int): GameConfig =
this.copy(viewport = GameViewport(width, height))
def withViewport(size: Size): GameConfig =
this.copy(viewport = GameViewport(size.width, size.height))
def withViewport(newViewport: GameViewport): GameConfig =
this.copy(viewport = newViewport)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,31 @@ import indigo.shared.datatypes.Point
import indigo.shared.datatypes.Rectangle
import indigo.shared.datatypes.Size

/** Respresents the initial size of the game's viewport.
*
* @param width
* Width in pixels
* @param height
* Height in pixels
*/
final case class GameViewport(size: Size) derives CanEqual:
val width: Int = size.width
val height: Int = size.height
val horizontalMiddle: Int = width / 2
val verticalMiddle: Int = height / 2
val center: Point = Point(horizontalMiddle, verticalMiddle)

@deprecated("use 'toRectangle' instead")
def asRectangle: Rectangle =
toRectangle
def toRectangle: Rectangle =
Rectangle(Point.zero, size)

def toPoint: Point =
size.toPoint

def toSize: Size =
size

def bounds: Rectangle =
toRectangle

def giveDimensions(magnification: Int): Rectangle =
Rectangle(0, 0, width / magnification, height / magnification)

opaque type GameViewport = Size
object GameViewport:
def apply(size: Size): GameViewport = size
def apply(width: Int, height: Int): GameViewport = Size(width, height)

val atWUXGA: GameViewport = GameViewport(1920, 1200)
val atWUXGABy2: GameViewport = GameViewport(960, 600)

val at1080p: GameViewport = GameViewport(1920, 1080)
val at1080pBy2: GameViewport = GameViewport(960, 540)

def apply(width: Int, height: Int): GameViewport =
GameViewport(Size(width, height))
val at720p: GameViewport = GameViewport(1280, 720)
val at720pBy2: GameViewport = GameViewport(640, 360)

val atWUXGA: GameViewport =
GameViewport(1920, 1200)
val atWUXGABy2: GameViewport =
GameViewport(960, 600)
extension (a: GameViewport)
def width: Int = a.width
def height: Int = a.height

val at1080p: GameViewport =
GameViewport(1920, 1080)
val at1080pBy2: GameViewport =
GameViewport(960, 540)
def horizontalMiddle: Int = a.width / 2
def verticalMiddle: Int = a.height / 2
def center: Point = Point(horizontalMiddle, verticalMiddle)

val at720p: GameViewport =
GameViewport(1280, 720)
val at720pBy2: GameViewport =
GameViewport(640, 360)
def toRectangle: Rectangle = Rectangle(Point.zero, a)
def size: Size = a
def toSize: Size = a
def bounds: Rectangle = toRectangle
def giveDimensions(magnification: Int): Rectangle =
Rectangle(0, 0, a.width / magnification, a.height / magnification)
17 changes: 4 additions & 13 deletions indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ import indigo.shared.datatypes.Size
*/
sealed trait Camera:
def position: Point
def topLeft(viewport: GameViewport): Point
def topLeft(viewport: Size): Point
def bounds(viewport: GameViewport): Rectangle
def bounds(viewport: Size): Rectangle
def frustum(viewport: GameViewport): Rectangle = bounds(viewport)
def frustum(viewport: Size): Rectangle = bounds(viewport)
def frustum(viewport: Size): Rectangle = bounds(viewport)
def zoom: Zoom
def rotation: Radians
def isLookAt: Boolean
Expand All @@ -31,11 +28,9 @@ object Camera:
* while controlling the position, zoom and rotation.
*/
final case class Fixed(position: Point, zoom: Zoom, rotation: Radians) extends Camera:
def topLeft(viewport: GameViewport): Point = position
def topLeft(viewport: Size): Point = position
def bounds(viewport: GameViewport): Rectangle = Rectangle(position, viewport.size)
def bounds(viewport: Size): Rectangle = Rectangle(position, viewport)
val isLookAt: Boolean = false
def topLeft(viewport: Size): Point = position
def bounds(viewport: Size): Rectangle = Rectangle(position, viewport)
val isLookAt: Boolean = false

def withX(newX: Int): Fixed =
this.copy(position = position.withX(newX))
Expand Down Expand Up @@ -78,12 +73,8 @@ object Camera:
final case class LookAt(target: Point, zoom: Zoom, rotation: Radians) extends Camera:
val isLookAt: Boolean = true
val position: Point = target
def topLeft(viewport: GameViewport): Point =
topLeft(viewport.size)
def topLeft(viewport: Size): Point =
target - (viewport.toPoint / 2) / zoom.toDouble.toInt
def bounds(viewport: GameViewport): Rectangle =
bounds(viewport.size)
def bounds(viewport: Size): Rectangle =
Rectangle(topLeft(viewport), viewport)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package example.game

import cats.effect.IO
import indigo._
import indigo.scenes._
import indigo.*
import indigo.scenes.*
import tyrian.TyrianSubSystem

final case class MyAwesomeGame(tyrianSubSystem: TyrianSubSystem[IO, String, Unit], clockwise: Boolean)
Expand Down

0 comments on commit f358c45

Please sign in to comment.