This commit is contained in:
CortexCore
2025-03-24 14:42:29 +08:00
parent ff7afe4133
commit 5fceb6f885
16 changed files with 153 additions and 16 deletions

View File

@@ -12,14 +12,31 @@ namespace Net.Project.B.Health
{
public interface IHealthComponent
{
public int HealthPoint { get;internal set; }
public int MaxHealthPoint { get; }
public event Action<int, int> OnHealthChanged;
}
[Serializable]
public class HealthComponent:IHealthComponent,IWorldNode
{
public int maxHealthPoint = 100;
public int healthPoint;
int IHealthComponent.HealthPoint
{
get => healthPoint;
set
{
var prevHp = healthPoint;
if(prevHp==value)return;
healthPoint = value;
OnHealthChanged?.Invoke(prevHp,healthPoint);
}
}
public int MaxHealthPoint => maxHealthPoint;
public event Action<int, int> OnHealthChanged;
}
/// <summary>
/// 生命值服务
@@ -66,13 +83,9 @@ namespace Net.Project.B.Health
private void OnAdd(IEntity obj)
{
if (obj.ServiceProvider.GetService<IHealthComponent>() is {} healthComponent)
{
if (HealthComponents.TryAdd(obj.Id, healthComponent))
{
Healths.TryAdd(obj.Id, healthComponent.MaxHealthPoint <=0 ? 100 : healthComponent.MaxHealthPoint);
}
}
if (obj.ServiceProvider.QueryComponents(out IHealthComponent healthComponent) is false) return;
HealthComponents[obj.Id] = healthComponent;
Healths[obj.Id] = healthComponent.HealthPoint = healthComponent.MaxHealthPoint <= 0 ? 100 : healthComponent.MaxHealthPoint;
}
[BITCommand]
@@ -87,9 +100,7 @@ namespace Net.Project.B.Health
{
Healths.GetOrAdd(id,100);
_singleton.AddHealth(id, newHealth, null).Forget();
}
IReadOnlyDictionary<int, int> IHealthService.Healths => Healths;
private static event Func<int,int,int,object,int> OnHealthChange;
@@ -115,7 +126,7 @@ namespace Net.Project.B.Health
value =func.Invoke(id,current, value, arg);
}
var newHp = Math.Clamp(current + value, -1, HealthComponents[id].MaxHealthPoint);
Healths.Set(id,newHp);
Healths.Set(id,HealthComponents[id].HealthPoint = newHp);
OnHealthChanged?.Invoke(id,current,newHp,arg);
return UniTask.FromResult(newHp);
}