using System;
using Framework.Input.States;
using UnityEngine;
namespace Framework.Actors.Platformer {
/// Lets the player control a squatting character
public class SquatMove : PlayerMove {
/// Called when the actor has started the move
public override void OnStarted() {
base.OnStarted();
EnableGravity(true);
}
/// Called once per physics frame
///
/// Updates the world position of the actor
///
public override void FixedUpdate() {
base.FixedUpdate();
// Update the grounded state (the actor's physics component will have
// moved it before or after this code ran, so the isGrounded state is now stale)
bool isGrounded = RecheckGrounding();
// If the actor is grounded, reset the jump count
if(isGrounded) {
ActorController.ConsecutiveJumpCount = 0;
} else { // If the actor walks off a cliff, he'll be airborne
SwitchToAirMove(jumped: false);
ActorController.ActiveMove.FixedUpdate();
return;
}
PlatformerInputState inputState = InputManager.GetState();
// Standard left/right movement of the actor
handleHorizontalMovement(inputState);
// If the player is no longer holding the down key and the actor is
// not blocked from standing up, do so
if(!inputState.Down.IsActive) {
if(CanStandUp()) {
SwitchToGroundMove();
}
}
// Begin dashing if the player hits the dash key and it's enabled
if(inputState.Dash.WasTriggered) {
if(Abilities.CanDash) {
SwitchToDashMove();
return;
}
}
// If the player pressed the jump key, we'll be airborne with upward velocity
if(inputState.Jump.WasTriggered) {
if(ActorController.ConsecutiveJumpCount < Abilities.JumpCount) {
SwitchToAirMove(jumped: true);
return;
}
}
}
/// Checker whether the actor can stand up
/// True if the actor can stand up
///
/// Override this to prevent the character from standing up if, for example,
/// there is not enough space over the actor's head.
///
protected virtual bool CanStandUp() {
return true;
}
///
/// Switches the presenter to the animation state appropriate for this move
///
protected override void SetPresenterState() {
PlatformerActorPresenter presenter = ActorPresenter;
if(presenter != null) {
presenter.SetSquatState();
}
}
/// Movement logic to move the actor to the left and right
/// Inputs the player has made this frame
private void handleHorizontalMovement(PlatformerInputState inputState) {
// Determine the speed at which the player wants to move and the highest
// possible speed the actor could achieve (used to calculate acceleration)
float fastestSpeed;
{
if(Abilities.CanSprint && inputState.Sprint.IsActive) {
fastestSpeed = Abilities.RunningSpeed;
} else {
fastestSpeed = Abilities.WalkingSpeed;
}
}
// Make the actor stand still
float acceleration = fastestSpeed / Abilities.SecondsToFullSpeed;
AccelerateToVelocity(0.0f, acceleration);
// Turn the actor towards the direction the player is directing
if(inputState.Horizontal < -0.1f) {
ActorController.FacingDirection = -1.0f;
} else if(inputState.Horizontal > 0.1f) {
ActorController.FacingDirection = +1.0f;
}
}
}
} // namespace Framework.Actors.Platformer