Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. public class MainMenu : Singleton<MainMenu>
  8. {
  9. #region Properties
  10. public List<Transform> MenuScreens;
  11. // 0 = main
  12. // 1 = lobby
  13. // 2 = leaderboards
  14. // 3 = options
  15.  
  16. private Transform activeMenu;
  17. public Transform ActiveMenu
  18. {
  19. get
  20. {
  21. return activeMenu;
  22. }
  23. set
  24. {
  25. activeMenu = value;
  26. }
  27. }
  28.  
  29. private Camera mainCamera;
  30. public Camera MainCamera
  31. {
  32. get
  33. {
  34. if (mainCamera == null)
  35. mainCamera = Camera.main;
  36.  
  37. return mainCamera;
  38. }
  39. }
  40.  
  41. private List<PlayerSlot> allPlayerSlots = new List<PlayerSlot>();
  42. public List<PlayerSlot> AllPlayerSlots
  43. {
  44. get
  45. {
  46. return allPlayerSlots;
  47. }
  48. }
  49.  
  50. private int activePlayerCount = 0;
  51. public int ActivePlayerCount
  52. {
  53. get
  54. {
  55. return activePlayerCount;
  56. }
  57. set
  58. {
  59. activePlayerCount = value;
  60. }
  61. }
  62.  
  63.  
  64. private List<Sprite> allSlotSprites = new List<Sprite>();
  65. public List<Sprite> AllSlotSprites
  66. {
  67. get
  68. {
  69. return allSlotSprites;
  70. }
  71. }
  72. private Button[] AllButtons;
  73. private Text[] AllText;
  74. #endregion
  75.  
  76. void Start()
  77. {
  78. LoadAllSlotSprites(); // load all sprite resources. this has to be the 1st funcion that is called. others depend on it
  79. StoreAllPlayerSlots(); // find all 'PlayerSlot' objects in scene and store them
  80. foreach (PlayerSlot slot in AllPlayerSlots) // awake functionality for all the found PlayerSlots
  81. {
  82. slot.InitializeSlot();
  83. }
  84.  
  85. AllButtons = GetAllButtonsInScene();
  86. AllText = GetAllTextInScene();
  87. // InitializeAudio();
  88.  
  89. StartCoroutine(LoadHighScores("city"));
  90. }
  91.  
  92. private IEnumerator LoadHighScores(params string[] types) {
  93. AirconsoleHighscoreManager.Instance.GetHighScores("test", types);
  94. yield return new WaitUntil(() => AirconsoleHighscoreManager.Instance.IsHighScoreReady(0));
  95.  
  96. string[] data = AirconsoleHighscoreManager.Instance.GetHighScoreValues(0);
  97.  
  98. // TODO: use data in the UI.
  99. // TODO: Uhh, change airconsole highscore manager to allow for actual high score tables, lol.
  100. }
  101.  
  102. private void InitializeAudio()
  103. {
  104. AudioManager audio = AudioManager.Instance;
  105. GameObject staticHolder = Camera.main.transform.Find("StaticAudioSourceHolder").transform.gameObject;
  106.  
  107. audio.PlayAudio(audio.files.bgm, staticHolder.transform.Find("BGM").transform.GetComponent<AudioSource>(), 100, true);
  108. }
  109.  
  110. private void StoreAllPlayerSlots()
  111. {
  112. allPlayerSlots.Clear();
  113. PlayerSlot[] temp_AllSlots = FindObjectsOfType(typeof(PlayerSlot)) as PlayerSlot[];
  114. foreach (PlayerSlot slot in temp_AllSlots)
  115. {
  116. allPlayerSlots.Add(slot);
  117. }
  118.  
  119. allPlayerSlots = AllPlayerSlots.OrderBy(go => go.name).ToList();
  120. }
  121.  
  122. private int GetNextAvailablePlayerSlotIndex()
  123. {
  124. if (AllPlayerSlots.Count == 0) return -1;
  125.  
  126. for (int i = 0; i < AllPlayerSlots.Count; i++)
  127. {
  128. if (AllPlayerSlots[i].IsAvailable)
  129. return i;
  130. }
  131.  
  132. Debug.Log("Exceeded the player count");
  133. return -1;
  134. }
  135.  
  136. private void LoadAllSlotSprites()
  137. {
  138. allSlotSprites.Clear();
  139. allSlotSprites = Resources.LoadAll(("MainMenu/SlotSprites"), typeof(Sprite)).Cast<Sprite>().ToList();
  140. allSlotSprites = allSlotSprites.OrderBy(go => go.name).ToList();
  141. }
  142.  
  143. private Sprite GetSlotSpriteToSet(int index)
  144. {
  145. if (index < (AllSlotSprites.Count - 1) && index >= 0)
  146. return AllSlotSprites[index];
  147.  
  148. // return empty slot
  149. return AllSlotSprites[4];
  150. }
  151.  
  152. private bool HasStarted = false;
  153.  
  154. public GameObject StartText;
  155.  
  156. private string DisplayTextToSet(int index)
  157. {
  158. if (ActivePlayerCount >= 2)
  159. {
  160. if (StartText != null) StartText.SetActive(true);
  161. return "";
  162. }
  163. else if (index < (AllSlotSprites.Count - 1) && index >= 0)
  164. {
  165. if (StartText != null) StartText.SetActive(false);
  166. return "Need more players..";
  167. }
  168. else if (index < 0)
  169. {
  170. if (StartText != null) StartText.SetActive(false);
  171. return "";
  172. }
  173.  
  174. return "Not connected..";
  175. }
  176.  
  177. public void SetPlayerSlot(PlayerSlot target, int index)
  178. {
  179. target.ActivateSlot((ActivePlayerCount + 1), GetSlotSpriteToSet(index), DisplayTextToSet(index));
  180. if (index < (AllSlotSprites.Count - 1) && index >= 0)
  181. ActivePlayerCount++;
  182.  
  183. for (int i = 0; i < ActivePlayerCount; i++)
  184. {
  185. allPlayerSlots[i].TextDisplay.text = DisplayTextToSet(i);
  186. }
  187. }
  188.  
  189.  
  190. private void InitializePlayers()
  191. {
  192. if (HasStarted) return;
  193.  
  194. HasStarted = true;
  195. PlayerCharacterHandler pch = PlayerCharacterHandler.Instance;
  196. AudioManager audio = AudioManager.Instance;
  197. pch.SpawnPlayers();
  198.  
  199. for (int i = 0; i < ActivePlayerCount; i++)
  200. {
  201. SetPlayerSlot(AllPlayerSlots[i], -1);
  202. activeMenu = GameObject.Find("Player_" + PlayerCharacterHandler.Instance.GetInstantiatedPlayers.Count).transform;
  203. allPlayerSlots[i].TextDisplay.enabled = false;
  204.  
  205. audio.PlayAudio(audio.files.voices[i].fly, pch.GetInstantiatedPlayers[i].transform.GetComponent<AudioSource>(), 100, false);
  206. }
  207.  
  208. foreach (Text t in AllText)
  209. {
  210. if (t.transform.gameObject.GetComponent<Animator>())
  211. {
  212. t.transform.gameObject.GetComponent<Animator>().enabled = false;
  213. Debug.Log("Disable animator on : " + t.transform.name);
  214. }
  215.  
  216. t.transform.gameObject.SetActive(false);
  217. }
  218. foreach (Button b in AllButtons)
  219. {
  220. b.transform.gameObject.SetActive(false);
  221. }
  222.  
  223. }
  224.  
  225. private Text[] GetAllTextInScene()
  226. {
  227. return FindObjectsOfType(typeof(Text)) as Text[];
  228. }
  229.  
  230. private Button[] GetAllButtonsInScene()
  231. {
  232. return FindObjectsOfType(typeof(Button)) as Button[];
  233. }
  234.  
  235. private void MoveInitializedPlayers()
  236. {
  237. PlayerCharacterHandler pch = PlayerCharacterHandler.Instance;
  238. int count = pch.GetInstantiatedPlayers.Count;
  239. if (count <= 0) return;
  240.  
  241. for (int i = 0; i < count; i++)
  242. {
  243. PlayerController pc = pch.GetInstantiatedPlayers[i].GetComponent<PlayerController>();
  244. pc.X_Axis_Input = 1f;
  245. }
  246. }
  247.  
  248. // to do:
  249. // - add 'disconnect' functionality
  250. // - should remove player at target location in chain
  251. // - should re-center target players that are on the right side of the removed player
  252. // - (AIRCONSOLE) should give correct coloring to the players
  253.  
  254. #region DebugKeys
  255. void Update()
  256. {
  257. if(Input.GetKeyDown(KeyCode.Alpha1))
  258. {
  259. Debug.Log("Menu 1 ");
  260. ActiveMenu = MenuScreens[0];
  261. }
  262. else if(Input.GetKeyDown(KeyCode.Alpha2))
  263. {
  264. ActiveMenu = MenuScreens[1];
  265. }
  266. else if(Input.GetKeyDown(KeyCode.Alpha3))
  267. {
  268. ActiveMenu = MenuScreens[2];
  269. }
  270. else if(Input.GetKeyDown(KeyCode.Q) && ActivePlayerCount < 4)
  271. {
  272. SetPlayerSlot(AllPlayerSlots[ActivePlayerCount], ActivePlayerCount);
  273. }
  274. else if(Input.GetKeyDown(KeyCode.Space) && ActivePlayerCount >= 2)
  275. {
  276. InitializePlayers();
  277. }
  278.  
  279. MoveInitializedPlayers();
  280. }
  281. #endregion
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement