1
This commit is contained in:
89
Src/Health/HealthService.cs
Normal file
89
Src/Health/HealthService.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Net.Project.B.Health
|
||||
{
|
||||
/// <summary>
|
||||
/// 生命值服务
|
||||
/// </summary>
|
||||
public interface IHealthService
|
||||
{
|
||||
/// <summary>
|
||||
/// 已缓存的生命值
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<int,int> Healths { get; }
|
||||
/// <summary>
|
||||
/// 在改变生命值前,ID,当前生命值,新的生命值,来源
|
||||
/// <returns>增加的数值</returns>
|
||||
/// </summary>
|
||||
public event Func<int,int,int,object,int> OnHealthPlus;
|
||||
/// <summary>
|
||||
/// 在改变生命值后
|
||||
/// </summary>
|
||||
/// /// <summary>
|
||||
/// 当健康值发生变化时触发的事件。
|
||||
/// </summary>
|
||||
public event Action<int,int,int,object> OnHealthChanged;
|
||||
/// <summary>
|
||||
/// 调整生命值
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value">+- not set</param>
|
||||
/// <returns></returns>
|
||||
public UniTask<int> AddHealth(int id,int value,object arg);
|
||||
}
|
||||
public class HealthService:IHealthService
|
||||
{
|
||||
private static HealthService _singleton;
|
||||
public HealthService()
|
||||
{
|
||||
_singleton = this;
|
||||
}
|
||||
[BITCommand]
|
||||
public static void SetHealth(int id, int newHealth)
|
||||
{
|
||||
Healths.GetOrAdd(id,100);
|
||||
Healths.Set(id,newHealth);
|
||||
OnHealthChanged?.Invoke(id,newHealth,newHealth,null);
|
||||
}
|
||||
[BITCommand]
|
||||
public static void AddHealth(int id, int newHealth)
|
||||
{
|
||||
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;
|
||||
private static event Action<int,int,int,object> OnHealthChanged;
|
||||
private static readonly ConcurrentDictionary<int, int> Healths = new();
|
||||
|
||||
event Func<int,int,int,object,int> IHealthService.OnHealthPlus
|
||||
{
|
||||
add => OnHealthChange += value;
|
||||
remove => OnHealthChange -= value;
|
||||
}
|
||||
event Action<int,int,int,object> IHealthService.OnHealthChanged
|
||||
{
|
||||
add => OnHealthChanged += value;
|
||||
remove => OnHealthChanged -= value;
|
||||
}
|
||||
public UniTask<int> AddHealth(int id, int value,object arg)
|
||||
{
|
||||
var current = Healths.GetOrAdd(id,100);
|
||||
foreach (var func in OnHealthChange.CastAsFunc())
|
||||
{
|
||||
value =func.Invoke(id,current, value, arg);
|
||||
}
|
||||
var newHp = Math.Clamp(current + value, -1, 100);
|
||||
Healths.Set(id,newHp);
|
||||
OnHealthChanged?.Invoke(id,current,newHp,arg);
|
||||
return UniTask.FromResult(newHp);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user