(client) feat:添加行为树和工作类
This commit is contained in:
63
Client/Assets/Scripts/AI/AIBase.cs
Normal file
63
Client/Assets/Scripts/AI/AIBase.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class AIBase
|
||||
{
|
||||
public List<AIBase> children = new();
|
||||
|
||||
public virtual JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
foreach (var aiBase in children)
|
||||
{
|
||||
var job = aiBase.GetJob(target);
|
||||
if (job != null)
|
||||
return job;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ContinuousMove : AIBase
|
||||
{
|
||||
override public JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayer : AIBase
|
||||
{
|
||||
|
||||
}
|
||||
public class WanderNode : AIBase
|
||||
{
|
||||
}
|
||||
public class ConditionalAI : AIBase
|
||||
{
|
||||
// 条件函数,返回 true 表示满足条件
|
||||
private Func<Entity.Entity, bool> condition;
|
||||
|
||||
// 构造函数,传入条件函数
|
||||
public ConditionalAI(Func<Entity.Entity, bool> conditionFunc)
|
||||
{
|
||||
condition = conditionFunc;
|
||||
}
|
||||
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
// 检查条件是否满足
|
||||
if (condition != null && condition(target))
|
||||
{
|
||||
// 如果条件满足,继续查找子节点的任务
|
||||
return base.GetJob(target);
|
||||
}
|
||||
|
||||
// 条件不满足,直接返回 null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/AI/AIBase.cs.meta
Normal file
3
Client/Assets/Scripts/AI/AIBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dba424df1b6411f91925da8288cb91f
|
||||
timeCreated: 1752983113
|
38
Client/Assets/Scripts/AI/JobBase.cs
Normal file
38
Client/Assets/Scripts/AI/JobBase.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Base;
|
||||
using Unity.VisualScripting;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class JobBase
|
||||
{
|
||||
public Entity.Entity entity;
|
||||
private int timeoutTicks = 1000;
|
||||
public bool Running=>timeoutTicks > 0;
|
||||
|
||||
public virtual void StartJob(Entity.Entity target)
|
||||
{
|
||||
entity = target;
|
||||
}
|
||||
|
||||
protected abstract void UpdateJob();
|
||||
|
||||
public bool Update()
|
||||
{
|
||||
if(!Running)
|
||||
return false;
|
||||
UpdateJob();
|
||||
timeoutTicks--;
|
||||
if (timeoutTicks <= 0)
|
||||
{
|
||||
StopJob();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public virtual void StopJob()
|
||||
{
|
||||
timeoutTicks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user