155 lines
5.1 KiB
C#
155 lines
5.1 KiB
C#
using System.Collections.Generic;
|
|
using SepCore.CustomUtility;
|
|
using SepCore.Definition;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
public class WeaponKnife : WeaponBase
|
|
{
|
|
private Sequence _attackSequence;
|
|
private Transform _attackParent;
|
|
private Vector3 _attackCenter;
|
|
|
|
[SerializeField] private float _attackDuration = 0.15f;
|
|
[SerializeField] private float _returnDuration = 0.2f;
|
|
[SerializeField] private int _maxHitColliders = 32;
|
|
|
|
private IWeaponAttackEffect _attackEffect;
|
|
private Collider[] _hitResults;
|
|
private readonly HashSet<int> _hitEntityIds = new();
|
|
|
|
public float HitRadius => WeaponData.GetParamFloat(ParamType.HitRadius);
|
|
|
|
protected override void BuildStates()
|
|
{
|
|
RegisterState(new WeaponKnifeIdleState());
|
|
RegisterState(new WeaponKnifeAimState());
|
|
RegisterState(new WeaponKnifeSeekState());
|
|
RegisterState(new WeaponKnifeAttackState());
|
|
}
|
|
|
|
public override void Attack()
|
|
{
|
|
StopAttackTween(false);
|
|
FaceTargetImmediately();
|
|
|
|
_isAttacking = true;
|
|
Transform parentTransform = CachedTransform.parent;
|
|
_attackParent = parentTransform;
|
|
_attackCenter = _target != null ? _target.CachedTransform.position : CachedTransform.position;
|
|
CachedTransform.SetParent(null);
|
|
|
|
Vector3 targetPos = CachedTransform.position + CachedTransform.forward * WeaponData.AttackRange;
|
|
_attackSequence = DOTween.Sequence();
|
|
_attackSequence.Append(CachedTransform.DOMove(targetPos, _attackDuration).SetEase(Ease.OutQuad));
|
|
_attackSequence.AppendCallback(() =>
|
|
{
|
|
_attackEffect.Play(this, _attackCenter, null, HitRadius);
|
|
ApplyGroundAreaDamage();
|
|
if (_attackParent != null)
|
|
{
|
|
CachedTransform.SetParent(_attackParent);
|
|
}
|
|
});
|
|
_attackSequence.Append(
|
|
CachedTransform.DOLocalMove(Vector3.zero, _returnDuration * 0.5f).SetEase(Ease.InQuad));
|
|
_attackSequence.AppendCallback(() =>
|
|
{
|
|
_isAttacking = false;
|
|
_attackSequence = null;
|
|
_attackParent = null;
|
|
});
|
|
}
|
|
|
|
private void ApplyGroundAreaDamage()
|
|
{
|
|
float radius = HitRadius;
|
|
if (radius <= 0f) return;
|
|
|
|
if (TryQueueAreaCollisionQuery(_attackCenter, radius, Mathf.Max(1, _maxHitColliders)))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_hitResults == null || _hitResults.Length == 0) return;
|
|
|
|
int hitCount = Physics.OverlapSphereNonAlloc(_attackCenter, radius, _hitResults, _hitMask,
|
|
QueryTriggerInteraction.Collide);
|
|
|
|
float radiusSqr = radius * radius;
|
|
_hitEntityIds.Clear();
|
|
for (int i = 0; i < hitCount; i++)
|
|
{
|
|
Collider collider = _hitResults[i];
|
|
if (collider == null) continue;
|
|
|
|
TargetableObject targetable = collider.GetComponentInParent<TargetableObject>();
|
|
if (targetable == null || !targetable.Available || targetable.IsDead) continue;
|
|
if (!_hitEntityIds.Add(targetable.Id)) continue;
|
|
|
|
Vector3 delta = targetable.CachedTransform.position - _attackCenter;
|
|
delta.y = 0f;
|
|
if (delta.sqrMagnitude > radiusSqr) continue;
|
|
|
|
AIUtility.PerformCollision(targetable, this);
|
|
}
|
|
}
|
|
|
|
protected override bool OnWeaponShow(object userData)
|
|
{
|
|
_attackEffect = new KnifeRangeAttackEffect();
|
|
|
|
int colliderCapacity = Mathf.Max(1, _maxHitColliders);
|
|
if (_hitResults == null || _hitResults.Length != colliderCapacity)
|
|
{
|
|
_hitResults = new Collider[colliderCapacity];
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected override void OnWeaponHide(object userData)
|
|
{
|
|
StopAttackTween(true);
|
|
_attackEffect = null;
|
|
}
|
|
|
|
protected override void OnWeaponDetach(EntityLogic parentEntity, object userData)
|
|
{
|
|
StopAttackTween(true);
|
|
base.OnWeaponDetach(parentEntity, userData);
|
|
}
|
|
|
|
protected override void OnEnabledChanged(bool enabled)
|
|
{
|
|
if (!enabled)
|
|
{
|
|
StopAttackTween(true);
|
|
}
|
|
}
|
|
|
|
private void StopAttackTween(bool resetTransform)
|
|
{
|
|
if (_attackSequence != null)
|
|
{
|
|
_attackSequence.Kill();
|
|
_attackSequence = null;
|
|
}
|
|
|
|
_isAttacking = false;
|
|
|
|
if (resetTransform && _attackParent != null)
|
|
{
|
|
CachedTransform.SetParent(_attackParent);
|
|
CachedTransform.localPosition = Vector3.zero;
|
|
}
|
|
|
|
_attackParent = null;
|
|
_hitEntityIds.Clear();
|
|
}
|
|
}
|
|
}
|