Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/Survival/PlayerAutoHealController.cs
2025-06-24 23:49:13 +08:00

66 lines
1.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using UnityEngine;
namespace Net.Project.B.Health
{
public class PlayerAutoHealController:IDisposable
{
public bool Enabled { get; set; } = true;
private readonly IEntitiesService _entitiesService;
private readonly IKnockedService _knockedService;
private readonly IHealthService _healthService;
private readonly ITicker _ticker;
private readonly ConcurrentDictionary<int, float> _healTime = new();
public PlayerAutoHealController(IHealthService healthService, IEntitiesService entitiesService, ITicker ticker, IKnockedService knockedService)
{
_healthService = healthService;
_entitiesService = entitiesService;
_ticker = ticker;
_knockedService = knockedService;
_healthService.OnHealthChanged += OnHealthChanged;
_ticker.Add(OnTick);
}
private void OnTick(float obj)
{
if(Enabled is false)return;
var currentTime = Time.time;
foreach (var (id, time) in _healTime)
{
if (currentTime < time) continue;
if (_knockedService.Knocked.Contains(id)) continue;
var currentHealthPoint = _healthService.Healths[id];
if (currentHealthPoint >= 0)
{
_healthService.AddHealth(id, 1, this);
}
}
}
private void OnHealthChanged(int arg1, int arg2, int arg3, object arg4)
{
if (arg3 < arg2)
{
_healTime.Set(arg1,Time.time + 3);
}
}
public void Dispose()
{
_healthService.OnHealthChanged -= OnHealthChanged;
_ticker.Remove(OnTick);
}
}
}