| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- using System.Collections.Generic;
- using UnityEngine;
- public class GameManager : MonoBehaviour
- {
- public static GameManager Instance { get; private set; }
- [Header("Level Flow")]
- [SerializeField] private GameObject winScreenPrefab;
- [SerializeField] private GameObject gameOverScreenPrefab;
- [Header("Ball Queue")]
- [SerializeField] private GameObject ballPrefab;
- [SerializeField] private int initialBallCount = 5;
- [SerializeField] private Vector3 ballSpawnOffset = new Vector3(0f, 0.7f, 0f);
- [SerializeField] private Transform reserveAnchor;
- [SerializeField] private float reserveBallSpacing = 0.5f;
- [SerializeField] private Vector2 reserveStartViewport = new Vector2(0.92f, 0.12f);
- [Header("Random Effects")]
- [SerializeField] private BallEffectDefinition[] randomEffectPool;
- private readonly Queue<BallScript> _reserveBalls = new Queue<BallScript>();
- private Transform _paddle;
- private Camera _gameCamera;
- private bool _gameWon;
- private bool _gameOver;
- private BallScript _currentQueueBall;
- private int _remainingBlockCount;
- private UpgradeState _upgrades;
- void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
- Time.timeScale = 1f;
- Time.fixedDeltaTime = 0.01f;
- _upgrades = new UpgradeState();
- }
- void Start()
- {
- ResolveSceneReferences();
- _remainingBlockCount = FindObjectsByType<blockScript>(FindObjectsSortMode.None).Length;
- InitializeBallQueue();
- }
- public void OnBallEnteredDeathZone(BallScript ball)
- {
- if (_gameWon || _gameOver || ball == null)
- return;
- bool wasCurrentQueueBall = ball == _currentQueueBall;
- Destroy(ball.gameObject);
- if (HasOtherBallsInPlay(ball))
- return;
- if (wasCurrentQueueBall)
- _currentQueueBall = null;
- if (_reserveBalls.Count > 0)
- {
- MoveNextQueuedBallToPaddle();
- return;
- }
- ShowGameOver();
- }
- private bool HasOtherBallsInPlay(BallScript excludedBall)
- {
- BallScript[] balls = FindObjectsByType<BallScript>(FindObjectsSortMode.None);
- for (int i = 0; i < balls.Length; i++)
- {
- BallScript otherBall = balls[i];
- if (otherBall == null || otherBall == excludedBall)
- continue;
- if (otherBall.IsInPlay)
- return true;
- }
- return false;
- }
- /// <summary>Called by BlockScript when a block is destroyed.</summary>
- public void OnBlockDestroyed()
- {
- if (_gameWon || _gameOver)
- return;
- _remainingBlockCount = Mathf.Max(0, _remainingBlockCount - 1);
- if (_remainingBlockCount == 0)
- WinGame();
- }
- private void InitializeBallQueue()
- {
- if (ballPrefab == null)
- {
- Debug.LogWarning("GameManager: ballPrefab is not assigned. Cannot create ball queue.");
- return;
- }
- if (_paddle == null)
- {
- Debug.LogWarning("GameManager: Paddle not found. Tag the paddle as 'Paddle' or ensure PaddleScript exists in scene.");
- return;
- }
- if (_gameCamera == null)
- {
- Debug.LogWarning("GameManager: No camera found for reserve-ball placement.");
- return;
- }
- if (initialBallCount <= 0)
- {
- Debug.LogWarning("GameManager: initialBallCount must be > 0.");
- return;
- }
- Debug.Log($"GameManager: Initializing {initialBallCount} balls. Random effect pool has {(randomEffectPool?.Length ?? 0)} effects.");
- for (int i = 0; i < initialBallCount; i++)
- {
- Vector3 reservePosition;
- if (reserveAnchor != null)
- {
- // Preferred placement: line reserve balls to the right from an explicit scene anchor.
- reservePosition = reserveAnchor.position + Vector3.right * (i * reserveBallSpacing);
- }
- else
- {
- // Fallback placement if no anchor is assigned.
- Vector3 reserveStartWorld = _gameCamera.ViewportToWorldPoint(
- new Vector3(reserveStartViewport.x, reserveStartViewport.y, Mathf.Abs(_gameCamera.transform.position.z)));
- reservePosition = reserveStartWorld + Vector3.right * (i * reserveBallSpacing);
- }
- reservePosition.z = 0f;
- GameObject ballObject = Instantiate(ballPrefab, reservePosition, Quaternion.identity);
- BallScript ball = ballObject.GetComponent<BallScript>();
- if (ball == null)
- {
- Debug.LogWarning("GameManager: Spawned ballPrefab has no BallScript component.");
- Destroy(ballObject);
- continue;
- }
- ball.SetQueueManaged(true);
- ball.SetPaddleReference(_paddle, ballSpawnOffset);
- BallEffectDefinition effect = GetRandomEffect();
- Debug.Log($"GameManager: Ball {i} assigned effect: {(effect != null ? effect.EffectType.ToString() : "NULL")}");
- ball.SetEffectDefinition(effect);
- ball.ParkAtPosition(reservePosition);
- _reserveBalls.Enqueue(ball);
- }
- MoveNextQueuedBallToPaddle();
- }
- private void ResolveSceneReferences()
- {
- _paddle = GameObject.FindGameObjectWithTag("Paddle")?.transform;
- if (_paddle == null)
- {
- PaddleScript paddleScript = FindFirstObjectByType<PaddleScript>();
- if (paddleScript != null)
- _paddle = paddleScript.transform;
- }
- if (_paddle == null)
- {
- GameObject paddleByName = GameObject.Find("Paddle");
- if (paddleByName != null)
- _paddle = paddleByName.transform;
- }
- _gameCamera = Camera.main;
- if (_gameCamera == null)
- _gameCamera = FindFirstObjectByType<Camera>();
- }
- private void MoveNextQueuedBallToPaddle()
- {
- while (_reserveBalls.Count > 0)
- {
- BallScript nextBall = _reserveBalls.Dequeue();
- if (nextBall == null)
- continue;
- _currentQueueBall = nextBall;
- _currentQueueBall.PrepareOnPaddle();
- return;
- }
- ShowGameOver();
- }
- private BallEffectDefinition GetRandomEffect()
- {
- if (randomEffectPool == null || randomEffectPool.Length == 0)
- return null;
- int idx = Random.Range(0, randomEffectPool.Length);
- BallEffectDefinition baseDefinition = randomEffectPool[idx];
- // Apply active upgrades to the definition
- return UpgradeManager.ApplyUpgrades(baseDefinition, _upgrades);
- }
- private void WinGame()
- {
- _gameWon = true;
- Time.timeScale = 0f;
- if (winScreenPrefab != null)
- Instantiate(winScreenPrefab);
- else
- Debug.Log("YOU WIN! All blocks destroyed!");
- }
- private void ShowGameOver()
- {
- if (_gameOver || _gameWon)
- return;
- _gameOver = true;
- Time.timeScale = 0f;
- if (gameOverScreenPrefab != null)
- Instantiate(gameOverScreenPrefab);
- else
- Debug.Log("GAME OVER! No balls remaining.");
- }
- #region Upgrade API
- /// <summary>Get the current upgrade state (for debugging or UI display).</summary>
- public UpgradeState GetUpgradeState() => _upgrades;
- /// <summary>Increase explosive effect radius.</summary>
- public void UpgradeExplosiveRadius(float radiusBonus)
- {
- _upgrades.ExplosiveRadiusBonus += radiusBonus;
- Debug.Log($"Upgraded Explosive Radius: +{radiusBonus} (Total: +{_upgrades.ExplosiveRadiusBonus})");
- }
- /// <summary>Increase explosive damage multiplier.</summary>
- public void UpgradeExplosiveDamage(float multiplier)
- {
- _upgrades.ExplosiveDamageMultiplier *= multiplier;
- Debug.Log($"Upgraded Explosive Damage: x{multiplier} (Total: x{_upgrades.ExplosiveDamageMultiplier:F2})");
- }
- /// <summary>Increase split angle diversity.</summary>
- public void UpgradeSplitAngle(float angleBonus)
- {
- _upgrades.SplitAngleBonus += angleBonus;
- Debug.Log($"Upgraded Split Angle: +{angleBonus} (Total: +{_upgrades.SplitAngleBonus})");
- }
- /// <summary>Increase split child projectile speed.</summary>
- public void UpgradeSplitSpeed(float multiplier)
- {
- _upgrades.SplitSpeedMultiplier *= multiplier;
- Debug.Log($"Upgraded Split Speed: x{multiplier} (Total: x{_upgrades.SplitSpeedMultiplier:F2})");
- }
- /// <summary>Increase split child projectile damage.</summary>
- public void UpgradeSplitDamage(float multiplier)
- {
- _upgrades.SplitDamageMultiplier *= multiplier;
- Debug.Log($"Upgraded Split Damage: x{multiplier} (Total: x{_upgrades.SplitDamageMultiplier:F2})");
- }
- /// <summary>Increase piercing hits capacity.</summary>
- public void UpgradePiercingHits(int hitBonus)
- {
- _upgrades.PiercingHitsBonus += hitBonus;
- Debug.Log($"Upgraded Piercing Hits: +{hitBonus} (Total: +{_upgrades.PiercingHitsBonus})");
- }
- /// <summary>Increase homing turn rate (aggressiveness).</summary>
- public void UpgradeHomingTurnRate(float multiplier)
- {
- _upgrades.HomingTurnRateMultiplier *= multiplier;
- Debug.Log($"Upgraded Homing Turn Rate: x{multiplier} (Total: x{_upgrades.HomingTurnRateMultiplier:F2})");
- }
- /// <summary>Increase teleport distance per jump.</summary>
- public void UpgradeTeleportDistance(float multiplier)
- {
- _upgrades.TeleportDistanceMultiplier *= multiplier;
- Debug.Log($"Upgraded Teleport Distance: x{multiplier} (Total: x{_upgrades.TeleportDistanceMultiplier:F2})");
- }
- /// <summary>Reset all upgrades to their default values.</summary>
- public void ResetAllUpgrades()
- {
- _upgrades.ResetAll();
- Debug.Log("All upgrades reset to default values.");
- }
- #endregion
- }
|