forked from nova/jmonkey-test
Rohan Sircar
3 years ago
34 changed files with 728 additions and 438 deletions
-
87build.sbt
-
36src/main/scala/wow/doge/mygame/AppError.scala
-
3src/main/scala/wow/doge/mygame/Main.scala
-
177src/main/scala/wow/doge/mygame/MainApp.scala
-
4src/main/scala/wow/doge/mygame/MainModule.scala
-
52src/main/scala/wow/doge/mygame/game/GameApp.scala
-
2src/main/scala/wow/doge/mygame/game/GameAppActor.scala
-
3src/main/scala/wow/doge/mygame/game/entities/player/PlayerActorSupervisor.scala
-
35src/main/scala/wow/doge/mygame/game/entities/player/PlayerController.scala
-
3src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala
-
4src/main/scala/wow/doge/mygame/game/subsystems/level/DefaultGameLevel.scala
-
37src/main/scala/wow/doge/mygame/game/subsystems/level/GameLevel.scala
-
7src/main/scala/wow/doge/mygame/game/subsystems/movement/CanMove2.scala
-
2src/main/scala/wow/doge/mygame/game/subsystems/movement/MovementActor.scala
-
11src/main/scala/wow/doge/mygame/implicits/CatsImplicits.scala
-
3src/main/scala/wow/doge/mygame/launcher/Launcher.scala
-
45src/main/scala/wow/doge/mygame/math/ImVector3f.scala
-
22src/main/scala/wow/doge/mygame/subsystems/events/EventBus.scala
-
50src/main/scala/wow/doge/mygame/subsystems/events/EventsModule.scala
-
260src/main/scala/wow/doge/mygame/subsystems/moddingsystem/ModdingSystem.scala
-
6src/main/scala/wow/doge/mygame/subsystems/scriptsystem/ScriptSystemModule.scala
-
8src/main/scala/wow/doge/mygame/utils/AkkaUtils.scala
-
7src/main/scala/wow/doge/mygame/utils/GenericTimerActor.scala
-
31src/main/scala/wow/doge/mygame/utils/ReaderDemo.scala
-
16src/main/scala/wow/doge/mygame/utils/wrappers/jme/AssetManager.scala
-
19src/main/scala/wow/doge/mygame/utils/wrappers/jme/CollisionShapeFactory.scala
-
14src/main/scala/wow/doge/mygame/utils/wrappers/jme/Node.scala
-
14src/main/scala/wow/doge/mygame/utils/wrappers/jme/PhysicsSpace.scala
-
21src/test/scala/wow/doge/mygame/AssetManagerTest.scala
-
5src/test/scala/wow/doge/mygame/CollisionShapeFactoryTest.scala
-
51src/test/scala/wow/doge/mygame/ImVector3fTest.scala
-
27src/test/scala/wow/doge/mygame/ModdingSystemTest.scala
-
67src/test/scala/wow/doge/mygame/ReaderT_Test.scala
-
37src/test/scala/wow/doge/mygame/ReaderTest.scala
@ -1,6 +1,40 @@ |
|||||
package wow.doge.mygame |
package wow.doge.mygame |
||||
|
|
||||
|
import java.util.concurrent.TimeoutException |
||||
|
|
||||
|
import cats.data.Reader |
||||
|
import monix.bio.IO |
||||
|
import wow.doge.mygame.utils.wrappers.jme.AssetManager |
||||
|
import wow.doge.mygame.utils.wrappers.jme.CollisionShapeFactory |
||||
|
import wow.doge.mygame.utils.wrappers.jme.NodeWrapper2 |
||||
|
|
||||
sealed trait AppError |
sealed trait AppError |
||||
object AppError { |
object AppError { |
||||
case class TimeoutError(reason: String) extends AppError |
|
||||
|
final case class TimeoutError(reason: String) extends AppError |
||||
|
object TimeoutError { |
||||
|
def reader = |
||||
|
Reader[Throwable, TimeoutError] { |
||||
|
case ex: TimeoutException => TimeoutError(ex.getMessage) |
||||
|
} |
||||
|
def from: PartialFunction[Throwable, IO[TimeoutError, Nothing]] = { |
||||
|
case ex: TimeoutException => IO.raiseError(TimeoutError(ex.getMessage)) |
||||
|
} |
||||
|
} |
||||
|
final case class DummyError(reason: String) extends AppError |
||||
|
object DummyError { |
||||
|
def reader = |
||||
|
Reader[Throwable, DummyError] { |
||||
|
case ex: NullPointerException => DummyError(ex.getMessage) |
||||
|
} |
||||
|
} |
||||
|
final case class AssetManagerError(error: AssetManager.Error) extends AppError |
||||
|
final case class AppNodeError(error: NodeWrapper2.Error) extends AppError |
||||
|
final case class CollisionShapeCreationFailed( |
||||
|
err: CollisionShapeFactory.Error |
||||
|
) extends AppError |
||||
|
|
||||
|
def fromThrowable: PartialFunction[Throwable, IO[AppError, Nothing]] = { |
||||
|
case ex: TimeoutException => IO.raiseError(TimeoutError(ex.getMessage)) |
||||
|
|
||||
|
} |
||||
} |
} |
@ -1,29 +1,44 @@ |
|||||
package wow.doge.mygame.math; |
package wow.doge.mygame.math; |
||||
|
|
||||
import cats.Show |
import cats.Show |
||||
|
import cats.kernel.Eq |
||||
|
import cats.syntax.eq._ |
||||
|
|
||||
import math.{abs, pow, sqrt} |
import math.{abs, pow, sqrt} |
||||
|
|
||||
case class ImVector3f(x: Float, y: Float, z: Float) |
case class ImVector3f(x: Float, y: Float, z: Float) |
||||
object ImVector3f { |
object ImVector3f { |
||||
val ZERO = ImVector3f(0, 0, 0) |
|
||||
val UNIT_X = ImVector3f(1, 0, 0) |
|
||||
val UNIT_Y = ImVector3f(0, 1, 0) |
|
||||
val UNIT_Z = ImVector3f(0, 0, 1) |
|
||||
|
|
||||
def dst(v1: ImVector3f, v2: ImVector3f) = |
|
||||
sqrt( |
|
||||
pow((v1.x - v2.x).toDouble, 2) + pow((v1.y - v2.y).toDouble, 2) + pow( |
|
||||
(v1.z - v2.z).toDouble, |
|
||||
2 |
|
||||
) |
|
||||
) |
|
||||
def manhattanDst(v1: ImVector3f, v2: ImVector3f) = |
|
||||
abs(v1.x - v2.x) + abs(v1.y - v2.y) + abs(v1.z - v2.z) |
|
||||
|
//format: off |
||||
|
val Zero = ImVector3f(0, 0, 0) |
||||
|
val UnitX = ImVector3f(1, 0, 0) |
||||
|
val UnitY = ImVector3f(0, 1, 0) |
||||
|
val UnitZ = ImVector3f(0, 0, 1) |
||||
|
val Unit = ImVector3f(1, 1, 1) |
||||
|
//format: on |
||||
|
val Max = ImVector3f(Float.MaxValue, Float.MaxValue, Float.MaxValue) |
||||
|
val Min = ImVector3f(Float.MinValue, Float.MinValue, Float.MinValue) |
||||
|
|
||||
implicit val showForImVector3f = new Show[ImVector3f] { |
|
||||
|
implicit val show = new Show[ImVector3f] { |
||||
def format(f: Float) = f.formatted("%.2f") |
def format(f: Float) = f.formatted("%.2f") |
||||
override def show(t: ImVector3f): String = |
override def show(t: ImVector3f): String = |
||||
s"ImVector3f(${format(t.x)},${format(t.y)},${format(t.z)})" |
s"ImVector3f(${format(t.x)},${format(t.y)},${format(t.z)})" |
||||
} |
} |
||||
|
|
||||
|
implicit val eq = Eq.fromUniversalEquals[ImVector3f] |
||||
|
|
||||
|
private def squareDiff(f1: Float, f2: Float) = |
||||
|
pow(f1.toDouble - f2.toDouble, 2) |
||||
|
// private def squareDiff2(f1: Float, f2: Float) = pow((f1 - f2).toDouble, 2) |
||||
|
|
||||
|
def dst(v1: ImVector3f, v2: ImVector3f): Double = |
||||
|
if (v1 === v2) 0 |
||||
|
else { |
||||
|
val total = |
||||
|
squareDiff(v1.x, v2.x) + squareDiff(v1.y, v2.y) + |
||||
|
squareDiff(v1.z, v2.z) |
||||
|
sqrt(total) |
||||
|
} |
||||
|
def manhattanDst(v1: ImVector3f, v2: ImVector3f) = |
||||
|
abs(v1.x - v2.x) + abs(v1.y - v2.y) + abs(v1.z - v2.z) |
||||
|
|
||||
} |
} |
@ -0,0 +1,31 @@ |
|||||
|
package wow.doge.mygame.utils |
||||
|
import cats.data.Reader |
||||
|
import cats.data.ReaderT |
||||
|
import monix.bio.UIO |
||||
|
|
||||
|
object ReaderDemo { |
||||
|
|
||||
|
type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]] |
||||
|
val IoReaderT = ReaderT |
||||
|
val t = |
||||
|
ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt) |
||||
|
.run("s") |
||||
|
.rethrow |
||||
|
val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt) |
||||
|
val t2 = r.run("s").rethrow |
||||
|
// Kleisli[IO, String, Unit](s => IO.unit) |
||||
|
|
||||
|
case class Environment(str: String, num: Int) |
||||
|
|
||||
|
def fun1: Reader[String, UIO[Unit]] = Reader(str => UIO(println(str))) |
||||
|
def fun2: Reader[Int, UIO[Unit]] = Reader(num => UIO(println(num))) |
||||
|
|
||||
|
def total: Reader[Environment, UIO[Unit]] = |
||||
|
for { |
||||
|
x <- fun1.local[Environment](_.str) |
||||
|
y <- fun2.local[Environment](_.num) |
||||
|
} yield UIO.parSequence(List(x, y)).void |
||||
|
|
||||
|
val io: UIO[Unit] = total.run(Environment("hello", 50)) |
||||
|
|
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package wow.doge.mygame |
||||
|
|
||||
|
import org.scalatest.funsuite.AnyFunSuite |
||||
|
import wow.doge.mygame.math.ImVector3f |
||||
|
import com.typesafe.scalalogging.LazyLogging |
||||
|
import cats.syntax.eq._ |
||||
|
import cats.syntax.show._ |
||||
|
|
||||
|
class ImVector3fTest extends AnyFunSuite with LazyLogging { |
||||
|
test("maxvalue") { |
||||
|
val v1 = ImVector3f.Max |
||||
|
val v2 = ImVector3f.Max |
||||
|
logger.info(ImVector3f.dst(v1, v2).show) |
||||
|
} |
||||
|
|
||||
|
test("minvalue") { |
||||
|
val v1 = ImVector3f.Min |
||||
|
val v2 = ImVector3f.Min |
||||
|
logger.info(ImVector3f.dst(v1, v2).show) |
||||
|
} |
||||
|
|
||||
|
test("maxvalue and unit") { |
||||
|
val v1 = ImVector3f.Max |
||||
|
val v2 = ImVector3f(1, 1, 1) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv 5.8938631329669654e38) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv ImVector3f.dst(v2, v1)) |
||||
|
} |
||||
|
|
||||
|
test("minvalue and unit") { |
||||
|
val v1 = ImVector3f.Min |
||||
|
val v2 = ImVector3f(1, 1, 1) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv 5.8938631329669654e38) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv ImVector3f.dst(v2, v1)) |
||||
|
} |
||||
|
|
||||
|
test("another") { |
||||
|
{ |
||||
|
val v1 = ImVector3f(1, 0, 0) |
||||
|
val v2 = ImVector3f(1, 1, 1) |
||||
|
logger.info(ImVector3f.dst(v1, v2).show) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv ImVector3f.dst(v2, v1)) |
||||
|
} |
||||
|
{ |
||||
|
val v1 = ImVector3f(1, 1, 0) |
||||
|
val v2 = ImVector3f(1, 1, 1) |
||||
|
logger.info(ImVector3f.dst(v1, v2).show) |
||||
|
assert(ImVector3f.dst(v1, v2) eqv ImVector3f.dst(v2, v1)) |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package wow.doge.mygame |
||||
|
|
||||
|
import org.scalatest.funsuite.AnyFunSuite |
||||
|
import wow.doge.mygame.subsystems.moddingsystem.ModdingSystem |
||||
|
import monix.execution.Scheduler.Implicits.global |
||||
|
import io.circe.Printer |
||||
|
import monix.bio.UIO |
||||
|
import cats.syntax.eq._ |
||||
|
|
||||
|
class ModdingSystemTest extends AnyFunSuite { |
||||
|
val printer = Printer.spaces2 |
||||
|
test("main") { |
||||
|
val io = for { |
||||
|
res <- ModdingSystem.run() |
||||
|
_ <- UIO( |
||||
|
assert( |
||||
|
(res.parseSuccesses.length + res.parseFailures.length) eqv res.readSuccesses.length |
||||
|
) |
||||
|
) |
||||
|
_ <- ModdingSystem.log(res) |
||||
|
} yield res |
||||
|
io.attempt.runSyncUnsafe() match { |
||||
|
case Left(value) => pprint.log(value); () |
||||
|
case Right(value) => () |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,67 @@ |
|||||
|
package wow.doge.mygame |
||||
|
|
||||
|
import cats.data.ReaderT |
||||
|
import monix.bio.UIO |
||||
|
import org.scalatest.funsuite.AnyFunSuite |
||||
|
import monix.execution.Scheduler.Implicits.global |
||||
|
import monix.bio.IO |
||||
|
|
||||
|
class ReaderT_Test extends AnyFunSuite { |
||||
|
|
||||
|
// type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]] |
||||
|
// val IoReaderT = ReaderT |
||||
|
// val t = |
||||
|
// ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt) |
||||
|
// .run("s") |
||||
|
// .rethrow |
||||
|
// val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt) |
||||
|
// val t2 = r.run("s").rethrow |
||||
|
// Kleisli[IO, String, Unit](s => IO.unit) |
||||
|
|
||||
|
case class Environment(str: String, num: Int) |
||||
|
|
||||
|
// test("runReaderT_Test") { |
||||
|
// def fun1: ReaderT[UIO, String, Unit] = ReaderT(str => UIO(println(str))) |
||||
|
// def fun2: ReaderT[UIO, Int, Unit] = ReaderT(num => UIO(println(num))) |
||||
|
// def total: ReaderT[UIO, Environment, Unit] = |
||||
|
// for { |
||||
|
// _ <- fun1.local[Environment](_.str) |
||||
|
// _ <- fun2.local[Environment](_.num) |
||||
|
// } yield () |
||||
|
|
||||
|
// val uio: UIO[Unit] = total.run(Environment("hello", 50)) |
||||
|
// uio.runSyncUnsafe() |
||||
|
// } |
||||
|
|
||||
|
test("2") { |
||||
|
def fun1: ReaderT[IO[String, ?], String, Unit] = |
||||
|
ReaderT(s => IO(println(s)).onErrorHandleWith(_ => IO.raiseError("wow"))) |
||||
|
def fun2: ReaderT[IO[String, ?], Int, Unit] = |
||||
|
ReaderT(num => |
||||
|
IO(println(num)).onErrorHandleWith(_ => IO.raiseError("whew")) |
||||
|
) |
||||
|
def fun3: ReaderT[IO[String, ?], Environment, Unit] = |
||||
|
for { |
||||
|
env <- ReaderT.ask[IO[String, ?], Environment] |
||||
|
} yield () |
||||
|
def fun4: ReaderT[IO[String, ?], Environment, Unit] = |
||||
|
for { |
||||
|
env <- ReaderT.ask[IO[String, ?], Environment] |
||||
|
_ <- ReaderT.liftF( |
||||
|
IO(println(env)).onErrorHandleWith(_ => IO.raiseError("wow")) |
||||
|
) |
||||
|
_ <- fun3 |
||||
|
} yield () |
||||
|
def app: ReaderT[IO[String, ?], Environment, Unit] = |
||||
|
for { |
||||
|
_ <- fun1.local[Environment](_.str) |
||||
|
_ <- fun2.local[Environment](_.num) |
||||
|
_ <- fun3 |
||||
|
_ <- fun4 |
||||
|
} yield () |
||||
|
|
||||
|
val io: IO[String, Unit] = app.run(Environment("hello", 50)) |
||||
|
io.attempt.runSyncUnsafe() |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
package wow.doge.mygame |
||||
|
|
||||
|
import cats.data.Reader |
||||
|
import monix.bio.UIO |
||||
|
import org.scalatest.funsuite.AnyFunSuite |
||||
|
import monix.execution.Scheduler.Implicits.global |
||||
|
|
||||
|
class ReaderTest extends AnyFunSuite { |
||||
|
|
||||
|
// type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]] |
||||
|
// val IoReaderT = ReaderT |
||||
|
// val t = |
||||
|
// ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt) |
||||
|
// .run("s") |
||||
|
// .rethrow |
||||
|
// val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt) |
||||
|
// val t2 = r.run("s").rethrow |
||||
|
// Kleisli[IO, String, Unit](s => IO.unit) |
||||
|
|
||||
|
case class Environment(str: String, num: Int) |
||||
|
|
||||
|
def fun1: Reader[String, UIO[Unit]] = Reader(str => UIO(println(str))) |
||||
|
def fun2: Reader[Int, UIO[Unit]] = Reader(num => UIO(println(num))) |
||||
|
|
||||
|
def total: Reader[Environment, UIO[Unit]] = |
||||
|
for { |
||||
|
x <- fun1.local[Environment](_.str) |
||||
|
y <- fun2.local[Environment](_.num) |
||||
|
} yield UIO.parSequence(List(x, y)).void |
||||
|
|
||||
|
val io: UIO[Unit] = total.run(Environment("hello", 50)) |
||||
|
|
||||
|
test("runTest") { |
||||
|
io.runSyncUnsafe() |
||||
|
} |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue