-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
ThisBuild / version := "0.1.0-SNAPSHOT" | ||
|
||
ThisBuild / scalaVersion := "3.1.1" | ||
|
||
lazy val root = (project in file(".")) | ||
.settings( | ||
name := "Pong", | ||
libraryDependencies ++= Dependencies.Gdx(), | ||
javacOptions ++= Seq( | ||
"-Xlint", | ||
"-encoding", "UTF-8", | ||
"-source", "17", | ||
"-target", "17" | ||
), | ||
scalacOptions ++= Seq( | ||
"-unchecked", | ||
"-deprecation", | ||
"-feature", | ||
"-encoding", "UTF-8", | ||
), | ||
assembly / assemblyJarName := "pong.jar" | ||
) | ||
|
||
ThisBuild / assemblyMergeStrategy := { | ||
case PathList(path @ _*) if path.last == "module-info.class" => | ||
MergeStrategy.discard | ||
case x => | ||
val strategy = (ThisBuild / assemblyMergeStrategy).value | ||
strategy(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import sbt._ | ||
|
||
object Dependencies { | ||
|
||
object Gdx { | ||
|
||
private def dependency(module: String): ModuleID = | ||
"com.badlogicgames.gdx" % module % "1.10.0" | ||
|
||
private def moduleName(module: Option[String]): String = | ||
"gdx" + module.map("-" + _).getOrElse("") | ||
|
||
private def dependency(module: Option[String]): ModuleID = { | ||
val name = moduleName(module) | ||
dependency(name) | ||
} | ||
|
||
private def nativeDependency(module: Option[String]): ModuleID = { | ||
val name = moduleName(module) + "-platform" | ||
dependency(name) classifier "natives-desktop" | ||
} | ||
|
||
def apply(): Seq[ModuleID] = { | ||
val box2d = Some("box2d") | ||
val ttf = Some("freetype") | ||
val core = None | ||
|
||
val dependencies = Seq( | ||
core, | ||
Some("backend-lwjgl3"), | ||
box2d, | ||
ttf, | ||
).map(dependency) | ||
|
||
val nativeDependencies = Seq( | ||
core, | ||
box2d, | ||
ttf, | ||
).map(nativeDependency) | ||
|
||
dependencies ++ nativeDependencies | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version = 1.6.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0") |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
64 changes: 64 additions & 0 deletions
64
src/main/scala/com/scalamandra/libgdx/routing/GameRouter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.scalamandra.libgdx.routing | ||
|
||
import com.badlogic.gdx.{ApplicationListener, Game, Gdx, Screen} | ||
import com.badlogic.gdx.utils.{Disposable, IntMap, TimeUtils} | ||
import com.scalamandra.libgdx.utils.Manageable | ||
|
||
import scala.collection.mutable | ||
|
||
abstract class GameRouter extends ApplicationListener with Router with Manageable { | ||
private var current: Screen | Null = null | ||
private val screens = IntMap[() => Screen]() | ||
|
||
private inline def withScreen(action: Screen => Unit): Unit = | ||
if(current != null) action(current) | ||
|
||
use { | ||
new Disposable { | ||
override def dispose(): Unit = withScreen(_.dispose()) | ||
} | ||
} | ||
|
||
protected def addScreen[T <: Screen](route: Route[T])(factory: => T): Unit = | ||
screens.put(route.internal, () => factory) | ||
|
||
protected def addScreen[T <: Screen, R <: Navigator[T]](navigator: R)(factory: => T): Unit = | ||
addScreen(navigator.route)(factory) | ||
|
||
override def moveTo[T <: Screen](route: Route[T]): Unit = | ||
screens.get(route.internal) match { | ||
case null => | ||
throw NoSuchScreen(route) | ||
case factory => | ||
setScreen(factory()) | ||
} | ||
|
||
def getScreen: Screen | Null = current | ||
|
||
def setScreen(screen: Screen | Null): Unit = { | ||
withScreen { s => | ||
s.hide() | ||
s.dispose() | ||
} | ||
current = screen | ||
withScreen { s => | ||
s.show() | ||
s.resize( | ||
Gdx.graphics.getWidth, | ||
Gdx.graphics.getHeight, | ||
) | ||
} | ||
} | ||
|
||
override def resize(width: Int, height: Int): Unit = | ||
withScreen(_.resize(width, height)) | ||
|
||
override def render(): Unit = | ||
withScreen(_.render(Gdx.graphics.getDeltaTime)) | ||
|
||
override def pause(): Unit = | ||
withScreen(_.pause) | ||
|
||
override def resume(): Unit = | ||
withScreen(_.resume) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/scala/com/scalamandra/libgdx/routing/Navigator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.scalamandra.libgdx.routing | ||
|
||
import com.badlogic.gdx.Screen | ||
|
||
trait Navigator[T <: Screen] { | ||
|
||
final val route = Route[T] | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/scala/com/scalamandra/libgdx/routing/NoSuchScreen.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.scalamandra.libgdx.routing | ||
|
||
import com.badlogic.gdx.Screen | ||
|
||
case class NoSuchScreen[T <: Screen](route: Route[T]) extends RuntimeException(s"No such screen, route: $route") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.scalamandra.libgdx.routing | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
opaque type Route[T] = Int | ||
object Route { | ||
private val counter = AtomicInteger(0) | ||
private[routing] def apply[T]: Route[T] = counter.incrementAndGet() | ||
} | ||
extension[T] (self: Route[T]) { | ||
|
||
private[routing] inline def internal: Int = self | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/scala/com/scalamandra/libgdx/routing/Router.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.scalamandra.libgdx.routing | ||
|
||
import com.badlogic.gdx.{Gdx, Screen} | ||
|
||
trait Router { | ||
|
||
def moveTo[T <: Screen](route: Route[T]): Unit | ||
|
||
final def moveTo[T <: Screen, R <: Navigator[T]](navigator: R): Unit = | ||
moveTo(navigator.route) | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/scala/com/scalamandra/libgdx/screens/GameScreen.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
import com.badlogic.gdx.Screen | ||
import com.badlogic.gdx.utils.Disposable | ||
import com.scalamandra.libgdx.utils.{*, given} | ||
|
||
abstract class GameScreen extends Screen with UpdatableView[Unit] with Manageable { | ||
|
||
override def render(delta: Float): Unit = { | ||
view() | ||
update(delta) | ||
} | ||
|
||
override def resize(width: Int, height: Int): Unit = () | ||
|
||
override def pause(): Unit = () | ||
|
||
override def resume(): Unit = () | ||
|
||
override def hide(): Unit = () | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/scala/com/scalamandra/libgdx/screens/SimpleDraw.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
import com.badlogic.gdx.Screen | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch | ||
import com.badlogic.gdx.graphics.OrthographicCamera | ||
import com.scalamandra.libgdx.utils.{*, given} | ||
|
||
trait SimpleDraw { | ||
this: Screen => | ||
|
||
protected def camera: OrthographicCamera | ||
|
||
protected inline final def draw[T: Renderer](via: T)(drawAction: => Unit): Unit = { | ||
camera.update() | ||
via.setProjectionMatrix(camera.combined) | ||
via.scoped { | ||
drawAction | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
import com.badlogic.gdx.graphics.g2d.{BitmapFont, GlyphLayout, SpriteBatch} | ||
import com.badlogic.gdx.math.Vector2 | ||
import TextAlign.* | ||
|
||
class Text( | ||
var x: Float, | ||
var y: Float, | ||
val layout: GlyphLayout, | ||
font: BitmapFont, | ||
batch: SpriteBatch, | ||
align: TextAlign, | ||
) extends View { | ||
|
||
override def view(): Unit = font.draw(batch, layout, x, y) | ||
|
||
def setText(text: String): Unit = { | ||
val prevWidth = layout.width | ||
layout.setText(font, text) | ||
val currWidth = layout.width | ||
val diff = currWidth - prevWidth | ||
align match { | ||
case Right => | ||
x -= diff | ||
case Center => | ||
x -= diff / 2 | ||
case Left => () | ||
} | ||
} | ||
|
||
} | ||
object Text { | ||
|
||
inline def apply(x: Float, | ||
y: Float, | ||
layout: GlyphLayout, | ||
font: BitmapFont, | ||
batch: SpriteBatch, | ||
align: TextAlign): Text = { | ||
new Text(x, y, layout, font, batch, align) | ||
} | ||
|
||
inline def apply(text: String, | ||
font: BitmapFont, | ||
batch: SpriteBatch, | ||
align: TextAlign = Center) | ||
(position: GlyphLayout => Vector2): Text = { | ||
val layout = GlyphLayout(font, text) | ||
val coordinates = position(layout) | ||
Text( | ||
coordinates.x, | ||
coordinates.y, | ||
layout, | ||
font, | ||
batch, | ||
align | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/scala/com/scalamandra/libgdx/screens/TextAlign.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
enum TextAlign { | ||
case Left extends TextAlign | ||
case Right extends TextAlign | ||
case Center extends TextAlign | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/scala/com/scalamandra/libgdx/screens/UpdatableView.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
trait UpdatableView[T] extends View with Update[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
trait Update[T] { | ||
|
||
def update(delta: Float): T | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.scalamandra.libgdx.screens | ||
|
||
trait View { | ||
|
||
def view(): Unit | ||
|
||
} |
Oops, something went wrong.