LocalGameScript.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 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. public string CurrentPlayerName { get => currentPlayerName; set => currentPlayerName = value; }
  25. public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
  26. public string NumberOfPlayers { get => numberOfPlayersText.text; set => numberOfPlayersText.text = value; }
  27. public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
  28. public string QuestionsNeededToWin { get => neededToWinText.text; set => neededToWinText.text = value; }
  29. public string GameMode { get => gameMode; set => gameMode = value; }
  30. public int GameId { get => gameId; set => gameId = value; }
  31. public string Round { get => roundText.text; set => roundText.text = value; }
  32. public string StartDate { get => dateStartedText.text; set => dateStartedText.text = value; }
  33. public string LastPlayedDate { get => lastPlayedText.text; set => lastPlayedText.text = value; }
  34. public string FinishedDate { get => finishedDateText.text; set => finishedDateText.text = value; }
  35. public List<KeyValuePair<string, int>> Players { get => players; set => players = value; }
  36. private Database db;
  37. private void Start() {
  38. startButton.onClick.AddListener(StartThisGame);
  39. deleteButton.onClick.AddListener(DeleteThisGame);
  40. db = GameObject.Find("Main Panel").GetComponent<Database>();
  41. }
  42. public void AddPlayer(string name, int points) {
  43. if (players == null) {
  44. players = new List<KeyValuePair<string, int>>();
  45. }
  46. KeyValuePair<string, int> p = new KeyValuePair<string, int>(name, points);
  47. Players.Add(p);
  48. GameObject go = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  49. PlayerInfo playerInfo = go.GetComponent<PlayerInfo>();
  50. playerInfo.SetName(name);
  51. playerInfo.SetPoints(points);
  52. playerInfo.transform.SetParent(content);
  53. playerInfo.transform.SetSiblingIndex(0);
  54. }
  55. public void StartThisGame() {
  56. Debug.Log("CLicked button to start game");
  57. PlayerPrefs.SetInt("GameId", GameId);
  58. SceneManager.LoadSceneAsync("narKampen");
  59. }
  60. public void DeleteThisGame() {
  61. db.RemoveGame(gameId);
  62. Destroy(this);
  63. Destroy(this.gameObject);
  64. }
  65. }