package wow.doge.mygame.events import akka.actor.typed.ActorRef import akka.actor.typed.SpawnProtocol import wow.doge.mygame.implicits._ import akka.actor.typed.scaladsl.AskPattern._ import akka.actor.typed.Props import akka.util.Timeout import akka.actor.typed.Scheduler import akka.actor.typed.LogOptions import com.typesafe.scalalogging.{Logger => SLLogger} import wow.doge.mygame.events.EventBus import akka.actor.typed.scaladsl.Behaviors import wow.doge.mygame.subsystems.events.MovementEvent.PlayerMovementEvent import akka.actor.typed.SupervisorStrategy trait EventsModule { def spawnProtocol: ActorRef[SpawnProtocol.Command] implicit def akkaScheduler: Scheduler implicit def timeout: Timeout def eventBusLogger = SLLogger[EventBus[_]] // val subscribingActor = // spawnProtocol.askT( // SpawnProtocol.Spawn[Events.PhysicsTick.type]( // SubscribingActor(), // "subscriber-1", // Props.empty, // _ // ) // ) lazy val tickEventBusTask = createEventBus[Events.Tick]("tickEventBus") // spawnProtocol.askL( // SpawnProtocol.Spawn[EventBus.Command[Events.Tick]]( // Behaviors.logMessages( // logOptions = LogOptions().withLogger(eventBusLogger.underlying), // EventBus[Events.Tick]() // ), // "tickEventBus", // Props.empty, // _ // ) // ) lazy val playerMovementEventBusTask = createEventBus[PlayerMovementEvent]("movementEventBus") // spawnProtocol.askL( // SpawnProtocol.Spawn[EventBus.Command[Events.Movement.PlayerMovement]]( // Behaviors.logMessages( // logOptions = LogOptions().withLogger(eventBusLogger.underlying), // EventBus[Events.Movement.PlayerMovement]() // ), // "movementEventBus", // Props.empty, // _ // ) // ) // tickEventBus ! EventBus.Subscribe(subscribingActor) // tickEventBus ! EventBus.Publish(Events.PhysicsTick, ctx.self) def createEventBus[T](busName: String) = spawnProtocol.askL( SpawnProtocol.Spawn[EventBus.Command[T]]( Behaviors.logMessages( logOptions = LogOptions().withLogger(eventBusLogger.underlying), Behaviors .supervise(EventBus[T]()) .onFailure[Exception](SupervisorStrategy.restart) ), busName, Props.empty, _ ) ) } object EventTypes { type EventBus[T] = ActorRef[EventBus.Command[T]] }