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.

55 lines
1.6 KiB

4 years ago
4 years ago
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.EntityMovementEvent.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. lazy val tickEventBusTask = createEventBus[Events.Tick]("tickEventBus")
  21. lazy val playerMovementEventBusTask =
  22. createEventBus[PlayerMovementEvent]("movementEventBus")
  23. def createEventBus[T](busName: String) =
  24. spawnProtocol.askL(
  25. SpawnProtocol.Spawn[EventBus.Command[T]](
  26. Behaviors.logMessages(
  27. logOptions = LogOptions().withLogger(eventBusLogger.underlying),
  28. Behaviors
  29. .supervise(EventBus[T]())
  30. .onFailure[Exception](SupervisorStrategy.restart)
  31. ),
  32. busName,
  33. Props.empty,
  34. _
  35. )
  36. )
  37. }
  38. object EventTypes {
  39. type EventBus[T] = ActorRef[EventBus.Command[T]]
  40. }
  41. // val subscribingActor =
  42. // spawnProtocol.askT(
  43. // SpawnProtocol.Spawn[Events.PhysicsTick.type](
  44. // SubscribingActor(),
  45. // "subscriber-1",
  46. // Props.empty,
  47. // _
  48. // )
  49. // )