perf(ai): AiRuntime 热路径去装箱/弱表一次注册 + 补 OnExit/Entry OnEnter 测试

This commit is contained in:
2026-07-03 14:20:19 +08:00
parent cdd1053bec
commit 5ad26b16e8
2 changed files with 34 additions and 5 deletions
+12 -5
View File
@@ -31,6 +31,8 @@ namespace BaseGames.AI
void EnterInitial()
{
_byContext.Remove(_ctx); // 幂等:避免同一 ctx 重复键异常
_byContext.Add(_ctx, this);
_current = _graph.GetState(_graph.EntryState);
_timeInState = 0f;
_current.OnEnter?.Invoke(_ctx);
@@ -44,9 +46,6 @@ namespace BaseGames.AI
public void Tick(float dt)
{
_byContext.Remove(_ctx);
_byContext.Add(_ctx, this);
// IsControllable 让位门:受击/硬直/击飞时挂起,不推进决策也不跑 Tick。
if (!_ctx.Vitals.IsControllable)
{
@@ -66,13 +65,21 @@ namespace BaseGames.AI
bool TryTransition()
{
// 全局/父层转换优先(本任务只评估条件型;事件型在 Task 6 处理)
foreach (var t in _graph.GlobalTransitions)
var globals = _graph.GlobalTransitions;
for (int i = 0; i < globals.Count; i++)
{
var t = globals[i];
if (!t.IsEvent && t.Condition(_ctx))
return Switch(t);
}
foreach (var t in _current.Transitions)
var locals = _current.Transitions;
for (int i = 0; i < locals.Count; i++)
{
var t = locals[i];
if (!t.IsEvent && t.Condition(_ctx))
return Switch(t);
}
return false;
}