Schmup-JIN/Assets/Scripts/AbstractShooter.cs

58 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections;
2019-09-17 15:43:40 +02:00
using System.Collections.Generic;
using UnityEngine;
public abstract class AbstractShooter : MonoBehaviour
2019-09-17 15:43:40 +02:00
{
// 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;
2019-09-17 15:43:40 +02:00
// Start is called before the first frame update
void Start()
{
_minDeltaTime = 1f / ShotsPerSecond;
_timeLeft = 0;
2019-09-17 15:43:40 +02:00
}
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;
2019-09-17 15:43:40 +02:00
}
protected abstract void InstantiateProjectiles();
2019-09-17 15:43:40 +02:00
}