(client) feat:实现实体动态创建,实体右键菜单
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite#41
This commit is contained in:
@ -16,63 +16,13 @@ namespace Prefab
|
||||
|
||||
public void Init(Data.PawnDef pawnDef)
|
||||
{
|
||||
entity.runtimeAttributes = pawnDef.attributes.Clone();
|
||||
entity.aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
entity.Init(pawnDef);
|
||||
|
||||
outline.Init();
|
||||
outline.Hide();
|
||||
}
|
||||
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
|
||||
{
|
||||
if (behaviorTreeDef == null)
|
||||
return null;
|
||||
AIBase aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
|
||||
if (behaviorTreeDef.childTree != null)
|
||||
{
|
||||
foreach (var child in behaviorTreeDef.childTree)
|
||||
{
|
||||
if (child != null)
|
||||
{
|
||||
aiBase.children.Add(ConvertToAIBase(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return aiBase;
|
||||
}
|
||||
|
||||
// 使用反射根据 className 创建具体的 AIBase 子类实例
|
||||
private static AIBase CreateAIBaseInstance(string className)
|
||||
{
|
||||
if (string.IsNullOrEmpty(className))
|
||||
throw new ArgumentException("className 不能为空");
|
||||
|
||||
// 定义可能的命名空间列表
|
||||
var possibleNamespaces = new[] { "AI"};
|
||||
|
||||
foreach (var ns in possibleNamespaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前程序集
|
||||
var assembly = typeof(AIBase).Assembly;
|
||||
|
||||
// 尝试查找类型
|
||||
var type = assembly.GetType($"{ns}.{className}");
|
||||
|
||||
if (type != null && typeof(AIBase).IsAssignableFrom(type))
|
||||
{
|
||||
// 如果找到合适的类型,则创建实例并返回
|
||||
return (AIBase)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略单个命名空间的错误,继续尝试下一个命名空间
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有命名空间都未找到对应的类型,抛出异常
|
||||
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
|
||||
}
|
||||
}
|
||||
}
|
44
Client/Assets/Scripts/Prefab/ImagePrefab.cs
Normal file
44
Client/Assets/Scripts/Prefab/ImagePrefab.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class ImagePrefab : MonoBehaviour
|
||||
{
|
||||
public Sprite defaultSprite;
|
||||
|
||||
private SpriteRenderer spriteRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
if (spriteRenderer == null)
|
||||
{
|
||||
Debug.LogError("SpriteRenderer组件未找到,请确保预制体包含该组件!");
|
||||
return;
|
||||
}
|
||||
if (defaultSprite != null)
|
||||
{
|
||||
spriteRenderer.sprite = defaultSprite;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetSprite(Sprite newSprite)
|
||||
{
|
||||
if (spriteRenderer != null && newSprite != null)
|
||||
{
|
||||
spriteRenderer.sprite = newSprite;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetColor(Color newColor)
|
||||
{
|
||||
if (spriteRenderer != null)
|
||||
{
|
||||
spriteRenderer.color = newColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/ImagePrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/ImagePrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c360a9f1baf4eaf9afa85617c395b0d
|
||||
timeCreated: 1753331527
|
89
Client/Assets/Scripts/Prefab/RightMenuPrefab.cs
Normal file
89
Client/Assets/Scripts/Prefab/RightMenuPrefab.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class RightMenuPrefab: Utils.MonoSingleton<RightMenuPrefab>,IPointerExitHandler
|
||||
{
|
||||
public GameObject menu;
|
||||
public ButtonPrefab buttonPrefab;
|
||||
|
||||
public void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void Init(List<(string name, UnityAction callback)> buttons)
|
||||
{
|
||||
if (menu == null || buttonPrefab == null)
|
||||
{
|
||||
Debug.LogError("Menu or ButtonPrefab is not assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
ClearMenu();
|
||||
foreach (var (label, callback) in buttons)
|
||||
{
|
||||
// 实例化按钮预制体
|
||||
var instantiatedButton = Instantiate(buttonPrefab.gameObject, menu.transform);
|
||||
var buttonInstance = instantiatedButton.GetComponent<ButtonPrefab>();
|
||||
|
||||
if (buttonInstance != null)
|
||||
{
|
||||
// 设置按钮文本
|
||||
buttonInstance.Label = label;
|
||||
|
||||
// 创建一个新的回调函数,包含原始回调和隐藏菜单的操作
|
||||
UnityAction wrappedCallback = () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 执行原始回调
|
||||
callback?.Invoke();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogError($"Error executing callback for button '{label}': {e.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 隐藏菜单
|
||||
Hide();
|
||||
}
|
||||
};
|
||||
|
||||
// 添加包装后的回调
|
||||
buttonInstance.AddListener(wrappedCallback);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to get ButtonPrefab component from instantiated object!");
|
||||
}
|
||||
}
|
||||
}
|
||||
public void ClearMenu()
|
||||
{
|
||||
// 遍历菜单下的所有子对象并销毁它们
|
||||
foreach (Transform child in menu.transform)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
protected override void OnStart()
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/RightMenuPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/RightMenuPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e45cfe2f36eb4f589b6d8f331567974d
|
||||
timeCreated: 1753196238
|
90
Client/Assets/Scripts/Prefab/SpriteAnimator.cs
Normal file
90
Client/Assets/Scripts/Prefab/SpriteAnimator.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class SpriteAnimator : MonoBehaviour, ITick
|
||||
{
|
||||
// 公开字段(可在编辑器中设置)
|
||||
[SerializeField] private Sprite[] _sprites; // 动画精灵序列
|
||||
[SerializeField] private float _fps = 2; // 每秒帧数
|
||||
[SerializeField] private Sprite _staticSprite; // 暂停时的静态精灵
|
||||
|
||||
private SpriteRenderer _renderer; // 渲染器组件
|
||||
private bool _isPaused; // 暂停状态
|
||||
private float _frameTimer; // 帧计时器
|
||||
private int _currentFrameIndex; // 当前帧索引
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_renderer = GetComponent<SpriteRenderer>();
|
||||
ValidateStartFrame();
|
||||
}
|
||||
|
||||
// ITick接口实现
|
||||
public void Tick()
|
||||
{
|
||||
var deltaTime=Time.deltaTime;
|
||||
if (_isPaused)
|
||||
{
|
||||
HandlePausedState();
|
||||
return;
|
||||
}
|
||||
|
||||
PlayAnimation(deltaTime);
|
||||
}
|
||||
|
||||
private void ValidateStartFrame()
|
||||
{
|
||||
// 确保有精灵时可显示有效帧
|
||||
if (_sprites != null && _sprites.Length > 0)
|
||||
{
|
||||
_currentFrameIndex = Mathf.Clamp(_currentFrameIndex, 0, _sprites.Length - 1);
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
_renderer.sprite = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandlePausedState()
|
||||
{
|
||||
// 优先使用静态精灵,否则保持当前帧
|
||||
if (_staticSprite)
|
||||
{
|
||||
_renderer.sprite = _staticSprite;
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAnimation(float deltaTime)
|
||||
{
|
||||
if (_sprites == null || _sprites.Length == 0) return;
|
||||
|
||||
// 更新帧计时器
|
||||
_frameTimer += deltaTime;
|
||||
float frameDuration = 1f / _fps;
|
||||
|
||||
// 检查帧切换条件
|
||||
while (_frameTimer >= frameDuration)
|
||||
{
|
||||
_frameTimer -= frameDuration;
|
||||
NextFrame();
|
||||
}
|
||||
}
|
||||
|
||||
private void NextFrame()
|
||||
{
|
||||
// 循环播放动画
|
||||
_currentFrameIndex = (_currentFrameIndex + 1) % _sprites.Length;
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
|
||||
// 外部控制方法
|
||||
public void SetPaused(bool paused) => _isPaused = paused;
|
||||
public void SetSprites(Sprite[] newSprites) => _sprites = newSprites;
|
||||
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
|
||||
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/SpriteAnimator.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/SpriteAnimator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 713a1742950a45d8b4be258a82321e45
|
||||
timeCreated: 1753282330
|
Reference in New Issue
Block a user