From 3c6e3098860d33e2995db0111c59d0135592a6c4 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 14 May 2026 01:26:07 +0200 Subject: [PATCH] refactor: extract GameUiCoordinator from GameBootstrap --- .../Infrastructure/Unity/GameBootstrap.cs | 61 +++----------- .../Infrastructure/Unity/GameUiCoordinator.cs | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 Assets/Scripts/Infrastructure/Unity/GameUiCoordinator.cs diff --git a/Assets/Scripts/Infrastructure/Unity/GameBootstrap.cs b/Assets/Scripts/Infrastructure/Unity/GameBootstrap.cs index 9fa78dc..5847c72 100644 --- a/Assets/Scripts/Infrastructure/Unity/GameBootstrap.cs +++ b/Assets/Scripts/Infrastructure/Unity/GameBootstrap.cs @@ -2,7 +2,6 @@ using System.Collections; using Core.Domain; using Core.Domain.Status.Effects; using Core.Ports; -using TMPro; using UnityEngine; using UnityEngine.SceneManagement; @@ -23,12 +22,7 @@ namespace Infrastructure.Unity [SerializeField] private HunterNpcController hunterNpcPrefab; [SerializeField] private FloorVisibilityManager floorVisibilityManager; - [Header("Ui")] - [SerializeField] private TMP_Text scoreText; - [SerializeField] private TMP_Text highScoreText; - [SerializeField] private GameObject gameOverUi; - [SerializeField] private GameObject pauseUi; - [SerializeField] private GameObject startScreenUi; + [SerializeField] private GameUiCoordinator uiCoordinator; [Header("Settings")] [SerializeField] private float restartTime = 3f; @@ -44,11 +38,11 @@ namespace Infrastructure.Unity private GameObject _currentOrbInstance; private bool _isGameRunning; private int _currentPlayerFloorIndex; - private int _currentDisplayedScore; private float _inputBlockTimer; private bool _isPaused; private bool _levelGenerated; + private void OnEnable() { _actions = new InputSystem_Actions(); @@ -88,18 +82,16 @@ namespace Infrastructure.Unity SpawnDeathPlane(); SpawnPlayer(); - if (gameOverUi) gameOverUi.SetActive(false); - if (startScreenUi) startScreenUi.SetActive(true); // Show start screen NOW + uiCoordinator?.ShowStartScreen(); + uiCoordinator?.UpdateHighScore(_gameSession.HighScore); WireEvents(); - UpdateScoreUi(_gameSession.Score); - + _levelGenerated = true; })); } - if (gameOverUi) gameOverUi.SetActive(false); - if (startScreenUi) startScreenUi.SetActive(true); + uiCoordinator?.ShowStartScreen(); } private void Update() @@ -172,14 +164,14 @@ namespace Infrastructure.Unity { Time.timeScale = 0f; if (soundManager) soundManager.SetPaused(true); - if (pauseUi) pauseUi.SetActive(true); + uiCoordinator?.ShowPauseUi(); if (rumbleManager) rumbleManager.SetPaused(true); } else { Time.timeScale = 1f; if (soundManager) soundManager.SetPaused(false); - if (pauseUi) pauseUi.SetActive(false); + uiCoordinator?.HidePauseUi(); if (rumbleManager) rumbleManager.SetPaused(false); } } @@ -207,34 +199,6 @@ namespace Infrastructure.Unity } } - private void UpdateScoreUi(int newScore) - { - if (!scoreText) return; - - LeanTween.cancel(scoreText.gameObject); - - scoreText.rectTransform.localScale = Vector3.one; - LeanTween.scale(scoreText.rectTransform, Vector3.one * 1.5f, 0.5f) - .setEasePunch(); - - LeanTween.value(scoreText.gameObject, (float val) => - { - var currentVal = Mathf.RoundToInt(val); - - var combo = _gameSession?.ComboMultiplier ?? 1; - var comboText = combo > 1 ? $" x{combo}" : ""; - - scoreText.text = $"{currentVal}{comboText}"; - }, _currentDisplayedScore, newScore, 0.5f) - .setEaseOutExpo(); - - _currentDisplayedScore = newScore; - - if (highScoreText && _gameSession != null) - highScoreText.text = $"BEST: {_gameSession.HighScore}"; - - } - private void SpawnPlayer() { var spawnPos = new Vector3(0f, 5f, 0f); @@ -268,8 +232,6 @@ namespace Infrastructure.Unity _isGameRunning = false; if (beatPulseController) beatPulseController.StopTracking(); - - if (gameOverUi) gameOverUi.SetActive(true); StartCoroutine(RestartRoutine()); } @@ -283,7 +245,7 @@ namespace Infrastructure.Unity private void WireEvents() { - _gameSession.OnScoreChanged += UpdateScoreUi; + uiCoordinator?.Subscribe(_gameSession); _gameSession.OnOrbSpawned += SpawnVisualOrb; _gameSession.OnOrbReset += HandleOrbReset; _gameSession.OnGameOver += HandleGameOver; @@ -340,9 +302,8 @@ namespace Infrastructure.Unity private void StartGameSequence() { _isGameRunning = true; - _currentDisplayedScore = 0; - - if (startScreenUi) startScreenUi.SetActive(false); + + uiCoordinator?.HideStartScreen(); if (soundManager) { diff --git a/Assets/Scripts/Infrastructure/Unity/GameUiCoordinator.cs b/Assets/Scripts/Infrastructure/Unity/GameUiCoordinator.cs new file mode 100644 index 0000000..e1608b4 --- /dev/null +++ b/Assets/Scripts/Infrastructure/Unity/GameUiCoordinator.cs @@ -0,0 +1,79 @@ +using Core.Domain; +using TMPro; +using UnityEngine; + +namespace Infrastructure.Unity +{ + public class GameUiCoordinator : MonoBehaviour + { + [Header("References")] + [SerializeField] private TMP_Text scoreText; + [SerializeField] private TMP_Text highScoreText; + [SerializeField] private GameObject gameOverUi; + [SerializeField] private GameObject pauseUi; + [SerializeField] private GameObject startScreenUi; + + private GameSession _session; + private int _currentDisplayedScore; + + public void Subscribe(GameSession session) + { + _session = session; + session.OnScoreChanged += UpdateScore; + session.OnGameOver += ShowGameOverUi; + } + + public void ShowStartScreen() + { + if (gameOverUi) gameOverUi.SetActive(false); + if (startScreenUi) startScreenUi.SetActive(true); + } + + public void HideStartScreen() + { + if (startScreenUi) startScreenUi.SetActive(false); + } + + public void ShowGameOverUi() + { + if (gameOverUi) gameOverUi.SetActive(true); + } + + public void ShowPauseUi() + { + if (pauseUi) pauseUi.SetActive(true); + } + + public void HidePauseUi() + { + if (pauseUi) pauseUi.SetActive(false); + } + + public void UpdateHighScore(int highScore) + { + if (highScoreText) highScoreText.text = $"BEST: {highScore}"; + } + + private void UpdateScore(int newScore) + { + if (!scoreText) return; + + LeanTween.cancel(scoreText.gameObject); + scoreText.rectTransform.localScale = Vector3.one; + LeanTween.scale(scoreText.rectTransform, Vector3.one * 1.5f, 0.5f).setEasePunch(); + + LeanTween.value(scoreText.gameObject, (float val) => + { + var combo = _session?.ComboMultiplier ?? 1; + var comboText = combo > 1 ? $" x{combo}" : ""; + scoreText.text = $"{Mathf.RoundToInt(val)}{comboText}"; + }, _currentDisplayedScore, newScore, 0.5f) + .setEaseOutExpo(); + + _currentDisplayedScore = newScore; + + if (highScoreText && _session != null) + highScoreText.text = $"BEST: {_session.HighScore}"; + } + } +}