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.

103 lines
3.0 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
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. case class Stop(stopSignal: ActorRef[CancelableFuture[Unit]]) extends Command
  22. case class GetSpawnProtocol(
  23. replyTo: ActorRef[ActorRef[SpawnProtocol.Command]]
  24. ) extends Command
  25. case class Props(tickEventBus: GameEventBus[TickEvent]) {
  26. def behavior =
  27. Behaviors.setup[Command] { ctx =>
  28. ctx.log.infoP("Hello from GameAppActor")
  29. val renderTickGenerator =
  30. ctx.spawnN(
  31. Behaviors
  32. .supervise(renderTickGeneratorBehavior)
  33. .onFailure[Exception](
  34. SupervisorStrategy.restart.withLimit(2, 100.millis)
  35. )
  36. )
  37. val tickGeneratorTimer = ctx.spawnN(
  38. GenericTimerActor
  39. .Props(renderTickGenerator, TickGenerator.Send, 10.millis)
  40. .behavior
  41. )
  42. val sp = ctx.spawn(SpawnProtocol(), "gameSpawnProtocol")
  43. ctx.spawn(
  44. GenericTimerActor
  45. .Props(ctx.self, Ping, 1000.millis)
  46. .behavior,
  47. "pingTimer"
  48. ) ! GenericTimerActor.Start
  49. val stopPromise = CancelablePromise[Unit]()
  50. Behaviors
  51. .receiveMessage[Command] {
  52. case Start =>
  53. tickGeneratorTimer ! GenericTimerActor.Start
  54. Behaviors.same
  55. case Pause =>
  56. tickGeneratorTimer ! GenericTimerActor.Stop
  57. Behaviors.same
  58. case Stop(replyTo) =>
  59. ctx.log.infoP("Received stop")
  60. tickGeneratorTimer ! GenericTimerActor.Stop
  61. replyTo ! stopPromise.future
  62. Behaviors.stopped
  63. case Ping =>
  64. ctx.log.debugP("ping")
  65. Behaviors.same
  66. case GetSpawnProtocol(replyTo) =>
  67. replyTo ! sp
  68. Behaviors.same
  69. }
  70. .receiveSignal {
  71. case (_, PostStop) =>
  72. stopPromise.success(())
  73. Behaviors.same
  74. }
  75. }
  76. val renderTickGeneratorBehavior =
  77. Behaviors.receiveMessage[TickGenerator.Command] {
  78. case Send =>
  79. tickEventBus ! EventBus.Publish(
  80. TickEvent.RenderTick,
  81. "tickGeneratorActor"
  82. )
  83. Behaviors.same
  84. }
  85. }
  86. }
  87. object TickGenerator {
  88. sealed trait Command
  89. case object Send extends Command
  90. }