90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using SepCore.CustomUtility;
|
|
using UnityEngine;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
public class WeaponHandgun : WeaponBase
|
|
{
|
|
#region Property
|
|
|
|
[SerializeField] private Vector3 _fireOriginOffset = Vector3.zero;
|
|
|
|
[SerializeField] private float _hitMarkerSize = 0.2f;
|
|
[SerializeField] private float _hitMarkerYOffset = 1.2f;
|
|
[SerializeField] private float _hitMarkerDuration = 0.15f;
|
|
[SerializeField] private Color _hitMarkerColor = new(1f, 0f, 0f, 0.95f);
|
|
|
|
private IWeaponAttackEffect _attackEffect;
|
|
|
|
#endregion
|
|
|
|
protected override void BuildStates()
|
|
{
|
|
RegisterState(new WeaponHandgunIdleState());
|
|
RegisterState(new WeaponHandgunSeekState());
|
|
RegisterState(new WeaponHandgunAimState());
|
|
RegisterState(new WeaponHandgunAttackState());
|
|
}
|
|
|
|
public override void Attack()
|
|
{
|
|
FaceTargetImmediately();
|
|
|
|
if (!TryResolveAttackTarget(out TargetableObject targetable, out Vector3 hitPosition))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_attackEffect.Play(this, hitPosition, targetable, 0f);
|
|
_isAttacking = true;
|
|
AIUtility.PerformCollision(targetable, this);
|
|
_isAttacking = false;
|
|
}
|
|
|
|
private bool TryResolveAttackTarget(out TargetableObject targetable, out Vector3 hitPosition)
|
|
{
|
|
targetable = _target as TargetableObject;
|
|
hitPosition = CachedTransform.position;
|
|
if (targetable == null || !targetable.Available || targetable.IsDead)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Vector3 targetPosition = targetable.CachedTransform.position;
|
|
hitPosition = targetPosition;
|
|
|
|
Vector3 fireOrigin = CachedTransform.TransformPoint(_fireOriginOffset);
|
|
Vector3 directionToTarget = targetPosition - fireOrigin;
|
|
float maxDistance = WeaponData.AttackRange;
|
|
if (directionToTarget.sqrMagnitude > Mathf.Epsilon &&
|
|
Physics.Raycast(fireOrigin, directionToTarget.normalized, out RaycastHit hit, maxDistance, _hitMask,
|
|
QueryTriggerInteraction.Collide))
|
|
{
|
|
TargetableObject raycastTarget = hit.collider.GetComponentInParent<TargetableObject>();
|
|
if (raycastTarget == targetable)
|
|
{
|
|
hitPosition = hit.point;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#region Lifecycle
|
|
|
|
protected override bool OnWeaponShow(object userData)
|
|
{
|
|
_attackEffect = new HandgunHitMarkerAttackEffect(_hitMarkerSize, _hitMarkerYOffset, _hitMarkerDuration,
|
|
_hitMarkerColor);
|
|
return true;
|
|
}
|
|
|
|
protected override void OnWeaponHide(object userData)
|
|
{
|
|
_attackEffect = null;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|