private bool SetAboveGroundIfNeeded(IEditableEntity entity) { var position = entity.Get <Position>(); var height = TextureManager.GetTexture(entity.Get <SpriteSheet>()).Height; if ((position.Y - height / 2) <= GroundLevel) { entity.Set(position.SetY(GroundLevel + height / 2)); entity.Set(entity.Get <Velocity>().UpdateYVelocity(vy => vy < 0 ? 0 : vy)); return(true); } return(false); }
public void Update(IEditableEntity entity, long frame) { var sheet = entity.Get <SpriteSheet>(); var animated = entity.Get <Animated>(); if (frame % animated.AnimationSpeed == 0) { animated = animated.SetCurrentFrame(animated.CurrentFrame + 1); if (animated.CurrentFrame >= sheet.FrameCount) { animated = animated.SetCurrentFrame(0); } entity.Set(animated); } }
public void Update(IEditableEntity entity, long frame) { var player = entity.Get <Player>(); var inputState = InputManager.GetInputState(player.PlayerID, frame); var position = entity.Get <Position>(); var velocity = entity.Get <Velocity>(); position = entity.Set(position + velocity); bool onGround = SetAboveGroundIfNeeded(entity); velocity = entity.Set(velocity.UpdateXVelocity(vx => vx * (onGround ? GroundFriction : AirFriction))); if (velocity.X != 0) { entity.Set(entity.Get <Animated>().SetFlipped(velocity.X < 0)); } switch (player.CurrentState) { case PlayerState.Idle: entity.Set(Idle); if (inputState.Up.IsDown() || !onGround) { entity.Set(player.SetState(PlayerState.Falling)); if (onGround) { entity.Set(velocity.SetYVelocity(JumpSpeed)); } } else { if (inputState.Left.IsDown() && inputState.Right.IsUp()) { entity.Set(player.SetState(PlayerState.WalkingLeft)); } if (inputState.Right.IsDown() && inputState.Left.IsUp()) { entity.Set(player.SetState(PlayerState.WalkingRight)); } } break; case PlayerState.Falling: entity.Set(Falling); if (onGround) { entity.Set(player.SetState(PlayerState.Idle)); entity.Set(velocity.SetYVelocity(0)); } else { entity.Set(velocity.UpdateYVelocity(vy => vy - GravityAcceleration)); } break; case PlayerState.WalkingLeft: entity.Set(Run); entity.Set(entity.Get <Animated>().SetFlipped(true)); entity.Set(velocity.UpdateXVelocity(vx => vx - RunSpeed)); if (inputState.Left.IsUp() || inputState.Right.IsDown() || inputState.Up.IsDown()) { entity.Set(player.SetState(PlayerState.Idle)); } break; case PlayerState.WalkingRight: entity.Set(Run); entity.Set(entity.Get <Animated>().SetFlipped(false)); entity.Set(velocity.UpdateXVelocity(vx => vx + RunSpeed)); if (inputState.Right.IsUp() || inputState.Left.IsDown() || inputState.Up.IsDown()) { entity.Set(player.SetState(PlayerState.Idle)); } break; } }