2
0
Fork 0

行为树 类结构优化

master
cd 2 years ago
parent 010158a7a8
commit d69a45fbb2

@ -12,15 +12,13 @@ namespace Game
{ {
BehaviorTree Tree(); BehaviorTree Tree();
void Create(); void Create();
void Reset(GameEntity entity);
void Tick(); void Tick();
} }
public partial class AIObjectBase<T> : IAIObject where T : new() public partial class AIObjectBase<T> : IAIObject where T : new()
{ {
private GameEntity _entity; protected BehaviorTreeBuilder _builder;
private BehaviorTreeBuilder _builder; protected BehaviorTree _tree;
private BehaviorTree _tree;
public static EventCallbackCreate GetCreator() public static EventCallbackCreate GetCreator()
{ {
@ -34,11 +32,6 @@ namespace Game
_tree = _builder.Build(); _tree = _builder.Build();
} }
public void Reset(GameEntity entity)
{
_entity = entity;
}
protected virtual void OnCreate() protected virtual void OnCreate()
{ {
} }

@ -14,7 +14,7 @@ namespace Game
protected void Selector(Action action) protected void Selector(Action action)
{ {
Selector("selector",action); Selector("selector", action);
} }
protected void Selector(string name, Action action) protected void Selector(string name, Action action)
@ -23,9 +23,10 @@ namespace Game
action(); action();
_builder.End(); _builder.End();
} }
protected void Sequence(Action action) protected void Sequence(Action action)
{ {
Sequence("sequence",action); Sequence("sequence", action);
} }
protected void Sequence(string name, Action action) protected void Sequence(string name, Action action)

