GameFlowExample.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// Example integration of the faceoff system with your main game loop
  5. /// This shows how to use all the components together
  6. /// </summary>
  7. public class GameFlowExample : MonoBehaviour
  8. {
  9. [Header("References")]
  10. [SerializeField] private FaceoffManager faceoffManager;
  11. [SerializeField] private FaceoffPositioning faceoffPositioning;
  12. [SerializeField] private PuckController puck;
  13. [Header("Game State")]
  14. [SerializeField] private int homeScore = 0;
  15. [SerializeField] private int awayScore = 0;
  16. [SerializeField] private float periodLength = 1200f; // 20 minutes in seconds
  17. private float periodTimer = 0f;
  18. private int currentPeriod = 1;
  19. private bool gameActive = false;
  20. void Start()
  21. {
  22. // Find components if not assigned
  23. if (faceoffManager == null)
  24. faceoffManager = FindObjectOfType<FaceoffManager>();
  25. if (faceoffPositioning == null)
  26. faceoffPositioning = FindObjectOfType<FaceoffPositioning>();
  27. if (puck == null)
  28. puck = FindObjectOfType<PuckController>();
  29. StartNewGame();
  30. }
  31. void Update()
  32. {
  33. if (gameActive)
  34. {
  35. periodTimer += Time.deltaTime;
  36. if (periodTimer >= periodLength)
  37. {
  38. EndPeriod();
  39. }
  40. CheckForGoal();
  41. }
  42. // Debug controls
  43. if (Input.GetKeyDown(KeyCode.G))
  44. {
  45. SimulateGoal(true); // Home team scores
  46. }
  47. }
  48. public void StartNewGame()
  49. {
  50. Debug.Log("=== NEW GAME STARTING ===");
  51. homeScore = 0;
  52. awayScore = 0;
  53. currentPeriod = 1;
  54. periodTimer = 0f;
  55. StartCoroutine(GameStartSequence());
  56. }
  57. IEnumerator GameStartSequence()
  58. {
  59. // Position players for opening faceoff
  60. faceoffPositioning.PositionPlayersForFaceoff(FaceoffPositioning.FaceoffLocation.CenterIce);
  61. yield return new WaitForSeconds(2f);
  62. // Start the game with a faceoff
  63. faceoffManager.StartFaceoff();
  64. yield return new WaitForSeconds(3f);
  65. gameActive = true;
  66. Debug.Log("Game is now active!");
  67. }
  68. void CheckForGoal()
  69. {
  70. // This is simplified - you'd check if puck crossed goal line
  71. // For example purposes only
  72. if (puck != null)
  73. {
  74. Vector3 puckPos = puck.transform.position;
  75. // Example: Check if puck is past goal line (adjust coordinates for your rink)
  76. if (puckPos.z > 28f) // Away team's goal
  77. {
  78. SimulateGoal(true); // Home scores
  79. }
  80. else if (puckPos.z < -28f) // Home team's goal
  81. {
  82. SimulateGoal(false); // Away scores
  83. }
  84. }
  85. }
  86. void SimulateGoal(bool homeTeamScored)
  87. {
  88. if (!gameActive) return;
  89. gameActive = false; // Pause game during celebration
  90. if (homeTeamScored)
  91. {
  92. homeScore++;
  93. Debug.Log($"🚨 HOME TEAM SCORES! Score: {homeScore} - {awayScore}");
  94. }
  95. else
  96. {
  97. awayScore++;
  98. Debug.Log($"🚨 AWAY TEAM SCORES! Score: {homeScore} - {awayScore}");
  99. }
  100. StartCoroutine(GoalSequence());
  101. }
  102. IEnumerator GoalSequence()
  103. {
  104. // Goal celebration
  105. Debug.Log("Goal scored! Celebrating...");
  106. yield return new WaitForSeconds(3f);
  107. // Return to center ice
  108. Debug.Log("Returning to center ice...");
  109. faceoffPositioning.PositionPlayersForFaceoff(FaceoffPositioning.FaceoffLocation.CenterIce);
  110. yield return new WaitForSeconds(2f);
  111. // Faceoff after goal
  112. faceoffManager.StartFaceoff();
  113. yield return new WaitForSeconds(3f);
  114. // Resume game
  115. gameActive = true;
  116. Debug.Log("Game resumed!");
  117. }
  118. void EndPeriod()
  119. {
  120. gameActive = false;
  121. currentPeriod++;
  122. Debug.Log($"=== END OF PERIOD {currentPeriod - 1} ===");
  123. Debug.Log($"Score: {homeScore} - {awayScore}");
  124. if (currentPeriod <= 3)
  125. {
  126. StartCoroutine(IntermissionSequence());
  127. }
  128. else
  129. {
  130. EndGame();
  131. }
  132. }
  133. IEnumerator IntermissionSequence()
  134. {
  135. Debug.Log("Intermission...");
  136. yield return new WaitForSeconds(5f);
  137. periodTimer = 0f;
  138. StartCoroutine(GameStartSequence()); // Start next period
  139. }
  140. void EndGame()
  141. {
  142. Debug.Log("=== GAME OVER ===");
  143. Debug.Log($"Final Score: {homeScore} - {awayScore}");
  144. if (homeScore > awayScore)
  145. {
  146. Debug.Log("HOME TEAM WINS! 🏆");
  147. }
  148. else if (awayScore > homeScore)
  149. {
  150. Debug.Log("AWAY TEAM WINS! 🏆");
  151. }
  152. else
  153. {
  154. Debug.Log("TIE GAME - Going to Overtime!");
  155. // You could implement overtime here
  156. }
  157. }
  158. // Example: AI uses puck information to make decisions
  159. public void ExampleAIDecisionMaking(PlayerController player)
  160. {
  161. if (player == null) return;
  162. // Check puck state
  163. if (puck.IsLoose)
  164. {
  165. // Puck is free - go get it!
  166. Vector3 toPuck = puck.transform.position - player.transform.position;
  167. // player.MoveTo(puck.transform.position);
  168. Debug.Log($"{player.stats.playerName}: Chasing loose puck!");
  169. }
  170. else if (player.HasPuck())
  171. {
  172. // I have the puck - decide what to do
  173. PlayerController openTeammate = FindOpenTeammate(player);
  174. if (openTeammate != null && IsGoodPassOpportunity(player, openTeammate))
  175. {
  176. player.Pass(openTeammate);
  177. Debug.Log($"{player.stats.playerName}: Passing to {openTeammate.stats.playerName}");
  178. }
  179. else if (IsGoodShootingPosition(player))
  180. {
  181. Vector3 goalDirection = GetGoalDirection(player);
  182. player.Shoot(goalDirection, 1.0f);
  183. Debug.Log($"{player.stats.playerName}: Taking a shot!");
  184. }
  185. else
  186. {
  187. // Skate with puck
  188. Debug.Log($"{player.stats.playerName}: Carrying puck...");
  189. }
  190. }
  191. else if (puck.CurrentCarrier != null)
  192. {
  193. // Someone else has it
  194. if (IsOpponent(player, puck.CurrentCarrier))
  195. {
  196. // Opponent has puck - apply pressure
  197. float distance = Vector3.Distance(player.transform.position, puck.CurrentCarrier.transform.position);
  198. if (distance < 2f)
  199. {
  200. player.Check(puck.CurrentCarrier);
  201. Debug.Log($"{player.stats.playerName}: Checking {puck.CurrentCarrier.stats.playerName}!");
  202. }
  203. else
  204. {
  205. // Chase the carrier
  206. Debug.Log($"{player.stats.playerName}: Pressuring opponent!");
  207. }
  208. }
  209. else
  210. {
  211. // Teammate has puck - position for support
  212. Debug.Log($"{player.stats.playerName}: Supporting teammate!");
  213. }
  214. }
  215. }
  216. // Helper methods (implement based on your game)
  217. private PlayerController FindOpenTeammate(PlayerController player)
  218. {
  219. // Find best open teammate for passing
  220. // This is simplified - you'd check actual positioning
  221. PlayerController[] allPlayers = FindObjectsOfType<PlayerController>();
  222. foreach (var candidate in allPlayers)
  223. {
  224. if (candidate != player && !IsOpponent(player, candidate))
  225. {
  226. // Check if they're open (not too close to opponents, etc.)
  227. return candidate;
  228. }
  229. }
  230. return null;
  231. }
  232. private bool IsGoodPassOpportunity(PlayerController passer, PlayerController receiver)
  233. {
  234. // Check if pass lane is clear
  235. float distance = Vector3.Distance(passer.transform.position, receiver.transform.position);
  236. return distance > 3f && distance < 20f;
  237. }
  238. private bool IsGoodShootingPosition(PlayerController player)
  239. {
  240. // Check if player is in good position to shoot
  241. Vector3 goalDirection = GetGoalDirection(player);
  242. float distanceToGoal = goalDirection.magnitude;
  243. return distanceToGoal < 15f; // Within shooting range
  244. }
  245. private Vector3 GetGoalDirection(PlayerController player)
  246. {
  247. // Determine which goal to shoot at based on team
  248. bool isHomeTeam = player.CompareTag("HomeTeam");
  249. Vector3 goalPosition = isHomeTeam ? new Vector3(0, 0, 30f) : new Vector3(0, 0, -30f);
  250. return goalPosition - player.transform.position;
  251. }
  252. private bool IsOpponent(PlayerController player1, PlayerController player2)
  253. {
  254. // Check if two players are on different teams
  255. return player1.CompareTag("HomeTeam") != player2.CompareTag("HomeTeam");
  256. }
  257. // GUI for testing
  258. void OnGUI()
  259. {
  260. GUILayout.BeginArea(new Rect(Screen.width - 310, 10, 300, 200));
  261. GUILayout.BeginVertical("box");
  262. GUILayout.Label($"<b>GAME STATE</b>", new GUIStyle(GUI.skin.label) { richText = true, fontSize = 16 });
  263. GUILayout.Label($"Period: {currentPeriod}");
  264. GUILayout.Label($"Time: {Mathf.FloorToInt(periodTimer / 60):00}:{Mathf.FloorToInt(periodTimer % 60):00}");
  265. GUILayout.Label($"Score: {homeScore} - {awayScore}");
  266. GUILayout.Label($"Game Active: {gameActive}");
  267. GUILayout.Space(10);
  268. if (GUILayout.Button("Simulate Home Goal (G)"))
  269. {
  270. SimulateGoal(true);
  271. }
  272. if (GUILayout.Button("Simulate Away Goal"))
  273. {
  274. SimulateGoal(false);
  275. }
  276. if (GUILayout.Button("Reset Game"))
  277. {
  278. StartNewGame();
  279. }
  280. GUILayout.EndVertical();
  281. GUILayout.EndArea();
  282. }
  283. }