2
0
Fork 0

行为树 类结构优化

master
cd 2 years ago
parent 010158a7a8
commit d69a45fbb2

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

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

@ -19,15 +19,15 @@ namespace Game
if (!interfaceType.IsAssignableFrom(type)) continue;
if (type.IsGenericType) continue;
if (type.IsInterface) continue;
if (type.BaseType == null) continue;
var method = type.BaseType.GetMethod("GetCreator", BindingFlags.Static | BindingFlags.Public);
if (type.BaseType?.BaseType == null) continue;
var method = type.BaseType.BaseType.GetMethod("GetCreator", BindingFlags.Static | BindingFlags.Public);
if (method == null) continue;
var creator = (EventCallbackCreate)method.Invoke(null, new object[] { });
_dict.Add(type.Name, creator);
}
}
public BehaviorTreePoolItem Create(GameEntity entity, string name)
public BehaviorTreePoolItem CreateAIEntity(GameEntity entity, string name)
{
if (!_dict.ContainsKey(name))
{
@ -40,7 +40,7 @@ namespace Game
item.AIObject = _dict[name]();
item.AIObject.Create();
});
ret.AIObject.Reset(entity);
((IAIEntity)ret.AIObject).Reset(entity);
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
{
public class AIMonster01 : AIObjectBase<AIMonster01>
public class AIMonster01 : AIEntityBase<AIMonster01>
{
protected override void OnCreate()
{
Sequence(() =>
{
CheckStagger();
FindTarget();
Selector(() =>
{

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

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

@ -82,10 +82,6 @@ namespace Game
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)
{
BlueprintPoolManager.Instance.Run(name, trigger, null);

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

@ -336,7 +336,7 @@ namespace Game
{
var ai = entity.aI;
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 goMonsterInfo = go.GetComponent<EntityMonsterInfo>();
if (goMonsterInfo == null)

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

Loading…
Cancel
Save