Characters.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class Characters : GameplayBase
  4. {
  5. public static Characters Instance = null;
  6. [SerializeField]
  7. private Transform player;
  8. [SerializeField]
  9. private List<string> npcIds;
  10. [SerializeField]
  11. private NPC tyra;
  12. public Location Nlocation;
  13. private List<GameObject> npcInstances;
  14. private List<string> activeNPCs;
  15. public List<GameObject> NpcInstances
  16. {
  17. get
  18. {
  19. return npcInstances;
  20. }
  21. }
  22. public List<string> ActiveNPCs
  23. {
  24. get
  25. {
  26. return activeNPCs;
  27. }
  28. }
  29. private void Awake()
  30. {
  31. Instance = this;
  32. npcInstances = new List<GameObject>();
  33. activeNPCs = new List<string>();
  34. }
  35. private bool EvaluateNpcActivation()
  36. {
  37. bool canActivateNpc = false;
  38. if (currentConversation.ConvType != DataTools.ConversationType.PhoneCall)
  39. {
  40. if (currentConversation.ConvType != DataTools.ConversationType.TextMessage)
  41. {
  42. if (currentConversation.ConvType != DataTools.ConversationType.Email)
  43. {
  44. canActivateNpc = true;
  45. }
  46. }
  47. }
  48. return canActivateNpc;
  49. }
  50. private void SetCharactersPosition()
  51. {
  52. DestroyNPC();
  53. if (Nlocation.isMap)
  54. {
  55. TurnOnOffPlayer(false);
  56. return;
  57. }
  58. else
  59. {
  60. TurnOnOffPlayer(true);
  61. SetPlayer(Nlocation.PlayerLocation);
  62. }
  63. if (currentConversation != null)
  64. {
  65. if (EvaluateNpcActivation())
  66. {
  67. if (currentConversation != null && Nlocation.ID.ToUpper() == currentConversation.Location.ToUpper())
  68. {
  69. int npcIndex = 0;
  70. foreach (Transform t in Nlocation.NPCLocation)
  71. {
  72. if (npcIndex < currentConversation.NpcId.Count)
  73. {
  74. NPCSetup(npcIndex, t);
  75. npcIndex++;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. protected override void OnLocationChanged(Location location)
  83. {
  84. Nlocation = location;
  85. Invoke(isGigActive ? "SetCharactersPositionForGig":"SetCharactersPosition", 0.3f);
  86. }
  87. protected override void SetConversation(DataTools.Conversation conversation)
  88. {
  89. bool hasSameNPCIds = false;
  90. if (currentConversation != null)
  91. {
  92. if (EvaluateNpcActivation())
  93. {
  94. if (currentConversation.NpcId.Count == conversation.NpcId.Count)
  95. {
  96. foreach (string npcId in currentConversation.NpcId)
  97. {
  98. hasSameNPCIds = conversation.NpcId.Find(n => n.ToUpper().Contains(npcId.ToUpper()) || npcId.ToUpper().Contains(n.ToUpper())) != null;
  99. if (!hasSameNPCIds)
  100. {
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. base.SetConversation(conversation);
  108. if (!hasSameNPCIds && currentConversation.Location.ToUpper() == Nlocation.ID.ToUpper())
  109. {
  110. if(EvaluateNpcActivation())
  111. {
  112. if (FadeScreen.Instance.active)
  113. {
  114. SetCharactersPosition();
  115. }
  116. else
  117. {
  118. FadeScreen.Instance.FadeIn(5, () =>
  119. {
  120. SetCharactersPosition();
  121. FadeScreen.Instance.FadeOut(2);
  122. });
  123. }
  124. }
  125. }
  126. }
  127. private void SetPlayer(Transform playerLoc)
  128. {
  129. player.transform.position = playerLoc.position;
  130. player.transform.localScale = playerLoc.localScale;
  131. player.transform.eulerAngles = playerLoc.eulerAngles;
  132. }
  133. private void NPCSetup(int index, Transform t)
  134. {
  135. GameObject npc = null;
  136. if (currentConversation.NpcId[index] != tyra.name)
  137. {
  138. string npcId = npcIds.Find(s => s.ToUpper() == currentConversation.NpcId[index].ToUpper());
  139. npc = Instantiate(Resources.Load(npcId) as GameObject);
  140. }
  141. else
  142. {
  143. npc = tyra.gameObject;
  144. }
  145. if (npc != null)
  146. {
  147. npc.transform.position = t.position;
  148. npc.transform.eulerAngles = t.eulerAngles;
  149. npc.transform.localScale = t.localScale;
  150. npcInstances.Add(npc);
  151. activeNPCs.Add(currentConversation.NpcId[index]);
  152. npc.gameObject.SetActive(true);
  153. Navigation.Instance.SetCurrentNpcs(npc.transform);
  154. }
  155. }
  156. public bool Exist(string npcId)
  157. {
  158. bool exist = npcIds.Find(n => n.ToUpper().Contains(npcId.ToUpper())) != null;
  159. if (!exist)
  160. {
  161. exist = tyra.Name.Contains(npcId);
  162. }
  163. return exist;
  164. }
  165. private void DestroyNPC()
  166. {
  167. foreach (GameObject npc in npcInstances)
  168. {
  169. if (npc.name != tyra.name)
  170. {
  171. Destroy(npc);
  172. }
  173. else
  174. {
  175. tyra.gameObject.SetActive(false);
  176. }
  177. }
  178. npcInstances.Clear();
  179. activeNPCs.Clear();
  180. Navigation.Instance.ClearCurrentNpcs();
  181. }
  182. private void TurnOnOffPlayer(bool _Active)
  183. {
  184. player.gameObject.SetActive(_Active);
  185. }
  186. public Transform GetCurrentNpc(string _nameNPC)
  187. {
  188. if (EvaluateNpcActivation())
  189. {
  190. int npcIndex = 0;
  191. foreach (Transform t in Nlocation.NPCLocation)
  192. {
  193. if (npcIndex < currentConversation.NpcId.Count)
  194. {
  195. if (currentConversation.NpcId[npcIndex].ToUpper() == _nameNPC.ToUpper())
  196. {
  197. return Nlocation.NPCLocation[npcIndex];
  198. }
  199. npcIndex++;
  200. }
  201. }
  202. }
  203. return null;
  204. }
  205. #region NPC during gigs
  206. private bool isGigActive = false;
  207. private DataTools.Gig currentGig = null;
  208. public bool IsGigActive
  209. {
  210. get
  211. {
  212. return isGigActive;
  213. }
  214. set
  215. {
  216. isGigActive = value;
  217. }
  218. }
  219. public void SetGig(DataTools.Gig gig)
  220. {
  221. currentGig = gig;
  222. List<string> gigCrew = null;
  223. if (currentGig != null && ((gigCrew = CommentsManager.Instance.GetGigCrew(currentGig.Location, currentGig.Id))!=null) && gigCrew.Count>0)
  224. {
  225. bool alreadyFlag = IsGigNPCsAlreadyInstantiated(gigCrew);
  226. bool locationFlag = Nlocation.ID.ToUpper() == currentGig.Location.ToUpper();
  227. if(!alreadyFlag && locationFlag)
  228. {
  229. FadeScreen.Instance.FadeIn(5, () =>
  230. {
  231. SetCharactersPositionForGig();
  232. FadeScreen.Instance.FadeOut(2);
  233. });
  234. }
  235. }
  236. }
  237. public void ResetGig()
  238. {
  239. currentGig = null;
  240. }
  241. /// <summary>
  242. /// Version of SetCharacterPositions() for spwning gig crew
  243. /// </summary>
  244. private void SetCharactersPositionForGig()
  245. {
  246. DestroyNPC();
  247. if (Nlocation.isMap)
  248. {
  249. TurnOnOffPlayer(false);
  250. return;
  251. }
  252. else
  253. {
  254. TurnOnOffPlayer(true);
  255. SetPlayer(Nlocation.PlayerLocation);
  256. }
  257. if (isGigActive)
  258. {
  259. if(currentGig != null && Nlocation.ID.ToUpper() == currentGig.Location.ToUpper())
  260. {
  261. List<string> gigCrew = CommentsManager.Instance.GetGigCrew(currentGig.Location, currentGig.Id);
  262. int count = Mathf.Min(Nlocation.NPCLocation.Length, gigCrew.Count);
  263. for(int i = 0; i < count; i++)
  264. {
  265. CreateNPC(gigCrew[i], Nlocation.NPCLocation[i]);
  266. }
  267. }
  268. }
  269. }
  270. /// <summary>
  271. /// Get specific NPC if it already present on location
  272. /// </summary>
  273. /// <param name="_npc">Name or ID of requred NPC</param>
  274. /// <returns>NPC component of instantiated gameobject</returns>
  275. public NPC GetInstantiatedNPC(string _npc)
  276. {
  277. foreach(GameObject go in npcInstances)
  278. {
  279. NPC npcObject = go.GetComponent<NPC>();
  280. if(npcObject.Name == _npc)
  281. {
  282. return npcObject;
  283. }
  284. }
  285. return null;
  286. }
  287. private NPC CreateNPC(string name, Transform t)
  288. {
  289. NPC npc = null;
  290. GameObject go = null;
  291. if(name != tyra.Name)
  292. {
  293. if(npcIds.Contains(name))
  294. {
  295. go = Instantiate(Resources.Load(name) as GameObject);
  296. npcInstances.Add(go);
  297. npc = go.GetComponent<NPC>();
  298. }
  299. else
  300. {
  301. Debug.LogWarning("NPC with name " + name + " does not exist");
  302. return null;
  303. }
  304. }
  305. else
  306. {
  307. npcInstances.Add(tyra.gameObject);
  308. go = tyra.gameObject;
  309. npc = tyra;
  310. }
  311. if (go != null)
  312. {
  313. go.transform.position = t.position;
  314. go.transform.eulerAngles = t.eulerAngles;
  315. go.transform.localScale = t.localScale;
  316. npcInstances.Add(go);
  317. activeNPCs.Add(name);
  318. go.SetActive(true);
  319. Navigation.Instance.SetCurrentNpcs(go.transform);
  320. }
  321. return npc;
  322. }
  323. public bool IsGigNPCsAlreadyInstantiated(List<string> _npcs)
  324. {
  325. bool instantiated = false;
  326. foreach (string npc in _npcs)
  327. {
  328. instantiated = Characters.Instance.ActiveNPCs.IndexOf(npc) != -1;
  329. if (!instantiated)
  330. {
  331. return instantiated;
  332. }
  333. }
  334. return instantiated;
  335. }
  336. #endregion
  337. }