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 _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); } } }