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.

77 lines
2.2 KiB

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
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
3 years ago
  1. package wow.doge.mygame.subsystems.events
  2. import java.util.concurrent.TimeoutException
  3. import scala.concurrent.duration._
  4. import scala.reflect.ClassTag
  5. import akka.actor.typed.ActorRef
  6. import akka.actor.typed.LogOptions
  7. import akka.actor.typed.Props
  8. import akka.actor.typed.Scheduler
  9. import akka.actor.typed.SpawnProtocol
  10. import akka.actor.typed.SupervisorStrategy
  11. import akka.actor.typed.scaladsl.Behaviors
  12. import akka.util.Timeout
  13. import com.typesafe.scalalogging.{Logger => SLogger}
  14. import monix.bio.IO
  15. import org.slf4j.event.Level
  16. import wow.doge.mygame.AppError
  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]("playerEventBus")
  32. // val playerCameraEventBusTask =
  33. // createEventBus[PlayerCameraEvent]("playerCameraEventBus", Level.DEBUG)
  34. val tickEventBus: IO[AppError, GameEventBus[TickEvent]] =
  35. createEventBus[TickEvent]("tickEventBus", Level.TRACE)
  36. val mainEventBus: IO[AppError, GameEventBus[Event]] =
  37. createEventBus[Event]("mainEventBus")
  38. def createEventBus[T: ClassTag](
  39. busName: String,
  40. logLevel: Level = Level.DEBUG
  41. ) =
  42. spawnProtocol
  43. .askL(
  44. SpawnProtocol.Spawn[EventBus.Command[T]](
  45. Behaviors.logMessages(
  46. logOptions = LogOptions()
  47. .withLevel(logLevel)
  48. .withLogger(eventBusLogger.underlying),
  49. Behaviors
  50. .supervise(EventBus[T]())
  51. .onFailure[Exception](SupervisorStrategy.restart)
  52. ),
  53. busName,
  54. Props.empty,
  55. _
  56. )
  57. )
  58. .onErrorHandleWith {
  59. case ex: TimeoutException =>
  60. IO.raiseError(AppError.TimeoutError(ex.getMessage))
  61. }
  62. }
  63. object EventsModule {
  64. type GameEventBus[T] = ActorRef[EventBus.Command[T]]
  65. }