MainMenu.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. using UnityEngine.SceneManagement;
  8. public class MainMenu : MonoBehaviour {
  9. public Button startNewGameButton;
  10. public Button quitButton;
  11. public Transform activeGamesPanel;
  12. public Transform newGamePanel;
  13. public Slider gameTypeSlider;
  14. public InputField localPlayers;
  15. public GameObject playerSettingPrefab;
  16. public Button startLocalGameButton;
  17. public InputField winningCountInputField;
  18. public InputField timeToAnswerInputField;
  19. public GameObject localGamePrefab;
  20. public GameObject onlineGamePrefab;
  21. private Database db;
  22. private readonly string gamesPostUrl = "nordh.xyz/narKampen/dbFiles/Games.php?";
  23. private void Start() {
  24. // GetGames();
  25. startNewGameButton.onClick.AddListener(StartNewGameAction);
  26. quitButton.onClick.AddListener(() => { Application.Quit(); });
  27. gameTypeSlider.onValueChanged.AddListener(SliderChoice);
  28. localPlayers.onValueChanged.AddListener(LocalPlayersAction);
  29. startLocalGameButton.onClick.AddListener(StartLocalGame);
  30. db = GameObject.Find("Main Panel").GetComponent<Database>();
  31. GetGames();
  32. }
  33. private void GetGames() {
  34. int loggedInUserId = PlayerPrefs.GetInt("UserId");
  35. List<LocalGameScript> localGames = db.GetLocalGames(localGamePrefab);
  36. Transform localGamesList = GameObject.Find("LocalGamesList").transform;
  37. int i = 1;
  38. foreach (LocalGameScript lgs in localGames) {
  39. lgs.transform.SetParent(localGamesList.transform);
  40. lgs.transform.SetSiblingIndex(i++);
  41. }
  42. if (checkInternetConnection()) {
  43. db.GetOnlineGames(loggedInUserId);
  44. } else {
  45. // Display no internet information
  46. Debug.Log("No internet");
  47. }
  48. }
  49. private bool checkInternetConnection() {
  50. bool internetConnectivityActive;
  51. UnityWebRequest www = UnityWebRequest.Get("http://google.com");
  52. www.SendWebRequest();
  53. if (www.isNetworkError || www.isHttpError) {
  54. internetConnectivityActive = false;
  55. } else {
  56. internetConnectivityActive = true;
  57. }
  58. return internetConnectivityActive;
  59. }
  60. private void LocalPlayersAction(string value) {
  61. Transform settingsPanel = GameObject.Find("PlayerSettingsPanel").transform;
  62. PlayerSetting[] players = settingsPanel.GetComponentsInChildren<PlayerSetting>();
  63. int playersCount = players.Length;
  64. if (!Int32.TryParse(value, out int newValue)) {
  65. newValue = 1;
  66. }
  67. if (newValue < 1) {
  68. newValue = 1;
  69. }
  70. if (playersCount < newValue) { // add new PlayerSettings
  71. for (int i = playersCount; i < newValue; i++) {
  72. GameObject go = Instantiate(playerSettingPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  73. PlayerSetting ps = go.GetComponent<PlayerSetting>();
  74. ps.SetPlayerText(i + 1);
  75. ps.SetPlayerName(i + 1);
  76. ps.transform.SetParent(settingsPanel);
  77. ps.transform.SetSiblingIndex(i + 1);
  78. }
  79. } else if (playersCount > newValue) { // remove playersettings from end
  80. for (int i = playersCount; i > newValue; i--) {
  81. PlayerSetting toDestroy = settingsPanel.GetChild(i - 1).GetComponent<PlayerSetting>();
  82. Destroy(toDestroy.gameObject);
  83. }
  84. }
  85. ScrollRect scrollPanel = GameObject.Find("LacalPlayersScrollView").GetComponent<ScrollRect>();
  86. scrollPanel.verticalNormalizedPosition = 0f;
  87. }
  88. private IEnumerator GetGamesFromServer() {
  89. string postUrl = gamesPostUrl + "userId=" + PlayerPrefs.GetInt("UserId");
  90. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  91. yield return www.SendWebRequest();
  92. }
  93. private void StartNewGameAction() {
  94. StartNewGamePanelChange();
  95. }
  96. private void StartNewGamePanelChange() {
  97. RectTransform activeGamesRect = activeGamesPanel.GetComponent<RectTransform>();
  98. RectTransform newGamesRect = newGamePanel.GetComponent<RectTransform>();
  99. startNewGameButton.gameObject.SetActive(false);
  100. Color startButtonColor = startNewGameButton.GetComponent<Image>().color;
  101. float activeGamesPanelheight = activeGamesRect.rect.height;
  102. GameObject.Find("GameType").gameObject.GetComponent<CanvasGroup>().alpha = 1;
  103. activeGamesPanel.gameObject.GetComponent<CanvasGroup>().alpha = 0;
  104. activeGamesPanel.gameObject.GetComponent<CanvasGroup>().blocksRaycasts = false;
  105. CanvasGroup localPanelCanvas = GameObject.Find("LocalGamesPanel").GetComponent<CanvasGroup>();
  106. CanvasGroup onlinePanelCanvas = GameObject.Find("OnlineGamesPanel").GetComponent<CanvasGroup>();
  107. localPanelCanvas.alpha = 0;
  108. localPanelCanvas.blocksRaycasts = false;
  109. onlinePanelCanvas.alpha = 0;
  110. onlinePanelCanvas.blocksRaycasts = false;
  111. }
  112. private void SliderChoice(float arg) {
  113. CanvasGroup localGameSettings = GameObject.Find("LocalGameSettings").gameObject.GetComponent<CanvasGroup>();
  114. CanvasGroup onlineGameSettings = GameObject.Find("OnlineGameSettings").gameObject.GetComponent<CanvasGroup>();
  115. if (arg == 0) { // Local game
  116. localGameSettings.alpha = 1;
  117. localGameSettings.blocksRaycasts = true;
  118. onlineGameSettings.alpha = 0;
  119. onlineGameSettings.blocksRaycasts = false;
  120. LocalPlayersAction("1");
  121. } else if (arg == 2) { // Online game
  122. onlineGameSettings.alpha = 1;
  123. onlineGameSettings.blocksRaycasts = true;
  124. localGameSettings.alpha = 0;
  125. localGameSettings.blocksRaycasts = false;
  126. } else { // No choice, hide all
  127. localGameSettings.alpha = 0;
  128. onlineGameSettings.alpha = 0;
  129. localGameSettings.blocksRaycasts = false;
  130. onlineGameSettings.blocksRaycasts = false;
  131. }
  132. }
  133. private void StartLocalGame() {
  134. db = GameObject.Find("Main Panel").GetComponent<Database>();
  135. db.SetLocalOrOnline("Local");
  136. List<string> playerNames = new List<string>();
  137. if (!Int32.TryParse(winningCountInputField.text, out int winningCount)) {
  138. winningCount = 10;
  139. }
  140. if (!Int32.TryParse(localPlayers.text, out int players)) {
  141. players = 1;
  142. }
  143. if (!Int32.TryParse(timeToAnswerInputField.text, out int questionTimer)) {
  144. questionTimer = 20;
  145. }
  146. int gameId = db.SetupNewLocalGame(winningCount, players, questionTimer);
  147. Transform settingsPanel = GameObject.Find("PlayerSettingsPanel").transform;
  148. PlayerSetting[] playerSettings = settingsPanel.GetComponentsInChildren<PlayerSetting>();
  149. for (int i = 0; i < playerSettings.Length; i++) {
  150. string playerName = playerSettings[i].GetComponent<PlayerSetting>().nameInput.text;
  151. playerNames.Add(playerName);
  152. }
  153. db.LinkPlayersToLocalGame(playerNames, gameId);
  154. PlayerPrefs.SetInt("GameId", gameId);
  155. SceneManager.LoadSceneAsync("narKampen");
  156. }
  157. }