NewStartLocalGameScript.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. public class NewStartLocalGameScript : MonoBehaviour
  7. {
  8. public Slider secondsToAnswer;
  9. public Slider correctsToWin;
  10. public Slider numberOfPlayer;
  11. [SerializeField] GameObject playersPanel;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. gameObject.GetComponent<Button>().onClick.AddListener(StartLocalGame);
  16. }
  17. void Update()
  18. {
  19. PlayerSetting[] playerSettings = playersPanel.GetComponentsInChildren<PlayerSetting>();
  20. foreach (PlayerSetting ps in playerSettings) {
  21. if (ps.nameInput.text.Equals("")) {
  22. gameObject.GetComponent<Button>().interactable = false;
  23. ps.nameInput.placeholder.GetComponent<Text>().text = LocalizationManager.Instance.GetText("PLAYER_SETTING_NAME_PLACEHOLDER");
  24. break;
  25. }
  26. gameObject.GetComponent<Button>().interactable = true;
  27. }
  28. }
  29. private void StartLocalGame() {
  30. Database.Instance.SetLocalOrOnline(Constants.LOCAL);
  31. List<string> playerNames = new List<string>();
  32. int correctsToWinInt = (int)correctsToWin.value;
  33. int numberOfPayersInt = (int)numberOfPlayer.value;
  34. int secondsToAnswerInt = (int)secondsToAnswer.value;
  35. int gameId = Database.Instance.SetupNewLocalGame(correctsToWinInt, numberOfPayersInt, secondsToAnswerInt);
  36. bool okToStart = true;
  37. Transform newPlayersPanel = GameObject.Find("NewPlayersPanel").transform;
  38. PlayerSetting[] playerSettings = newPlayersPanel.GetComponentsInChildren<PlayerSetting>();
  39. for (int i = 0; i < playerSettings.Length; i++) {
  40. string playerName = playerSettings[i].GetComponent<PlayerSetting>().nameInput.text;
  41. playerNames.Add(playerName);
  42. }
  43. Database.Instance.LinkPlayersToLocalGame(playerNames, gameId);
  44. PlayerPrefs.SetString(Constants.GAME_MODE, Constants.LOCAL);
  45. PlayerPrefs.SetInt(Constants.GAME_ID, gameId);
  46. PlayerPrefs.SetInt(Constants.GAME_QUESTION_TIMER, secondsToAnswerInt);
  47. PlayerPrefs.SetInt(Constants.GAME_TO_WIN, correctsToWinInt);
  48. if (okToStart) {
  49. SceneManager.LoadSceneAsync(Constants.SCENE_NAR_KAMPEN);
  50. }
  51. }
  52. }