NewStartLocalGameScript.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 = "DU MÅSTE HA ETT NAMN"; //TODO Lang variabel
  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. Transform newPlayersPanel = GameObject.Find("NewPlayersPanel").transform;
  37. PlayerSetting[] playerSettings = newPlayersPanel.GetComponentsInChildren<PlayerSetting>();
  38. for (int i = 0; i < playerSettings.Length; i++) {
  39. string playerName = playerSettings[i].GetComponent<PlayerSetting>().nameInput.text;
  40. playerNames.Add(playerName);
  41. }
  42. Database.Instance.LinkPlayersToLocalGame(playerNames, gameId);
  43. PlayerPrefs.SetString(Constants.GAME_MODE, Constants.LOCAL);
  44. PlayerPrefs.SetInt(Constants.GAME_ID, gameId);
  45. PlayerPrefs.SetInt(Constants.GAME_QUESTION_TIMER, secondsToAnswerInt);
  46. PlayerPrefs.SetInt(Constants.GAME_TO_WIN, correctsToWinInt);
  47. SceneManager.LoadSceneAsync(Constants.SCENE_NAR_KAMPEN);
  48. }
  49. }