Schmup-JIN/Assets/Scripts/AbstractShooter.cs
trotFunky 42706712aa Implemented shooting
Fixed abstract character not depleting energy when reaching zero
Created prefabs for player and enemy bullets
Added physics layers for players and enemies
2019-09-19 12:36:24 +02:00

57 lines
1.2 KiB
C#

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();
}