123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- using System.Collections.Generic;
- using UnityEngine;
- public class Characters : GameplayBase
- {
- public static Characters Instance = null;
- [SerializeField]
- private Transform player;
- [SerializeField]
- private List<string> npcIds;
- [SerializeField]
- private NPC tyra;
- public Location Nlocation;
- private List<GameObject> npcInstances;
- private List<string> activeNPCs;
- public List<GameObject> NpcInstances
- {
- get
- {
- return npcInstances;
- }
- }
- public List<string> ActiveNPCs
- {
- get
- {
- return activeNPCs;
- }
- }
- private void Awake()
- {
- Instance = this;
- npcInstances = new List<GameObject>();
- activeNPCs = new List<string>();
- }
- 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<string> 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;
- }
- /// <summary>
- /// Version of SetCharacterPositions() for spwning gig crew
- /// </summary>
- 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<string> 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]);
- }
- }
- }
- }
- /// <summary>
- /// Get specific NPC if it already present on location
- /// </summary>
- /// <param name="_npc">Name or ID of requred NPC</param>
- /// <returns>NPC component of instantiated gameobject</returns>
- public NPC GetInstantiatedNPC(string _npc)
- {
- foreach(GameObject go in npcInstances)
- {
- NPC npcObject = go.GetComponent<NPC>();
- 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<NPC>();
- }
- 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<string> _npcs)
- {
- bool instantiated = false;
- foreach (string npc in _npcs)
- {
- instantiated = Characters.Instance.ActiveNPCs.IndexOf(npc) != -1;
- if (!instantiated)
- {
- return instantiated;
- }
- }
- return instantiated;
- }
- #endregion
- }
|