feat(ai): IAiDefinition + AiRecipeSO 配方基类(含关闭域重载的缓存重置)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
using BaseGames.Core.Events;
|
||||
|
||||
namespace BaseGames.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// AI 配方资产基类。子类以序列化字段描述一张图,Build 里组装。
|
||||
/// 一个资产 = 一种敌人 AI;所有引用该资产的实例共享同一张 AiGraph(flyweight)。
|
||||
/// </summary>
|
||||
public abstract class AiRecipeSO : ScriptableObject, IAiDefinition
|
||||
{
|
||||
AiGraph _cached;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
// 项目已关闭 Domain Reload:SO 的运行时态在多次 Play 会话间会残留,
|
||||
// 改了配方再进 Play 仍用旧图。登记到统一的"进入 Play 前重置"通道。
|
||||
PlayModeResetHook.Register(ClearCache);
|
||||
}
|
||||
|
||||
void ClearCache() => _cached = null;
|
||||
|
||||
public AiGraph GetOrBuildGraph()
|
||||
{
|
||||
if (_cached == null)
|
||||
{
|
||||
var b = new BrainBuilder();
|
||||
Build(b);
|
||||
_cached = b.Build();
|
||||
}
|
||||
return _cached;
|
||||
}
|
||||
|
||||
protected abstract void Build(BrainBuilder b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b4ea7eede5a322448cfccec3eff2acf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -14,7 +14,7 @@ namespace BaseGames.AI
|
||||
/// 一种敌人 AI 的声明基类(唯一事实来源)。子类实现 Build 用 fluent 描述状态机。
|
||||
/// 构建结果 AiGraph 每类型只建一次、实例共享(flyweight)。
|
||||
/// </summary>
|
||||
public abstract class AiScript
|
||||
public abstract class AiScript : IAiDefinition
|
||||
{
|
||||
AiGraph _cached;
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"name": "BaseGames.AI",
|
||||
"rootNamespace": "BaseGames.AI",
|
||||
"references": [
|
||||
"BaseGames.Core"
|
||||
"BaseGames.Core",
|
||||
"BaseGames.Core.Events"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace BaseGames.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// 能产出共享 AiGraph 的东西。两个实现:
|
||||
/// AiScript(定制路径,写 C# 类)与 AiRecipeSO(配方路径,建资产)。
|
||||
/// </summary>
|
||||
public interface IAiDefinition
|
||||
{
|
||||
/// <summary>惰性构建并缓存共享图(flyweight:每个定义只建一次)。</summary>
|
||||
AiGraph GetOrBuildGraph();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdf86cde06fe7b8468b243b2bca11d22
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user