@ -19,15 +19,15 @@ namespace Game
if (!interfaceType.IsAssignableFrom(type)) continue; if (!interfaceType.IsAssignableFrom(type)) continue;
if (type.IsGenericType) continue; if (type.IsGenericType) continue;
if (type.IsInterface) continue; if (type.IsInterface) continue;
if (type.BaseType == null) continue; if (type.BaseType?.BaseType == null) continue;
var method = type.BaseType.GetMethod("GetCreator", BindingFlags.Static | BindingFlags.Public); var method = type.BaseType.BaseType.GetMethod("GetCreator", BindingFlags.Static | BindingFlags.Public);
if (method == null) continue; if (method == null) continue;
var creator = (EventCallbackCreate)method.Invoke(null, new object[] { }); var creator = (EventCallbackCreate)method.Invoke(null, new object[] { });
_dict.Add(type.Name, creator); _dict.Add(type.Name, creator);
} }
} }
public BehaviorTreePoolItem Create(GameEntity entity, string name) public BehaviorTreePoolItem CreateAIEntity(GameEntity entity, string name)
{ {
if (!_dict.ContainsKey(name)) if (!_dict.ContainsKey(name))
{ {
@ -40,7 +40,7 @@ namespace Game
item.AIObject = _dict[name](); item.AIObject = _dict[name]();
item.AIObject.Create(); item.AIObject.Create();
}); });
ret.AIObject.Reset(entity); ((IAIEntity)ret.AIObject).Reset(entity);
return ret; return ret;
} }
} }

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 74d9b9671dc9421b9034b1cc8da51c53
timeCreated: 1685467902

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 837ddc9b1e71476586fae46b0718215c
timeCreated: 1685467862

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using CleverCrow.Fluid.BTs.Trees;
using UnityEngine;
using UnityEngine.Timeline;
namespace Game
{
public interface IAIEntity
{
void Reset(GameEntity entity);
}
public partial class AIEntityBase<T> : AIObjectBase<T>, IAIEntity where T : new()
{
private GameEntity _entity;
public void Reset(GameEntity entity)
{
_entity = entity;
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 552291c8ded048dc9addebeba764a4df
timeCreated: 1685468018

@ -1,11 +1,12 @@
namespace Game namespace Game
{ {
public class AIMonster01 : AIObjectBase<AIMonster01> public class AIMonster01 : AIEntityBase<AIMonster01>
{ {
protected override void OnCreate() protected override void OnCreate()
{ {
Sequence(() => Sequence(() =>
{ {
CheckStagger();
FindTarget(); FindTarget();
Selector(() => Selector(() =>
{ {

@ -1,6 +1,6 @@
namespace Game namespace Game
{ {
public partial class AIObjectBase<T> where T : new() public partial class AIEntityBase<T> : AIObjectBase<T> where T : new()
{ {
protected void FindTarget() protected void FindTarget()
{ {
@ -16,6 +16,11 @@
}); });
} }
protected void CheckStagger()
{
Condition($"检查硬直状态", () => !Util.EntityIsStagger(_entity));
}
protected void CheckModuleType(EAIModuleType moduleType) protected void CheckModuleType(EAIModuleType moduleType)
{ {
Condition($"检查模块类型-{moduleType}", () => _entity.aI.ModuleType.Value == moduleType); Condition($"检查模块类型-{moduleType}", () => _entity.aI.ModuleType.Value == moduleType);

@ -1,6 +1,6 @@
namespace Game namespace Game
{ {
public partial class AIObjectBase<T> where T : new() public partial class AIEntityBase<T> : AIObjectBase<T> where T : new()
{ {
protected void MoveTo() protected void MoveTo()
{ {

@ -82,10 +82,6 @@ namespace Game
public abstract partial class Util public abstract partial class Util
{ {
public static void RunBp(string name, string trigger, BlueprintBasicData data)
{
BlueprintPoolManager.Instance.Run(name, trigger, data);
}
public static void RunBp(string name, string trigger) public static void RunBp(string name, string trigger)
{ {
BlueprintPoolManager.Instance.Run(name, trigger, null); BlueprintPoolManager.Instance.Run(name, trigger, null);

@ -1,13 +1,16 @@
using Entitas; using Entitas;
using UnityEngine; using UnityEngine;
using Game; using Game;
public class AISystem : IExecuteSystem, IInitializeSystem public class AISystem : IExecuteSystem, IInitializeSystem
{ {
private IGroup<GameEntity> _entities; private IGroup<GameEntity> _entities;
public void Initialize() public void Initialize()
{ {
_entities = Util.GetGroup(GameMatcher.AI); _entities = Util.GetGroup(GameMatcher.AI);
} }
public void Execute() public void Execute()
{ {
foreach (var entity in _entities) foreach (var entity in _entities)
@ -25,14 +28,17 @@ public class AISystem : IExecuteSystem, IInitializeSystem
{ {
return; return;
} }
if (ai.IsHungryFull.Value) if (ai.IsHungryFull.Value)
{ {
return; return;
} }
if (ai.AttackPermit.Value) if (ai.AttackPermit.Value)
{ {
return; return;
} }
var newHungry = ai.Hungry.Value + ai.HungryIncrease * ai.HungryIncreaseRate * Time.deltaTime; var newHungry = ai.Hungry.Value + ai.HungryIncrease * ai.HungryIncreaseRate * Time.deltaTime;
ai.Hungry.Value = Mathf.Min(ai.HungryMax.Value, newHungry); ai.Hungry.Value = Mathf.Min(ai.HungryMax.Value, newHungry);
if (ai.Hungry.Value >= ai.HungryMax.Value) if (ai.Hungry.Value >= ai.HungryMax.Value)
@ -53,25 +59,23 @@ public class AISystem : IExecuteSystem, IInitializeSystem
{ {
return; return;
} }
if (!ai.IsInit) if (!ai.IsInit)
{ {
ai.IsInit = true; ai.IsInit = true;
} }
ai.ThinkCdNow -= Time.deltaTime; ai.ThinkCdNow -= Time.deltaTime;
if (ai.ThinkCdNow > 0) if (ai.ThinkCdNow > 0)
{ {
return; return;
} }
ai.ThinkCdNow = ai.ThinkCdMax; ai.ThinkCdNow = ai.ThinkCdMax;
if (Util.EntityIsStagger(entity))
{
return;
}
if (skill.IsRunning) if (skill.IsRunning)
{ {
return; return;
} }
ai.BehaviorTree.Tick(); ai.BehaviorTree.Tick();
} }
} }

@ -336,7 +336,7 @@ namespace Game
{ {
var ai = entity.aI; var ai = entity.aI;
var view = entity.view; var view = entity.view;
ai.BehaviorTree = BehaviorTreePoolManager.Instance.Create(entity, cfg.AIRes); ai.BehaviorTree = BehaviorTreePoolManager.Instance.CreateAIEntity(entity, cfg.AIRes);
var go = view.GameObject; var go = view.GameObject;
var goMonsterInfo = go.GetComponent<EntityMonsterInfo>(); var goMonsterInfo = go.GetComponent<EntityMonsterInfo>();
if (goMonsterInfo == null) if (goMonsterInfo == null)

@ -173,6 +173,8 @@ public class LevelPoolItem : ObjectPoolItemBase
}); });
} }
NextWave(); NextWave();
} }
@ -200,21 +202,16 @@ public class LevelPoolItem : ObjectPoolItemBase
{ {
foreach (var item in _levelData) foreach (var item in _levelData)
{ {
if (item is LevelNodeMonsterWave) if (!(item is LevelNodeMonsterWave itemLevelNodeMonsterWave)) continue;
if (itemLevelNodeMonsterWave.GetWaveIndex() != _waveNow) continue;
foreach (var monster in itemLevelNodeMonsterWave.GetMonsters())
{ {
var itemLevelNodeMonsterWave = item as LevelNodeMonsterWave; Util.CreateEntity(monster.GetCfgId(), (entity) => { entity.SetPos(monster.GetPos()); });
if (itemLevelNodeMonsterWave.GetWaveIndex() == _waveNow)
{
foreach (var monster in itemLevelNodeMonsterWave.GetMonsters())
{
Util.CreateEntity(monster.GetCfgId(), (entity) => { entity.SetPos(monster.GetPos()); });
}
isOk = true;
CurWave = itemLevelNodeMonsterWave;
break;
}
} }
isOk = true;
CurWave = itemLevelNodeMonsterWave;
break;
} }
if (isOk) if (isOk)

Loading…
Cancel
Save