using System.Collections.Generic; using UnityEngine; public class Characters : GameplayBase { public static Characters Instance = null; [SerializeField] private Transform player; [SerializeField] private List npcIds; [SerializeField] private NPC tyra; public Location Nlocation; private List npcInstances; private List activeNPCs; public List NpcInstances { get { return npcInstances; } } public List ActiveNPCs { get { return activeNPCs; } } private void Awake() { Instance = this; npcInstances = new List(); activeNPCs = new List(); } private bool EvaluateNpcActivation() { bool canActivateNpc = false; if (currentConversation.ConvType != DataTools.ConversationType.PhoneCall) { if (currentConversation.ConvType != DataTools.ConversationType.TextMessage) { if (currentConversation.ConvType != DataTools.ConversationType.Email) { canActivateNpc = true; } } } return canActivateNpc; } private void SetCharactersPosition() { DestroyNPC(); if (Nlocation.isMap) { TurnOnOffPlayer(false); return; } else { TurnOnOffPlayer(true); SetPlayer(Nlocation.PlayerLocation); } if (currentConversation != null) { if (EvaluateNpcActivation()) { if (currentConversation != null && Nlocation.ID.ToUpper() == currentConversation.Location.ToUpper()) { int npcIndex = 0; foreach (Transform t in Nlocation.NPCLocation) { if (npcIndex < currentConversation.NpcId.Count) { NPCSetup(npcIndex, t); npcIndex++; } } } } } } protected override void OnLocationChanged(Location location) { Nlocation = location; Invoke(isGigActive ? "SetCharactersPositionForGig":"SetCharactersPosition", 0.3f); } protected override void SetConversation(DataTools.Conversation conversation) { bool hasSameNPCIds = false; if (currentConversation != null) { if (EvaluateNpcActivation()) { if (currentConversation.NpcId.Count == conversation.NpcId.Count) { foreach (string npcId in currentConversation.NpcId) { hasSameNPCIds = conversation.NpcId.Find(n => n.ToUpper().Contains(npcId.ToUpper()) || npcId.ToUpper().Contains(n.ToUpper())) != null; if (!hasSameNPCIds) { break; } } } } } base.SetConversation(conversation); if (!hasSameNPCIds && currentConversation.Location.ToUpper() == Nlocation.ID.ToUpper()) { if(EvaluateNpcActivation()) { if (FadeScreen.Instance.active) { SetCharactersPosition(); } else { FadeScreen.Instance.FadeIn(5, () => { SetCharactersPosition(); FadeScreen.Instance.FadeOut(2); }); } } } } private void SetPlayer(Transform playerLoc) { player.transform.position = playerLoc.position; player.transform.localScale = playerLoc.localScale; player.transform.eulerAngles = playerLoc.eulerAngles; } private void NPCSetup(int index, Transform t) { GameObject npc = null; if (currentConversation.NpcId[index] != tyra.name) { string npcId = npcIds.Find(s => s.ToUpper() == currentConversation.NpcId[index].ToUpper()); npc = Instantiate(Resources.Load(npcId) as GameObject); } else { npc = tyra.gameObject; } if (npc != null) { npc.transform.position = t.position; npc.transform.eulerAngles = t.eulerAngles; npc.transform.localScale = t.localScale; npcInstances.Add(npc); activeNPCs.Add(currentConversation.NpcId[index]); npc.gameObject.SetActive(true); Navigation.Instance.SetCurrentNpcs(npc.transform); } } public bool Exist(string npcId) { bool exist = npcIds.Find(n => n.ToUpper().Contains(npcId.ToUpper())) != null; if (!exist) { exist = tyra.Name.Contains(npcId); } return exist; } private void DestroyNPC() { foreach (GameObject npc in npcInstances) { if (npc.name != tyra.name) { Destroy(npc); } else { tyra.gameObject.SetActive(false); } } npcInstances.Clear(); activeNPCs.Clear(); Navigation.Instance.ClearCurrentNpcs(); } private void TurnOnOffPlayer(bool _Active) { player.gameObject.SetActive(_Active); } public Transform GetCurrentNpc(string _nameNPC) { if (EvaluateNpcActivation()) { int npcIndex = 0; foreach (Transform t in Nlocation.NPCLocation) { if (npcIndex < currentConversation.NpcId.Count) { if (currentConversation.NpcId[npcIndex].ToUpper() == _nameNPC.ToUpper()) { return Nlocation.NPCLocation[npcIndex]; } npcIndex++; } } } return null; } #region NPC during gigs private bool isGigActive = false; private DataTools.Gig currentGig = null; public bool IsGigActive { get { return isGigActive; } set { isGigActive = value; } } public void SetGig(DataTools.Gig gig) { currentGig = gig; List gigCrew = null; if (currentGig != null && ((gigCrew = CommentsManager.Instance.GetGigCrew(currentGig.Location, currentGig.Id))!=null) && gigCrew.Count>0) { bool alreadyFlag = IsGigNPCsAlreadyInstantiated(gigCrew); bool locationFlag = Nlocation.ID.ToUpper() == currentGig.Location.ToUpper(); if(!alreadyFlag && locationFlag) { FadeScreen.Instance.FadeIn(5, () => { SetCharactersPositionForGig(); FadeScreen.Instance.FadeOut(2); }); } } } public void ResetGig() { currentGig = null; } /// /// Version of SetCharacterPositions() for spwning gig crew /// private void SetCharactersPositionForGig() { DestroyNPC(); if (Nlocation.isMap) { TurnOnOffPlayer(false); return; } else { TurnOnOffPlayer(true); SetPlayer(Nlocation.PlayerLocation); } if (isGigActive) { if(currentGig != null && Nlocation.ID.ToUpper() == currentGig.Location.ToUpper()) { List gigCrew = CommentsManager.Instance.GetGigCrew(currentGig.Location, currentGig.Id); int count = Mathf.Min(Nlocation.NPCLocation.Length, gigCrew.Count); for(int i = 0; i < count; i++) { CreateNPC(gigCrew[i], Nlocation.NPCLocation[i]); } } } } /// /// Get specific NPC if it already present on location /// /// Name or ID of requred NPC /// NPC component of instantiated gameobject public NPC GetInstantiatedNPC(string _npc) { foreach(GameObject go in npcInstances) { NPC npcObject = go.GetComponent(); if(npcObject.Name == _npc) { return npcObject; } } return null; } private NPC CreateNPC(string name, Transform t) { NPC npc = null; GameObject go = null; if(name != tyra.Name) { if(npcIds.Contains(name)) { go = Instantiate(Resources.Load(name) as GameObject); npcInstances.Add(go); npc = go.GetComponent(); } else { Debug.LogWarning("NPC with name " + name + " does not exist"); return null; } } else { npcInstances.Add(tyra.gameObject); go = tyra.gameObject; npc = tyra; } if (go != null) { go.transform.position = t.position; go.transform.eulerAngles = t.eulerAngles; go.transform.localScale = t.localScale; npcInstances.Add(go); activeNPCs.Add(name); go.SetActive(true); Navigation.Instance.SetCurrentNpcs(go.transform); } return npc; } public bool IsGigNPCsAlreadyInstantiated(List _npcs) { bool instantiated = false; foreach (string npc in _npcs) { instantiated = Characters.Instance.ActiveNPCs.IndexOf(npc) != -1; if (!instantiated) { return instantiated; } } return instantiated; } #endregion }