Add rest of the player movement messages

This commit is contained in:
Rohan Sircar 2021-03-08 19:18:28 +05:30
parent be9acf81d5
commit f0ae3625bf
4 changed files with 71 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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])