Schmup-JIN/Assets/Scripts/AbstractCharacter.cs

102 lines
2.5 KiB
C#
Raw Normal View History

2019-09-17 15:43:40 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class AbstractCharacter : MonoBehaviour
2019-09-17 15:43:40 +02:00
{
//
// Core components of a character
//
protected Mover _mover;
protected AbstractShooter _shooter;
//
// Characteristics and status of a character
//
// Speed in m/s
[SerializeField] protected float _maxSpeed;
public float MaxSpeed
{
get { return _maxSpeed; }
set { _maxSpeed = value; }
}
[SerializeField] protected int _maxHealth;
public int MaxHealth => _maxHealth;
public int CurrentHealth { get; set; }
[SerializeField] protected float _maxEnergy;
public float MaxEnergy => _maxEnergy;
public float CurrentEnergy { get; set; }
// Energy gained back per second
[SerializeField] private float _energyRegen;
public float EnergyRegen
{
get { return _energyRegen; }
set { _energyRegen = value; }
}
protected bool _isShooting;
// True if the energy went down to zero
protected bool _isDepleted;
2019-09-17 15:43:40 +02:00
// Start is called before the first frame update
void Start()
{
MaxSpeed = 10;
_maxHealth = 1;
CurrentHealth = MaxHealth;
_maxEnergy = 100;
CurrentEnergy = MaxEnergy;
EnergyRegen = 20;
_mover = GetComponentInParent<Mover>();
_shooter = GetComponentInParent<AbstractShooter>();
if (_mover == null)
{
throw new MissingComponentException("No Mover !");
}
if (_shooter == null)
{
throw new MissingComponentException("No Shooter !");
}
2019-09-17 15:43:40 +02:00
}
// Manages the energy of the character
2019-09-17 15:43:40 +02:00
void Update()
{
if (!_isShooting && CurrentEnergy < MaxEnergy)
{
var regen = _isDepleted ? EnergyRegen * 0.75f : EnergyRegen;
CurrentEnergy += regen * Time.deltaTime;
}
if (CurrentEnergy > MaxEnergy)
{
CurrentEnergy = MaxEnergy;
if (_isDepleted)
{
_isDepleted = false;
}
}
if (CurrentEnergy < 0)
{
CurrentEnergy = 0;
}
// The InputManager is executed before and updates it to true if needed
_isShooting = false;
}
public abstract void Move(Vector2 movementDirection);
public abstract void Shoot();
// TODO : Manage hurt + OnCollision + Death'n stuff
public void Hurt()
{
throw new System.NotImplementedException();
2019-09-17 15:43:40 +02:00
}
}