Compare commits

...

2 Commits

Author SHA1 Message Date
Stedd ea2119913a Added some summaries 2024-10-15 23:44:33 +02:00
Stedd 6a49fa1812 Changed how weapons count total damage dealt and kills 2024-10-15 23:42:05 +02:00
4 changed files with 48 additions and 10 deletions

View File

@ -1,10 +1,17 @@
using UnityEngine;
namespace GameDev.CoreSystems
{
public interface IDamageable
{
#region Methods
public void ModifyHealth(int healthChange);
/// <summary>
/// Modify the health of the damageable
/// </summary>
/// <param name="healthChange">Amount of health change</param>
/// <param name="source">Source GameObject, used to modify stats of the source. Total kills, etc.</param>
public void ModifyHealth(int healthChange, GameObject source);
#endregion

View File

@ -2,7 +2,13 @@ namespace GameDev.CoreSystems
{
public interface IWeapon
{
/// <summary>
/// [s] Fire rate of the weapon
/// </summary>
public float FireRate { get; set; }
/// <summary>
/// Weapon damage
/// </summary>
public int Damage { get; set; }
}
}

View File

@ -17,6 +17,11 @@ namespace GameDev.CoreSystems
public UnityEvent DeathEvent;
/// <summary>
/// Used to ensure only one damage dealer gets the kill score
/// </summary>
private bool _isAlreadyDead;
private void OnEnable()
{
_activeDamageableRuntimeSet.Add(this);
@ -29,13 +34,30 @@ namespace GameDev.CoreSystems
#region Methods
public void ModifyHealth(int healthChange)
public void ModifyHealth(int healthChange, GameObject source)
{
if (IsInvulnerable) return;
var receivingDamage = healthChange < 0;
var receivingHealing = healthChange > 0;
if (IsInvulnerable && receivingDamage) return;
CurrentHealth += healthChange;
if (CurrentHealth >= 0) return;
source.TryGetComponent(out Weapon weapon);
if (weapon != null && !_isAlreadyDead)
{
weapon.IncrementDamageDealt(-healthChange);
}
if (CurrentHealth > 0 || _isAlreadyDead) return;
_isAlreadyDead = true;
if (weapon != null)
{
weapon.IncrementKillCount();
}
print($"{transform.parent.parent.name} died");
DeathEvent.Invoke();

View File

@ -26,7 +26,8 @@ namespace GameDev.CoreSystems
{
_timeUntilFire -= Time.deltaTime;
if (!(_timeUntilFire <= 0)) return;
var reloading = _timeUntilFire > 0;
if (reloading) return;
var position = BarrelEndPoint.position;
var forward = BarrelEndPoint.forward;
@ -36,12 +37,9 @@ namespace GameDev.CoreSystems
{
if (hitInfo.transform.TryGetComponent(out Targetable target))
{
target.Damageable.DeathEvent.AddListener(IncrementKillCount);
Debug.DrawRay(position, forward * Vector3.Distance(position, target.GetCenterPosition()),
Color.red, 0.1f);
target.Damageable.ModifyHealth(-Damage);
DamageDealt += Damage;
target.Damageable.DeathEvent.RemoveListener(IncrementKillCount);
target.Damageable.ModifyHealth(-Damage, gameObject);
}
}
@ -53,7 +51,12 @@ namespace GameDev.CoreSystems
}
}
private void IncrementKillCount()
public void IncrementDamageDealt(int damage)
{
DamageDealt += damage;
}
public void IncrementKillCount()
{
NumberOfKills += 1;
}