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.

74 lines
2.2 KiB

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