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.

65 lines
1.8 KiB

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