Core.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DG.Tweening;
  6. using UnityEngine.AI;
  7. using UnityEngine.SceneManagement;
  8. [Serializable]
  9. public class PlayerData
  10. {
  11. [SerializeField]
  12. public int Heart;
  13. //[SerializeField]
  14. //public int Level;
  15. [SerializeField]
  16. public int Stage;
  17. }
  18. public class Core : MonoBehaviour
  19. {
  20. public static Core Instance;
  21. public List<GameObject> PlayerMalePrefabs;
  22. public List<GameObject> PlayerFemalePrefabs;
  23. public int CurIndexPlayer;
  24. public Transform ParentMale;
  25. public Transform ParentFemale;
  26. //public Transform SpawnPoint;
  27. public Transform _player;
  28. [SerializeField]
  29. public PlayerData PlayerData;
  30. public InputController Controller;
  31. public Transform PartPlayerInGame;
  32. public bool LevelEnd;
  33. public enum Gender
  34. {
  35. Male,
  36. Female
  37. }
  38. public Gender CurGender;
  39. public enum StateModeGame
  40. {
  41. IsGameMode,
  42. Kiss,
  43. Menu,
  44. StartLevel
  45. }
  46. public StateModeGame IsGameMode;
  47. public RunnersController PlayerGame;
  48. public void Start()
  49. {
  50. Instance = this;
  51. Load();
  52. }
  53. public void GetPlayerForSelecting(int index, Gender gender)
  54. {
  55. CurIndexPlayer = index;
  56. GameObject go;
  57. switch (gender)
  58. {
  59. case Gender.Male:
  60. if (_player != null)
  61. {
  62. _player.DOScale(0, 0.5f).OnComplete(() => {
  63. Destroy(_player.gameObject);
  64. go = Instantiate(PlayerMalePrefabs[index]);
  65. _player = go.transform;
  66. go.transform.SetParent(LevelManager.Instance.CurLevel.SpawnPointPlayer);
  67. go.transform.localPosition = Vector3.zero;
  68. go.transform.localScale = Vector3.zero;
  69. go.transform.localEulerAngles = Vector3.zero;
  70. _player.DOScale(1, 0.5f);
  71. });
  72. }
  73. else
  74. {
  75. go = Instantiate(PlayerMalePrefabs[index]);
  76. _player = go.transform;
  77. go.transform.SetParent(LevelManager.Instance.CurLevel.SpawnPointPlayer);
  78. go.transform.localPosition = Vector3.zero;
  79. go.transform.localScale = Vector3.zero;
  80. _player.DOScale(1, 0.5f);
  81. }
  82. break;
  83. case Gender.Female:
  84. if (_player != null)
  85. {
  86. _player.DOScale(0, 0.5f).OnComplete(() => {
  87. Destroy(_player.gameObject);
  88. go = Instantiate(PlayerFemalePrefabs[index]);
  89. _player = go.transform;
  90. go.transform.SetParent(LevelManager.Instance.CurLevel.SpawnPointPlayer);
  91. go.transform.localPosition = Vector3.zero;
  92. go.transform.localScale = Vector3.zero;
  93. go.transform.localEulerAngles = Vector3.zero;
  94. _player.DOScale(1, 0.5f);
  95. });
  96. }
  97. else
  98. {
  99. go = Instantiate(PlayerFemalePrefabs[index]);
  100. _player = go.transform;
  101. go.transform.SetParent(LevelManager.Instance.CurLevel.SpawnPointPlayer);
  102. go.transform.localPosition = Vector3.zero;
  103. go.transform.localScale = Vector3.zero;
  104. _player.DOScale(1, 0.5f);
  105. }
  106. break;
  107. }
  108. }
  109. public void SetCristal()
  110. {
  111. ///TO DO
  112. }
  113. public void SetMale()
  114. {
  115. ParentMale.gameObject.SetActive(true);
  116. }
  117. public void SetFemale()
  118. {
  119. ParentFemale.gameObject.SetActive(true);
  120. }
  121. public void SetHeart()
  122. {
  123. PlayerData.Heart++;
  124. }
  125. public void SetPlayer()
  126. {
  127. switch(CurGender)
  128. {
  129. case Gender.Female:
  130. Controller.OurPlayer = PlayerFemalePrefabs[CurIndexPlayer].GetComponent<SwipeMove>();
  131. UIManager.Instance.CompleteCameraController.Target = PlayerFemalePrefabs[CurIndexPlayer].transform;
  132. Controller.OurPlayer.enabled = true;
  133. PlayerFemalePrefabs[CurIndexPlayer].GetComponent<RunnersController>().SetPlayer(UIManager.Instance.InputName.text);
  134. PlayerFemalePrefabs[CurIndexPlayer].GetComponent<RunnersController>().IsPlayer = true;
  135. Core.Instance.PlayerGame = PlayerFemalePrefabs[CurIndexPlayer].GetComponent<RunnersController>();
  136. break;
  137. case Gender.Male:
  138. Controller.OurPlayer = PlayerMalePrefabs[CurIndexPlayer].GetComponent<SwipeMove>();
  139. Controller.OurPlayer.enabled = true;
  140. UIManager.Instance.CompleteCameraController.Target = PlayerMalePrefabs[CurIndexPlayer].transform;
  141. PlayerMalePrefabs[CurIndexPlayer].GetComponent<RunnersController>().SetPlayer(UIManager.Instance.InputName.text);
  142. PlayerMalePrefabs[CurIndexPlayer].GetComponent<RunnersController>().IsPlayer = true;
  143. Core.Instance.PlayerGame = PlayerMalePrefabs[CurIndexPlayer].GetComponent<RunnersController>();
  144. break;
  145. }
  146. }
  147. public void ResetLevel()
  148. {
  149. UIManager.Instance.ResetLevelData();
  150. }
  151. public void ResetPlayers()
  152. {
  153. //StartCoroutine(LoadSceneAndSetActive("Loc_2"));
  154. switch (CurGender)
  155. {
  156. case Gender.Male:
  157. for (var i = 0; i < PlayerMalePrefabs.Count; i++)
  158. {
  159. PlayerMalePrefabs[i].SetActive(true);
  160. PlayerMalePrefabs[i].transform.SetParent(ParentMale);
  161. //PlayerMalePrefabs[i].transform.position = PlayerMalePrefabs[i].GetComponent<RunnersController>().StartPos;
  162. PlayerMalePrefabs[i].transform.localEulerAngles = new Vector3(0,180,0);
  163. PlayerMalePrefabs[i].GetComponent<SwipeMove>().enabled = false;
  164. PlayerMalePrefabs[i].GetComponent<NavMeshAgent>().enabled = false;
  165. PlayerMalePrefabs[i].GetComponent<RunnersController>().enabled = true;
  166. PlayerMalePrefabs[i].GetComponent<RunnersController>().Agent.speed = 8;
  167. PlayerMalePrefabs[i].GetComponent<RunnersController>().ResetAnimation();
  168. }
  169. break;
  170. case Gender.Female:
  171. for (var i = 0; i < PlayerFemalePrefabs.Count; i++)
  172. {
  173. PlayerFemalePrefabs[i].SetActive(true);
  174. PlayerFemalePrefabs[i].transform.SetParent(ParentFemale);
  175. //PlayerFemalePrefabs[i].transform.position = PlayerFemalePrefabs[i].GetComponent<RunnersController>().StartPos;
  176. PlayerMalePrefabs[i].transform.localEulerAngles = new Vector3(0, 180, 0);
  177. PlayerFemalePrefabs[i].GetComponent<SwipeMove>().enabled = false;
  178. PlayerFemalePrefabs[i].GetComponent<NavMeshAgent>().enabled = false;
  179. PlayerMalePrefabs[i].GetComponent<RunnersController>().enabled = true;
  180. PlayerFemalePrefabs[i].GetComponent<RunnersController>().Agent.speed = 8;
  181. PlayerFemalePrefabs[i].GetComponent<RunnersController>().ResetAnimation();
  182. }
  183. break;
  184. }
  185. //ResetAnimation();
  186. Core.Instance.SetPlayerNewLevel();
  187. }
  188. public void SetPlayerNewLevel()
  189. {
  190. for (var i = 0; i < PlayerMalePrefabs.Count; i++)
  191. {
  192. PlayerMalePrefabs[i].transform.position = LevelManager.Instance.CurLevel.ListPlayerSpawnPoints[i].position;
  193. PlayerMalePrefabs[i].GetComponent<RunnersController>()._target = LevelManager.Instance.CurLevel.LevelTargets.ListLevelTargets[0];
  194. }
  195. for (var i = 0; i < PlayerFemalePrefabs.Count; i++)
  196. {
  197. PlayerFemalePrefabs[i].transform.position = LevelManager.Instance.CurLevel.ListPlayerSpawnPoints[i].position;
  198. PlayerFemalePrefabs[i].GetComponent<RunnersController>()._target = LevelManager.Instance.CurLevel.LevelTargets.ListLevelTargets[0];
  199. }
  200. }
  201. public void ResetPlayerPos()
  202. {
  203. //LoadSceneAndSetActive("Loc_2");
  204. ResetPlayers();
  205. //switch (CurGender)
  206. //{
  207. // case Gender.Female:
  208. // for (var i = 0; i < PlayerFemalePrefabs.Count; i++)
  209. // {
  210. // PlayerFemalePrefabs[i].GetComponent<RunnersController>().ResetPos();
  211. // PlayerFemalePrefabs[i].SetActive(true);
  212. // }
  213. // break;
  214. // case Gender.Male:
  215. // for (var i = 0; i < PlayerMalePrefabs.Count; i++)
  216. // {
  217. // PlayerMalePrefabs[i].GetComponent<RunnersController>().ResetPos();
  218. // PlayerFemalePrefabs[i].SetActive(true);
  219. // }
  220. // break;
  221. //}
  222. }
  223. private IEnumerator LoadSceneAndSetActive(string sceneName)
  224. {
  225. Debug.Log("LOAD");
  226. var sceneActive = SceneManager.GetActiveScene();
  227. if (sceneActive.name != "Loc_2")
  228. {
  229. SceneManager.UnloadSceneAsync(sceneActive.buildIndex);
  230. var async = SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive);
  231. while (!async.isDone)
  232. {
  233. yield return new WaitForEndOfFrame();
  234. }
  235. Debug.Log("DONE LOAD");
  236. SceneManager.SetActiveScene(SceneManager.GetSceneByName("Loc_2".ToString()));
  237. Destroy(LevelManager.Instance.ListLevels[0].gameObject);
  238. }
  239. yield return new WaitForEndOfFrame();
  240. Debug.Log("No LOAD");
  241. //var async = Application.LoadLevelAdditive("Loc_1");
  242. //Debug.Log(currentLevel.IndexEnvironment.ToString());
  243. }
  244. //public void IncreaseLevelPlayer()
  245. //{
  246. // PlayerData.Level++;
  247. //}
  248. public void IncreaseStagePlayer()
  249. {
  250. PlayerData.Stage++;
  251. Core.Instance.Save();
  252. }
  253. public void Load()
  254. {
  255. PlayerData.Stage = PlayerPrefs.GetInt("Stage");
  256. }
  257. public void Save()
  258. {
  259. PlayerPrefs.SetInt("Stage", PlayerData.Stage);
  260. PlayerPrefs.Save();
  261. }
  262. private void OnApplicationPause(bool pause)
  263. {
  264. //Save();
  265. }
  266. private void OnApplicationQuit()
  267. {
  268. //Save();
  269. }
  270. private void OnApplicationFocus(bool focus)
  271. {
  272. //Debug.LogError("Save");
  273. //Save();
  274. }
  275. }