LocalGameScript.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. public class LocalGameScript : MonoBehaviour
  7. {
  8. public Transform content;
  9. public GameObject playerInfoPrefab;
  10. public Button startButton;
  11. public Button deleteButton;
  12. public Text numberOfPlayersText;
  13. public Text neededToWinText;
  14. public Text roundText;
  15. /*public Text dateStartedText;
  16. public Text lastPlayedText;
  17. public Text finishedDateText;
  18. */
  19. private string startDate = "";
  20. private string lastPlayedDate = "";
  21. private string finishedDate = "";
  22. private int answerTimer;
  23. private string gameMode;
  24. private string currentPlayer;
  25. private string currentPlayerName;
  26. private int gameId;
  27. private List<KeyValuePair<string, int>> players;
  28. private LocalizationManager LANG = LocalizationManager.Instance;
  29. public string CurrentPlayerName { get => currentPlayerName; set => currentPlayerName = value; }
  30. public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
  31. public string NumberOfPlayers { get => numberOfPlayersText.text; set => numberOfPlayersText.text = value; }
  32. public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
  33. public string QuestionsNeededToWin { get => neededToWinText.text; set => neededToWinText.text = value; }
  34. public string GameMode { get => gameMode; set => gameMode = value; }
  35. public int GameId { get => gameId; set => gameId = value; }
  36. public string Round { get => roundText.text; set => roundText.text = value; }
  37. public string StartDate { get => startDate; set => startDate = value; }
  38. public string LastPlayedDate { get => lastPlayedDate; set => lastPlayedDate = value; }
  39. public string FinishedDate { get => finishedDate; set => finishedDate = value; }
  40. public List<KeyValuePair<string, int>> Players { get => players; set => players = value; }
  41. private void Start() {
  42. startButton.onClick.AddListener(StartThisGame);
  43. deleteButton.onClick.AddListener(DeleteThisGame);
  44. }
  45. public void AddPlayer(string name, int points) {
  46. if (players == null) {
  47. players = new List<KeyValuePair<string, int>>();
  48. }
  49. KeyValuePair<string, int> p = new KeyValuePair<string, int>(name, points);
  50. Players.Add(p);
  51. GameObject go = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  52. PlayerInfo playerInfo = go.GetComponent<PlayerInfo>();
  53. playerInfo.SetName(name);
  54. playerInfo.SetPoints(points);
  55. playerInfo.transform.SetParent(content);
  56. playerInfo.transform.SetSiblingIndex(0);
  57. }
  58. public void StartThisGame() {
  59. PlayerPrefs.SetInt("GameId", GameId);
  60. PlayerPrefs.SetString("GameMode", "Local");
  61. PlayerPrefs.SetInt("QuestionTimer", AnswerTimer);
  62. SceneManager.LoadSceneAsync("narKampen");
  63. }
  64. public void DeleteThisGame() {
  65. GenericDialog dialog = GenericDialog.Instance();
  66. dialog.SetTitle(LANG.GetText("REMOVE_GAME_TITLE_KEY"));
  67. dialog.SetMessage(LANG.GetText("REMOVE_GAME_MESSAGE_KEY"));
  68. dialog.SetOnAccept(LANG.GetText("YES"),() => {
  69. Database.Instance.RemoveGame(gameId);
  70. Destroy(this);
  71. Destroy(this.gameObject);
  72. dialog.Hide();
  73. });
  74. dialog.SetOnDecline(LANG.GetText("NO"), () => { dialog.Hide(); });
  75. dialog.Show();
  76. }
  77. public void DisableStartButton() {
  78. Destroy(startButton.gameObject);
  79. }
  80. }