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.

58 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.subsystems.events
  2. import scala.concurrent.duration._
  3. import akka.actor.typed.ActorRef
  4. import akka.actor.typed.ActorSystem
  5. import akka.actor.typed.LogOptions
  6. import akka.actor.typed.Props
  7. import akka.actor.typed.SpawnProtocol
  8. import akka.actor.typed.SupervisorStrategy
  9. import akka.actor.typed.scaladsl.Behaviors
  10. import akka.util.Timeout
  11. import com.typesafe.scalalogging.{Logger => SLogger}
  12. import org.slf4j.event.Level
  13. import wow.doge.mygame.implicits._
  14. import wow.doge.mygame.subsystems.events.Event
  15. import wow.doge.mygame.subsystems.events.EventBus
  16. import wow.doge.mygame.subsystems.events.TickEvent
  17. class EventsModule(spawnProtocol: ActorSystem[SpawnProtocol.Command]) {
  18. implicit lazy val s = spawnProtocol.scheduler
  19. implicit lazy val timeout = Timeout(1.second)
  20. lazy val eventBusLogger = SLogger[EventBus[_]]
  21. lazy val playerEventBusTask =
  22. createEventBus[PlayerEvent]("playerEventBus")
  23. // lazy val playerCameraEventBusTask =
  24. // createEventBus[PlayerCameraEvent]("playerCameraEventBus", Level.DEBUG)
  25. lazy val tickEventBusTask =
  26. createEventBus[TickEvent]("tickEventBus", Level.TRACE)
  27. lazy val mainEventBusTask = createEventBus[Event]("mainEventBus")
  28. def createEventBus[T](busName: String, logLevel: Level = Level.DEBUG) =
  29. spawnProtocol.askL(
  30. SpawnProtocol.Spawn[EventBus.Command[T]](
  31. Behaviors.logMessages(
  32. logOptions = LogOptions()
  33. .withLevel(logLevel)
  34. .withLogger(eventBusLogger.underlying),
  35. Behaviors
  36. .supervise(EventBus[T]())
  37. .onFailure[Exception](SupervisorStrategy.restart)
  38. ),
  39. busName,
  40. Props.empty,
  41. _
  42. )
  43. )
  44. }
  45. object EventsModule {
  46. type GameEventBus[T] = ActorRef[EventBus.Command[T]]
  47. }