GameManager.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class GameManager : MonoBehaviour
  4. {
  5. public static GameManager Instance { get; private set; }
  6. [Header("Level Flow")]
  7. [SerializeField] private GameObject winScreenPrefab;
  8. [SerializeField] private GameObject gameOverScreenPrefab;
  9. [Header("Ball Queue")]
  10. [SerializeField] private GameObject ballPrefab;
  11. [SerializeField] private int initialBallCount = 5;
  12. [SerializeField] private Vector3 ballSpawnOffset = new Vector3(0f, 0.7f, 0f);
  13. [SerializeField] private Transform reserveAnchor;
  14. [SerializeField] private float reserveBallSpacing = 0.5f;
  15. [SerializeField] private Vector2 reserveStartViewport = new Vector2(0.92f, 0.12f);
  16. [Header("Random Effects")]
  17. [SerializeField] private BallEffectDefinition[] randomEffectPool;
  18. private readonly Queue<BallScript> _reserveBalls = new Queue<BallScript>();
  19. private Transform _paddle;
  20. private Camera _gameCamera;
  21. private bool _gameWon;
  22. private bool _gameOver;
  23. private BallScript _currentQueueBall;
  24. private int _remainingBlockCount;
  25. private UpgradeState _upgrades;
  26. void Awake()
  27. {
  28. if (Instance != null && Instance != this)
  29. {
  30. Destroy(gameObject);
  31. return;
  32. }
  33. Instance = this;
  34. Time.timeScale = 1f;
  35. Time.fixedDeltaTime = 0.01f;
  36. _upgrades = new UpgradeState();
  37. }
  38. void Start()
  39. {
  40. ResolveSceneReferences();
  41. _remainingBlockCount = FindObjectsByType<blockScript>(FindObjectsSortMode.None).Length;
  42. InitializeBallQueue();
  43. }
  44. public void OnBallEnteredDeathZone(BallScript ball)
  45. {
  46. if (_gameWon || _gameOver || ball == null)
  47. return;
  48. bool wasCurrentQueueBall = ball == _currentQueueBall;
  49. Destroy(ball.gameObject);
  50. if (HasOtherBallsInPlay(ball))
  51. return;
  52. if (wasCurrentQueueBall)
  53. _currentQueueBall = null;
  54. if (_reserveBalls.Count > 0)
  55. {
  56. MoveNextQueuedBallToPaddle();
  57. return;
  58. }
  59. ShowGameOver();
  60. }
  61. private bool HasOtherBallsInPlay(BallScript excludedBall)
  62. {
  63. BallScript[] balls = FindObjectsByType<BallScript>(FindObjectsSortMode.None);
  64. for (int i = 0; i < balls.Length; i++)
  65. {
  66. BallScript otherBall = balls[i];
  67. if (otherBall == null || otherBall == excludedBall)
  68. continue;
  69. if (otherBall.IsInPlay)
  70. return true;
  71. }
  72. return false;
  73. }
  74. /// <summary>Called by BlockScript when a block is destroyed.</summary>
  75. public void OnBlockDestroyed()
  76. {
  77. if (_gameWon || _gameOver)
  78. return;
  79. _remainingBlockCount = Mathf.Max(0, _remainingBlockCount - 1);
  80. if (_remainingBlockCount == 0)
  81. WinGame();
  82. }
  83. private void InitializeBallQueue()
  84. {
  85. if (ballPrefab == null)
  86. {
  87. Debug.LogWarning("GameManager: ballPrefab is not assigned. Cannot create ball queue.");
  88. return;
  89. }
  90. if (_paddle == null)
  91. {
  92. Debug.LogWarning("GameManager: Paddle not found. Tag the paddle as 'Paddle' or ensure PaddleScript exists in scene.");
  93. return;
  94. }
  95. if (_gameCamera == null)
  96. {
  97. Debug.LogWarning("GameManager: No camera found for reserve-ball placement.");
  98. return;
  99. }
  100. if (initialBallCount <= 0)
  101. {
  102. Debug.LogWarning("GameManager: initialBallCount must be > 0.");
  103. return;
  104. }
  105. Debug.Log($"GameManager: Initializing {initialBallCount} balls. Random effect pool has {(randomEffectPool?.Length ?? 0)} effects.");
  106. for (int i = 0; i < initialBallCount; i++)
  107. {
  108. Vector3 reservePosition;
  109. if (reserveAnchor != null)
  110. {
  111. // Preferred placement: line reserve balls to the right from an explicit scene anchor.
  112. reservePosition = reserveAnchor.position + Vector3.right * (i * reserveBallSpacing);
  113. }
  114. else
  115. {
  116. // Fallback placement if no anchor is assigned.
  117. Vector3 reserveStartWorld = _gameCamera.ViewportToWorldPoint(
  118. new Vector3(reserveStartViewport.x, reserveStartViewport.y, Mathf.Abs(_gameCamera.transform.position.z)));
  119. reservePosition = reserveStartWorld + Vector3.right * (i * reserveBallSpacing);
  120. }
  121. reservePosition.z = 0f;
  122. GameObject ballObject = Instantiate(ballPrefab, reservePosition, Quaternion.identity);
  123. BallScript ball = ballObject.GetComponent<BallScript>();
  124. if (ball == null)
  125. {
  126. Debug.LogWarning("GameManager: Spawned ballPrefab has no BallScript component.");
  127. Destroy(ballObject);
  128. continue;
  129. }
  130. ball.SetQueueManaged(true);
  131. ball.SetPaddleReference(_paddle, ballSpawnOffset);
  132. BallEffectDefinition effect = GetRandomEffect();
  133. Debug.Log($"GameManager: Ball {i} assigned effect: {(effect != null ? effect.EffectType.ToString() : "NULL")}");
  134. ball.SetEffectDefinition(effect);
  135. ball.ParkAtPosition(reservePosition);
  136. _reserveBalls.Enqueue(ball);
  137. }
  138. MoveNextQueuedBallToPaddle();
  139. }
  140. private void ResolveSceneReferences()
  141. {
  142. _paddle = GameObject.FindGameObjectWithTag("Paddle")?.transform;
  143. if (_paddle == null)
  144. {
  145. PaddleScript paddleScript = FindFirstObjectByType<PaddleScript>();
  146. if (paddleScript != null)
  147. _paddle = paddleScript.transform;
  148. }
  149. if (_paddle == null)
  150. {
  151. GameObject paddleByName = GameObject.Find("Paddle");
  152. if (paddleByName != null)
  153. _paddle = paddleByName.transform;
  154. }
  155. _gameCamera = Camera.main;
  156. if (_gameCamera == null)
  157. _gameCamera = FindFirstObjectByType<Camera>();
  158. }
  159. private void MoveNextQueuedBallToPaddle()
  160. {
  161. while (_reserveBalls.Count > 0)
  162. {
  163. BallScript nextBall = _reserveBalls.Dequeue();
  164. if (nextBall == null)
  165. continue;
  166. _currentQueueBall = nextBall;
  167. _currentQueueBall.PrepareOnPaddle();
  168. return;
  169. }
  170. ShowGameOver();
  171. }
  172. private BallEffectDefinition GetRandomEffect()
  173. {
  174. if (randomEffectPool == null || randomEffectPool.Length == 0)
  175. return null;
  176. int idx = Random.Range(0, randomEffectPool.Length);
  177. BallEffectDefinition baseDefinition = randomEffectPool[idx];
  178. // Apply active upgrades to the definition
  179. return UpgradeManager.ApplyUpgrades(baseDefinition, _upgrades);
  180. }
  181. private void WinGame()
  182. {
  183. _gameWon = true;
  184. Time.timeScale = 0f;
  185. if (winScreenPrefab != null)
  186. Instantiate(winScreenPrefab);
  187. else
  188. Debug.Log("YOU WIN! All blocks destroyed!");
  189. }
  190. private void ShowGameOver()
  191. {
  192. if (_gameOver || _gameWon)
  193. return;
  194. _gameOver = true;
  195. Time.timeScale = 0f;
  196. if (gameOverScreenPrefab != null)
  197. Instantiate(gameOverScreenPrefab);
  198. else
  199. Debug.Log("GAME OVER! No balls remaining.");
  200. }
  201. #region Upgrade API
  202. /// <summary>Get the current upgrade state (for debugging or UI display).</summary>
  203. public UpgradeState GetUpgradeState() => _upgrades;
  204. /// <summary>Increase explosive effect radius.</summary>
  205. public void UpgradeExplosiveRadius(float radiusBonus)
  206. {
  207. _upgrades.ExplosiveRadiusBonus += radiusBonus;
  208. Debug.Log($"Upgraded Explosive Radius: +{radiusBonus} (Total: +{_upgrades.ExplosiveRadiusBonus})");
  209. }
  210. /// <summary>Increase explosive damage multiplier.</summary>
  211. public void UpgradeExplosiveDamage(float multiplier)
  212. {
  213. _upgrades.ExplosiveDamageMultiplier *= multiplier;
  214. Debug.Log($"Upgraded Explosive Damage: x{multiplier} (Total: x{_upgrades.ExplosiveDamageMultiplier:F2})");
  215. }
  216. /// <summary>Increase split angle diversity.</summary>
  217. public void UpgradeSplitAngle(float angleBonus)
  218. {
  219. _upgrades.SplitAngleBonus += angleBonus;
  220. Debug.Log($"Upgraded Split Angle: +{angleBonus} (Total: +{_upgrades.SplitAngleBonus})");
  221. }
  222. /// <summary>Increase split child projectile speed.</summary>
  223. public void UpgradeSplitSpeed(float multiplier)
  224. {
  225. _upgrades.SplitSpeedMultiplier *= multiplier;
  226. Debug.Log($"Upgraded Split Speed: x{multiplier} (Total: x{_upgrades.SplitSpeedMultiplier:F2})");
  227. }
  228. /// <summary>Increase split child projectile damage.</summary>
  229. public void UpgradeSplitDamage(float multiplier)
  230. {
  231. _upgrades.SplitDamageMultiplier *= multiplier;
  232. Debug.Log($"Upgraded Split Damage: x{multiplier} (Total: x{_upgrades.SplitDamageMultiplier:F2})");
  233. }
  234. /// <summary>Increase piercing hits capacity.</summary>
  235. public void UpgradePiercingHits(int hitBonus)
  236. {
  237. _upgrades.PiercingHitsBonus += hitBonus;
  238. Debug.Log($"Upgraded Piercing Hits: +{hitBonus} (Total: +{_upgrades.PiercingHitsBonus})");
  239. }
  240. /// <summary>Increase homing turn rate (aggressiveness).</summary>
  241. public void UpgradeHomingTurnRate(float multiplier)
  242. {
  243. _upgrades.HomingTurnRateMultiplier *= multiplier;
  244. Debug.Log($"Upgraded Homing Turn Rate: x{multiplier} (Total: x{_upgrades.HomingTurnRateMultiplier:F2})");
  245. }
  246. /// <summary>Increase teleport distance per jump.</summary>
  247. public void UpgradeTeleportDistance(float multiplier)
  248. {
  249. _upgrades.TeleportDistanceMultiplier *= multiplier;
  250. Debug.Log($"Upgraded Teleport Distance: x{multiplier} (Total: x{_upgrades.TeleportDistanceMultiplier:F2})");
  251. }
  252. /// <summary>Reset all upgrades to their default values.</summary>
  253. public void ResetAllUpgrades()
  254. {
  255. _upgrades.ResetAll();
  256. Debug.Log("All upgrades reset to default values.");
  257. }
  258. #endregion
  259. }