PongBehavior.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Assets.StreetLight.Poco;
  2. using Assets.StreetLight.Scripts;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using TMPro;
  12. using Unity.VisualScripting;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. namespace Assets.Pong
  16. {
  17. public class PongBehavior : MonoBehaviour
  18. {
  19. Person playerLeft;
  20. Person playerRight;
  21. State state = State.WaitingForPlayers;
  22. Ball ball;
  23. BlockingCollection<Action> taskQueue;
  24. string logFileName;
  25. enum State
  26. {
  27. WaitingForPlayers,
  28. ReadyForKickoff,
  29. Running
  30. }
  31. PersonManager PersonManager => personManagerLazy.Value;
  32. public int ScoreLeft { get; set; }
  33. public int ScoreRight { get; set; }
  34. Lazy<PersonManager> personManagerLazy;
  35. private void Awake()
  36. {
  37. personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
  38. }
  39. private void Start()
  40. {
  41. PersonManager.PersonAppeared += PersonManager_PersonAppeared;
  42. PersonManager.PersonDisappeared += PersonManager_PersonDisappeared;
  43. var sphere = GameObject.Find("Sphere");
  44. ball = sphere.GetComponent<Ball>();
  45. taskQueue = new BlockingCollection<Action>();
  46. logFileName = Path.Combine(@".\Logs", $"Log{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.txt");
  47. InitializeLogFile();
  48. }
  49. private void InitializeLogFile()
  50. {
  51. var directoryPath = Path.GetDirectoryName(logFileName);
  52. if (!Directory.Exists(directoryPath))
  53. {
  54. Directory.CreateDirectory(directoryPath);
  55. }
  56. if (!File.Exists(logFileName))
  57. {
  58. File.WriteAllText(logFileName, string.Empty);
  59. }
  60. }
  61. private void AppendNewLogEntry(string message)
  62. {
  63. taskQueue.Add(() => File.AppendAllLines(logFileName, new string[] { $"{DateTime.Now}: {message}" }));
  64. Task.Run(() => taskQueue.Take().Invoke());
  65. }
  66. private void PersonManager_PersonDisappeared(object sender, Person e)
  67. {
  68. if (playerLeft.Id == e.Id)
  69. {
  70. playerLeft = null;
  71. if (state == State.Running)
  72. {
  73. EndGame();
  74. }
  75. }
  76. else if (playerRight.Id == e.Id)
  77. {
  78. playerRight = null;
  79. if (state == State.Running)
  80. {
  81. EndGame();
  82. }
  83. }
  84. }
  85. private void EndGame()
  86. {
  87. AppendNewLogEntry($"Game ended with score {ScoreLeft}:{ScoreRight}.");
  88. ScoreLeft = 0;
  89. ScoreRight = 0;
  90. ball.GoalScored -= Ball_GoalScored;
  91. ball.Recenter();
  92. state = State.WaitingForPlayers;
  93. }
  94. private void PersonManager_PersonAppeared(object sender, Person e)
  95. {
  96. if (playerLeft == null && playerRight == null)
  97. {
  98. if (e.GetUnityPosition().x > 0)
  99. {
  100. playerRight = e;
  101. AppendNewLogEntry("Right player joined. Waiting for left player...");
  102. }
  103. else
  104. {
  105. playerLeft = e;
  106. AppendNewLogEntry("Left player joined. Waiting for right player...");
  107. }
  108. }
  109. else if (playerLeft == null)
  110. {
  111. playerLeft = e;
  112. AppendNewLogEntry("All players joined. Let's go!");
  113. StartGame();
  114. }
  115. else if (playerRight == null)
  116. {
  117. playerRight = e;
  118. AppendNewLogEntry("All players joined. Let's go!");
  119. StartGame();
  120. }
  121. }
  122. private void StartGame()
  123. {
  124. state = State.Running;
  125. AppendNewLogEntry($"New game started.");
  126. ball.GoalScored += Ball_GoalScored;
  127. ball.Kickoff();
  128. }
  129. private void Ball_GoalScored(object sender, Side e)
  130. {
  131. var ball = (Ball)sender;
  132. if (e == Side.Left)
  133. {
  134. ScoreRight++;
  135. AppendNewLogEntry($"Right player scored a goal. Current score: {ScoreLeft}:{ScoreRight}");
  136. }
  137. else
  138. {
  139. ScoreLeft++;
  140. AppendNewLogEntry($"Left player scored a goal. Current score: {ScoreLeft}:{ScoreRight}");
  141. }
  142. ball.Recenter();
  143. ball.Kickoff();
  144. }
  145. void Update()
  146. {
  147. if (playerLeft != null)
  148. {
  149. var bumper = GameObject.Find("Bump1");
  150. bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, playerLeft.GetUnityPosition().z);
  151. }
  152. else
  153. {
  154. var bumper = GameObject.Find("Bump1");
  155. bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, 0);
  156. }
  157. if (playerRight != null)
  158. {
  159. var bumper = GameObject.Find("Bump2");
  160. bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, playerRight.GetUnityPosition().z);
  161. }
  162. else
  163. {
  164. var bumper = GameObject.Find("Bump2");
  165. bumper.transform.position = new Vector3(bumper.transform.position.x, bumper.transform.position.y, 0);
  166. }
  167. }
  168. }
  169. }