feat():完成3种模式的基本运行框架
This commit is contained in:
20
Assets/Script/Gameplay/UI/EditableComponentViewer.cs
Normal file
20
Assets/Script/Gameplay/UI/EditableComponentViewer.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
namespace UI
|
||||
{
|
||||
public class EditableComponentViewer : MonoBehaviour
|
||||
{
|
||||
public Component component;
|
||||
public bool IsOpen;
|
||||
|
||||
public void SetComponent()
|
||||
{
|
||||
if (component != null)
|
||||
{
|
||||
IsOpen = !IsOpen;
|
||||
component.gameObject.SetActive(IsOpen);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 126d8afe0e304d548dc13c4df6405e17
|
||||
timeCreated: 1760671378
|
181
Assets/Script/Gameplay/UI/PlayerEditViewer.cs
Normal file
181
Assets/Script/Gameplay/UI/PlayerEditViewer.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Core;
|
||||
using Interface;
|
||||
using Gameplay.Player;
|
||||
using Script.Gameplay.Facility;
|
||||
using UnityEngine;
|
||||
using Script.Gameplay.Input;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
// 用于显示和编辑 Facility 上的各个 IEditComponent 组件的 UI 面板
|
||||
public class PlayerEditViewer : UIBase
|
||||
{
|
||||
private PlayerEditController controller;
|
||||
private PlayerWatchModeController watchModeController;
|
||||
private IEditable editable;
|
||||
private List<IEditableComponent> components = new List<IEditableComponent>();
|
||||
private List<EditableComponentViewer> componentViewers = new List<EditableComponentViewer>();
|
||||
[SerializeField] private GameObject componentEditViewerPrefab;
|
||||
[SerializeField] private GameObject noEditableTargetPanel;
|
||||
[SerializeField] private float panelRadius = 100f;
|
||||
|
||||
private InputManager inputManager;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
inputManager = InputManager.Instance;
|
||||
ControllerLocator.Instance.TryGetWait<PlayerEditController>(GetPlayerEditControllerHandle);
|
||||
ControllerLocator.Instance.TryGetWait<PlayerWatchModeController>(GetPlayerWatchModeControllerHandle);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
inputManager = InputManager.Instance;
|
||||
}
|
||||
|
||||
public void OnEnterInsideWatchMode()
|
||||
{
|
||||
this.gameObject.SetActive(true);
|
||||
if (GetEditableFromController() != null)
|
||||
{
|
||||
GetEditableComponentsFromEditable();
|
||||
ClearComponentUI();
|
||||
GenerateComponentUI();
|
||||
}
|
||||
else
|
||||
{
|
||||
//TODO: 这里可能需要显示“无可编辑对象”之类的提示
|
||||
if (noEditableTargetPanel != null)
|
||||
{
|
||||
noEditableTargetPanel.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
inputManager.SetCursorState(true, CursorLockMode.Confined);
|
||||
inputManager.SetInputForLook(false);
|
||||
inputManager.SetInputForMove(false);
|
||||
}
|
||||
|
||||
public void OnExitInsideWatchMode()
|
||||
{
|
||||
this.gameObject.SetActive(false);
|
||||
|
||||
ClearComponentUI();
|
||||
if (noEditableTargetPanel != null)
|
||||
{
|
||||
noEditableTargetPanel.SetActive(false);
|
||||
}
|
||||
|
||||
inputManager.SetCursorState(true, CursorLockMode.Locked);
|
||||
inputManager.SetInputForLook(true);
|
||||
inputManager.SetInputForMove(true);
|
||||
}
|
||||
|
||||
// 获取 PlayerEditController 的回调
|
||||
private void GetPlayerEditControllerHandle(PlayerEditController editController)
|
||||
{
|
||||
controller = editController;
|
||||
//Debug.Log("PlayerEditViewer obtained PlayerEditController.");
|
||||
}
|
||||
|
||||
// 获取 PlayerWatchModeController 的回调
|
||||
private void GetPlayerWatchModeControllerHandle(PlayerWatchModeController watchModeCtrl)
|
||||
{
|
||||
watchModeController = watchModeCtrl;
|
||||
watchModeController.OnEnterInsideWatchMode += OnEnterInsideWatchMode;
|
||||
watchModeController.OnExitInsideWatchMode += OnExitInsideWatchMode;
|
||||
//Debug.Log("PlayerEditViewer subscribed to WatchMode events.");
|
||||
}
|
||||
|
||||
private IEditable GetEditableFromController()
|
||||
{
|
||||
if (controller == null)
|
||||
{
|
||||
Debug.Log($"controller is null in {nameof(PlayerEditViewer)}");
|
||||
return null;
|
||||
}
|
||||
|
||||
return editable = controller.GetCurrentTarget();
|
||||
}
|
||||
|
||||
private void GetEditableComponentsFromEditable()
|
||||
{
|
||||
if (editable == null)
|
||||
{
|
||||
Debug.Log($"editable is null in {nameof(PlayerEditViewer)}");
|
||||
return;
|
||||
}
|
||||
|
||||
components = editable.GetEditableComponents()
|
||||
.Where(c => c != null)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private void ClearComponentUI()
|
||||
{
|
||||
foreach (var viewer in componentViewers)
|
||||
{
|
||||
if (viewer != null)
|
||||
{
|
||||
if (Application.isPlaying) Destroy(viewer.gameObject);
|
||||
else DestroyImmediate(viewer.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
componentViewers.Clear();
|
||||
}
|
||||
|
||||
// TODO: 根据 components 生成 UI, 根据拥有的 IEditComponent 的数量动态生成 UI 元素。布局为环绕着面板中心排列(自适应间隔)。
|
||||
private void GenerateComponentUI()
|
||||
{
|
||||
if (components == null || components.Count == 0 || componentEditViewerPrefab == null) return;
|
||||
|
||||
// 计算半径:如果有 RectTransform 就根据面板大小自适应,否则使用默认值
|
||||
float radius = panelRadius;
|
||||
var rect = GetComponent<RectTransform>();
|
||||
if (rect != null)
|
||||
{
|
||||
radius = Mathf.Min(rect.rect.width, rect.rect.height) * 0.4f;
|
||||
}
|
||||
|
||||
int n = components.Count;
|
||||
float angleStep = 360f / n;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
float angleDeg = angleStep * i;
|
||||
float angleRad = Mathf.Deg2Rad * angleDeg;
|
||||
Vector2 localPos = new Vector2(Mathf.Cos(angleRad), Mathf.Sin(angleRad)) * radius;
|
||||
|
||||
var go = Instantiate(componentEditViewerPrefab, transform);
|
||||
go.name = $"{componentEditViewerPrefab.name}_{i}";
|
||||
var viewer = go.GetComponent<EditableComponentViewer>();
|
||||
if (viewer != null)
|
||||
{
|
||||
componentViewers.Add(viewer);
|
||||
}
|
||||
|
||||
// 如果是 UI 元素,优先设置 RectTransform.anchoredPosition
|
||||
var childRect = go.GetComponent<RectTransform>();
|
||||
if (childRect != null)
|
||||
{
|
||||
childRect.anchoredPosition = localPos;
|
||||
childRect.localRotation = Quaternion.identity;
|
||||
childRect.localScale = Vector3.one;
|
||||
}
|
||||
else
|
||||
{
|
||||
go.transform.localPosition = new Vector3(localPos.x, localPos.y, 0f);
|
||||
go.transform.localRotation = Quaternion.identity;
|
||||
go.transform.localScale = Vector3.one;
|
||||
}
|
||||
|
||||
// 如果预制件包含接收组件的方法(例如 SetComponent),通过 SendMessage 传递。若无则忽略。
|
||||
//go.SendMessage("SetComponent", components[i], SendMessageOptions.DontRequireReceiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/UI/PlayerEditViewer.cs.meta
Normal file
3
Assets/Script/Gameplay/UI/PlayerEditViewer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29d585e25c3a4840871167630b3c7a49
|
||||
timeCreated: 1760670557
|
32
Assets/Script/Gameplay/UI/PlayerHpViewer.cs
Normal file
32
Assets/Script/Gameplay/UI/PlayerHpViewer.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Core;
|
||||
using Gameplay.Player;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PlayerHpViewer : UIBase
|
||||
{
|
||||
private PlayerController _playerController;
|
||||
public Text hpText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_playerController != null)
|
||||
{
|
||||
hpText.text = "HP: " + _playerController.CurrentHealth;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGet(PlayerController playerController)
|
||||
{
|
||||
_playerController = playerController;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/UI/PlayerHpViewer.cs.meta
Normal file
3
Assets/Script/Gameplay/UI/PlayerHpViewer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a450025fc367419faaf7481787bfac88
|
||||
timeCreated: 1760514338
|
33
Assets/Script/Gameplay/UI/PlayerWatchModeViewer.cs
Normal file
33
Assets/Script/Gameplay/UI/PlayerWatchModeViewer.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Core;
|
||||
using Gameplay.Player;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PlayerWatchModeViewer : UIBase
|
||||
{
|
||||
private PlayerWatchModeController watchModeController;
|
||||
[SerializeField] private Text modeText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
ControllerLocator.Instance.TryGetWait<PlayerWatchModeController>(OnGet);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (watchModeController != null)
|
||||
{
|
||||
modeText.text = "Watch Mode: " + watchModeController.CurrentWatchMode.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGet(PlayerWatchModeController watchModeCtrl)
|
||||
{
|
||||
watchModeController = watchModeCtrl;
|
||||
//Debug.Log("PlayerWatchModeViewer obtained PlayerWatchModeController.");
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/UI/PlayerWatchModeViewer.cs.meta
Normal file
3
Assets/Script/Gameplay/UI/PlayerWatchModeViewer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1493e2f66aa46bf9c89362bd0d9013d
|
||||
timeCreated: 1760696175
|
12
Assets/Script/Gameplay/UI/StartGameButton.cs
Normal file
12
Assets/Script/Gameplay/UI/StartGameButton.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
using Core;
|
||||
namespace UI
|
||||
{
|
||||
public class StartGameButton : UIBase
|
||||
{
|
||||
public void StartGame()
|
||||
{
|
||||
ScenesManager.Instance.LoadGameplay("Level1");
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/UI/StartGameButton.cs.meta
Normal file
3
Assets/Script/Gameplay/UI/StartGameButton.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca5b9d4538234778ab1f51d220e79e8b
|
||||
timeCreated: 1760406127
|
Reference in New Issue
Block a user