(client) feat:实体的手动控制

This commit is contained in:
m0_75251201
2025-07-21 13:54:08 +08:00
parent a48ccca5f4
commit 6b7ddbbd40
14 changed files with 321 additions and 35 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5760fa63a6a454c4ba22c0780f247ef2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -33,8 +33,12 @@ namespace AI
{
}
public class WanderNode : AIBase
public class RandomWander : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
return new WanderJob();
}
}
public class ConditionalAI : AIBase
{

View File

@ -1,12 +1,13 @@
using Base;
using Unity.VisualScripting;
using UnityEngine;
namespace AI
{
public abstract class JobBase
{
public Entity.Entity entity;
private int timeoutTicks = 1000;
private int timeoutTicks = 100;
public bool Running=>timeoutTicks > 0;
public virtual void StartJob(Entity.Entity target)
@ -33,6 +34,28 @@ namespace AI
timeoutTicks = 0;
}
}
public class WanderJob : JobBase
{
public override void StartJob(Entity.Entity target)
{
base.StartJob(target);
Vector3 move=new(Random.Range(-10,10), Random.Range(-10,10));
var targetPosition=entity.transform.position+move;
entity.SetTarget(targetPosition);
entity.IsChase = false;
}
protected override void UpdateJob()
{
entity.TryMove();
}
override public void StopJob()
{
base.StopJob();
entity.IsChase = true;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d2497fdaa11d3554287c58d696dab7e9

View File

@ -1,6 +1,6 @@
using UnityEngine;
namespace Camera
namespace CameraControl
{
public class CameraControl:MonoBehaviour,Base.ITick
{

View File

@ -5,7 +5,7 @@ namespace Data
public class AttributesDef : Define
{
public int health = 10;
public int moveSpeed = 1;
public float moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;

View File

@ -1,25 +1,28 @@
using System;
using System.Linq;
using AI;
using Base;
using Data;
using UnityEngine;
namespace Entity
{
public class Character : Entity,ITick
public class Character : Entity
{
public CharacterDef characterDef;
public GameObject body;
private void Start()
{
aiTree = new RandomWander();
runtimeAttributes = new AttributesDef();
}
public void Init()
{
if (characterDef == null)
return;
}
public new void Tick()
{
base.Tick();
}
}
}

View File

@ -3,46 +3,60 @@ using AI;
using Base;
using Data;
using UnityEngine;
using UnityEngine.Serialization;
namespace Entity
{
public abstract class Entity:MonoBehaviour,ITick
{
public string name;
public bool playerControlled = false;
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;
private int _warningTicks = 0;
public void Tick()
{
if (currentJob == null || !currentJob.Running)
if (PlayerControlled)
{
currentJob = aiTree.GetJob(this);
if (currentJob == null)
{
if (warningTicks<=0)
{
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
warningTicks += WarningInterval;
}
warningTicks--;
return;
}
UpdatePlayerControls();
}
else
{
AutoBehave();
}
currentJob.Update();
}
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)
{
@ -50,6 +64,80 @@ namespace Entity
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();
}
}
}
}

View File

@ -6,12 +6,18 @@ namespace Entity
{
public GameObject body;
public SpriteRenderer outlineRenderer;
public CapsuleCollider2D outlineCollider;
public void Show()
public void Init()
{
var size = GetSize();
outlineRenderer.gameObject.SetActive(true);
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()

View File

@ -1,5 +1,6 @@
using System;
using AI;
using Base;
using Data;
using Entity;
using Unity.VisualScripting;
@ -10,10 +11,16 @@ namespace Prefab
public class EntityPrefab : MonoBehaviour
{
public Entity.Entity entity;
public Outline outline;
public void Init(Data.PawnDef pawnDef)
{
entity.runtimeAttributes = pawnDef.attributes.Clone();
entity.aiTree = ConvertToAIBase(pawnDef.behaviorTree);
outline.Init();
outline.Hide();
}
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
{