using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using BITKit; using BITKit.Entities; using UnityEngine; namespace BITFALL.GameMode { public class PlayerAutoSpawnController : MonoBehaviour { [SerializeReference, SubclassSelector] private IEntitiesService entitiesService; private void Start() { entitiesService.OnAdd += OnEntityAdded; destroyCancellationToken.Register(() => { entitiesService.OnAdd -= OnEntityAdded; }); } private void OnEntityAdded(IEntity obj) { if (obj.TryGetComponent(out var heal) is false) return; var token = destroyCancellationToken; if (obj is MonoBehaviour behaviour) { token = behaviour.destroyCancellationToken; } heal.OnSetAlive += async alive => { if (alive) return; var time = Data.Get(BITConstant.Environment.mp_respawn_on_death); switch (time) { case 0: heal.HealthPoint = heal.MaxHealthPoint; return; case -1: return; default: try { await Task.Delay(TimeSpan.FromSeconds(time), token); } catch (OperationCanceledException) { return; } heal.HealthPoint = heal.MaxHealthPoint; break; } }; } } }