Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolDalek authored Mar 4, 2022
1 parent 92a50db commit c356c91
Show file tree
Hide file tree
Showing 39 changed files with 1,329 additions and 0 deletions.
30 changes: 30 additions & 0 deletions build.sbt
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)
}
46 changes: 46 additions & 0 deletions project/Dependencies.scala
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
}

}

}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 1.6.2
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")
Binary file added src/main/resources/assets/font.ttf
Binary file not shown.
Binary file added src/main/resources/assets/paddle_hit.wav
Binary file not shown.
Binary file added src/main/resources/assets/score.wav
Binary file not shown.
Binary file added src/main/resources/assets/wall_hit.wav
Binary file not shown.
64 changes: 64 additions & 0 deletions src/main/scala/com/scalamandra/libgdx/routing/GameRouter.scala
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 src/main/scala/com/scalamandra/libgdx/routing/Navigator.scala
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]

}
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")
14 changes: 14 additions & 0 deletions src/main/scala/com/scalamandra/libgdx/routing/Route.scala
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 src/main/scala/com/scalamandra/libgdx/routing/Router.scala
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 src/main/scala/com/scalamandra/libgdx/screens/GameScreen.scala
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 src/main/scala/com/scalamandra/libgdx/screens/SimpleDraw.scala
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
}
}

}
60 changes: 60 additions & 0 deletions src/main/scala/com/scalamandra/libgdx/screens/Text.scala
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 src/main/scala/com/scalamandra/libgdx/screens/TextAlign.scala
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
}
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]
7 changes: 7 additions & 0 deletions src/main/scala/com/scalamandra/libgdx/screens/Update.scala
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

}
7 changes: 7 additions & 0 deletions src/main/scala/com/scalamandra/libgdx/screens/View.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.scalamandra.libgdx.screens

trait View {

def view(): Unit

}
Loading

0 comments on commit c356c91

Please sign in to comment.