(client) feat:实现指定位置生成实体

This commit is contained in:
m0_75251201
2025-08-04 13:26:04 +08:00
parent e1ff66ff28
commit 3c9be3a30c
8 changed files with 2404 additions and 195 deletions

View File

@ -8,214 +8,170 @@ using Object = UnityEngine.Object;
namespace Base
{
/// <summary>
/// UI窗口输入控制和管理类
/// 负责根据输入显示/隐藏UI并根据UI状态管理游戏暂停。
/// </summary>
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
{
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
private List<UIBase> noKeyWindows = new();
private List<UIBase> allWindows = new(); // 新增:所有窗口的集合
// 存储场景中所有UIBase的实例
private List<UIBase> _allWindows = new List<UIBase>();
// 缓存当前可见的窗口
private readonly List<UIBase> _visibleWindows = new List<UIBase>();
private bool needUpdate = false;
/// <summary>
/// 查找并注册场景中所有的UI窗口包括非激活状态的
/// </summary>
private void RegisterAllWindows()
{
_allWindows.Clear();
// 获取当前活动场景中的所有 GameObject
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
if (!activeScene.isLoaded)
{
Debug.LogWarning("当前场景未加载,无法注册窗口!");
return;
}
// 遍历场景中的所有根对象,并查找其子对象中的 UIBase
foreach (var rootGameObject in activeScene.GetRootGameObjects())
{
var windows = rootGameObject.GetComponentsInChildren<UIBase>(true);
_allWindows.AddRange(windows);
}
// 去重(如果有重复的窗口)
_allWindows = _allWindows.Distinct().ToList();
// 初始化所有窗口为隐藏状态
foreach (var window in _allWindows)
{
if (window != null && window.gameObject != null)
{
window.Hide();
}
}
needUpdate = true;
Debug.Log($"窗口数量{_allWindows.Count}");
}
/// <summary>
/// UI逻辑更新循环需要被外部的某个管理器在Update中调用
/// </summary>
public void TickUI()
{
foreach (var kvp in UIwindowKeys)
//使用这个是为了让输入独占窗口关闭自己后不会立即激活其他窗口的按键,延迟一帧
if (needUpdate)
{
if (Input.GetKeyDown(kvp.Key))
{
HandleWindowActivation(kvp.Value);
break;
}
// 更新可见窗口缓存和暂停状态
UpdateVisibleWindowsCache();
UpdatePauseState();
needUpdate = false;
return;
}
}
private void Init()
{
UIwindowKeys.Clear();
noKeyWindows.Clear();
allWindows.Clear(); // 清空所有窗口集合
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
foreach (var uiBase in uiInstances)
if(_visibleWindows.Any(window => window.isInputOccupied))
return;
foreach (var window in _allWindows)
{
allWindows.Add(uiBase); // 添加到所有窗口集合
var key = uiBase.actionButton;
if (key == KeyCode.None)
{
noKeyWindows.Add(uiBase);
uiBase.Hide();
continue;
}
if (UIwindowKeys.ContainsKey(key))
{
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
continue;
}
UIwindowKeys[key] = uiBase;
uiBase.Hide();
}
}
private void HandleWindowActivation(UIBase targetWindow)
{
bool wasTargetVisible = targetWindow.IsVisible;
bool shouldCloseExclusive = false;
bool exclusiveWindowWasOpen = false;
// 第一次遍历:检查是否有需要关闭的独占窗口
foreach (var window in allWindows)
{
if (window == targetWindow) continue;
if (window.IsVisible && window.exclusive)
{
// 记录有独占窗口打开且需要关闭
shouldCloseExclusive = true;
exclusiveWindowWasOpen = true;
break;
}
}
// 第二次遍历:根据条件关闭窗口
foreach (var window in allWindows)
{
if (window == targetWindow) continue;
// 检查窗口是否设置了有效的激活按键,并且该按键在本帧被按下
if (window.actionButton == KeyCode.None || !Input.GetKeyDown(window.actionButton)) continue;
if (window.IsVisible)
{
// 关闭所有独占窗口(无论是新窗口打开还是关闭)
// 或当目标窗口是独占窗口时关闭所有其他窗口
if (window.exclusive || targetWindow.exclusive || shouldCloseExclusive)
// 如果窗口当前是可见的,且未占用输入,则通过按键隐藏它
if (!window.isInputOccupied)
{
window.Hide();
Hide(window);
}
}
else
{
// 如果窗口当前是隐藏的,则显示它
Show(window);
}
}
// 切换目标窗口状态
if (wasTargetVisible)
{
targetWindow.Hide();
}
else
{
targetWindow.Show();
}
// 更新暂停状态(优化版)
UpdatePauseState(exclusiveWindowWasOpen, targetWindow);
}
private void UpdatePauseState(bool exclusiveWindowWasOpen, UIBase targetWindow)
/// <summary>
/// 公开的显示窗口方法
/// </summary>
/// <param name="windowToShow">要显示的窗口</param>
public void Show(UIBase windowToShow)
{
bool needPause = false;
if (!windowToShow || windowToShow.IsVisible) return;
foreach (var window in allWindows)
// 如果窗口是独占的,隐藏所有其他窗口
if (windowToShow.exclusive)
{
if (window.IsVisible && window.needPause)
List<UIBase> windowsToHide = new List<UIBase>(_visibleWindows);
foreach (var visibleWindow in windowsToHide)
{
needPause = true;
break;
Hide(visibleWindow);
}
}
// 只在状态改变时更新
if (Base.Clock.Instance.Pause != needPause)
{
Base.Clock.Instance.Pause = needPause;
}
// 显示目标窗口并更新缓存与暂停状态
windowToShow.Show();
needUpdate = true;
}
/// <summary>
/// 模拟按键输入切换窗口
/// 公开的隐藏窗口方法
/// </summary>
/// <param name="keyCode">要模拟的按键</param>
public void SimulateKeyPress(KeyCode keyCode)
/// <param name="windowToHide">要隐藏的窗口</param>
public void Hide(UIBase windowToHide)
{
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow))
{
HandleWindowActivation(targetWindow); // 调用内部逻辑处理
}
else
{
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
}
if (!windowToHide || !windowToHide.IsVisible) return;
// 隐藏目标窗口并更新缓存与暂停状态
windowToHide.Hide();
needUpdate = true;
}
/// <summary>
/// 打开指定的窗口(无论是否有激活键)
/// 根据当前所有可见窗口的 needPause 属性来更新游戏时钟的暂停状态
/// </summary>
/// <param name="window">要打开的窗口</param>
public void OpenWindow(UIBase window)
private void UpdatePauseState()
{
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
bool shouldPause = _visibleWindows.Any(w => w.needPause);
if (Base.Clock.Instance.Pause != shouldPause)
{
Debug.LogWarning("Cannot open the specified window as it is not registered.");
return;
Base.Clock.Instance.Pause = shouldPause;
}
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
}
/// <summary>
/// 关闭指定的窗口(无论是否有激活键)
/// 更新当前可见窗口的缓存列表
/// </summary>
/// <param name="window">要关闭的窗口</param>
public void CloseWindow(UIBase window)
private void UpdateVisibleWindowsCache()
{
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
_visibleWindows.Clear();
foreach (var window in _allWindows)
{
Debug.LogWarning("Cannot close the specified window as it is not registered.");
return;
if (window.IsVisible)
{
_visibleWindows.Add(window);
}
}
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
}
/// <summary>
/// 切换指定窗口的显示状态(无论是否有激活键)
/// </summary>
/// <param name="window">要切换的窗口</param>
public void ToggleWindow(UIBase window)
{
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
{
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
return;
}
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
}
/// <summary>
/// 在对象销毁时清理事件监听
/// </summary>
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
/// <summary>
/// 在对象启动时初始化
/// </summary>
protected override void OnStart()
{
// 注册场景加载事件
SceneManager.sceneLoaded += OnSceneLoaded;
// 初始化时调用一次
Init();
// RegisterAllWindows();
}
/// <summary>
/// 场景加载完成后重新初始化
/// </summary>
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// 场景加载完成后调用 Init 方法
Init();
RegisterAllWindows();
}
}
}

