// ------------------------------------------------------------------------ // Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved. // See LICENSE file in the project root for license information. // ------------------------------------------------------------------------ namespace Pal3.Game.GameSystems.WorldMap { using System; using System.Collections.Generic; using System.Linq; using Command; using Command.Extensions; using Core.Command; using Core.Command.SceCommands; using Core.Contract.Constants; using Core.Contract.Enums; using Core.Utilities; using Engine.Extensions; using Input; using Script; using Scene; using State; using TMPro; using UI; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.DualShock; using UnityEngine.UI; public sealed class WorldMapManager : IDisposable, ICommandExecutor, ICommandExecutor, ICommandExecutor, ICommandExecutor, ICommandExecutor { private readonly EventSystem _eventSystem; private readonly GameStateManager _gameStateManager; private readonly InputManager _inputManager; private readonly PlayerInputActions _playerInputActions; private readonly ScriptManager _scriptManager; private readonly SceneManager _sceneManager; private readonly CanvasGroup _worldMapCanvas; private readonly GridLayoutGroup _worldMapCanvasGridLayoutGroup; private readonly GameObject _worldMapRegionButtonPrefab; private readonly RectTransform _worldMapBackgroundTransform; private readonly CanvasGroup _worldMapBackgroundCanvasGroup; private bool _isVisible; private readonly Guid _stateLockerGuid = Guid.NewGuid(); private readonly List _selectionButtons = new(); private readonly Dictionary _regionEnablementInfo = new (); public WorldMapManager(EventSystem eventSystem, GameStateManager gameStateManager, SceneManager sceneManager, InputManager inputManager, ScriptManager scriptManager, CanvasGroup worldMapCanvas, GameObject worldMapRegionButtonPrefab, GameObject worldMapBackground) { _eventSystem = Requires.IsNotNull(eventSystem, nameof(eventSystem)); _gameStateManager = Requires.IsNotNull(gameStateManager, nameof(gameStateManager)); _sceneManager = Requires.IsNotNull(sceneManager, nameof(sceneManager)); _inputManager = Requires.IsNotNull(inputManager, nameof(inputManager)); _scriptManager = Requires.IsNotNull(scriptManager, nameof(scriptManager)); _worldMapCanvas = Requires.IsNotNull(worldMapCanvas, nameof(worldMapCanvas)); _worldMapCanvasGridLayoutGroup = Requires.IsNotNull( _worldMapCanvas.GetComponent(), "worldMapCanvasGridLayoutGroup"); _worldMapRegionButtonPrefab = Requires.IsNotNull(worldMapRegionButtonPrefab, nameof(worldMapRegionButtonPrefab)); Requires.IsNotNull(worldMapBackground, nameof(worldMapBackground)); _worldMapBackgroundTransform = Requires.IsNotNull( worldMapBackground.GetComponent(), "worldMapBackgroundRectTransform"); _worldMapBackgroundCanvasGroup = Requires.IsNotNull( worldMapBackground.GetComponent(), "worldMapBackgroundCanvasGroup"); _playerInputActions = inputManager.GetPlayerInputActions(); _worldMapCanvas.alpha = 0f; _worldMapCanvas.interactable = false; _worldMapBackgroundCanvasGroup.alpha = 0f; _isVisible = false; _playerInputActions.Gameplay.ToggleWorldMap.performed += ToggleWorldMapOnPerformed; _playerInputActions.UI.ToggleWorldMap.performed += ToggleWorldMapOnPerformed; _playerInputActions.UI.ExitCurrentShowingMenu.performed += HideWorldMapOnPerformed; CommandExecutorRegistry.Instance.Register(this); } public void Dispose() { CommandExecutorRegistry.Instance.UnRegister(this); _playerInputActions.Gameplay.ToggleWorldMap.performed -= ToggleWorldMapOnPerformed; _playerInputActions.UI.ToggleWorldMap.performed -= ToggleWorldMapOnPerformed; _playerInputActions.UI.ExitCurrentShowingMenu.performed -= HideWorldMapOnPerformed; } private void ToggleWorldMapOnPerformed(InputAction.CallbackContext _) { ToggleWorldMap(); } private void HideWorldMapOnPerformed(InputAction.CallbackContext _) { if (_isVisible) { Hide(); _gameStateManager.TryGoToState(GameState.Gameplay); } } public bool IsVisible() { return _isVisible; } public Dictionary GetRegionEnablementInfo() { return _regionEnablementInfo; } private void ToggleWorldMap() { if (_sceneManager.GetCurrentScene() == null) return; if (_isVisible) { Hide(); _gameStateManager.TryGoToState(GameState.Gameplay); } else if (_gameStateManager.GetCurrentState() == GameState.Gameplay) { // Only enable world map toggling in non-maze scenes. if (_sceneManager.GetCurrentScene().GetSceneInfo().SceneType != SceneType.Maze) { Show(); } } } private void Show() { if (_regionEnablementInfo.Count == 0) { Pal3.Instance.Execute(new UIDisplayNoteCommand("大地图尚未开启")); return; } _gameStateManager.TryGoToState(GameState.UI); // Scene script can execute GameSwitchRenderingStateCommand to toggle WorldMap // After the script finishes, the state will be reset to Gameplay. Thus we need to // block the state change. _gameStateManager.AddGamePlayStateLocker(_stateLockerGuid); Pal3.Instance.Execute(new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID)); GameObject exitButtonObj = UnityEngine.Object.Instantiate(_worldMapRegionButtonPrefab, _worldMapCanvas.transform); TextMeshProUGUI exitButtonTextUI = exitButtonObj.GetComponentInChildren(); exitButtonTextUI.text = "关闭"; Button exitButton = exitButtonObj.GetComponent