From f0ae3625bf58c696cfedc7a191dc0018770d165c Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Mon, 8 Mar 2021 19:18:28 +0530 Subject: [PATCH] Add rest of the player movement messages --- .../game/entities/player/PlayerActor.scala | 14 +++-- .../entities/player/PlayerController.scala | 4 +- .../player/PlayerEventListeners.scala | 20 ++++--- .../player/behaviors/IdleBehavior.scala | 57 +++++++++++++++---- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerActor.scala b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerActor.scala index 25dfc2e..325c2b4 100644 --- a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerActor.scala +++ b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerActor.scala @@ -40,7 +40,7 @@ object PlayerActor { case object Dead extends Status } - sealed trait Command + sealed trait Command extends Product with Serializable final case class TakeDamage( value: CharacterStats.DamageHealth, replyTo: ActorRef[Unit] @@ -57,8 +57,15 @@ object PlayerActor { replyTo: ActorRef[UIO[Observable[CharacterStats]]] ) extends Command - sealed trait Movement extends Command - final case class MoveLeft(pressed: Boolean) extends Movement + final case class MoveLeft(pressed: Boolean) extends Command + final case class MoveUp(pressed: Boolean) extends Command + final case class MoveRight(pressed: Boolean) extends Command + final case class MoveDown(pressed: Boolean) extends Command + case object Jump extends Command + private[player] final case class HandleMovement( + b: CharacterStats, + pressed: Boolean + ) extends Command private[player] case object Die extends Command private[player] final case class StatsResponse( @@ -102,7 +109,6 @@ object PlayerActor { Behaviors .supervise( new PlayerMovementEventListener.Props( - playerMovementActor, ctx.self, scheduler ).behavior diff --git a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerController.scala b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerController.scala index 3e324ab..0e0238d 100644 --- a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerController.scala +++ b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerController.scala @@ -18,6 +18,8 @@ import wow.doge.mygame.AppError import wow.doge.mygame.executors.Schedulers import wow.doge.mygame.executors.Schedulers.AsyncScheduler import wow.doge.mygame.game.GameApp +import wow.doge.mygame.game.entities.CharacterStats +import wow.doge.mygame.game.entities.StatsActor import wow.doge.mygame.implicits._ import wow.doge.mygame.math.ImVector3f import wow.doge.mygame.subsystems.events.EventsModule.GameEventBus @@ -26,8 +28,6 @@ import wow.doge.mygame.subsystems.events.TickEvent import wow.doge.mygame.subsystems.movement.ImMovementActor import wow.doge.mygame.types._ import wow.doge.mygame.utils.wrappers.jme._ -import wow.doge.mygame.game.entities.StatsActor -import wow.doge.mygame.game.entities.CharacterStats object PlayerController { sealed trait Error diff --git a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerEventListeners.scala b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerEventListeners.scala index d61754f..0549a8c 100644 --- a/src/main/scala/wow/doge/mygame/game/entities/player/PlayerEventListeners.scala +++ b/src/main/scala/wow/doge/mygame/game/entities/player/PlayerEventListeners.scala @@ -2,12 +2,13 @@ package wow.doge.mygame.game.entities.player import scala.concurrent.duration._ -import akka.actor.typed.ActorRef import akka.actor.typed.Behavior import akka.actor.typed.LogOptions import akka.actor.typed.scaladsl.ActorContext +import akka.actor.typed.scaladsl.AskPattern._ import akka.actor.typed.scaladsl.Behaviors import akka.util.Timeout +import cats.syntax.eq._ import com.typesafe.scalalogging.Logger import monix.eval.Task import monix.execution.CancelableFuture @@ -17,14 +18,11 @@ import wow.doge.mygame.executors.Schedulers import wow.doge.mygame.game.entities.CharacterStats import wow.doge.mygame.implicits._ import wow.doge.mygame.subsystems.events.PlayerMovementEvent -import wow.doge.mygame.subsystems.movement.ImMovementActor -import cats.syntax.eq._ object PlayerMovementEventListener { final case class State(keysPressed: Int, staminaTimer: CancelableFuture[Unit]) class Props( - val movementActor: ActorRef[ImMovementActor.Command], val playerActor: PlayerActor.Ref, val asyncScheduler: Schedulers.AsyncScheduler ) { @@ -97,16 +95,22 @@ class PlayerMovementEventListener( props.playerActor ! PlayerActor.MoveLeft(pressed) receive(handleStamina(pressed)) case PlayerMovedRight(pressed) => - props.movementActor ! ImMovementActor.MoveRight(pressed) + props.playerActor ! PlayerActor.MoveRight(pressed) receive(handleStamina(pressed)) case PlayerMovedForward(pressed) => - props.movementActor ! ImMovementActor.MoveUp(pressed) + props.playerActor ! PlayerActor.MoveUp(pressed) receive(handleStamina(pressed)) case PlayerMovedBackward(pressed) => - props.movementActor ! ImMovementActor.MoveDown(pressed) + props.playerActor ! PlayerActor.MoveDown(pressed) receive(handleStamina(pressed)) case PlayerJumped => - props.movementActor ! ImMovementActor.Jump + props.playerActor ! PlayerActor.Jump + // props.playerActor + // ctx.ask( + // props.playerActor, + // PlayerActor + // .ConsumeStamina(CharacterStats.DamageStamina(1), _) + // )(_ => ()) Behaviors.same // case PlayerTurnedRight => // movementActor ! ImMovementActor.RotateRight diff --git a/src/main/scala/wow/doge/mygame/game/entities/player/behaviors/IdleBehavior.scala b/src/main/scala/wow/doge/mygame/game/entities/player/behaviors/IdleBehavior.scala index fd6c34b..f6cea97 100644 --- a/src/main/scala/wow/doge/mygame/game/entities/player/behaviors/IdleBehavior.scala +++ b/src/main/scala/wow/doge/mygame/game/entities/player/behaviors/IdleBehavior.scala @@ -23,7 +23,6 @@ class IdleBehaviorFactory( deadState: Behavior[PlayerActor.Command], consumptionMultiplier: Int => Int )(implicit timeout: Timeout) { - import IdleBehaviorFactory.IdleBehavior import env._ implicit val sched = ctx.system.scheduler @@ -32,11 +31,46 @@ class IdleBehaviorFactory( IdleBehavior( Behaviors .receiveMessage[PlayerActor.Command] { + + case PlayerActor.HandleMovement(curStats, pressed) => + if (curStats.stamina.toInt > 0) { + children.movementActor ! ImMovementActor.MoveLeft(pressed) + if (pressed) nextStateFn(AliveSubstate.Moving(Walking)) + else nextStateFn(AliveSubstate.Idle) + } else Behaviors.same + case PlayerActor.MoveLeft(pressed) => - children.movementActor ! ImMovementActor.MoveLeft(pressed) - if (pressed) - nextStateFn(AliveSubstate.Moving(Walking)) + implicit val ec = props.scheduler.value + for { + curStats <- children.statsActor.ask(StatsActor.CurrentStats) + res <- Future.successful( + ctx.self ! PlayerActor.HandleMovement(curStats, pressed) + ) + } yield res + Behaviors.same + + case PlayerActor.MoveRight(pressed) => + children.movementActor ! ImMovementActor.MoveRight(pressed) + if (pressed) nextStateFn(AliveSubstate.Moving(Walking)) else nextStateFn(AliveSubstate.Idle) + + case PlayerActor.MoveUp(pressed) => + children.movementActor ! ImMovementActor.MoveUp(pressed) + if (pressed) nextStateFn(AliveSubstate.Moving(Walking)) + else nextStateFn(AliveSubstate.Idle) + + case PlayerActor.MoveDown(pressed) => + children.movementActor ! ImMovementActor.MoveDown(pressed) + if (pressed) nextStateFn(AliveSubstate.Moving(Walking)) + else nextStateFn(AliveSubstate.Idle) + + case PlayerActor.Jump => + children.movementActor ! ImMovementActor.Jump + ctx.self.ask( + PlayerActor.ConsumeStamina(CharacterStats.DamageStamina(10), _) + ) + Behaviors.same + case PlayerActor.TakeDamage(value, replyTo) => implicit val ec = props.scheduler.value for { @@ -47,6 +81,7 @@ class IdleBehaviorFactory( ) } yield () Behaviors.same + case PlayerActor.ConsumeStamina(value, replyTo) => implicit val ec = props.scheduler.value val newValue = @@ -60,15 +95,19 @@ class IdleBehaviorFactory( .StatsResponse(response, replyTo) } yield () Behaviors.same + case PlayerActor.CurrentStats(replyTo) => children.statsActor ! StatsActor.CurrentStats(replyTo) Behaviors.same + case PlayerActor.Heal(value) => children.statsActor ! StatsActor.HealResult(value) Behaviors.same + case PlayerActor.GetStatus(replyTo) => replyTo ! PlayerActor.Status.Alive Behaviors.same + case PlayerActor.GetStatsObservable(replyTo) => import monix.{eval => me} replyTo ! @@ -82,6 +121,7 @@ class IdleBehaviorFactory( ) Behaviors.same + case PlayerActor.StatsResponse(response, replyTo) => response match { case (dead, stats) => @@ -94,8 +134,9 @@ class IdleBehaviorFactory( }(props.scheduler.value) } Behaviors.same - // nextStateFn(InCombat(Moving(Walking))) + case PlayerActor.Die => deadState + case PlayerActor.LogError(ex) => ctx.log.error(ex.getMessage) Behaviors.same @@ -108,8 +149,4 @@ class IdleBehaviorFactory( ) } -object IdleBehaviorFactory { - final case class IdleBehavior( - value: Behavior[PlayerActor.Command] - ) -} +final case class IdleBehavior(value: Behavior[PlayerActor.Command])