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
This commit is contained in:
parent
f6529e33da
commit
42706712aa
15 changed files with 458 additions and 29 deletions
|
@ -78,13 +78,17 @@ public abstract class AbstractCharacter : MonoBehaviour
|
|||
_isDepleted = false;
|
||||
}
|
||||
}
|
||||
if (CurrentEnergy < 0)
|
||||
if (CurrentEnergy <= 0)
|
||||
{
|
||||
CurrentEnergy = 0;
|
||||
_isDepleted = true;
|
||||
}
|
||||
|
||||
// The InputManager is executed before and updates it to true if needed
|
||||
_isShooting = false;
|
||||
|
||||
// FIXME : Debug drawing to show energy level
|
||||
Debug.DrawLine(new Vector3(-5,4), new Vector3(-5 + 10 * CurrentEnergy/MaxEnergy,4),Color.blue);
|
||||
}
|
||||
|
||||
public abstract void Move(Vector2 movementDirection);
|
||||
|
|
|
@ -1,18 +1,57 @@
|
|||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AbstractShooter : MonoBehaviour
|
||||
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;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -4,15 +4,26 @@ using UnityEngine;
|
|||
|
||||
public class BasicShooter : AbstractShooter
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
// Bullet that will be fired
|
||||
[SerializeField] private GameObject _bullet;
|
||||
|
||||
private Transform _parentTransform;
|
||||
|
||||
// Direction the bullet will be going
|
||||
[SerializeField] private Vector2 _direction = Vector2.right;
|
||||
|
||||
// Offset between parent and newly created bullet
|
||||
[SerializeField] private Vector2 _offset = Vector2.zero;
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
_parentTransform = GetComponentInParent<Transform>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
protected override void InstantiateProjectiles()
|
||||
{
|
||||
|
||||
Vector3 newPosition = _parentTransform.position + (Vector3)_offset;
|
||||
GameObject child = Instantiate(_bullet, newPosition, Quaternion.identity);
|
||||
child.GetComponent<Bullet>().Direction = _direction;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,12 +35,6 @@ public class Bullet : MonoBehaviour
|
|||
mover.MaxSpeed = Speed;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Destroy on Stay, collision effects should be handled on Enter
|
||||
private void OnCollisionStay2D(Collision2D other)
|
||||
{
|
||||
|
|
|
@ -12,8 +12,16 @@ public class PlayerCharacter : AbstractCharacter
|
|||
|
||||
public override void Shoot()
|
||||
{
|
||||
// TODO : Implement
|
||||
throw new System.NotImplementedException();
|
||||
_isShooting = true;
|
||||
if (!_isDepleted && _shooter.Shoot(CurrentEnergy))
|
||||
{
|
||||
CurrentEnergy -= _shooter.EnergyCost;
|
||||
}
|
||||
else if (_isDepleted)
|
||||
{
|
||||
// Force regen when depleted even if holding the trigger
|
||||
_isShooting = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Death()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue