#nullable enable using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] public class PlayerMovement : MonoBehaviour { [SerializeField] PlayerStats playerStats = null!; Rigidbody2D rb = null!; Vector2 moveDirection; BaseState currentState = new ImmobileMovementState(); SafeZone? safeZone; bool lastJumpButton; void Awake() => rb = GetComponent(); void FixedUpdate() { if (currentState.FixedUpdateState(this) is {} newState) SwitchState(newState); } void OnDrawGizmos() => currentState.OnDrawGizmos(this); public void OnMove(InputAction.CallbackContext ctx) { moveDirection = ctx.ReadValue(); if (moveDirection.sqrMagnitude > 1.0f) moveDirection.Normalize(); } public void OnJump(InputAction.CallbackContext ctx) { //feels pretty redundant ^^' bool newValue = ctx.ReadValueAsButton(); bool wasJustPressed = !lastJumpButton && newValue; lastJumpButton = newValue; if (!wasJustPressed) return; if (safeZone == null) return; if (safeZone.IsInSafeZone) { if (moveDirection.magnitude >= safeZone.stats.minJumpJoystickValue) SwitchState(new ExitSafeZoneMovementState(safeZone, moveDirection)); } else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90) SwitchState(new EnterSafeZoneMovementState(safeZone)); } void SwitchState(BaseState newState) { currentState.LeaveState(this); currentState = newState; newState.EnterState(this); } void OnTriggerEnter2D(Collider2D other) { if (other.GetComponent() is {} triggeredSafeZone) safeZone = triggeredSafeZone; } //Probably should remove, mostly for ImmobileState's draw gizmos void OnTriggerStay2D(Collider2D other) { if (other.GetComponent() is {} triggeredSafeZone) safeZone = triggeredSafeZone; } void OnTriggerExit2D(Collider2D other) { if (other.GetComponent() is {}) safeZone = null; } class BaseState { public virtual void EnterState(PlayerMovement playerMovement) {} public virtual void LeaveState(PlayerMovement playerMovement) {} public virtual BaseState? UpdateState(PlayerMovement playerMovement) => null; public virtual BaseState? FixedUpdateState(PlayerMovement playerMovement) => null; public virtual void OnDrawGizmos(PlayerMovement playerMovement) {} } class NormalMovementState : BaseState { public override BaseState? FixedUpdateState(PlayerMovement playerMovement) { playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.playerStats.movementSpeed; return null; } } class JumpingMovementState : BaseState { readonly float duration; readonly Vector3 target; Vector3 startPosition; float startTime; public JumpingMovementState(float duration, Vector3 target) { this.duration = duration; this.target = target; } public override void EnterState(PlayerMovement playerMovement) { startPosition = playerMovement.transform.position; startTime = Time.time; playerMovement.rb.velocity = Vector2.zero; } public override BaseState? FixedUpdateState(PlayerMovement playerMovement) { float currentTime = Time.time - startTime; if (currentTime >= duration) return Transition(); playerMovement.rb.MovePosition(Vector3.Lerp( startPosition, target, CubicEaseOut(currentTime / duration) )); return null; } protected virtual BaseState Transition() => new NormalMovementState(); static float CubicEaseOut(float t) => 1 - Mathf.Pow(1 - t, 3); } class EnterSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; public EnterSafeZoneMovementState(SafeZone safeZone) : base(safeZone.stats.jumpDuration, safeZone.transform.position) { this.safeZone = safeZone; } public override void EnterState(PlayerMovement playerMovement) { base.EnterState(playerMovement); safeZone.EnterSafeZone(); playerMovement.rb.velocity = Vector2.zero; } protected override BaseState Transition() => new ImmobileMovementState(); } class ExitSafeZoneMovementState : JumpingMovementState { readonly SafeZone safeZone; public ExitSafeZoneMovementState(SafeZone safeZone, Vector2 direction) : base(safeZone.stats.jumpDuration, safeZone.GetOutsidePosition(direction)) { this.safeZone = safeZone; } public override void LeaveState(PlayerMovement playerMovement) { base.EnterState(playerMovement); safeZone.ExitSafeZone(); playerMovement.rb.velocity = Vector2.zero; } } class ImmobileMovementState : BaseState { #if UNITY_EDITOR public override void OnDrawGizmos(PlayerMovement playerMovement) { if (playerMovement.safeZone is null) return; Vector3 dropPosition = playerMovement.safeZone.GetOutsidePosition(playerMovement.moveDirection); bool canJump = playerMovement.moveDirection.magnitude >= playerMovement.safeZone.stats.minJumpJoystickValue; Gizmos.color = canJump ? Color.green : Color.red; Gizmos.DrawLine(playerMovement.transform.position, dropPosition); if (canJump) Gizmos.DrawSphere(dropPosition, .5f); else Gizmos.DrawWireSphere(dropPosition, .5f); } #endif } }