StatsScript.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class StatsScript : MonoBehaviour {
  7. public GameObject statsNames;
  8. public GameObject statsValues;
  9. public GameObject statsLinePrefab;
  10. private Text[] statsTextNames;
  11. private Text[] statsTextValues;
  12. private List<StatsLine> statLines;
  13. GameManagerScript gms;
  14. private void Start() {
  15. statLines = new List<StatsLine>();
  16. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  17. CreateStandardStats();
  18. }
  19. private void CreateStandardStats() {
  20. StatsLine round = CreateStatLine();
  21. round.SetStatName("Round");
  22. round.SetStatValue("1");
  23. round.name = "roundStat";
  24. StatsLine lostQuestions = CreateStatLine();
  25. lostQuestions.SetStatName("Questions lost");
  26. lostQuestions.SetStatValue("0");
  27. lostQuestions.name = "questionsLost";
  28. StatsLine playerTitle = CreateStatLine();
  29. playerTitle.SetStatName("Players");
  30. playerTitle.SetStatValue("");
  31. playerTitle.MakeBold();
  32. statLines.Add(round);
  33. statLines.Add(lostQuestions);
  34. statLines.Add(playerTitle);
  35. }
  36. private StatsLine CreateStatLine() {
  37. GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  38. StatsLine statLine = slp.GetComponent<StatsLine>();
  39. statLine.transform.SetParent(this.transform);
  40. return statLine;
  41. }
  42. public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {
  43. foreach (KeyValuePair<string,int> player in players) {
  44. StatsLine p = CreateStatLine();
  45. p.SetStatName(player.Key);
  46. p.SetStatValue(player.Value.ToString());
  47. statLines.Add(p);
  48. }
  49. }
  50. private void AddTextComponent(GameObject parent, string text, bool alignCenter, int index) {
  51. GameObject playerHeaderGameObject = new GameObject();
  52. playerHeaderGameObject.transform.parent = parent.transform;
  53. Text newTextObject = playerHeaderGameObject.AddComponent<Text>();
  54. newTextObject.text = text;
  55. newTextObject.font = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
  56. newTextObject.color = new Color32(50, 50, 50, 255);
  57. newTextObject.resizeTextForBestFit = true;
  58. newTextObject.resizeTextMinSize = 10;
  59. newTextObject.resizeTextMaxSize = 14;
  60. if (index >= 0) {
  61. newTextObject.name = "Player" + index;
  62. }
  63. if (alignCenter) {
  64. newTextObject.alignment = TextAnchor.MiddleCenter;
  65. }
  66. RectTransform rectTransform = newTextObject.GetComponent<RectTransform>();
  67. rectTransform.sizeDelta = new Vector2(0, 15);
  68. rectTransform.localPosition = new Vector3(0, 0, 0);
  69. rectTransform.localScale = new Vector3(1, 1, 1);
  70. }
  71. public void IncreaseRoundValue() {
  72. foreach (Text text in statsTextValues) {
  73. if (text.name.Equals("Stats Value Round")) {
  74. Int32.TryParse(text.text, out int round);
  75. round++;
  76. text.text = round.ToString();
  77. gms.GetDatabase().SetRoundValue(gms.GameId, round);
  78. break;
  79. }
  80. }
  81. }
  82. public void SetQuestionsInAnswerLine(string playerName, int count) {
  83. for (int i = 0; i < statsTextNames.Length; i++) {
  84. if (statsTextNames[i].text.Equals(playerName)) {
  85. statsTextValues[i].text = count.ToString();
  86. }
  87. }
  88. }
  89. }