67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using GeometryTD.Definition;
|
|
using UnityEngine;
|
|
|
|
namespace Components
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public class BasicBaseComp : MonoBehaviour
|
|
{
|
|
[SerializeField] [Min(0.01f)] private float _attackSpeed = 1f;
|
|
[SerializeField] private AttackPropertyType _attackPropertyType = AttackPropertyType.Physics;
|
|
|
|
private float _cooldownRemaining;
|
|
|
|
public float AttackSpeed => _attackSpeed;
|
|
public AttackPropertyType AttackPropertyType => _attackPropertyType;
|
|
public bool CanAttack => _cooldownRemaining <= 0f;
|
|
|
|
public void OnInit(float attackSpeed, AttackPropertyType attackPropertyType)
|
|
{
|
|
_attackSpeed = Mathf.Max(0.01f, attackSpeed);
|
|
_attackPropertyType = attackPropertyType;
|
|
_cooldownRemaining = 0f;
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_attackSpeed = 1f;
|
|
_attackPropertyType = AttackPropertyType.None;
|
|
_cooldownRemaining = 0f;
|
|
}
|
|
|
|
public void Tick(float deltaTime)
|
|
{
|
|
if (_cooldownRemaining <= 0f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_cooldownRemaining = Mathf.Max(0f, _cooldownRemaining - Mathf.Max(0f, deltaTime));
|
|
}
|
|
|
|
public bool TryAttack(BasicBearingComp bearingComp, ShooterMuzzleComp shooterMuzzleComp, Transform target, float deltaTime)
|
|
{
|
|
if (bearingComp == null || shooterMuzzleComp == null || target == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Tick(deltaTime);
|
|
bool isAligned = bearingComp.TrackTarget(target, deltaTime);
|
|
if (!isAligned || !CanAttack)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool fired = shooterMuzzleComp.Attack(target, _attackPropertyType);
|
|
if (!fired)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
_cooldownRemaining = 1f / _attackSpeed;
|
|
return true;
|
|
}
|
|
}
|
|
}
|