Files

83 lines
2.9 KiB
C#

using System;
using UnityEngine;
using CharacterMovement;
// 掛在 Player 上 : 把 MotionCore 的移動規則調成接近 Apex
// 1. 身體不轉向移動方向 (視角只由 PlayerLook 控制)
// 2. 自動衝刺,但只有按 W 時才生效
// 3. 按著蹲下落地時直接接滑鏟 (滑鏟跳可以連續接)
[RequireComponent(typeof(PlayerController))]
public class ApexMovementRules : MonoBehaviour, ICharacterMovementModifier
{
// 前後分量要大於這個值才衝刺 : W = 1,W+A / W+D ≈ 0.7
private const float ForwardThreshold = 0.9f;
private PlayerController player;
void Awake()
{
player = GetComponent<PlayerController>();
}
void OnEnable()
{
player.SetMovementFacingEnabled(false);
player.AddMovementModifier(this);
player.Landed += OnLanded;
}
void OnDisable()
{
player.RemoveMovementModifier(this);
player.Landed -= OnLanded;
}
void Update()
{
// PlayerController 在自己的 Start 才設定輸入來源,太早包會被蓋掉,所以等到 Update
if (!(player.InputSource is AutoSprintInput) && TryGetComponent(out InputHandler handler))
{
player.SetInputSource(new AutoSprintInput(handler));
}
}
// MotionCore 每幀呼叫 : 不是往前就禁止衝刺
public void ApplyMovementModifiers(ref CharacterMovementModifiers modifiers)
{
if (player.InputSource.MoveDirection.y < ForwardThreshold)
{
modifiers.Block(MovementCapability.Sprint);
}
}
// 原本按著蹲下落地會變蹲走,改成速度夠就接滑鏟 (條件由 CanStartSlide 判斷)
void OnLanded()
{
if (player.InputSource.IsCrouching && player.CanStartSlide())
{
player.BeginSlide();
}
}
// 包住原本的輸入,只有「衝刺鍵」永遠視為按住
private class AutoSprintInput : ICharacterInputSource
{
private readonly ICharacterInputSource inner;
public AutoSprintInput(ICharacterInputSource inner) { this.inner = inner; }
public Vector2 MoveDirection => inner.MoveDirection;
public bool IsMoving => inner.IsMoving;
public bool IsSprinting => true;
public bool IsWalking => inner.IsWalking;
public bool IsCrouching => inner.IsCrouching;
public bool IsProne => inner.IsProne;
public bool IsJumpHeld => inner.IsJumpHeld;
public event Action JumpPerformed { add => inner.JumpPerformed += value; remove => inner.JumpPerformed -= value; }
public event Action DashPerformed { add => inner.DashPerformed += value; remove => inner.DashPerformed -= value; }
public event Action CrouchPerformed { add => inner.CrouchPerformed += value; remove => inner.CrouchPerformed -= value; }
public event Action PronePerformed { add => inner.PronePerformed += value; remove => inner.PronePerformed -= value; }
}
}