LocalGameScript.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. private string startDate = "";
  16. private string lastPlayedDate = "";
  17. private string finishedDate = "";
  18. private int answerTimer;
  19. private string gameMode;
  20. private string currentPlayer;
  21. private string currentPlayerName;
  22. private int gameId;
  23. private List<KeyValuePair<string, int>> players;
  24. private LocalizationManager LANG = LocalizationManager.Instance;
  25. public string CurrentPlayerName { get => currentPlayerName; set => currentPlayerName = value; }
  26. public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
  27. public string NumberOfPlayers { get => numberOfPlayersText.text; set => numberOfPlayersText.text = value; }
  28. public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
  29. public string QuestionsNeededToWin { get => neededToWinText.text; set => neededToWinText.text = value; }
  30. public string GameMode { get => gameMode; set => gameMode = value; }
  31. public int GameId { get => gameId; set => gameId = value; }
  32. public string Round { get => roundText.text; set => roundText.text = value; }
  33. public string StartDate { get => startDate; set => startDate = value; }
  34. public string LastPlayedDate { get => lastPlayedDate; set => lastPlayedDate = value; }
  35. public string FinishedDate { get => finishedDate; set => finishedDate = value; }
  36. public List<KeyValuePair<string, int>> Players { get => players; set => players = value; }
  37. private void Start() {
  38. startButton.onClick.AddListener(StartThisGame);
  39. deleteButton.onClick.AddListener(DeleteThisGame);
  40. }
  41. public void AddPlayer(string name, int points) {
  42. if (players == null) {
  43. players = new List<KeyValuePair<string, int>>();
  44. }
  45. KeyValuePair<string, int> p = new KeyValuePair<string, int>(name, points);
  46. Players.Add(p);
  47. GameObject go = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  48. PlayerInfo playerInfo = go.GetComponent<PlayerInfo>();
  49. playerInfo.SetName(name);
  50. playerInfo.SetPoints(points);
  51. playerInfo.transform.SetParent(content);
  52. playerInfo.transform.SetSiblingIndex(0);
  53. }
  54. public void StartThisGame() {
  55. PlayerPrefs.SetInt(Constants.GAME_ID, GameId);
  56. PlayerPrefs.SetString(Constants.GAME_MODE, Constants.LOCAL);
  57. PlayerPrefs.SetInt(Constants.GAME_QUESTION_TIMER, AnswerTimer);
  58. SceneManager.LoadSceneAsync(Constants.SCENE_NAR_KAMPEN);
  59. }
  60. public void DeleteThisGame() {
  61. GenericDialog dialog = GenericDialog.Instance();
  62. dialog.SetTitle(LANG.GetText("REMOVE_GAME_TITLE_KEY"));
  63. dialog.SetMessage(LANG.GetText("REMOVE_GAME_MESSAGE_KEY"));
  64. dialog.SetOnAccept(LANG.GetText("YES"),() => {
  65. Database.Instance.RemoveGame(gameId);
  66. Destroy(this);
  67. Destroy(this.gameObject);
  68. dialog.Hide();
  69. });
  70. dialog.SetOnDecline(LANG.GetText("NO"), () => { dialog.Hide(); });
  71. dialog.Show();
  72. }
  73. public void DestroyStartButton() {
  74. Destroy(startButton.gameObject);
  75. }
  76. }