View File

@ -9,14 +9,4 @@ namespace Entity
}
}
public class MonsterAttributes
{
public int health = 10;
public int moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
}

View File

@ -78,7 +78,7 @@ namespace Entity
{
var rightMenu = Prefab.RightMenuPrefab.Instance;
rightMenu.Init(GetMenu());
rightMenu.transform.position=Input.mousePosition;
rightMenu.transform.position = Input.mousePosition;
rightMenu.Show();
}
}
@ -86,12 +86,12 @@ namespace Entity
private List<(string name, UnityAction callback)> GetMenu()
{
var result = new List<(string name, UnityAction callback)>();
if(entity.PlayerControlled)
result.Add(("结束操控",()=>entity.PlayerControlled=false));
if (entity.PlayerControlled)
result.Add(("结束操控", () => entity.PlayerControlled = false));
else
result.Add(("手动操控",()=>entity.PlayerControlled=true));
result.Add(("杀死",()=>entity.Kill()));
result.Add(("变成笨蛋",()=>BecomeDefault()));
result.Add(("手动操控", () => entity.PlayerControlled = true));
result.Add(("杀死", () => entity.Kill()));
result.Add(("变成笨蛋", () => BecomeDefault()));
return result;
}
@ -99,5 +99,6 @@ namespace Entity
{
entity.Kill();
Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position);
}
}
}

