Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- public class MainMenu : Singleton<MainMenu>
- {
- #region Properties
- public List<Transform> MenuScreens;
- // 0 = main
- // 1 = lobby
- // 2 = leaderboards
- // 3 = options
- private Transform activeMenu;
- public Transform ActiveMenu
- {
- get
- {
- return activeMenu;
- }
- set
- {
- activeMenu = value;
- }
- }
- private Camera mainCamera;
- public Camera MainCamera
- {
- get
- {
- if (mainCamera == null)
- mainCamera = Camera.main;
- return mainCamera;
- }
- }
- private List<PlayerSlot> allPlayerSlots = new List<PlayerSlot>();
- public List<PlayerSlot> AllPlayerSlots
- {
- get
- {
- return allPlayerSlots;
- }
- }
- private int activePlayerCount = 0;
- public int ActivePlayerCount
- {
- get
- {
- return activePlayerCount;
- }
- set
- {
- activePlayerCount = value;
- }
- }
- private List<Sprite> allSlotSprites = new List<Sprite>();
- public List<Sprite> AllSlotSprites
- {
- get
- {
- return allSlotSprites;
- }
- }
- private Button[] AllButtons;
- private Text[] AllText;
- #endregion
- void Start()
- {
- LoadAllSlotSprites(); // load all sprite resources. this has to be the 1st funcion that is called. others depend on it
- StoreAllPlayerSlots(); // find all 'PlayerSlot' objects in scene and store them
- foreach (PlayerSlot slot in AllPlayerSlots) // awake functionality for all the found PlayerSlots
- {
- slot.InitializeSlot();
- }
- AllButtons = GetAllButtonsInScene();
- AllText = GetAllTextInScene();
- // InitializeAudio();
- StartCoroutine(LoadHighScores("city"));
- }
- private IEnumerator LoadHighScores(params string[] types) {
- AirconsoleHighscoreManager.Instance.GetHighScores("test", types);
- yield return new WaitUntil(() => AirconsoleHighscoreManager.Instance.IsHighScoreReady(0));
- string[] data = AirconsoleHighscoreManager.Instance.GetHighScoreValues(0);
- // TODO: use data in the UI.
- // TODO: Uhh, change airconsole highscore manager to allow for actual high score tables, lol.
- }
- private void InitializeAudio()
- {
- AudioManager audio = AudioManager.Instance;
- GameObject staticHolder = Camera.main.transform.Find("StaticAudioSourceHolder").transform.gameObject;
- audio.PlayAudio(audio.files.bgm, staticHolder.transform.Find("BGM").transform.GetComponent<AudioSource>(), 100, true);
- }
- private void StoreAllPlayerSlots()
- {
- allPlayerSlots.Clear();
- PlayerSlot[] temp_AllSlots = FindObjectsOfType(typeof(PlayerSlot)) as PlayerSlot[];
- foreach (PlayerSlot slot in temp_AllSlots)
- {
- allPlayerSlots.Add(slot);
- }
- allPlayerSlots = AllPlayerSlots.OrderBy(go => go.name).ToList();
- }
- private int GetNextAvailablePlayerSlotIndex()
- {
- if (AllPlayerSlots.Count == 0) return -1;
- for (int i = 0; i < AllPlayerSlots.Count; i++)
- {
- if (AllPlayerSlots[i].IsAvailable)
- return i;
- }
- Debug.Log("Exceeded the player count");
- return -1;
- }
- private void LoadAllSlotSprites()
- {
- allSlotSprites.Clear();
- allSlotSprites = Resources.LoadAll(("MainMenu/SlotSprites"), typeof(Sprite)).Cast<Sprite>().ToList();
- allSlotSprites = allSlotSprites.OrderBy(go => go.name).ToList();
- }
- private Sprite GetSlotSpriteToSet(int index)
- {
- if (index < (AllSlotSprites.Count - 1) && index >= 0)
- return AllSlotSprites[index];
- // return empty slot
- return AllSlotSprites[4];
- }
- private bool HasStarted = false;
- public GameObject StartText;
- private string DisplayTextToSet(int index)
- {
- if (ActivePlayerCount >= 2)
- {
- if (StartText != null) StartText.SetActive(true);
- return "";
- }
- else if (index < (AllSlotSprites.Count - 1) && index >= 0)
- {
- if (StartText != null) StartText.SetActive(false);
- return "Need more players..";
- }
- else if (index < 0)
- {
- if (StartText != null) StartText.SetActive(false);
- return "";
- }
- return "Not connected..";
- }
- public void SetPlayerSlot(PlayerSlot target, int index)
- {
- target.ActivateSlot((ActivePlayerCount + 1), GetSlotSpriteToSet(index), DisplayTextToSet(index));
- if (index < (AllSlotSprites.Count - 1) && index >= 0)
- ActivePlayerCount++;
- for (int i = 0; i < ActivePlayerCount; i++)
- {
- allPlayerSlots[i].TextDisplay.text = DisplayTextToSet(i);
- }
- }
- private void InitializePlayers()
- {
- if (HasStarted) return;
- HasStarted = true;
- PlayerCharacterHandler pch = PlayerCharacterHandler.Instance;
- AudioManager audio = AudioManager.Instance;
- pch.SpawnPlayers();
- for (int i = 0; i < ActivePlayerCount; i++)
- {
- SetPlayerSlot(AllPlayerSlots[i], -1);
- activeMenu = GameObject.Find("Player_" + PlayerCharacterHandler.Instance.GetInstantiatedPlayers.Count).transform;
- allPlayerSlots[i].TextDisplay.enabled = false;
- audio.PlayAudio(audio.files.voices[i].fly, pch.GetInstantiatedPlayers[i].transform.GetComponent<AudioSource>(), 100, false);
- }
- foreach (Text t in AllText)
- {
- if (t.transform.gameObject.GetComponent<Animator>())
- {
- t.transform.gameObject.GetComponent<Animator>().enabled = false;
- Debug.Log("Disable animator on : " + t.transform.name);
- }
- t.transform.gameObject.SetActive(false);
- }
- foreach (Button b in AllButtons)
- {
- b.transform.gameObject.SetActive(false);
- }
- }
- private Text[] GetAllTextInScene()
- {
- return FindObjectsOfType(typeof(Text)) as Text[];
- }
- private Button[] GetAllButtonsInScene()
- {
- return FindObjectsOfType(typeof(Button)) as Button[];
- }
- private void MoveInitializedPlayers()
- {
- PlayerCharacterHandler pch = PlayerCharacterHandler.Instance;
- int count = pch.GetInstantiatedPlayers.Count;
- if (count <= 0) return;
- for (int i = 0; i < count; i++)
- {
- PlayerController pc = pch.GetInstantiatedPlayers[i].GetComponent<PlayerController>();
- pc.X_Axis_Input = 1f;
- }
- }
- // to do:
- // - add 'disconnect' functionality
- // - should remove player at target location in chain
- // - should re-center target players that are on the right side of the removed player
- // - (AIRCONSOLE) should give correct coloring to the players
- #region DebugKeys
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.Alpha1))
- {
- Debug.Log("Menu 1 ");
- ActiveMenu = MenuScreens[0];
- }
- else if(Input.GetKeyDown(KeyCode.Alpha2))
- {
- ActiveMenu = MenuScreens[1];
- }
- else if(Input.GetKeyDown(KeyCode.Alpha3))
- {
- ActiveMenu = MenuScreens[2];
- }
- else if(Input.GetKeyDown(KeyCode.Q) && ActivePlayerCount < 4)
- {
- SetPlayerSlot(AllPlayerSlots[ActivePlayerCount], ActivePlayerCount);
- }
- else if(Input.GetKeyDown(KeyCode.Space) && ActivePlayerCount >= 2)
- {
- InitializePlayers();
- }
- MoveInitializedPlayers();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement