(client):feat:添加对列表定义读取的支持,ui管理器将对没有键的窗口也进行统一管理
This commit is contained in:
35
Client/Assets/Scripts/Entity/AIBase.cs
Normal file
35
Client/Assets/Scripts/Entity/AIBase.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class AIBase
|
||||
{
|
||||
public virtual bool Run(Entity target)
|
||||
{
|
||||
foreach (var aiBase in children)
|
||||
{
|
||||
if (aiBase.Run(target))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public List<AIBase> children=new();
|
||||
}
|
||||
|
||||
public class ContinuousMove : AIBase
|
||||
{
|
||||
override public bool Run(Entity target)
|
||||
{
|
||||
target.gameObject.transform.position +=
|
||||
Time.deltaTime * target.runtimeAttributes.moveSpeed * target.direction;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayer : AIBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/AIBase.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/AIBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dba424df1b6411f91925da8288cb91f
|
||||
timeCreated: 1752983113
|
@ -1,96 +1,25 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Character : MonoBehaviour
|
||||
public class Character : Entity,ITick
|
||||
{
|
||||
private void Start()
|
||||
public CharacterDef characterDef;
|
||||
public GameObject body;
|
||||
public void Init()
|
||||
{
|
||||
if (Managers.DefineManager.Instance.defines.TryGetValue("DrawingOrderDef",out var typeDict))
|
||||
{
|
||||
GenerateDrawNode(typeDict.Values?.FirstOrDefault()as DrawingOrderDef);
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(CharacterDef def)
|
||||
{
|
||||
|
||||
if (def == null)
|
||||
if (characterDef == null)
|
||||
return;
|
||||
GenerateDrawNode(def.GetDrawingOrder(Orientation.Down));
|
||||
}
|
||||
|
||||
// 生成图片或动画节点
|
||||
private void GenerateDrawNode(DrawingOrderDef def)
|
||||
public new void Tick()
|
||||
{
|
||||
// Debug.Log(def);
|
||||
// 删除现有子节点
|
||||
DeleteAllChildren();
|
||||
|
||||
// 生成根节点下的所有节点
|
||||
foreach (var nodeDef in def.drawNodes) GenerateNode(nodeDef, transform); // 在当前节点下生成
|
||||
}
|
||||
|
||||
// 递归生成节点
|
||||
private void GenerateNode(DrawNodeDef nodeDef, Transform parent)
|
||||
{
|
||||
// Debug.Log(nodeDef.nodeName);
|
||||
// 创建新的 GameObject 表示节点
|
||||
var nodeObject = new GameObject(nodeDef.nodeName);
|
||||
|
||||
// 设置父节点
|
||||
nodeObject.transform.SetParent(parent, false);
|
||||
|
||||
// 根据节点类型生成不同的内容
|
||||
switch (nodeDef.drawNodeType)
|
||||
{
|
||||
case DrawNodeType.Image:
|
||||
CreateImageNode(nodeObject);
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
CreateAnimationNode(nodeObject);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.LogWarning($"Unsupported node type: {nodeDef.drawNodeType}");
|
||||
break;
|
||||
}
|
||||
|
||||
// 递归生成子节点
|
||||
if (nodeDef.children != null && nodeDef.children.Count > 0)
|
||||
foreach (var childNodeDef in nodeDef.children)
|
||||
GenerateNode(childNodeDef, nodeObject.transform); // 在当前节点下生成子节点
|
||||
}
|
||||
|
||||
// 创建图片节点
|
||||
private void CreateImageNode(GameObject nodeObject)
|
||||
{
|
||||
// 添加 SpriteRenderer 组件表示图片
|
||||
var spriteRenderer = nodeObject.AddComponent<SpriteRenderer>();
|
||||
spriteRenderer.sprite = Resources.Load<Sprite>("DefaultImage"); // 加载默认图片
|
||||
spriteRenderer.color = Color.white; // 设置默认颜色
|
||||
}
|
||||
|
||||
// 创建动画节点
|
||||
private void CreateAnimationNode(GameObject nodeObject)
|
||||
{
|
||||
// 添加 Animator 组件表示动画
|
||||
var animator = nodeObject.AddComponent<Animator>();
|
||||
animator.runtimeAnimatorController =
|
||||
Resources.Load<RuntimeAnimatorController>("DefaultAnimation"); // 加载默认动画控制器
|
||||
}
|
||||
|
||||
private void DeleteAllChildren()
|
||||
{
|
||||
// 获取当前对象的 Transform
|
||||
var parentTransform = transform;
|
||||
|
||||
// 删除所有子对象
|
||||
foreach (Transform child in parentTransform) Destroy(child.gameObject); // 使用 Destroy 方法删除子对象
|
||||
base.Tick();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
35
Client/Assets/Scripts/Entity/Entity.cs
Normal file
35
Client/Assets/Scripts/Entity/Entity.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class Entity:MonoBehaviour,ITick
|
||||
{
|
||||
public bool playerControlled = false;
|
||||
public List<AIBase> aiTree=new();
|
||||
public Data.AttributesDef runtimeAttributes;
|
||||
public Vector3 direction;
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var aiBase in aiTree)
|
||||
{
|
||||
if (aiBase.Run(this))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TryAttck()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public virtual void OnHit(Entity from)
|
||||
{
|
||||
var hit = from.runtimeAttributes.attack - runtimeAttributes.defense;
|
||||
if (hit < 0)
|
||||
hit = from.runtimeAttributes.attack / 100;
|
||||
runtimeAttributes.health -= hit;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/Entity.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/Entity.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbde354e0bcc4409b3378ee9b698ddc0
|
||||
timeCreated: 1752932072
|
45
Client/Assets/Scripts/Entity/Outline.cs
Normal file
45
Client/Assets/Scripts/Entity/Outline.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Outline:MonoBehaviour
|
||||
{
|
||||
public GameObject body;
|
||||
public SpriteRenderer outlineRenderer;
|
||||
|
||||
public void Show()
|
||||
{
|
||||
var size = GetSize();
|
||||
outlineRenderer.gameObject.SetActive(true);
|
||||
outlineRenderer.size = size;
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
outlineRenderer.gameObject.SetActive(false);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定对象及其所有子对象组成的图像的大小。
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// 返回一个 Vector3 对象,表示对象在世界空间中的总大小(宽度、高度、深度)。
|
||||
/// 如果没有找到任何渲染器,则返回 (-1, -1, -1) 表示无效大小。
|
||||
/// </returns>
|
||||
public Vector3 GetSize()
|
||||
{
|
||||
var renderers = body.GetComponentsInChildren<Renderer>();
|
||||
|
||||
if (renderers.Length == 0)
|
||||
{
|
||||
return new(-1,-1);
|
||||
}
|
||||
var totalBounds = renderers[0].bounds;
|
||||
for (var i = 1; i < renderers.Length; i++)
|
||||
{
|
||||
totalBounds.Encapsulate(renderers[i].bounds);
|
||||
}
|
||||
var size = totalBounds.size;
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Entity/Outline.cs.meta
Normal file
2
Client/Assets/Scripts/Entity/Outline.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acf99b9aa18d4ab9b4ce796d513d476b
|
Reference in New Issue
Block a user