diff --git a/src/Entity/IHasArmor.cs b/src/Entity/IHasArmor.cs index f34c259..bffbfd4 100644 --- a/src/Entity/IHasArmor.cs +++ b/src/Entity/IHasArmor.cs @@ -8,5 +8,6 @@ namespace Mombae.Entity; public interface IHasArmor : IHasHealth { - public int ArmorPoints { get; } + public int MaxArmorPoints { get; } + public int CurrentArmorPoints { get; set; } } diff --git a/src/Entity/IHasHealth.cs b/src/Entity/IHasHealth.cs index 171feb1..e2aba9f 100644 --- a/src/Entity/IHasHealth.cs +++ b/src/Entity/IHasHealth.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; namespace Mombae.Entity; public interface IHasHealth { - public int Health { get; } - public void ReceiveDamage(int hp); + public int MaxHealth { get; } + public int CurrentHealth { get; set; } } diff --git a/src/Entity/Vehicle/VehicleBase.cs b/src/Entity/Vehicle/VehicleBase.cs index 24d42b3..6ca7f00 100644 --- a/src/Entity/Vehicle/VehicleBase.cs +++ b/src/Entity/Vehicle/VehicleBase.cs @@ -9,22 +9,8 @@ namespace Mombae.Entity.Vehicle; public abstract class VehicleBase : Unit, IHasArmor { - public abstract int ArmorPoints { get; protected set; } - public abstract int Health { get; protected set; } - - public void ReceiveDamage(int hp) - { - if (ArmorPoints > 0) - { - ArmorPoints -= hp; - if (ArmorPoints < 0) - { - Health -= Math.Abs(ArmorPoints); - } - } - else - { - Health -= Math.Max(0, Health - hp); - } - } + public abstract int MaxArmorPoints { get; protected set; } + public abstract int MaxHealth { get; protected set; } + public int CurrentArmorPoints { get; set; } + public int CurrentHealth { get; set; } } diff --git a/src/Entity/Vehicle/Vidulg.cs b/src/Entity/Vehicle/Vidulg.cs index 40d01e6..eef12a3 100644 --- a/src/Entity/Vehicle/Vidulg.cs +++ b/src/Entity/Vehicle/Vidulg.cs @@ -7,6 +7,6 @@ namespace Mombae.Entity.Vehicle; public class Vidulg : VehicleBase { - public override int ArmorPoints { get; protected set; } = 300; - public override int Health { get; protected set; } = 1200; + public override int MaxArmorPoints { get; protected set; } = 300; + public override int MaxHealth { get; protected set; } = 1200; } diff --git a/src/Services/EntityManager.cs b/src/Services/EntityManager.cs new file mode 100644 index 0000000..82267fe --- /dev/null +++ b/src/Services/EntityManager.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Mombae.Entity; + +namespace Mombae.src.Services; + +public class EntityManager +{ + private readonly HashSet _entityRegistry = new(); + + //todo + + public void ClearEntities() + { + //todo mgmt + _entityRegistry.Clear(); + } + + public void Spawn() where T : Unit + { + Spawn((_) => { }); + } + + public void Spawn(Action dataConfig) where T : Unit + { + T u = Activator.CreateInstance(); + dataConfig.Invoke(u); + + _entityRegistry.Add(u); + + //todo display mgmt + } + + public void Damage(int hp, Unit u) + { + if (!u.GetType().IsAssignableTo(typeof(IHasHealth))) + { + return; + } + + IHasHealth h = (IHasHealth)u; + + int toWithdraw = 0; + + if (u.GetType().IsAssignableTo(typeof(IHasArmor))) + { + IHasArmor a = (IHasArmor)h; + + if (a.CurrentArmorPoints > 0) + { + a.CurrentArmorPoints -= hp; + if (a.CurrentArmorPoints < 0) + { + toWithdraw = Math.Abs(a.CurrentArmorPoints); + a.CurrentArmorPoints = 0; + } + } + + } + else toWithdraw = hp; + + h.CurrentHealth -= Math.Max(h.CurrentHealth - toWithdraw, 0); + + if (h.CurrentHealth == 0) + { + Kill(u); + } + } + + public void Kill(Unit u) + { + if (!u.GetType().IsAssignableTo(typeof(IHasHealth))) + { + return; + } + + //_entityRegistry.Remove(u); + //todo mgmt + } +}