2019-09-17 20:45:08 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2019-09-17 15:43:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2019-09-17 18:52:55 +02:00
|
|
|
|
public abstract class AbstractCharacter : MonoBehaviour
|
2019-09-17 15:43:40 +02:00
|
|
|
|
{
|
2019-09-17 18:52:55 +02:00
|
|
|
|
//
|
|
|
|
|
// Core components of a character
|
|
|
|
|
//
|
|
|
|
|
protected Mover _mover;
|
|
|
|
|
protected AbstractShooter _shooter;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Characteristics and status of a character
|
|
|
|
|
//
|
|
|
|
|
// Speed in m/s
|
2019-09-19 12:06:23 +02:00
|
|
|
|
[SerializeField] protected float _maxSpeed = 10;
|
2019-09-17 18:52:55 +02:00
|
|
|
|
public float MaxSpeed
|
|
|
|
|
{
|
|
|
|
|
get { return _maxSpeed; }
|
|
|
|
|
set { _maxSpeed = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 12:06:23 +02:00
|
|
|
|
[SerializeField] protected int _maxHealth = 1;
|
2019-09-17 18:52:55 +02:00
|
|
|
|
public int MaxHealth => _maxHealth;
|
|
|
|
|
public int CurrentHealth { get; set; }
|
|
|
|
|
|
2019-09-19 12:06:23 +02:00
|
|
|
|
[SerializeField] protected float _maxEnergy = 100;
|
2019-09-17 18:52:55 +02:00
|
|
|
|
public float MaxEnergy => _maxEnergy;
|
|
|
|
|
public float CurrentEnergy { get; set; }
|
|
|
|
|
// Energy gained back per second
|
2019-09-19 12:06:23 +02:00
|
|
|
|
[SerializeField] private float _energyRegen = 20;
|
2019-09-17 18:52:55 +02:00
|
|
|
|
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()
|
|
|
|
|
{
|
2019-09-17 18:52:55 +02:00
|
|
|
|
CurrentHealth = MaxHealth;
|
|
|
|
|
CurrentEnergy = MaxEnergy;
|
|
|
|
|
|
|
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 18:52:55 +02:00
|
|
|
|
// Manages the energy of the character
|
2019-09-17 15:43:40 +02:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
2019-09-17 18:52:55 +02:00
|
|
|
|
if (!_isShooting && CurrentEnergy < MaxEnergy)
|
|
|
|
|
{
|
|
|
|
|
var regen = _isDepleted ? EnergyRegen * 0.75f : EnergyRegen;
|
|
|
|
|
CurrentEnergy += regen * Time.deltaTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CurrentEnergy > MaxEnergy)
|
|
|
|
|
{
|
|
|
|
|
CurrentEnergy = MaxEnergy;
|
|
|
|
|
if (_isDepleted)
|
|
|
|
|
{
|
|
|
|
|
_isDepleted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-19 12:36:09 +02:00
|
|
|
|
if (CurrentEnergy <= 0)
|
2019-09-17 18:52:55 +02:00
|
|
|
|
{
|
|
|
|
|
CurrentEnergy = 0;
|
2019-09-19 12:36:09 +02:00
|
|
|
|
_isDepleted = true;
|
2019-09-17 18:52:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The InputManager is executed before and updates it to true if needed
|
|
|
|
|
_isShooting = false;
|
2019-09-19 12:36:09 +02:00
|
|
|
|
|
|
|
|
|
// FIXME : Debug drawing to show energy level
|
|
|
|
|
Debug.DrawLine(new Vector3(-5,4), new Vector3(-5 + 10 * CurrentEnergy/MaxEnergy,4),Color.blue);
|
2019-09-17 18:52:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void Move(Vector2 movementDirection);
|
|
|
|
|
public abstract void Shoot();
|
|
|
|
|
|
2019-09-17 20:45:08 +02:00
|
|
|
|
private void OnCollisionEnter2D(Collision2D other)
|
2019-09-17 18:52:55 +02:00
|
|
|
|
{
|
2019-09-17 20:45:08 +02:00
|
|
|
|
Bullet bullet = other.gameObject.GetComponent<Bullet>();
|
|
|
|
|
if (bullet == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Hurt(bullet.Damage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Hurt(int damage)
|
|
|
|
|
{
|
|
|
|
|
CurrentHealth -= damage;
|
|
|
|
|
if (CurrentHealth <= 0)
|
|
|
|
|
{
|
|
|
|
|
Death();
|
|
|
|
|
}
|
2019-09-17 15:43:40 +02:00
|
|
|
|
}
|
2019-09-17 20:45:08 +02:00
|
|
|
|
// Handles the death of the character, should be overriden
|
|
|
|
|
protected abstract void Death();
|
2019-09-17 15:43:40 +02:00
|
|
|
|
}
|