feat():完成3种模式的基本运行框架

This commit is contained in:
2025-10-18 08:55:38 +08:00
parent 3d03c59dc3
commit 345930843d
39 changed files with 909 additions and 427 deletions

View File

@@ -0,0 +1,44 @@
using System;
using Gameplay.Player;
using UnityEngine;
using Interface;
namespace Script.Gameplay.Facility
{
public class DoorInteractController : InteractableBaseController, IEditableComponent
{
[SerializeField] private LockLevel lockLevel;
private bool isOpened = false;
public override void Interact(GameObject interactor)
{
if (isOpened)
{
CloseDoor();
isOpened = false;
}
else
{
OpenDoor();
isOpened = true;
}
}
public LockLevel LockLevel => lockLevel;
public void SetActive(bool active)
{
// 实现激活或禁用门的逻辑
Interactable = active;
}
private void OpenDoor()
{
Debug.Log("Open door");
}
private void CloseDoor()
{
Debug.Log("Close door");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3bdc5380046d4cacb0dd579877f320eb
timeCreated: 1760693181

View File

@@ -1,7 +0,0 @@
namespace Script.Gameplay.Facility
{
public class EditComponent
{
}
}

View File

@@ -1,69 +0,0 @@
using System;
using Gameplay.Player;
using UnityEngine;
using Interface;
namespace Script.Gameplay.Facility
{
public class FacilityController : MonoBehaviour, IInteractable, IEditable
{
public FacilityModifier facilityModifier;
private void Awake()
{
if (facilityModifier == null)
{
facilityModifier = GetComponent<FacilityModifier>();
}
}
private void Start()
{
}
public string GetInteractPrompt()
{
return "";
}
public void Interact(GameObject interactor)
{
}
public void OnGazeEnter(GameObject editor)
{
// 物体弹出按F可交互菜单
}
public void OnGazeExit(GameObject editor)
{
// 物体取消菜单
}
public void OnGazeEnter(PlayerEditController editor)
{
}
public void OnGazeExit(PlayerEditController editor)
{
}
public void BeginEdit()
{
}
public void EndEdit()
{
}
public IEditable GetEditable()
{
return this;
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Gameplay.Player;
using UnityEngine;
using Interface;
namespace Script.Gameplay.Facility
{
public class FacilityEditableController : MonoBehaviour, IEditable
{
public void OnGazeEnter(PlayerEditController editor)
{
}
public void OnGazeExit(PlayerEditController editor)
{
}
public void BeginEdit()
{
}
public void EndEdit()
{
}
public List<IEditableComponent> GetEditableComponents()
{
var components = new List<IEditableComponent>();
foreach (var mb in GetComponentsInChildren<MonoBehaviour>(true))
{
if (mb is IEditableComponent editableComponent)
{
components.Add(editableComponent);
}
}
return components;
}
}
}

View File

@@ -1,27 +0,0 @@
using UnityEngine;
namespace Script.Gameplay.Facility
{
public class FacilityModifier : MonoBehaviour
{
private Transform _transform;
private Collider _collider;
private Rigidbody _rigidbody;
public void ModifyComponent<T>(bool isOpen, T component) where T : Component
{
var targetComponent = this.GetComponent<T>();
if (targetComponent != null)
{
if (isOpen)
{
targetComponent.gameObject.SetActive(true);
}
else
{
targetComponent.gameObject.SetActive(false);
}
}
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: b1db33a437984a1a8fae09ab8c5de7e4
timeCreated: 1760667210

View File

@@ -0,0 +1,15 @@
namespace Script.Gameplay.Facility
{
public enum LockLevel
{
Red,
Blue,
Yellow,
}
public interface IEditableComponent
{
public LockLevel LockLevel { get; }
public void SetActive(bool active);
}
}

View File

@@ -0,0 +1,30 @@
using UnityEngine;
using Interface;
namespace Script.Gameplay.Facility
{
public abstract class InteractableBaseController : MonoBehaviour, IInteractable
{
public bool Interactable = true;
public string GetInteractPrompt()
{
return "按F进行交互";
}
public virtual void Interact(GameObject interactor)
{
if (!Interactable) return;
Debug.Log($"{gameObject.name} 被 {interactor.name} 交互了!");
}
public void OnGazeEnter(GameObject editor)
{
// 修改鼠标指针或高亮显示物体等
}
public void OnGazeExit(GameObject editor)
{
// 恢复鼠标指针或取消高亮显示物体等
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f5781ad9dd2842a584b8ef333ae75e60
timeCreated: 1760693365

View File

@@ -1,5 +1,8 @@
using UnityEngine;
using Gameplay.Player;
using System.Collections.Generic;
using Script.Gameplay.Facility;
namespace Interface
{
public interface IEditable
@@ -8,6 +11,6 @@ namespace Interface
void OnGazeExit(PlayerEditController editor); // 玩家停止注视时触发
void BeginEdit();
void EndEdit();
IEditable GetEditable();
List<IEditableComponent> GetEditableComponents();
}
}

View File

@@ -1,3 +1,4 @@
using Core;
using UnityEngine;
using Interface;
using Script.Gameplay.Input;
@@ -21,11 +22,8 @@ namespace Gameplay.Player
inputManager = InputManager.Instance;
if (playerCamera == null)
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
inputManager.Input.Player.SwitchWatchMode.performed += ctx =>
{
if (currentTarget != null) currentTarget.BeginEdit();
};
ControllerLocator.Instance.Register(this);
}
void Update()
@@ -73,6 +71,11 @@ namespace Gameplay.Player
}
}
public IEditable GetCurrentTarget()
{
return currentTarget;
}
void OnDrawGizmos()
{
if(!isDrawGizmos) return;

View File

@@ -21,7 +21,13 @@ namespace Gameplay.Player
playerCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
var input = InputManager.Instance.Input;
input.Player.SwitchWatchMode.performed += ctx => currentTarget.Interact(this.gameObject);;
input.Player.Interact.performed += ctx =>
{
if (currentTarget != null)
{
currentTarget.Interact(this.gameObject);
}
};
}
void Update()

View File

@@ -1,6 +1,7 @@
using System;
using Script.Gameplay.Input;
using UnityEngine;
using Core;
namespace Gameplay.Player
{
@@ -10,34 +11,46 @@ namespace Gameplay.Player
Inside,
Outside
}
public class PlayerWatchModeSwitcher : MonoBehaviour
public class PlayerWatchModeController : MonoBehaviour
{
[SerializeField] private Camera cam;
public event Action OnEnterInsideWatchMode;
public event Action OnExitInsideWatchMode;
private InputManager inputManager;
private WatchMode currentMode = WatchMode.Normal;
public WatchMode CurrentWatchMode
{
get => currentMode;
set
{
if (value != WatchMode.Inside)
{
OnExitInsideWatchMode?.Invoke();
}
if (value == WatchMode.Inside)
{
OnEnterInsideWatchMode?.Invoke();
}
currentMode = value;
SwitchWatchMode(currentMode);
}
}
private void Start()
{
inputManager = InputManager.Instance;
SetWatchMode(currentMode);
var input = inputManager.Input;
input.Player.SwitchWatchMode.performed += ctx =>
{
currentMode = (WatchMode)(((int)currentMode + 1) % 3);
SetWatchMode(currentMode);
var modeTemp = (WatchMode)(((int)CurrentWatchMode + 1) % 3);
CurrentWatchMode = modeTemp;
};;
ControllerLocator.Instance.Register(this);
}
public void SetWatchMode(WatchMode mode)
{
currentMode = mode;
SwitchWatchMode(mode);
}
// 实现按Tap键实现在WatchMode 3个模式切换并通过SwitchWatchMode设置正确的相机模式
private void SwitchWatchMode(WatchMode watchMode)
{
switch (watchMode)

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 492ef2bd8fce4535b1b8c32d2a3e36ef
timeCreated: 1760695944

View File

@@ -3,11 +3,11 @@ using UnityEngine;
using UnityEngine;
namespace UI
{
public class FacilityComponentEditViewer : MonoBehaviour
public class EditableComponentViewer : MonoBehaviour
{
public Component component;
public bool IsOpen;
public void SetComponent()
{
if (component != null)

View 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);
}
}
}
}

View File

@@ -11,7 +11,7 @@ namespace UI
private PlayerController _playerController;
public Text hpText;
private void Start()
private void Awake()
{
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
}

View 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.");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b1493e2f66aa46bf9c89362bd0d9013d
timeCreated: 1760696175

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 5895badf61464ca6ad9af54cca1a8474
timeCreated: 1760361415

View File

@@ -1,24 +0,0 @@
using System;
using UnityEngine;
using Core;
using Gameplay.Player;
using Script.Gameplay.Facility;
namespace UI
{
public class PlayerEditPanelViewer : UIBase
{
private PlayerEditController controller;
private FacilityController currentFacilityController;
private void Start()
{
}
// 根据currentFacilityController 生成其对应的组件
private void GenerateComponent()
{
}
}
}

View File

@@ -1,17 +0,0 @@
{
"name": "UI",
"rootNamespace": "",
"references": [
"GUID:fd0e97c21c15497f9406b8ee23c1f67e",
"GUID:ceb6a2c14699464ca35be17af198f904"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 65e534cfaf6f474895bcfa96809b48ca
timeCreated: 1760361420