This commit is contained in:
femsci 2023-06-29 17:02:51 +02:00
parent d5ff070db6
commit 946317a595
Signed by: femsci
GPG key ID: 08F7911F0E650C67
5 changed files with 95 additions and 23 deletions

View file

@ -8,5 +8,6 @@ namespace Mombae.Entity;
public interface IHasArmor : IHasHealth
{
public int ArmorPoints { get; }
public int MaxArmorPoints { get; }
public int CurrentArmorPoints { get; set; }
}

View file

@ -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; }
}

View file

@ -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; }
}

View file

@ -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;
}

View file

@ -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<Unit> _entityRegistry = new();
//todo
public void ClearEntities()
{
//todo mgmt
_entityRegistry.Clear();
}
public void Spawn<T>() where T : Unit
{
Spawn<T>((_) => { });
}
public void Spawn<T>(Action<T> dataConfig) where T : Unit
{
T u = Activator.CreateInstance<T>();
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
}
}