using System.Collections; using UnityEngine; public class BG_NPCRefContainer : NpcRefrenceContainer { [Header("BG NPC Parameters")] [SerializeField] float lifeTime; [SerializeField] float minTargetRange; [SerializeField] float maxTargetRange; [SerializeField] NpcInteract NpcInteract; protected Coroutine despawnCoroutine; protected override void OnGamePauseToggleHandler() { base.OnGamePauseToggleHandler(); if (GameManager.Instance.stateManager.CurrentState == GameState.Paused) { npcMoveBehaviour.navMeshAgent.isStopped = true; // Stop lifetime coroutine if (despawnCoroutine != null) StopCoroutine(despawnCoroutine); // Stop the fade coroutine if (gameObjectFade.fadeCoroutine != null) StopCoroutine(gameObjectFade.fadeCoroutine); } else { npcMoveBehaviour.navMeshAgent.isStopped = false; // Resume lifetime coroutine if (despawnCoroutine != null) despawnCoroutine = StartCoroutine(DespawnAfterTime()); // Resume the fade coroutine if (gameObjectFade.fadeCoroutine != null && gameObjectFade.fadeCoroutineId == 1) gameObjectFade.FadeIn(); else if (gameObjectFade.fadeCoroutine != null && gameObjectFade.fadeCoroutineId == 2) gameObjectFade.FadeOut(); } } public override void OnObjectSpawn() { base.OnObjectSpawn(); gameObject.SetActive(true); canBeDespawned = false; // Reference NpcInteract.mainCamera = GameManager.Instance.refrenceManager.cameraRefrence.GetMainCamera(); // Behaviour npcMoveBehaviour.targetPosition = ((INavMeshReliant)this).GenerateRandomPosFromSelf(transform.position, minTargetRange, maxTargetRange); npcMoveBehaviour.SetTarget(npcMoveBehaviour.targetPosition); // Rotate towards the target position transform.rotation = Quaternion.LookRotation(npcMoveBehaviour.targetPosition - transform.position, Vector3.up); TriggerFadeIn(); } protected override void OnFadeInComplete() { base.OnFadeInComplete(); canBeDespawned = true; despawnCoroutine = StartCoroutine(DespawnAfterTime()); } protected override void OnFadeOutComplete() { canBeDespawned = false; despawnCoroutine = null; gameObject.SetActive(false); } IEnumerator DespawnAfterTime() { yield return new WaitForSeconds(lifeTime); TriggerFadeOut(); } }