View File

@ -10,6 +10,8 @@ namespace UI
{
public GameObject menuContent;
public EntityPlacementUI entityPlacementUI;
public Prefab.TextPrefab textTemplate;
public Prefab.ButtonPrefab buttonTemplate;
@ -35,7 +37,7 @@ namespace UI
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
// button.text.text = i.ToString();
// }
}
private void InitCharacter()
@ -46,7 +48,7 @@ namespace UI
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
foreach (var def in defList)
{
var button=InstantiatePrefab(buttonTemplate, menuContent.transform);
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
button.Label = def.label;
var pawnDef = def;
button.AddListener(() => GenerateEntityCallback(pawnDef));
@ -57,8 +59,8 @@ namespace UI
{
var title = InstantiatePrefab(textTemplate, menuContent.transform);
title.Label = "生成怪物";
var defList=Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
foreach (var def in defList)
{
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
@ -67,7 +69,7 @@ namespace UI
button.AddListener(() => GenerateEntityCallback(pawnDef));
}
}
/// <summary>
/// 通用的实例化函数,返回实例化的预制件脚本组件。
/// </summary>
@ -84,10 +86,10 @@ namespace UI
}
// 实例化预制件
GameObject instance = Instantiate(prefab.gameObject, parent);
var instance = Instantiate(prefab.gameObject, parent);
// 获取实例化对象的脚本组件
T instantiatedComponent = instance.GetComponent<T>();
var instantiatedComponent = instance.GetComponent<T>();
if (instantiatedComponent == null)
{
@ -99,7 +101,16 @@ namespace UI
private void GenerateEntityCallback(PawnDef pawnDef)
{
Managers.EntityManage.Instance.GenerateEntity(pawnDef, new(0, 0));
entityPlacementUI.currentAction = () =>
{
// 将鼠标屏幕坐标转换为世界坐标,并确保 Z 值为 0
if (!Camera.main) return;
var worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPosition.z = 0;
Managers.EntityManage.Instance.GenerateEntity(pawnDef, worldPosition);
};
entityPlacementUI.Prompt = $"当前生成器:\n名称{pawnDef.label}\n描述{pawnDef.description}";
Base.UIInputControl.Instance.Show(entityPlacementUI);
}
}

View File

@ -1,24 +1,34 @@
using Base;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
namespace UI
{
public delegate void NonReturnCallback();
public class EntityPlacementUI:UIBase,ITickUI
{
public UnityAction currentAction = null;
public TMP_Text promptText;
public NonReturnCallback currentAction;
public string Prompt
{
get => promptText.text;
set => promptText.text = value;
}
public void TickUI()
{
if (!IsVisible||currentAction==null)
if (!IsVisible)
return;
if (Input.GetMouseButton(0))
{
currentAction.Invoke();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Hide();
Base.UIInputControl.Instance.Hide(this);
}
if (currentAction!=null&&Input.GetMouseButtonDown(0))
{
currentAction();
}
}

View File

@ -6,7 +6,7 @@ namespace UI
{
public void ContinueButton()
{
Base.UIInputControl.Instance.CloseWindow(this);
Base.UIInputControl.Instance.Hide(this);
}
public void ExitButton()