using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public abstract class AbstractShooter : MonoBehaviour { // Energy consumed per shot [SerializeField] protected float _energyCost = 2; public float EnergyCost => _energyCost; // Minimum time between two shots protected float _minDeltaTime = 1f / 5; [SerializeField] private int _shotsPerSecond = 5; public int ShotsPerSecond { get { return _shotsPerSecond; } set { _shotsPerSecond = value; _minDeltaTime = 1f / _shotsPerSecond; } } private float _timeLeft; // Start is called before the first frame update void Start() { _minDeltaTime = 1f / ShotsPerSecond; _timeLeft = 0; } void Update() { _timeLeft -= Time.deltaTime; } private void OnValidate() { _minDeltaTime = 1f / ShotsPerSecond; } public bool Shoot(float currentEnergy) { if (_timeLeft <= 0 && currentEnergy > 0) { InstantiateProjectiles(); _timeLeft = _minDeltaTime; return true; } return false; } protected abstract void InstantiateProjectiles(); }