78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
using UnityEngine;
|
|
using Zenject;
|
|
using FlappyBird.Core;
|
|
namespace FlappyBird.Gameplay
|
|
{
|
|
/// <summary>
|
|
/// Thin MonoBehaviour — the single scene entry-point for game-state transitions.
|
|
/// Responsibilities:
|
|
/// - Start the game (Menu -> Playing) on first tap
|
|
/// - Detect bird collision with pipes or boundaries -> GameOver
|
|
/// - Restart (GameOver -> Playing) on tap after death
|
|
/// NO state logic lives here; all calls are delegated to IGameStateManager.
|
|
/// </summary>
|
|
public sealed class GameManagerView : MonoBehaviour
|
|
{
|
|
private IGameStateManager _gameStateManager;
|
|
private IScoreManager _scoreManager;
|
|
[Inject]
|
|
private void Construct(IGameStateManager gameStateManager, IScoreManager scoreManager)
|
|
{
|
|
_gameStateManager = gameStateManager;
|
|
_scoreManager = scoreManager;
|
|
}
|
|
// ── Unity lifecycle ────────────────────────────────────────────────
|
|
private void Update()
|
|
{
|
|
HandleInput();
|
|
}
|
|
// ── Public API (called by BirdCollisionView) ───────────────────────
|
|
/// <summary>
|
|
/// Called when the bird hits a pipe or boundary collider.
|
|
/// Transitions Playing -> GameOver.
|
|
/// </summary>
|
|
public void NotifyBirdDied()
|
|
{
|
|
if (_gameStateManager.CurrentState != GameState.Playing) return;
|
|
_gameStateManager.ChangeState(GameState.GameOver);
|
|
}
|
|
// ── Private helpers ────────────────────────────────────────────────
|
|
/// <summary>
|
|
/// Menu + tap -> Playing (start game, reset score)
|
|
/// GameOver + tap -> Playing (restart game, reset score)
|
|
/// </summary>
|
|
private void HandleInput()
|
|
{
|
|
if (!IsTapDetected()) return;
|
|
switch (_gameStateManager.CurrentState)
|
|
{
|
|
case GameState.Menu:
|
|
StartGame();
|
|
break;
|
|
case GameState.GameOver:
|
|
RestartGame();
|
|
break;
|
|
}
|
|
}
|
|
private void StartGame()
|
|
{
|
|
_scoreManager.ResetScore();
|
|
_gameStateManager.ChangeState(GameState.Playing);
|
|
}
|
|
private void RestartGame()
|
|
{
|
|
_scoreManager.ResetScore();
|
|
_gameStateManager.ChangeState(GameState.Playing);
|
|
}
|
|
private static bool IsTapDetected()
|
|
{
|
|
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
|
|
return true;
|
|
if (Input.GetMouseButtonDown(0))
|
|
return true;
|
|
if (Input.GetKeyDown(KeyCode.Space))
|
|
return true;
|
|
return false;
|
|
}
|
|
}
|
|
} |