forked from nova/jmonkey-test
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.
64 lines
2.0 KiB
64 lines
2.0 KiB
package wow.doge.mygame.subsystems.events
|
|
|
|
import akka.actor.typed.ActorRef
|
|
import akka.actor.typed.SpawnProtocol
|
|
import wow.doge.mygame.implicits._
|
|
import akka.actor.typed.Props
|
|
import akka.actor.typed.LogOptions
|
|
import com.typesafe.scalalogging.{Logger => SLLogger}
|
|
import wow.doge.mygame.events.EventBus
|
|
import akka.actor.typed.scaladsl.Behaviors
|
|
import scala.concurrent.duration._
|
|
import akka.util.Timeout
|
|
import akka.actor.typed.SupervisorStrategy
|
|
import cats.effect.Resource
|
|
import akka.actor.typed.ActorSystem
|
|
import monix.bio.Task
|
|
import org.slf4j.event.Level
|
|
import wow.doge.mygame.subsystems.events.EntityMovementEvent.PlayerMovementEvent
|
|
|
|
class EventsModule2(
|
|
spawnProtocol: ActorSystem[SpawnProtocol.Command]
|
|
) {
|
|
private implicit lazy val s = spawnProtocol.scheduler
|
|
|
|
private implicit lazy val timeout = Timeout(1.second)
|
|
|
|
private lazy val eventBusLogger = SLLogger[EventBus[_]]
|
|
|
|
private lazy val playerMovementEventBusTask =
|
|
createEventBus[PlayerMovementEvent]("movementEventBus")
|
|
|
|
private lazy val playerCameraEventBusTask =
|
|
createEventBus[PlayerCameraEvent]("playerCameraEventBus", Level.DEBUG)
|
|
|
|
def createEventBus[T](busName: String, logLevel: Level = Level.DEBUG) =
|
|
spawnProtocol.askL(
|
|
SpawnProtocol.Spawn[EventBus.Command[T]](
|
|
Behaviors.logMessages(
|
|
logOptions = LogOptions()
|
|
.withLevel(logLevel)
|
|
.withLogger(eventBusLogger.underlying),
|
|
Behaviors
|
|
.supervise(EventBus[T]())
|
|
.onFailure[Exception](SupervisorStrategy.restart)
|
|
),
|
|
busName,
|
|
Props.empty,
|
|
_
|
|
)
|
|
)
|
|
|
|
type EventBuses = (
|
|
ActorRef[
|
|
EventBus.Command[EntityMovementEvent.PlayerMovementEvent],
|
|
],
|
|
ActorRef[EventBus.Command[PlayerCameraEvent]]
|
|
)
|
|
|
|
val resource: Resource[Task, EventBuses] =
|
|
Resource.liftF(for {
|
|
playerMovementEventBus <- playerMovementEventBusTask
|
|
playerCameraEventBus <- playerCameraEventBusTask
|
|
} yield (playerMovementEventBus, playerCameraEventBus))
|
|
}
|