geometry-tower-defense-base/src-ref/Components/BasicBaseComp.cs

78 lines
2.2 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;
[SerializeField] private SpriteRenderer _renderer;
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 SetColor(Color color)
{
if (_renderer != null)
{
_renderer.color = color;
}
}
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;
}
}
}