Player cannot walk when stamina is 0

This commit is contained in:
Rohan Sircar 2021-03-09 19:14:17 +05:30
parent 9b484e895b
commit 44f0538b8b
6 changed files with 93 additions and 43 deletions

View File

@ -175,7 +175,7 @@ class MainApp(
15.seconds
).toTask
.void
case _ => monix.eval.Task.unit
case _ => me.Task.unit
}
_ <- Resource.make(obs.completedL.toIO.hideErrors.start)(_.cancel)
// _ <-

View File

@ -29,6 +29,7 @@ import wow.doge.mygame.subsystems.events.PlayerEvent
import wow.doge.mygame.subsystems.events.TickEvent
import wow.doge.mygame.subsystems.events.TickEvent.RenderTick
import wow.doge.mygame.subsystems.movement.ImMovementActor
import wow.doge.mygame.utils.MovementDirection
object PlayerActor {
@ -64,14 +65,15 @@ object PlayerActor {
replyTo: ActorRef[UIO[Observable[CharacterStats]]]
) extends Command
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
final case class Walk(pressed: Boolean, dir: MovementDirection)
extends Command
case object StopMoving extends Command
case object Jump extends Command
private[player] final case class HandleMovement(
private[player] final case class HandleWalk(
b: CharacterStats,
pressed: Boolean
pressed: Boolean,
direction: MovementDirection
) extends Command
private[player] case object Die extends Command

View File

@ -18,6 +18,7 @@ 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.utils.MovementDirection
object PlayerMovementEventListener {
final case class State(
@ -60,14 +61,19 @@ class PlayerMovementEventListener(
Observable
.interval(250.millis)
.doOnNext(_ => Task(pprint.log("Sending Stamina Consume Item")))
.doOnNextF(_ =>
.mapEvalF(_ =>
props.playerActor
.askL(
PlayerActor
.ConsumeStamina(CharacterStats.DamageStamina(25), _)
)
.void
)
.doOnNext(stats =>
if (stats.stamina.toInt === 0)
Task(props.playerActor ! PlayerActor.StopMoving)
else Task.unit
)
.takeWhile(_.stamina.toInt >= 0)
.completedL
)
@ -121,16 +127,32 @@ class PlayerMovementEventListener(
Behaviors.receiveMessage {
case PlayerMovedLeft(pressed) =>
// props.movementActor ! ImMovementActor.MoveLeft(pressed)
props.playerActor ! PlayerActor.MoveLeft(pressed)
// props.playerActor ! PlayerActor.MoveLeft(pressed)
props.playerActor ! PlayerActor.Walk(
pressed,
MovementDirection.Left
)
receive(handleStamina(pressed))
case PlayerMovedRight(pressed) =>
props.playerActor ! PlayerActor.MoveRight(pressed)
// props.playerActor ! PlayerActor.MoveRight(pressed)
props.playerActor ! PlayerActor.Walk(
pressed,
MovementDirection.Right
)
receive(handleStamina(pressed))
case PlayerMovedForward(pressed) =>
props.playerActor ! PlayerActor.MoveUp(pressed)
// props.playerActor ! PlayerActor.MoveUp(pressed)
props.playerActor ! PlayerActor.Walk(
pressed,
MovementDirection.Forward
)
receive(handleStamina(pressed))
case PlayerMovedBackward(pressed) =>
props.playerActor ! PlayerActor.MoveDown(pressed)
// props.playerActor ! PlayerActor.MoveDown(pressed)
props.playerActor ! PlayerActor.Walk(
pressed,
MovementDirection.Backward
)
receive(handleStamina(pressed))
case PlayerJumped =>
props.playerActor ! PlayerActor.Jump

View File

@ -16,6 +16,7 @@ import wow.doge.mygame.game.entities.character.CharacterStates._
import wow.doge.mygame.game.entities.player.PlayerActor
import wow.doge.mygame.implicits._
import wow.doge.mygame.subsystems.movement.ImMovementActor
import wow.doge.mygame.utils.MovementDirection
class IdleBehaviorFactory(
env: PlayerActor.Env,
@ -32,41 +33,43 @@ class IdleBehaviorFactory(
Behaviors
.receiveMessage[PlayerActor.Command] {
case PlayerActor.HandleMovement(curStats, pressed) =>
case PlayerActor.HandleWalk(curStats, pressed, direction) =>
if (curStats.stamina.toInt > 0) {
children.movementActor ! ImMovementActor.MoveLeft(pressed)
children.movementActor ! (direction match {
case MovementDirection.Forward =>
ImMovementActor.MoveUp(pressed)
case MovementDirection.Backward =>
ImMovementActor.MoveDown(pressed)
case MovementDirection.Left => ImMovementActor.MoveLeft(pressed)
case MovementDirection.Right =>
ImMovementActor.MoveRight(pressed)
})
if (pressed) nextStateFn(AliveSubstate.Moving(Walking))
else nextStateFn(AliveSubstate.Idle)
} else {
children.movementActor ! ImMovementActor.MoveLeft(false)
children.movementActor ! ImMovementActor.StopMoving
Behaviors.same
}
case PlayerActor.MoveLeft(pressed) =>
// case PlayerActor.Walk(pressed, Direction.Up) => Behaviors.same
// case PlayerActor.Walk(pressed, Direction.Down) => Behaviors.same
// case PlayerActor.Walk(pressed, Direction.Left) => Behaviors.same
// case PlayerActor.Walk(pressed, Direction.Right) => Behaviors.same
case PlayerActor.StopMoving =>
children.movementActor ! ImMovementActor.StopMoving
Behaviors.same
case PlayerActor.Walk(pressed, direction) =>
implicit val ec = props.scheduler.value
for {
curStats <- children.statsActor.ask(StatsActor.CurrentStats)
res <- Future.successful(
ctx.self ! PlayerActor.HandleMovement(curStats, pressed)
_ <- Future.successful(
ctx.self ! PlayerActor.HandleWalk(curStats, pressed, direction)
)
} yield res
} yield ()
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(
@ -152,7 +155,8 @@ class IdleBehaviorFactory(
case (status, stats) =>
status match {
case StatsActor.Status.Dead => ctx.self ! PlayerActor.Die
case StatsActor.Status.Alive =>
case StatsActor.Status.Alive => ()
}
props.statsQueue
.offer(stats)
.foreach { _ =>
@ -160,7 +164,6 @@ class IdleBehaviorFactory(
replyTo ! stats
}(props.scheduler.value)
}
}
Behaviors.same
case PlayerActor.Die => deadState

View File

@ -36,6 +36,7 @@ object ImMovementActor {
final case class MoveUp(pressed: Boolean) extends Movement
final case class MoveRight(pressed: Boolean) extends Movement
final case class MoveDown(pressed: Boolean) extends Movement
case object StopMoving extends Movement
case object Jump extends Movement
class Props(
@ -77,6 +78,10 @@ class ImMovementActor[T](
.receiveMessage[Command] {
case m: Movement =>
m match {
case StopMoving =>
cm.stop(movable)
receive(State(CardinalDirection.default), new Vector3f)
case MoveLeft(pressed) =>
stopIfNotPressed(pressed)
receive(

View File

@ -0,0 +1,18 @@
package wow.doge.mygame.utils
import enumeratum._
import cats.kernel.Eq
import cats.Show
sealed trait MovementDirection extends EnumEntry
object MovementDirection extends Enum[MovementDirection] {
val values = findValues
case object Forward extends MovementDirection
case object Backward extends MovementDirection
case object Left extends MovementDirection
case object Right extends MovementDirection
implicit val eq = Eq.fromUniversalEquals[MovementDirection]
implicit val show = Show.fromToString[MovementDirection]
}