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.

82 lines
2.4 KiB

4 years ago
  1. package wow.doge.mygame.events
  2. import akka.actor.typed.ActorRef
  3. import akka.actor.typed.SpawnProtocol
  4. import wow.doge.mygame.implicits._
  5. import akka.actor.typed.scaladsl.AskPattern._
  6. import akka.actor.typed.Props
  7. import akka.util.Timeout
  8. import akka.actor.typed.Scheduler
  9. import akka.actor.typed.LogOptions
  10. import com.typesafe.scalalogging.{Logger => SLLogger}
  11. import wow.doge.mygame.events.EventBus
  12. import akka.actor.typed.scaladsl.Behaviors
  13. import wow.doge.mygame.subsystems.events.MovementEvent.PlayerMovementEvent
  14. import akka.actor.typed.SupervisorStrategy
  15. trait EventsModule {
  16. def spawnProtocol: ActorRef[SpawnProtocol.Command]
  17. implicit def akkaScheduler: Scheduler
  18. implicit def timeout: Timeout
  19. def eventBusLogger = SLLogger[EventBus[_]]
  20. // val subscribingActor =
  21. // spawnProtocol.askT(
  22. // SpawnProtocol.Spawn[Events.PhysicsTick.type](
  23. // SubscribingActor(),
  24. // "subscriber-1",
  25. // Props.empty,
  26. // _
  27. // )
  28. // )
  29. lazy val tickEventBusTask = createEventBus[Events.Tick]("tickEventBus")
  30. // spawnProtocol.askL(
  31. // SpawnProtocol.Spawn[EventBus.Command[Events.Tick]](
  32. // Behaviors.logMessages(
  33. // logOptions = LogOptions().withLogger(eventBusLogger.underlying),
  34. // EventBus[Events.Tick]()
  35. // ),
  36. // "tickEventBus",
  37. // Props.empty,
  38. // _
  39. // )
  40. // )
  41. lazy val playerMovementEventBusTask =
  42. createEventBus[PlayerMovementEvent]("movementEventBus")
  43. // spawnProtocol.askL(
  44. // SpawnProtocol.Spawn[EventBus.Command[Events.Movement.PlayerMovement]](
  45. // Behaviors.logMessages(
  46. // logOptions = LogOptions().withLogger(eventBusLogger.underlying),
  47. // EventBus[Events.Movement.PlayerMovement]()
  48. // ),
  49. // "movementEventBus",
  50. // Props.empty,
  51. // _
  52. // )
  53. // )
  54. // tickEventBus ! EventBus.Subscribe(subscribingActor)
  55. // tickEventBus ! EventBus.Publish(Events.PhysicsTick, ctx.self)
  56. def createEventBus[T](busName: String) =
  57. spawnProtocol.askL(
  58. SpawnProtocol.Spawn[EventBus.Command[T]](
  59. Behaviors.logMessages(
  60. logOptions = LogOptions().withLogger(eventBusLogger.underlying),
  61. Behaviors
  62. .supervise(EventBus[T]())
  63. .onFailure[Exception](SupervisorStrategy.restart)
  64. ),
  65. busName,
  66. Props.empty,
  67. _
  68. )
  69. )
  70. }
  71. object EventTypes {
  72. type EventBus[T] = ActorRef[EventBus.Command[T]]
  73. }