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.

104 lines
3.1 KiB

4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 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
  1. package wow.doge.mygame.game
  2. import scala.concurrent.duration._
  3. import akka.actor.typed.ActorRef
  4. import akka.actor.typed.PostStop
  5. import akka.actor.typed.SpawnProtocol
  6. import akka.actor.typed.SupervisorStrategy
  7. import akka.actor.typed.scaladsl.Behaviors
  8. import monix.execution.CancelableFuture
  9. import monix.execution.CancelablePromise
  10. import wow.doge.mygame.game.TickGenerator.Send
  11. import wow.doge.mygame.implicits._
  12. import wow.doge.mygame.subsystems.events.EventBus
  13. import wow.doge.mygame.subsystems.events.EventsModule.GameEventBus
  14. import wow.doge.mygame.subsystems.events.TickEvent
  15. import wow.doge.mygame.utils.GenericTimerActor
  16. object GameAppActor {
  17. sealed trait Command
  18. case object Start extends Command
  19. case object Pause extends Command
  20. case object Ping extends Command
  21. final case class Stop(stopSignal: ActorRef[CancelableFuture[Unit]])
  22. extends Command
  23. final case class GetSpawnProtocol(
  24. replyTo: ActorRef[ActorRef[SpawnProtocol.Command]]
  25. ) extends Command
  26. final case class Props(tickEventBus: GameEventBus[TickEvent]) {
  27. def behavior =
  28. Behaviors.setup[Command] { ctx =>
  29. ctx.log.infoP("Hello from GameAppActor")
  30. val renderTickGenerator =
  31. ctx.spawnN(
  32. Behaviors
  33. .supervise(renderTickGeneratorBehavior)
  34. .onFailure[Exception](
  35. SupervisorStrategy.restart.withLimit(2, 100.millis)
  36. )
  37. )
  38. val tickGeneratorTimer = ctx.spawnN(
  39. GenericTimerActor
  40. .Props(renderTickGenerator, TickGenerator.Send, 10.millis)
  41. .behavior
  42. )
  43. val sp = ctx.spawn(SpawnProtocol(), "gameSpawnProtocol")
  44. // ctx.spawn(
  45. // GenericTimerActor
  46. // .Props(ctx.self, Ping, 1000.millis)
  47. // .behavior,
  48. // "pingTimer"
  49. // ) ! GenericTimerActor.Start
  50. val stopPromise = CancelablePromise[Unit]()
  51. Behaviors
  52. .receiveMessage[Command] {
  53. case Start =>
  54. tickGeneratorTimer ! GenericTimerActor.Start
  55. Behaviors.same
  56. case Pause =>
  57. tickGeneratorTimer ! GenericTimerActor.Stop
  58. Behaviors.same
  59. case Stop(replyTo) =>
  60. ctx.log.infoP("Received stop")
  61. tickGeneratorTimer ! GenericTimerActor.Stop
  62. replyTo ! stopPromise.future
  63. Behaviors.stopped
  64. case Ping =>
  65. ctx.log.debugP("ping")
  66. Behaviors.same
  67. case GetSpawnProtocol(replyTo) =>
  68. replyTo ! sp
  69. Behaviors.same
  70. }
  71. .receiveSignal {
  72. case (_, PostStop) =>
  73. stopPromise.success(())
  74. Behaviors.same
  75. }
  76. }
  77. val renderTickGeneratorBehavior =
  78. Behaviors.receiveMessage[TickGenerator.Command] {
  79. case Send =>
  80. tickEventBus ! EventBus.Publish(
  81. TickEvent.RenderTick,
  82. "tickGeneratorActor"
  83. )
  84. Behaviors.same
  85. }
  86. }
  87. }
  88. object TickGenerator {
  89. sealed trait Command
  90. case object Send extends Command
  91. }