2026-05-15 충돌오류 진행중
This commit is contained in:
@@ -27,11 +27,31 @@ public class PlayerController : MonoBehaviour
|
||||
private bool IsTouchingWall => _isTouchingLeftWall || _isTouchingRightWall;
|
||||
private int _wallDirection;
|
||||
private float _inputLockTimer;
|
||||
private float _facingLockTimer;
|
||||
|
||||
[Header("Motion")]
|
||||
[SerializeField] private ComboNode _dashRootNode;
|
||||
[SerializeField] private ComboNode _rollRootNode;
|
||||
private readonly Dictionary<ActionData, float> _motionCooldownTimers = new();
|
||||
private readonly List<ActionData> _motionCooldownKeys = new();
|
||||
private readonly List<IgnoredLayerCollision> _ignoredLayerCollisions = new();
|
||||
private readonly List<Collider2D> _overlapResults = new();
|
||||
private readonly List<RaycastHit2D> _castResults = new();
|
||||
private int _activeCollisionRecoveryLayerMask;
|
||||
private Collider2D[] _bodyColliders;
|
||||
private CancellationTokenSource _restoreCollisionCts;
|
||||
private CancellationTokenSource _motionCts;
|
||||
|
||||
[Header("Collision Recovery")]
|
||||
[SerializeField] private float _overlapRecoverySpeed = 8f;
|
||||
[SerializeField] private float _overlapRecoveryOtherBodyRatio = 0.5f;
|
||||
[SerializeField] private float _bodyBlockCastDistance = 0.08f;
|
||||
|
||||
[Header("Attack")]
|
||||
[SerializeField] private ComboNode _punchRootNode;
|
||||
[SerializeField] private ComboNode _kickRootNode;
|
||||
[SerializeField] private LayerMask _enemyLayer;
|
||||
[SerializeField] private LayerMask _bodyCollisionIgnoredLayers;
|
||||
[SerializeField] private string _idleAnimationState = "Idle";
|
||||
[SerializeField] private float _bufferOpenTime = 0.1f;
|
||||
[SerializeField] private float _bufferLifetime = 0.5f;
|
||||
@@ -41,10 +61,11 @@ public class PlayerController : MonoBehaviour
|
||||
private ComboNode _currentNode;
|
||||
private float _comboWindowTimer;
|
||||
private CancellationTokenSource _attackCts;
|
||||
private AttackData _lastAttackGizmoData;
|
||||
private CancellationTokenSource _animationSpeedCts;
|
||||
private ActionData _lastAttackGizmoData;
|
||||
private float _lastAttackGizmoTime = -1f;
|
||||
[SerializeField] private float _hitGizmoFadeDuration = 0.5f;
|
||||
private AttackData _lastHitData;
|
||||
private ActionData _lastHitData;
|
||||
private Vector2 _lastHitCenter;
|
||||
private float _lastHitTime = -1f;
|
||||
|
||||
@@ -62,6 +83,8 @@ private void Awake()
|
||||
_rb = GetComponent<Rigidbody2D>();
|
||||
_anim = GetComponent<Animator>();
|
||||
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
_bodyColliders = GetComponentsInChildren<Collider2D>();
|
||||
IgnoreBodyCollisions();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -71,6 +94,8 @@ private void Start()
|
||||
|
||||
InputManager.Instance.OnPunch_Event += OnPunchInput;
|
||||
InputManager.Instance.OnKick_Event += OnKickInput;
|
||||
InputManager.Instance.OnDash_Event += OnDashInput;
|
||||
InputManager.Instance.OnRoll_Event += OnRollInput;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
@@ -81,10 +106,19 @@ private void OnDestroy()
|
||||
InputManager.Instance.OnJump_Event -= OnJumpInput;
|
||||
InputManager.Instance.OnPunch_Event -= OnPunchInput;
|
||||
InputManager.Instance.OnKick_Event -= OnKickInput;
|
||||
InputManager.Instance.OnDash_Event -= OnDashInput;
|
||||
InputManager.Instance.OnRoll_Event -= OnRollInput;
|
||||
}
|
||||
|
||||
_attackCts?.Cancel();
|
||||
_attackCts?.Dispose();
|
||||
_animationSpeedCts?.Cancel();
|
||||
_animationSpeedCts?.Dispose();
|
||||
_motionCts?.Cancel();
|
||||
_motionCts?.Dispose();
|
||||
_restoreCollisionCts?.Cancel();
|
||||
_restoreCollisionCts?.Dispose();
|
||||
RestoreIgnoredLayerCollisions();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
@@ -97,6 +131,8 @@ private void FixedUpdate()
|
||||
if (_attackCooldownTimer > 0f)
|
||||
_attackCooldownTimer -= Time.fixedDeltaTime;
|
||||
|
||||
UpdateMotionCooldowns();
|
||||
|
||||
if (_attackCooldownTimer <= 0f && _pendingInput.HasValue)
|
||||
{
|
||||
ComboInputType buffered = _pendingInput.Value;
|
||||
@@ -115,7 +151,12 @@ private void FixedUpdate()
|
||||
if (_inputLockTimer > 0f)
|
||||
_inputLockTimer -= Time.fixedDeltaTime;
|
||||
else
|
||||
_rb.linearVelocity = new Vector2(_moveInputX * _moveSpeed, _rb.linearVelocity.y);
|
||||
_rb.linearVelocity = new Vector2(GetBodyBlockedXVelocity(_moveInputX * _moveSpeed), _rb.linearVelocity.y);
|
||||
|
||||
if (_facingLockTimer > 0f)
|
||||
_facingLockTimer -= Time.fixedDeltaTime;
|
||||
else
|
||||
UpdateFacingFromMoveInput();
|
||||
|
||||
if (IsTouchingWall && !_isGrounded && _rb.linearVelocity.y < -_wallSlideSpeed)
|
||||
_rb.linearVelocity = new Vector2(_rb.linearVelocity.x, -_wallSlideSpeed);
|
||||
@@ -125,6 +166,13 @@ private void OnMoveInput(Vector2 value)
|
||||
{
|
||||
_moveInputX = value.x == 0f ? 0f : Mathf.Sign(value.x);
|
||||
|
||||
if (_facingLockTimer > 0f) return;
|
||||
|
||||
UpdateFacingFromMoveInput();
|
||||
}
|
||||
|
||||
private void UpdateFacingFromMoveInput()
|
||||
{
|
||||
if (_moveInputX != 0f && _spriteRenderer != null)
|
||||
_spriteRenderer.flipX = _moveInputX < 0f;
|
||||
}
|
||||
@@ -143,7 +191,9 @@ private void OnJumpInput()
|
||||
}
|
||||
|
||||
private void OnPunchInput() => HandleComboInput(ComboInputType.Punch);
|
||||
private void OnKickInput() => HandleComboInput(ComboInputType.Kick);
|
||||
private void OnKickInput() => HandleComboInput(ComboInputType.Kick);
|
||||
private void OnDashInput() => ExecuteMotionNode(_dashRootNode);
|
||||
private void OnRollInput() => ExecuteMotionNode(_rollRootNode);
|
||||
|
||||
private void HandleComboInput(ComboInputType input)
|
||||
{
|
||||
@@ -168,10 +218,10 @@ private void ExecuteComboInput(ComboInputType input)
|
||||
foreach (var transition in _currentNode.Transitions)
|
||||
{
|
||||
if (transition.Trigger != input) continue;
|
||||
if (transition.Next == null || transition.Next.Attack == null) continue;
|
||||
if (transition.Next == null || transition.Next.Action == null) continue;
|
||||
|
||||
ApplyForwardStep(transition.ForwardStep, transition.ForwardStepDuration);
|
||||
PerformAttack(transition.Next.Attack);
|
||||
PerformAttack(transition.Next.Action, transition.ForwardStep > 0f);
|
||||
_currentNode = transition.Next;
|
||||
_comboWindowTimer = transition.Next.ComboWindow;
|
||||
return;
|
||||
@@ -184,14 +234,262 @@ private void ExecuteComboInput(ComboInputType input)
|
||||
ComboInputType.Kick => _kickRootNode,
|
||||
_ => null
|
||||
};
|
||||
if (root == null || root.Attack == null) return;
|
||||
if (root == null || root.Action == null) return;
|
||||
|
||||
PerformAttack(root.Attack);
|
||||
PerformAttack(root.Action);
|
||||
_currentNode = root;
|
||||
_comboWindowTimer = root.ComboWindow;
|
||||
}
|
||||
|
||||
private async void PerformAttack(AttackData data)
|
||||
private void ExecuteMotionNode(ComboNode root)
|
||||
{
|
||||
if (root == null || root.Action == null) return;
|
||||
if (IsMotionOnCooldown(root.Action)) return;
|
||||
|
||||
PerformMotion(root.Action);
|
||||
_currentNode = root;
|
||||
_comboWindowTimer = root.ComboWindow;
|
||||
}
|
||||
|
||||
private void UpdateMotionCooldowns()
|
||||
{
|
||||
if (_motionCooldownTimers.Count == 0) return;
|
||||
|
||||
_motionCooldownKeys.Clear();
|
||||
foreach (var pair in _motionCooldownTimers)
|
||||
_motionCooldownKeys.Add(pair.Key);
|
||||
|
||||
foreach (var action in _motionCooldownKeys)
|
||||
{
|
||||
float remaining = _motionCooldownTimers[action] - Time.fixedDeltaTime;
|
||||
if (remaining <= 0f)
|
||||
_motionCooldownTimers.Remove(action);
|
||||
else
|
||||
_motionCooldownTimers[action] = remaining;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMotionOnCooldown(ActionData data)
|
||||
{
|
||||
return data != null && _motionCooldownTimers.ContainsKey(data);
|
||||
}
|
||||
|
||||
private void SetMotionCooldown(ActionData data)
|
||||
{
|
||||
if (data == null || data.Cooldown <= 0f) return;
|
||||
|
||||
_motionCooldownTimers[data] = data.Cooldown;
|
||||
}
|
||||
|
||||
private void IgnoreBodyCollisions()
|
||||
{
|
||||
int ignoredLayers = _bodyCollisionIgnoredLayers.value;
|
||||
if (ignoredLayers == 0) return;
|
||||
|
||||
int playerLayer = gameObject.layer;
|
||||
for (int layer = 0; layer < 32; layer++)
|
||||
{
|
||||
if ((ignoredLayers & (1 << layer)) == 0) continue;
|
||||
Physics2D.IgnoreLayerCollision(playerLayer, layer, true);
|
||||
}
|
||||
}
|
||||
|
||||
private float GetBodyBlockedXVelocity(float xVelocity, ActionData action = null)
|
||||
{
|
||||
if (Mathf.Approximately(xVelocity, 0f)) return 0f;
|
||||
if (_bodyCollisionIgnoredLayers.value == 0) return xVelocity;
|
||||
if (CanPassBodyCollision(action)) return xVelocity;
|
||||
if (!IsBodyBlocked(Mathf.Sign(xVelocity))) return xVelocity;
|
||||
|
||||
return 0f;
|
||||
}
|
||||
|
||||
private bool CanPassBodyCollision(ActionData action)
|
||||
{
|
||||
return action != null
|
||||
&& action.IgnoreCollisionDuringAction
|
||||
&& (action.IgnoredCollisionLayers.value & _bodyCollisionIgnoredLayers.value) != 0;
|
||||
}
|
||||
|
||||
private bool IsBodyBlocked(float direction)
|
||||
{
|
||||
if (_bodyColliders == null || _bodyColliders.Length == 0)
|
||||
_bodyColliders = GetComponentsInChildren<Collider2D>();
|
||||
|
||||
ContactFilter2D filter = new ContactFilter2D
|
||||
{
|
||||
useLayerMask = true,
|
||||
layerMask = _bodyCollisionIgnoredLayers,
|
||||
useTriggers = false
|
||||
};
|
||||
|
||||
Vector2 castDirection = new Vector2(direction, 0f);
|
||||
for (int i = 0; i < _bodyColliders.Length; i++)
|
||||
{
|
||||
Collider2D bodyCollider = _bodyColliders[i];
|
||||
if (bodyCollider == null || bodyCollider.isTrigger) continue;
|
||||
|
||||
_castResults.Clear();
|
||||
if (bodyCollider.Cast(castDirection, filter, _castResults, _bodyBlockCastDistance) > 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void IgnoreCollisionsIfNeeded(ActionData data)
|
||||
{
|
||||
_restoreCollisionCts?.Cancel();
|
||||
if (!data.IgnoreCollisionDuringAction)
|
||||
{
|
||||
RestoreIgnoredLayerCollisionsWhenClear();
|
||||
return;
|
||||
}
|
||||
|
||||
int playerLayer = gameObject.layer;
|
||||
int ignoredLayers = data.IgnoredCollisionLayers.value;
|
||||
_activeCollisionRecoveryLayerMask = ignoredLayers;
|
||||
for (int layer = 0; layer < 32; layer++)
|
||||
{
|
||||
if ((ignoredLayers & (1 << layer)) == 0) continue;
|
||||
if (Physics2D.GetIgnoreLayerCollision(playerLayer, layer)) continue;
|
||||
|
||||
Physics2D.IgnoreLayerCollision(playerLayer, layer, true);
|
||||
_ignoredLayerCollisions.Add(new IgnoredLayerCollision(playerLayer, layer));
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreIgnoredLayerCollisions()
|
||||
{
|
||||
for (int i = 0; i < _ignoredLayerCollisions.Count; i++)
|
||||
{
|
||||
IgnoredLayerCollision ignored = _ignoredLayerCollisions[i];
|
||||
Physics2D.IgnoreLayerCollision(ignored.LayerA, ignored.LayerB, false);
|
||||
}
|
||||
|
||||
_ignoredLayerCollisions.Clear();
|
||||
_activeCollisionRecoveryLayerMask = 0;
|
||||
}
|
||||
|
||||
private void RestoreIgnoredLayerCollisionsWhenClear(LayerMask ignoredLayers = default)
|
||||
{
|
||||
int recoveryLayerMask = ignoredLayers.value != 0 ? ignoredLayers.value : _activeCollisionRecoveryLayerMask;
|
||||
if (_ignoredLayerCollisions.Count == 0 && recoveryLayerMask == 0) return;
|
||||
|
||||
_restoreCollisionCts?.Cancel();
|
||||
_restoreCollisionCts?.Dispose();
|
||||
_restoreCollisionCts = CancellationTokenSource.CreateLinkedTokenSource(destroyCancellationToken);
|
||||
WaitUntilClearThenRestoreCollisions(recoveryLayerMask, _restoreCollisionCts.Token);
|
||||
}
|
||||
|
||||
private async void WaitUntilClearThenRestoreCollisions(int recoveryLayerMask, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (IsOverlappingIgnoredLayers(recoveryLayerMask))
|
||||
{
|
||||
ResolveIgnoredLayerOverlap(recoveryLayerMask);
|
||||
await Awaitable.NextFrameAsync(token);
|
||||
}
|
||||
}
|
||||
catch (System.OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RestoreIgnoredLayerCollisions();
|
||||
}
|
||||
|
||||
private void ResolveIgnoredLayerOverlap(int layerMask)
|
||||
{
|
||||
if (layerMask == 0) return;
|
||||
|
||||
if (_bodyColliders == null || _bodyColliders.Length == 0)
|
||||
_bodyColliders = GetComponentsInChildren<Collider2D>();
|
||||
|
||||
ContactFilter2D filter = new ContactFilter2D
|
||||
{
|
||||
useLayerMask = true,
|
||||
layerMask = layerMask,
|
||||
useTriggers = false
|
||||
};
|
||||
|
||||
Vector2 selfCenter = _rb.position;
|
||||
float distance = _overlapRecoverySpeed * Time.deltaTime;
|
||||
|
||||
for (int i = 0; i < _bodyColliders.Length; i++)
|
||||
{
|
||||
Collider2D bodyCollider = _bodyColliders[i];
|
||||
if (bodyCollider == null || bodyCollider.isTrigger) continue;
|
||||
|
||||
_overlapResults.Clear();
|
||||
bodyCollider.Overlap(filter, _overlapResults);
|
||||
|
||||
for (int j = 0; j < _overlapResults.Count; j++)
|
||||
{
|
||||
Collider2D other = _overlapResults[j];
|
||||
if (other == null) continue;
|
||||
|
||||
Vector2 pushDirection = selfCenter - (Vector2)other.bounds.center;
|
||||
if (pushDirection.sqrMagnitude < 0.0001f)
|
||||
pushDirection = GetFacingDirection();
|
||||
pushDirection.Normalize();
|
||||
|
||||
Rigidbody2D otherRb = other.attachedRigidbody;
|
||||
bool canMoveOther = otherRb != null && otherRb != _rb && otherRb.bodyType != RigidbodyType2D.Static;
|
||||
float otherDistance = canMoveOther ? distance * _overlapRecoveryOtherBodyRatio : 0f;
|
||||
float selfDistance = distance - otherDistance;
|
||||
|
||||
_rb.position += pushDirection * selfDistance;
|
||||
if (canMoveOther)
|
||||
otherRb.position -= pushDirection * otherDistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetFacingDirection()
|
||||
{
|
||||
float facing = _spriteRenderer != null && _spriteRenderer.flipX ? -1f : 1f;
|
||||
return new Vector2(facing, 0f);
|
||||
}
|
||||
|
||||
private bool IsOverlappingIgnoredLayers(int layerMask)
|
||||
{
|
||||
if (layerMask == 0) return false;
|
||||
|
||||
if (_bodyColliders == null || _bodyColliders.Length == 0)
|
||||
_bodyColliders = GetComponentsInChildren<Collider2D>();
|
||||
|
||||
ContactFilter2D filter = new ContactFilter2D
|
||||
{
|
||||
useLayerMask = true,
|
||||
layerMask = layerMask,
|
||||
useTriggers = false
|
||||
};
|
||||
|
||||
for (int i = 0; i < _bodyColliders.Length; i++)
|
||||
{
|
||||
Collider2D bodyCollider = _bodyColliders[i];
|
||||
if (bodyCollider == null || bodyCollider.isTrigger) continue;
|
||||
|
||||
_overlapResults.Clear();
|
||||
if (bodyCollider.Overlap(filter, _overlapResults) > 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private int GetCurrentIgnoredLayerMask()
|
||||
{
|
||||
int layerMask = 0;
|
||||
for (int i = 0; i < _ignoredLayerCollisions.Count; i++)
|
||||
layerMask |= 1 << _ignoredLayerCollisions[i].LayerB;
|
||||
|
||||
return layerMask;
|
||||
}
|
||||
|
||||
private async void PerformAttack(ActionData data, bool preserveHorizontalVelocity = false)
|
||||
{
|
||||
_attackCts?.Cancel();
|
||||
_attackCts?.Dispose();
|
||||
@@ -203,20 +501,217 @@ private async void PerformAttack(AttackData data)
|
||||
_attackStartTime = Time.time;
|
||||
_hitFired = false;
|
||||
|
||||
if (_anim != null && !string.IsNullOrEmpty(data.AnimationState))
|
||||
_anim.Play(data.AnimationState);
|
||||
PlayActionAnimation(data);
|
||||
|
||||
if (data.HasMotion)
|
||||
{
|
||||
ApplyActionVelocity(data);
|
||||
}
|
||||
LockMovementIfNeeded(data, preserveHorizontalVelocity);
|
||||
LockFacingIfNeeded(data);
|
||||
IgnoreCollisionsIfNeeded(data);
|
||||
|
||||
try
|
||||
{
|
||||
await HitRoutine(data, token);
|
||||
}
|
||||
catch (System.OperationCanceledException) { }
|
||||
RestoreIgnoredLayerCollisionsWhenClear(data.IgnoredCollisionLayers);
|
||||
}
|
||||
|
||||
private async Awaitable HitRoutine(AttackData data, CancellationToken token)
|
||||
private async void PerformMotion(ActionData data)
|
||||
{
|
||||
if (data == null || IsMotionOnCooldown(data)) return;
|
||||
|
||||
CancelAttack();
|
||||
_motionCts?.Cancel();
|
||||
_motionCts?.Dispose();
|
||||
_motionCts = CancellationTokenSource.CreateLinkedTokenSource(destroyCancellationToken);
|
||||
CancellationToken token = _motionCts.Token;
|
||||
|
||||
SetMotionCooldown(data);
|
||||
|
||||
FaceMotionDirection(data);
|
||||
PlayActionAnimation(data);
|
||||
|
||||
LockMovementIfNeeded(data);
|
||||
LockFacingIfNeeded(data);
|
||||
IgnoreCollisionsIfNeeded(data);
|
||||
|
||||
bool completed = false;
|
||||
try
|
||||
{
|
||||
await MotionRoutine(data, token);
|
||||
completed = true;
|
||||
}
|
||||
catch (System.OperationCanceledException) { }
|
||||
|
||||
if (completed)
|
||||
{
|
||||
StopActionVelocity(data);
|
||||
PlayIdleAnimation();
|
||||
}
|
||||
RestoreIgnoredLayerCollisionsWhenClear(data.IgnoredCollisionLayers);
|
||||
}
|
||||
|
||||
private async Awaitable MotionRoutine(ActionData data, CancellationToken token)
|
||||
{
|
||||
float elapsed = 0f;
|
||||
float duration = Mathf.Max(data.MotionDuration, 0.01f);
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
float normalizedTime = Mathf.Clamp01(elapsed / duration);
|
||||
ApplyActionVelocity(data, normalizedTime);
|
||||
|
||||
if (data.ReturnToIdleOnAnimationComplete && IsActionAnimationComplete(data))
|
||||
return;
|
||||
|
||||
await Awaitable.NextFrameAsync(token);
|
||||
elapsed += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelAttack()
|
||||
{
|
||||
_attackCts?.Cancel();
|
||||
_attackCooldownTimer = 0f;
|
||||
_pendingInput = null;
|
||||
}
|
||||
|
||||
private void ApplyActionVelocity(ActionData data, float normalizedTime = 0f)
|
||||
{
|
||||
float direction = GetMotionDirection(data);
|
||||
float speedMultiplier = data.MotionSpeedCurve != null
|
||||
? data.MotionSpeedCurve.Evaluate(normalizedTime)
|
||||
: 1f;
|
||||
Vector2 velocity = data.Velocity * speedMultiplier;
|
||||
velocity.x *= direction;
|
||||
velocity.x = GetBodyBlockedXVelocity(velocity.x, data);
|
||||
|
||||
if (data.PreserveYVelocity)
|
||||
velocity.y = _rb.linearVelocity.y;
|
||||
|
||||
_rb.linearVelocity = velocity;
|
||||
}
|
||||
|
||||
private void StopActionVelocity(ActionData data)
|
||||
{
|
||||
if (!data.StopHorizontalVelocityOnEnd) return;
|
||||
|
||||
_rb.linearVelocity = new Vector2(0f, _rb.linearVelocity.y);
|
||||
}
|
||||
|
||||
private bool IsActionAnimationComplete(ActionData data)
|
||||
{
|
||||
if (_anim == null || string.IsNullOrEmpty(data.AnimationState)) return false;
|
||||
|
||||
AnimatorStateInfo stateInfo = _anim.GetCurrentAnimatorStateInfo(0);
|
||||
return stateInfo.IsName(data.AnimationState) && stateInfo.normalizedTime >= 1f;
|
||||
}
|
||||
|
||||
private void LockMovementIfNeeded(ActionData data, bool preserveHorizontalVelocity = false)
|
||||
{
|
||||
if (data.CanMoveDuringAction) return;
|
||||
|
||||
if (!preserveHorizontalVelocity && !data.HasMotion)
|
||||
_rb.linearVelocity = new Vector2(0f, _rb.linearVelocity.y);
|
||||
|
||||
_inputLockTimer = Mathf.Max(data.MotionDuration, 0.02f);
|
||||
}
|
||||
|
||||
private void LockFacingIfNeeded(ActionData data)
|
||||
{
|
||||
if (data.CanTurnDuringAction) return;
|
||||
|
||||
_facingLockTimer = Mathf.Max(data.MotionDuration, 0.02f);
|
||||
}
|
||||
|
||||
private void PlayIdleAnimation()
|
||||
{
|
||||
if (_anim == null) return;
|
||||
|
||||
_animationSpeedCts?.Cancel();
|
||||
_anim.speed = 1f;
|
||||
if (!string.IsNullOrEmpty(_idleAnimationState))
|
||||
_anim.Play(_idleAnimationState);
|
||||
}
|
||||
|
||||
private void PlayActionAnimation(ActionData data)
|
||||
{
|
||||
if (_anim == null) return;
|
||||
|
||||
_animationSpeedCts?.Cancel();
|
||||
_animationSpeedCts?.Dispose();
|
||||
_animationSpeedCts = CancellationTokenSource.CreateLinkedTokenSource(destroyCancellationToken);
|
||||
|
||||
_anim.speed = GetAnimationSpeed(data, 0f);
|
||||
if (!string.IsNullOrEmpty(data.AnimationState))
|
||||
_anim.Play(data.AnimationState);
|
||||
|
||||
ApplyAnimationSpeedCurve(data, _animationSpeedCts.Token);
|
||||
}
|
||||
|
||||
private async void ApplyAnimationSpeedCurve(ActionData data, CancellationToken token)
|
||||
{
|
||||
float duration = Mathf.Max(data.MotionDuration, data.HitTiming + data.HitDuration, 0.01f);
|
||||
float elapsed = 0f;
|
||||
|
||||
try
|
||||
{
|
||||
while (elapsed < duration)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
float normalizedTime = Mathf.Clamp01(elapsed / duration);
|
||||
_anim.speed = GetAnimationSpeed(data, normalizedTime);
|
||||
|
||||
await Awaitable.NextFrameAsync(token);
|
||||
elapsed += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
catch (System.OperationCanceledException) { }
|
||||
}
|
||||
|
||||
private float GetAnimationSpeed(ActionData data, float normalizedTime)
|
||||
{
|
||||
float curveMultiplier = data.AnimationSpeedCurve != null
|
||||
? data.AnimationSpeedCurve.Evaluate(normalizedTime)
|
||||
: 1f;
|
||||
|
||||
return Mathf.Max(data.AnimationSpeed * curveMultiplier, 0.01f);
|
||||
}
|
||||
|
||||
private float GetMotionDirection(ActionData data)
|
||||
{
|
||||
if (data.UseInputDirection && _moveInputX != 0f)
|
||||
return _moveInputX;
|
||||
|
||||
return _spriteRenderer != null && _spriteRenderer.flipX ? -1f : 1f;
|
||||
}
|
||||
|
||||
private void FaceMotionDirection(ActionData data)
|
||||
{
|
||||
if (_spriteRenderer == null) return;
|
||||
if (!data.UseInputDirection || _moveInputX == 0f) return;
|
||||
|
||||
_spriteRenderer.flipX = _moveInputX < 0f;
|
||||
_facingLockTimer = 0f;
|
||||
}
|
||||
|
||||
private async Awaitable HitRoutine(ActionData data, CancellationToken token)
|
||||
{
|
||||
float attackStartTime = Time.time;
|
||||
|
||||
if (!data.HasHit)
|
||||
{
|
||||
if (data.MotionDuration > 0f)
|
||||
await Awaitable.WaitForSecondsAsync(data.MotionDuration, token);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.HitTiming > 0f)
|
||||
await Awaitable.WaitForSecondsAsync(data.HitTiming, token);
|
||||
|
||||
@@ -245,11 +740,10 @@ private async Awaitable HitRoutine(AttackData data, CancellationToken token)
|
||||
if (remaining > 0f)
|
||||
await Awaitable.WaitForSecondsAsync(remaining, token);
|
||||
|
||||
if (_anim != null && !string.IsNullOrEmpty(_idleAnimationState))
|
||||
_anim.Play(_idleAnimationState);
|
||||
PlayIdleAnimation();
|
||||
}
|
||||
|
||||
private void ApplyDamageInArea(AttackData data, HashSet<IDamageable> alreadyHit)
|
||||
private void ApplyDamageInArea(ActionData data, HashSet<IDamageable> alreadyHit)
|
||||
{
|
||||
Vector2 center = GetAttackCenter(data.Offset);
|
||||
|
||||
@@ -268,10 +762,23 @@ private void ApplyDamageInArea(AttackData data, HashSet<IDamageable> alreadyHit)
|
||||
alreadyHit.Add(target);
|
||||
}
|
||||
|
||||
target.TakeDamage(data.Damage);
|
||||
target.TakeDamage(data.Damage, GetHitVelocity(data.HitVelocity), data.HitReactionAnimationState);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetHitVelocity(Vector2 hitVelocity)
|
||||
{
|
||||
float facing = _spriteRenderer != null && _spriteRenderer.flipX ? -1f : 1f;
|
||||
hitVelocity.x *= facing;
|
||||
return hitVelocity;
|
||||
}
|
||||
|
||||
private string GetActionName(ActionData data)
|
||||
{
|
||||
if (data == null) return string.Empty;
|
||||
return string.IsNullOrEmpty(data.ActionName) ? data.name : data.ActionName;
|
||||
}
|
||||
|
||||
private Vector2 GetAttackCenter(Vector2 offset)
|
||||
{
|
||||
float facing = _spriteRenderer != null && _spriteRenderer.flipX ? -1f : 1f;
|
||||
@@ -338,7 +845,7 @@ private void DrawLastAttackGizmo()
|
||||
float elapsed = Time.time - _lastAttackGizmoTime;
|
||||
if (elapsed < 0f) return;
|
||||
|
||||
AttackData data = _lastAttackGizmoData;
|
||||
ActionData data = _lastAttackGizmoData;
|
||||
float activeDuration = Mathf.Max(data.HitDuration, 0.05f);
|
||||
float fadeDuration = 0.4f;
|
||||
float total = activeDuration + fadeDuration;
|
||||
@@ -360,7 +867,7 @@ private void OnGUI()
|
||||
{
|
||||
if (!_showAttackDebug || _lastAttackGizmoData == null) return;
|
||||
|
||||
AttackData data = _lastAttackGizmoData;
|
||||
ActionData data = _lastAttackGizmoData;
|
||||
float elapsed = Time.time - _attackStartTime;
|
||||
|
||||
string status = _hitFired
|
||||
@@ -375,7 +882,7 @@ private void OnGUI()
|
||||
: (elapsed >= _bufferOpenTime && _attackCooldownTimer > 0f ? "ready to buffer" : "-");
|
||||
|
||||
string info =
|
||||
$"<b>{(string.IsNullOrEmpty(data.AttackName) ? data.name : data.AttackName)}</b> [{status}]\n" +
|
||||
$"<b>{GetActionName(data)}</b> [{status}]\n" +
|
||||
$"Elapsed : {elapsed:F3} s\n" +
|
||||
$"HitTiming : {data.HitTiming:F3} s\n" +
|
||||
$"HitDuration : {data.HitDuration:F3} s\n" +
|
||||
@@ -397,7 +904,7 @@ private void OnGUI()
|
||||
DrawTimelineBar(data, elapsed);
|
||||
}
|
||||
|
||||
private void DrawTimelineBar(AttackData data, float elapsed)
|
||||
private void DrawTimelineBar(ActionData data, float elapsed)
|
||||
{
|
||||
float barX = 10f;
|
||||
float barY = 302f;
|
||||
@@ -431,4 +938,16 @@ private void DrawTimelineBar(AttackData data, float elapsed)
|
||||
|
||||
GUI.color = Color.white;
|
||||
}
|
||||
|
||||
private readonly struct IgnoredLayerCollision
|
||||
{
|
||||
public readonly int LayerA;
|
||||
public readonly int LayerB;
|
||||
|
||||
public IgnoredLayerCollision(int layerA, int layerB)
|
||||
{
|
||||
LayerA = layerA;
|
||||
LayerB = layerB;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user