Implemented movement

AbstractCharacter mainly fleshed out
InputManager is done
Player movement is done, not ennemy
Energy management is done (AbstractCharacter)
This commit is contained in:
trotFunky 2019-09-17 18:52:55 +02:00
parent 99013c5d7a
commit ea9203ca8a
12 changed files with 304 additions and 26 deletions

View file

@ -2,17 +2,100 @@
using System.Collections.Generic;
using UnityEngine;
public class AbstractCharacter : MonoBehaviour
public abstract class AbstractCharacter : MonoBehaviour
{
//
// 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;
// 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 !");
}
}
// Update is called once per frame
// Manages the energy of the character
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();
}
}

View file

@ -4,7 +4,7 @@ MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
executionOrder: -2
icon: {instanceID: 0}
userData:
assetBundleName:

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class Shooter : MonoBehaviour
public class AbstractShooter : MonoBehaviour
{
// Start is called before the first frame update
void Start()

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class PlayerShooter : MonoBehaviour
public class BasicShooter : AbstractShooter
{
// Start is called before the first frame update
void Start()

View file

@ -4,7 +4,7 @@ MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
executionOrder: -3
icon: {instanceID: 0}
userData:
assetBundleName:

View file

@ -4,15 +4,32 @@ using UnityEngine;
public class InputManager : MonoBehaviour
{
// Start is called before the first frame update
private AbstractCharacter _character;
void Start()
{
_character = GetComponentInParent<AbstractCharacter>();
}
// Update is called once per frame
void Update()
{
//
// Manage movement inputs
//
var movementDirection = Vector2.zero;
movementDirection += Input.GetAxis("Horizontal") * Vector2.right;
movementDirection += Input.GetAxis("Vertical") * Vector2.up;
_character.Move(movementDirection);
//
// Manage action inputs
//
if (Input.GetButton("Fire1"))
{
_character.Shoot();
}
}
}

View file

@ -4,7 +4,7 @@ MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
executionOrder: -5
icon: {instanceID: 0}
userData:
assetBundleName:

View file

@ -2,17 +2,31 @@
using System.Collections.Generic;
using UnityEngine;
public class MovementManager : MonoBehaviour
public class Mover : MonoBehaviour
{
// Start is called before the first frame update
void Start()
private AbstractCharacter _character;
private Transform _transform;
private Vector2 _direction;
private float _maxSpeed;
public Vector2 Direction
{
get { return _direction; }
set { _direction = value; }
}
// Update is called once per frame
void Start()
{
_transform = GetComponentInParent<Transform>();
_character = GetComponentInParent<AbstractCharacter>();
_maxSpeed = _character.MaxSpeed;
_direction = Vector2.zero;
}
void Update()
{
_maxSpeed = _character.MaxSpeed;
Vector2 currentPosition = _transform.position;
_transform.position = currentPosition + _maxSpeed * Time.deltaTime * _direction;
}
}

View file

@ -2,17 +2,19 @@
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharacter : MonoBehaviour
public class PlayerCharacter : AbstractCharacter
{
// Start is called before the first frame update
void Start()
public override void Move(Vector2 movementDirection)
{
// TODO : Implement limits
Debug.Log("Direction:");
Debug.Log(movementDirection);
_mover.Direction = movementDirection;
}
// Update is called once per frame
void Update()
public override void Shoot()
{
// TODO : Implement
throw new System.NotImplementedException();
}
}

View file

@ -4,7 +4,7 @@ MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
executionOrder: -1
icon: {instanceID: 0}
userData:
assetBundleName: