(client) feat:主要实现实体的行为树和工作类 (#40)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -1,96 +1,28 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Character : MonoBehaviour
|
||||
public class Character : Entity
|
||||
{
|
||||
public CharacterDef characterDef;
|
||||
public GameObject body;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (Managers.DefineManager.Instance.defines.TryGetValue("DrawingOrderDef",out var typeDict))
|
||||
{
|
||||
GenerateDrawNode(typeDict.Values?.FirstOrDefault()as DrawingOrderDef);
|
||||
}
|
||||
aiTree = new RandomWander();
|
||||
runtimeAttributes = new AttributesDef();
|
||||
}
|
||||
|
||||
public void Init(CharacterDef def)
|
||||
public void Init()
|
||||
{
|
||||
|
||||
if (def == null)
|
||||
if (characterDef == null)
|
||||
return;
|
||||
GenerateDrawNode(def.GetDrawingOrder(Orientation.Down));
|
||||
}
|
||||
|
||||
// 生成图片或动画节点
|
||||
private void GenerateDrawNode(DrawingOrderDef def)
|
||||
{
|
||||
// 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 方法删除子对象
|
||||
}
|
||||
|
||||
}
|
||||
}
|
143
Client/Assets/Scripts/Entity/Entity.cs
Normal file
143
Client/Assets/Scripts/Entity/Entity.cs
Normal file
@ -0,0 +1,143 @@
|
||||
using System.Collections.Generic;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class Entity:MonoBehaviour,ITick
|
||||
{
|
||||
public AIBase aiTree;
|
||||
public JobBase currentJob;
|
||||
public AttributesDef runtimeAttributes;
|
||||
public Vector3 direction;
|
||||
|
||||
public bool canSelect = true;
|
||||
public bool IsChase { set; get; } = true;
|
||||
public bool PlayerControlled
|
||||
{
|
||||
set
|
||||
{
|
||||
if (!value)
|
||||
{
|
||||
IsChase = true;
|
||||
}
|
||||
_isPlayerControlled = value;
|
||||
}
|
||||
get => _isPlayerControlled;
|
||||
}
|
||||
|
||||
private bool _isPlayerControlled = false;
|
||||
|
||||
private const int WarningInterval = 5000;
|
||||
private int _warningTicks = 0;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
if (PlayerControlled)
|
||||
{
|
||||
UpdatePlayerControls();
|
||||
}
|
||||
else
|
||||
{
|
||||
AutoBehave();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TryAttck()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 往对应朝向移动moveSpeed*deltaTime的距离
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
transform.position+=direction * (runtimeAttributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
}
|
||||
|
||||
public virtual void OnHit(Entity from)
|
||||
{
|
||||
var hit = from.runtimeAttributes.attack - runtimeAttributes.defense;
|
||||
if (hit < 0)
|
||||
hit = from.runtimeAttributes.attack / 100;
|
||||
runtimeAttributes.health -= hit;
|
||||
|
||||
currentJob.StopJob();
|
||||
}
|
||||
|
||||
public virtual void SetTarget(Vector3 pos)
|
||||
{
|
||||
direction = (pos - transform.position).normalized;
|
||||
}
|
||||
|
||||
public virtual void Kill(float delay = 0)
|
||||
{
|
||||
Destroy(gameObject,delay);
|
||||
}
|
||||
|
||||
private void AutoBehave()
|
||||
{
|
||||
if (currentJob == null || !currentJob.Running)
|
||||
{
|
||||
currentJob = aiTree.GetJob(this);
|
||||
if (currentJob == null)
|
||||
{
|
||||
if (_warningTicks<=0)
|
||||
{
|
||||
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
||||
_warningTicks += WarningInterval;
|
||||
}
|
||||
|
||||
_warningTicks--;
|
||||
return;
|
||||
}
|
||||
currentJob.StartJob(this);
|
||||
}
|
||||
|
||||
currentJob.Update();
|
||||
}
|
||||
|
||||
private void UpdatePlayerControls()
|
||||
{
|
||||
// 获取当前键盘输入状态
|
||||
var inputDirection = new Vector3();
|
||||
|
||||
// 检测 WASD 输入
|
||||
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
|
||||
{
|
||||
inputDirection += Vector3.forward; // 向前移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
||||
{
|
||||
inputDirection += Vector3.back; // 向后移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
inputDirection += Vector3.left; // 向左移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
inputDirection += Vector3.right; // 向右移动
|
||||
}
|
||||
|
||||
// 如果有输入方向,则设置目标位置并尝试移动
|
||||
if (inputDirection != Vector3.zero)
|
||||
{
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
inputDirection = inputDirection.normalized;
|
||||
|
||||
// 设置目标位置(假设当前位置为 transform.position)
|
||||
Vector3 targetPosition = transform.position + inputDirection;
|
||||
|
||||
// 调用 SetTarget 方法设置目标位置
|
||||
SetTarget(targetPosition);
|
||||
|
||||
// 调用 TryMove 方法处理实际移动逻辑
|
||||
TryMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
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
|
51
Client/Assets/Scripts/Entity/Outline.cs
Normal file
51
Client/Assets/Scripts/Entity/Outline.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Outline:MonoBehaviour
|
||||
{
|
||||
public GameObject body;
|
||||
public SpriteRenderer outlineRenderer;
|
||||
public CapsuleCollider2D outlineCollider;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var size = GetSize();
|
||||
outlineRenderer.size = size;
|
||||
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
|
||||
outlineCollider.size = size;
|
||||
}
|
||||
public void Show()
|
||||
{
|
||||
outlineRenderer.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
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