using System.Collections.Generic; using UnityEngine; public class UnitRespawnerForBuildings : MonoBehaviour { public Hp hp; public List units; public List timeToRespawnAUnitDependingOnLevel = new List(); private float timeTillNextRespawn; public BuildSlot myBuildSlot; public TaggedObject taggedObject; public ProductionBar productionBar; [Header("Quick arrange units:")] public Transform copyUnitPositionsFrom; private bool gladiatorSchool; private bool elliteWarriors; private float gladiatorSchoolSpeed; private float elliteWarriorSpeed; private float totalTrainingSpeed = 1f; [SerializeField] private Equippable gladiatorSchoolPerk; [SerializeField] private Equippable elliteWarriorsPerk; private bool godOfDeathActive; private float ResetCooldownTime => timeToRespawnAUnitDependingOnLevel[Mathf.Clamp(myBuildSlot.Level, 0, timeToRespawnAUnitDependingOnLevel.Count - 1)]; private void Start() { godOfDeathActive = PerkManager.instance.GodOfDeathActive; timeTillNextRespawn = ResetCooldownTime; productionBar.UpdateVisual(0f); gladiatorSchool = PerkManager.IsEquipped(gladiatorSchoolPerk); elliteWarriors = PerkManager.IsEquipped(elliteWarriorsPerk); gladiatorSchoolSpeed = PerkManager.instance.gladiatorSchool_TrainingSpeedMultiplyer; elliteWarriorSpeed = PerkManager.instance.elliteWarriors_TrainingSpeedMultiplyer; totalTrainingSpeed = 1f * (gladiatorSchool ? gladiatorSchoolSpeed : 1f) * (elliteWarriors ? elliteWarriorSpeed : 1f); } private void Update() { if (godOfDeathActive) { return; } if (hp.KnockedOut) { productionBar.UpdateVisual(0f); } else if (AtLeastOneUnitIsKnockedOut()) { timeTillNextRespawn -= Time.deltaTime; productionBar.UpdateVisual(1f - timeTillNextRespawn / ResetCooldownTime * totalTrainingSpeed); if (timeTillNextRespawn <= 0f) { timeTillNextRespawn += ResetCooldownTime; timeTillNextRespawn /= totalTrainingSpeed; RespawnAKnockedOutUnit(); } } else { timeTillNextRespawn = ResetCooldownTime; timeTillNextRespawn /= totalTrainingSpeed; productionBar.UpdateVisual(0f); } } private bool AtLeastOneUnitIsKnockedOut() { for (int i = 0; i < units.Count; i++) { Hp hp = units[i]; if (hp.gameObject.activeInHierarchy && hp.KnockedOut) { return true; } } return false; } private void RespawnAKnockedOutUnit() { for (int i = 0; i < units.Count; i++) { Hp hp = units[i]; if (hp.gameObject.activeInHierarchy && hp.KnockedOut) { hp.Revive(); PathfindMovementPlayerunit component = hp.GetComponent(); Vector3 position = taggedObject.colliderForBigOjectsToMeasureDistance.ClosestPoint(component.HopePositionOriginal); hp.transform.position = position; component.SnapToNavmesh(); break; } } } }