(client) feat:主要实现实体的行为树和工作类 (#40)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
67
Client/Assets/Scripts/AI/AIBase.cs
Normal file
67
Client/Assets/Scripts/AI/AIBase.cs
Normal file
@ -0,0 +1,67 @@
|
||||
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 RandomWander : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new WanderJob();
|
||||
}
|
||||
}
|
||||
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
|
61
Client/Assets/Scripts/AI/JobBase.cs
Normal file
61
Client/Assets/Scripts/AI/JobBase.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using Base;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class JobBase
|
||||
{
|
||||
public Entity.Entity entity;
|
||||
private int timeoutTicks = 100;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
2
Client/Assets/Scripts/AI/JobBase.cs.meta
Normal file
2
Client/Assets/Scripts/AI/JobBase.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2497fdaa11d3554287c58d696dab7e9
|
Reference in New Issue
Block a user