MainMenu.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  106. private void SliderChoice(float arg) {
  107. CanvasGroup localGameSettings = GameObject.Find("LocalGameSettings").gameObject.GetComponent<CanvasGroup>();
  108. CanvasGroup onlineGameSettings = GameObject.Find("OnlineGameSettings").gameObject.GetComponent<CanvasGroup>();
  109. if (arg == 0) { // Local game
  110. localGameSettings.alpha = 1;
  111. localGameSettings.blocksRaycasts = true;
  112. onlineGameSettings.alpha = 0;
  113. onlineGameSettings.blocksRaycasts = false;
  114. LocalPlayersAction("1");
  115. } else if (arg == 2) { // Online game
  116. onlineGameSettings.alpha = 1;
  117. onlineGameSettings.blocksRaycasts = true;
  118. localGameSettings.alpha = 0;
  119. localGameSettings.blocksRaycasts = false;
  120. } else { // No choice, hide all
  121. localGameSettings.alpha = 0;
  122. onlineGameSettings.alpha = 0;
  123. localGameSettings.blocksRaycasts = false;
  124. onlineGameSettings.blocksRaycasts = false;
  125. }
  126. }
  127. private void StartLocalGame() {
  128. db = GameObject.Find("Main Panel").GetComponent<Database>();
  129. db.SetLocalOrOnline("Local");
  130. List<string> playerNames = new List<string>();
  131. if (!Int32.TryParse(winningCountInputField.text, out int winningCount)) {
  132. winningCount = 10;
  133. }
  134. if (!Int32.TryParse(localPlayers.text, out int players)) {
  135. players = 1;
  136. }
  137. if (!Int32.TryParse(timeToAnswerInputField.text, out int questionTimer)) {
  138. questionTimer = 20;
  139. }
  140. int gameId = db.SetupNewLocalGame(winningCount, players, questionTimer);
  141. Transform settingsPanel = GameObject.Find("PlayerSettingsPanel").transform;
  142. PlayerSetting[] playerSettings = settingsPanel.GetComponentsInChildren<PlayerSetting>();
  143. for (int i = 0; i < playerSettings.Length; i++) {
  144. string playerName = playerSettings[i].GetComponent<PlayerSetting>().nameInput.text;
  145. playerNames.Add(playerName);
  146. }
  147. db.LinkPlayersToLocalGame(playerNames, gameId);
  148. PlayerPrefs.SetInt("GameId", gameId);
  149. SceneManager.LoadSceneAsync("narKampen");
  150. }
  151. }