Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.0 KiB

4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
  1. package wow.doge.mygame.game
  2. import scala.concurrent.duration._
  3. import akka.actor.typed.SupervisorStrategy
  4. import akka.actor.typed.scaladsl.Behaviors
  5. import wow.doge.mygame.game.TickGenerator.Send
  6. import wow.doge.mygame.implicits._
  7. import wow.doge.mygame.subsystems.events.EventBus
  8. import wow.doge.mygame.subsystems.events.EventsModule.GameEventBus
  9. import wow.doge.mygame.subsystems.events.TickEvent
  10. import wow.doge.mygame.subsystems.events.TickEvent.PhysicsTick
  11. import wow.doge.mygame.utils.GenericTimerActor
  12. object GameAppActor {
  13. sealed trait Command
  14. case object Start extends Command
  15. case object Pause extends Command
  16. case object Stop extends Command
  17. case class Props(tickEventBus: GameEventBus[TickEvent]) {
  18. def behavior =
  19. Behaviors.setup[Command] { ctx =>
  20. ctx.log.infoP("Hello from GameAppActor")
  21. val renderTickGenerator =
  22. ctx.spawnN(
  23. Behaviors
  24. .supervise(renderTickGeneratorBehavior)
  25. .onFailure[Exception](
  26. SupervisorStrategy.restart.withLimit(2, 100.millis)
  27. )
  28. )
  29. val tickGeneratorTimer = ctx.spawnN(
  30. GenericTimerActor
  31. .Props(renderTickGenerator, TickGenerator.Send, 10.millis)
  32. .behavior
  33. )
  34. Behaviors.receiveMessage {
  35. case Start =>
  36. tickGeneratorTimer ! GenericTimerActor.Start
  37. Behaviors.same
  38. case Pause =>
  39. tickGeneratorTimer ! GenericTimerActor.Stop
  40. Behaviors.same
  41. case Stop =>
  42. ctx.log.info("Received stop")
  43. tickGeneratorTimer ! GenericTimerActor.Stop
  44. Behaviors.stopped
  45. }
  46. }
  47. val renderTickGeneratorBehavior =
  48. Behaviors.receiveMessage[TickGenerator.Command] {
  49. case Send =>
  50. tickEventBus ! EventBus.Publish(
  51. TickEvent.RenderTick,
  52. "tickGeneratorActor"
  53. )
  54. Behaviors.same
  55. }
  56. }
  57. }
  58. object TickGenerator {
  59. sealed trait Command
  60. case object Send extends Command
  61. }
  62. object SubscribingActor {
  63. def apply() =
  64. Behaviors.receive[PhysicsTick.type] { (ctx, msg) =>
  65. ctx.log.debugP(s"received event $msg")
  66. Behaviors.same
  67. }
  68. }
  69. object Methods {
  70. def old() = {
  71. // val movementActor =
  72. // ctx.spawn(
  73. // MovementActor(MovementActor.Props(app, geom)),
  74. // "movementActor"
  75. // // DispatcherSelector.fromConfig("jme-dispatcher")
  76. // )
  77. // val movementActorTimer = ctx.spawn(
  78. // MovementActorTimer(movementActor),
  79. // "movementActorTimer"
  80. // )
  81. }
  82. def old2() = {
  83. // ctx.log.info("here")
  84. // {
  85. // implicit val s = schedulers.async
  86. // Task
  87. // .parZip2(
  88. // loggerL.info("Test").executeOn(app.scheduler),
  89. // app
  90. // .enqueueL(() => loggerL.info("here 2").executeOn(app.scheduler))
  91. // .flatten
  92. // )
  93. // .runToFuture
  94. // }
  95. // app
  96. // .getRootNode()
  97. // .depthFirst(s =>
  98. // // s match {
  99. // // case node: Node =>
  100. // // println("node" + s.getName() + " children " + node.getChildren())
  101. // // case g: Geometry => println(s.getName())
  102. // // }
  103. // println(s.getName())
  104. // )
  105. // println("----------------")
  106. // {
  107. // app
  108. // .getRootNode()
  109. // .observableDepthFirst()
  110. // .map(s => s.getName())
  111. // // .takeWhileInclusive(_.getName() != "level")
  112. // .onErrorHandle(e => e.getMessage())
  113. // .foreach(println)(schedulers.async)
  114. // }
  115. // println("----------------")
  116. // {
  117. // app
  118. // .getRootNode()
  119. // .observableBreadthFirst()
  120. // .map(s => s.getName())
  121. // // .takeWhileInclusive(_.getName() != "level")
  122. // .onErrorHandle(e => e.getMessage())
  123. // .foreach(println)(schedulers.async)
  124. // }
  125. // app.start()
  126. // Behaviors.same
  127. }
  128. }
  129. // new PlayerMovementState(
  130. // // movementActor,
  131. // // movementActorTimer,
  132. // imMovementActor,
  133. // // geom,
  134. // // camNode,
  135. // playerNode
  136. // // ctx.self
  137. // )