update template

This commit is contained in:
codetypes committed 2024-07-13 10:21:18 +08:00
1 parent 5b249c14c8
commit aa50a34738
2 files changed
+347 -351

No files matched your search

+1 -1
View File
@@ -103,7 +103,7 @@ const NodeInspector: FC = () => {
form.setFieldValue("disabled", data.disabled); form.setFieldValue("disabled", data.disabled);
form.setFieldValue("path", data.path); form.setFieldValue("path", data.path);
def.args?.forEach((v) => { def.args?.forEach((v) => {
form.setFieldValue(`args.${v.name}`, data.args?.[v.name]); form.setFieldValue(`args.${v.name}`, data.args?.[v.name] ?? v.default);
}); });
def.input?.forEach((_, i) => { def.input?.forEach((_, i) => {
form.setFieldValue(`input.${i}`, data.input?.[i]); form.setFieldValue(`input.${i}`, data.input?.[i]);
+346 -350
View File
@@ -1,319 +1,174 @@
type Status = "success" | "failure" | "running"; export const zhNodeDef = () => {
return JSON.stringify(
type Constructor<T = unknown> = new (...args: unknown[]) => T; [
{
interface TreeEnv {} name: "AlwaysFail",
interface Node {} type: "Decorator",
desc: "始终返回失败",
interface NodeDef { doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 不管子节点是否成功都返回`失败`\n",
name: string; },
type: "Action" | "Decorator" | "Condition" | "Composite"; {
desc: string; name: "AlwaysSuccess",
icon?: string; type: "Decorator",
color?: string; desc: "始终返回成功",
input?: string[]; doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 不管子节点是否成功都返回`成功`\n",
args?: { },
name: string; {
type: name: "Assert",
| "boolean" type: "Decorator",
| "boolean?" desc: "断言",
| "int" args: [
| "int?" {
| "float" name: "message",
| "float?" type: "string",
| "enum" desc: "消息",
| "enum?" },
| "string" ],
| "string?" doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 当子节点返回`失败`时,抛出异常\n+ 其余情况返回子节点的执行状态\n",
| "code" },
| "code?"; {
desc: string; name: "Calculate",
options?: { name: string; value: unknown }[]; type: "Action",
}[]; desc: "简单的数值公式计算",
output?: string[]; args: [
doc?: string; {
} name: "value",
type: "code",
abstract class Process { desc: "计算公式",
init(node: Node): void {} },
],
run(node: Node, env: TreeEnv): Status { output: ["计算结果"],
return "success"; doc: "+ 做简单的数值公式计算,返回结果到输出\n",
} },
{
abstract get descriptor(): NodeDef; name: "Check",
} type: "Condition",
desc: "检查True或False",
namespace zh { args: [
export namespace action { {
export class Clear extends Process { name: "value",
override get descriptor(): NodeDef { type: "code",
return { desc: "值",
},
],
doc: "+ 做简单数值公式判定,返回`成功`或`失败`\n",
},
{
name: "Clear", name: "Clear",
type: "Action", type: "Action",
desc: "清除变量", desc: "清除变量",
output: ["清除的变量名"], output: ["清除的变量名"],
}; },
} {
} name: "Concat",
export class Log extends Process {
override get descriptor(): NodeDef {
return {
name: "Log",
type: "Action", type: "Action",
desc: "打印日志", desc: "将两个输入合并为一个数组,并返回新数组",
args: [{ name: "message", type: "string", desc: "日志" }], input: ["数组1", "数组2"],
}; output: ["新数组"],
} doc: "+ 如果输入不是数组,则返回`失败`\n",
} },
{
export class Now extends Process { name: "Filter",
override get descriptor(): NodeDef {
return {
name: "Now",
type: "Action", type: "Action",
desc: "获取当前时间", desc: "返回满足条件的元素",
output: ["当前时间"], input: ["输入数组"],
}; output: ["变量", "新数组"],
} doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 遍历输入数组,将当前元素写入`变量`,满足条件的元素放入新数组\n+ 只有当新数组不为空时,才返回`成功`\n",
} },
{
export class Wait extends Process {
override get descriptor(): NodeDef {
return {
name: "Wait",
type: "Action",
desc: "等待",
input: ["等待时间?"],
args: [
{ name: "time", type: "float", desc: "等待时间" },
{ name: "random", type: "float?", desc: "随机范围" },
],
};
}
}
}
export namespace composite {
export class Foreach extends Process {
override get descriptor(): NodeDef {
return {
name: "ForEach", name: "ForEach",
type: "Composite", type: "Action",
desc: "遍历数组", desc: "遍历数组",
input: ["数组"], input: ["数组"],
output: ["变量"], output: ["变量"],
doc: ` doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 遍历输入数组,将当前元素写入`变量`\n+ 当子节点返回`失败`时,退出遍历并返回`失败`状态\n+ 执行完所有子节点后,返回`成功`",
+ 每次执行子节点前会设置当前遍历到的变量 },
+ 会执行所有子节点 {
+ 永远返回「成功」/「运行中」`, name: "Includes",
};
}
}
export class Loop extends Process {
override get descriptor(): NodeDef {
return {
name: "Loop",
type: "Composite",
desc: "循环执行",
input: ["循环次数?"],
args: [{ name: "count", type: "int", desc: "循环次数" }],
};
}
}
export class Parallel extends Process {
override get descriptor(): NodeDef {
return {
name: "Parallel",
type: "Composite",
desc: "并行执行",
doc: `执行所有子节点并返回「成功」/「运行中」`,
};
}
}
export class Selector extends Process {
override get descriptor(): NodeDef {
return {
name: "Selector",
type: "Composite",
desc: "选择执行",
doc: `
+ 一直往下执行,有子节点返回「成功」则返回「成功」
+ 若全部节点返回「失败」则返回「失败」
+ 子节点是或 (OR) 的关系`,
};
}
}
export class Sequence extends Process {
override get descriptor(): NodeDef {
return {
name: "Sequence",
type: "Composite",
desc: "顺序执行",
doc: `
+ 一直往下执行,只有当所有子节点都返回「成功」, 才返回「成功」
+ 子节点是与(AND)的关系`,
};
}
}
}
export namespace condition {
export class Check extends Process {
override get descriptor(): NodeDef {
return {
name: "Check",
type: "Condition", type: "Condition",
desc: "检查True或False", desc: "判断元素是否在数组中",
args: [{ name: "value", type: "code", desc: "值" }], input: ["数组", "元素"],
doc: ` doc: "+ 若输入的元素不合法,返回`失败`\n+ 只有数组包含元素时返回`成功`,否则返回`失败`\n",
+ 做简单数值公式判定,返回「成功」或「失败」`, },
}; {
} name: "Index",
} type: "Action",
export class IsNull extends Process { desc: "索引输入的数组或对象",
override get descriptor(): NodeDef { args: [
return { {
name: "idx",
type: "string",
desc: "索引",
},
],
input: ["输入目标", "索引?"],
output: ["输出目标"],
doc: "+ 合法元素不包括 `undefined` 和 `null`\n+ 只有索引到有合法元素时候才会返回`成功`,否则返回`失败`\n",
},
{
name: "Inverter",
type: "Decorator",
desc: "反转子节点运行结果",
doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 当子节点返回`成功`时返回`失败`\n+ 当子节点返回`失败`时返回`成功`\n",
},
{
name: "IsNull", name: "IsNull",
type: "Condition", type: "Condition",
desc: "判断变量是否不存在", desc: "判断变量是否不存在",
input: ["判断的变量"], input: ["判断的变量"],
}; },
} {
} name: "IsStatus",
export class NotNull extends Process {
override get descriptor(): NodeDef {
return {
name: "NotNull",
type: "Condition", type: "Condition",
desc: "判断变量是否存在", desc: "检查子节点状态",
input: ["判断的变量"], args: [
}; {
} name: "status",
} type: "enum",
} desc: "检查子节点的执行状态",
options: [
export namespace decorator { {
export class AlwaysFail extends Process { name: "成功",
override get descriptor(): NodeDef { value: "success",
return { },
name: "AlwaysFail", {
type: "Decorator", name: "失败",
desc: "始终返回失败", value: "failure",
doc: ` },
+ 只能有一个子节点,多个仅执行第一个 {
+ 不管子节点是否成功都返回「失败」`, name: "运行中",
}; value: "running",
} },
} ],
export class AlwaysSuccess extends Process { },
override get descriptor(): NodeDef { ],
return { doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 只有当子节点的执行状态与指定状态相同时才返回`成功`,其余返回失败\n+ 若子节点返回`运行中`状态,将中断子节点并清理子节点的执行栈",
name: "AlwaysSuccess", },
type: "Decorator", {
desc: "始终返回成功", name: "Let",
doc: ` type: "Action",
+ 只能有一个子节点,多个仅执行第一个 desc: "定义新的变量名",
+ 不管子节点是否成功都返回「成功」`, input: ["变量名"],
}; output: ["新变量名"],
} },
} {
export class Inverter extends Process {
override get descriptor(): NodeDef {
return {
name: "Inverter",
type: "Decorator",
desc: "反转子节点运行结果",
doc: `
+ 只能有一个子节点,多个仅执行第一个
+ 当子节点返回「成功」时返回「失败」
+ 当子节点返回「失败」时返回「成功」
+ 其余返回「运行中」`,
};
}
}
export class Once extends Process {
override get descriptor(): NodeDef {
return {
name: "Once",
type: "Decorator",
desc: "只执行一次",
doc: `
+ 只能有一个子节点,多个仅执行第一个
+ 被打断后该节点后的子节点依旧不会执行
+ 该节点执行后永远返回「成功」`,
};
}
}
export class RepeatUntilFailure extends Process {
override get descriptor(): NodeDef {
return {
name: "RepeatUntilFailure",
type: "Decorator",
desc: "一直尝试直到子节点返回失败",
input: ["最大循环次数?"],
args: [{ name: "maxLoop", type: "int?", desc: "最大循环次数" }],
doc: `
+ 只能有一个子节点,多个仅执行第一个
+ 只有当子节点返回「失败」时,才返回「成功」,其它情况返回「运行中」状态
+ 如果设定了尝试次数,超过指定次数则返回「失败」`,
};
}
}
export class RepeatUntilSuccess extends Process {
override get descriptor(): NodeDef {
return {
name: "RepeatUntilSuccess",
type: "Decorator",
desc: "一直尝试直到子节点返回成功",
input: ["最大循环次数?"],
args: [{ name: "maxLoop", type: "int?", desc: "最大循环次数" }],
doc: `
+ 只能有一个子节点,多个仅执行第一个
+ 只有当子节点返回「成功」时,才返回「成功」,其它情况返回「运行中」状态
+ 如果设定了尝试次数,超过指定次数则返回「失败」`,
};
}
}
export class Timeout extends Process {
override get descriptor(): NodeDef {
return {
name: "Timeout",
type: "Decorator",
desc: "超时",
args: [{ name: "time", type: "float", desc: "超时时间" }],
doc: `
+ 只能有一个子节点,多个仅执行第一个
+ 当子节点执行超时或返回「失败」时,返回「失败」
+ 其余情况返回子节点的执行状态
`,
};
}
}
const enum TreeEvent {
INTERRUPTED = "interrupted",
BEFORE_RUN = "beforeRun",
AFTER_RUN = "afterRun",
AFTER_RUN_SUCCESS = "afterRunSuccess",
AFTER_RUN_FAILURE = "afterRunFailure",
}
export class Listen extends Process {
override get descriptor(): NodeDef {
return {
name: "Listen", name: "Listen",
type: "Decorator", type: "Decorator",
desc: "侦听事件",
input: ["目标对象?"],
output: ["事件参数?", "事件目标?"],
args: [
{
name: "event",
type: "string",
desc: "事件",
},
],
doc: "+ 当事件触发时,执行第一个子节点,多个仅执行第一个\n+ 如果子节点返回 `运行中`,会中断执行并清理执行栈",
},
{
name: "ListenTree",
type: "Decorator",
desc: "侦听行为树事件", desc: "侦听行为树事件",
args: [ args: [
{ {
@@ -323,74 +178,215 @@ namespace zh {
options: [ options: [
{ {
name: "行为树被中断", name: "行为树被中断",
value: TreeEvent.INTERRUPTED, value: "interrupted",
}, },
{ {
name: "行为树开始执行前", name: "行为树开始执行前",
value: TreeEvent.BEFORE_RUN, value: "beforeRun",
}, },
{ {
name: "行为树执行完成后", name: "行为树执行完成后",
value: TreeEvent.AFTER_RUN, value: "afterRun",
}, },
{ {
name: "行为树执行成功后", name: "行为树执行成功后",
value: TreeEvent.AFTER_RUN_SUCCESS, value: "afterRunSuccess",
}, },
{ {
name: "行为树执行失败后", name: "行为树执行失败后",
value: TreeEvent.AFTER_RUN_FAILURE, value: "afterRunFailure",
},
],
},
],
doc: "+ 当事件触发时,执行第一个子节点,多个仅执行第一个\n+ 如果子节点返回 `运行中`,会中断执行并清理执行栈",
},
{
name: "Log",
type: "Action",
desc: "打印日志",
args: [
{
name: "message",
type: "string",
desc: "日志",
},
{
name: "level",
type: "enum",
desc: "日志级别",
default: "info",
options: [
{
name: "INFO",
value: "info",
},
{
name: "DEBUG",
value: "debug",
},
{
name: "WARN",
value: "warn",
},
{
name: "ERROR",
value: "error",
},
],
}, },
], ],
}, },
{ {
name: "event", name: "Loop",
type: "string?", type: "Action",
desc: "自定义事件", desc: "循环执行",
input: ["循环次数?"],
args: [
{
name: "count",
type: "int",
desc: "循环次数",
}, },
], ],
doc: ` doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 当子节点返回`失败`时,退出遍历并返回`失败`状态\n+ 执行完所有子节点后,返回`成功`\n",
+ 当事件触发时,执行第一个子节点,多个仅执行第一个 },
+ 如果子节点返回 「运行中」,会中断执行并清理执行栈`, {
}; name: "NotNull",
} type: "Condition",
} desc: "判断变量是否存在",
} input: ["判断的变量"],
} },
{
const toString = (nodes: Constructor<Process>[]) => { name: "Now",
const defs: NodeDef[] = []; type: "Action",
for (const cls of nodes) { desc: "获取当前时间",
defs.push(new cls().descriptor); output: ["当前时间"],
} },
defs.sort((a, b) => a.name.localeCompare(b.name)); {
let str = JSON.stringify(defs, null, 2); name: "Once",
str = str.replace(/"doc": "\\n +/g, '"doc": "'); type: "Decorator",
str = str.replace(/\\n +/g, "\\n"); desc: "只执行一次",
return str; doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 第一次执行完全部子节点时返回`成功`,之后永远返回`失败`",
}; },
{
export const zhNodeDef = () => { name: "Parallel",
return toString([ type: "Composite",
zh.action.Clear, desc: "并行执行",
zh.action.Log, doc: "+ 并行执行所有子节点\n+ 当有子节点返回`运行中`时,返回`运行中`状态\n+ 执行完所有子节点后,返回`成功`",
zh.action.Now, },
zh.action.Wait, {
zh.composite.Foreach, name: "Push",
zh.composite.Loop, type: "Action",
zh.composite.Parallel, desc: "向数组中添加元素",
zh.composite.Selector, input: ["数组", "元素"],
zh.composite.Sequence, doc: "+ 当变量`数组`不是数组类型时返回`失败`\n+ 其余返回`成功`\n",
zh.condition.Check, },
zh.condition.IsNull, {
zh.condition.NotNull, name: "Random",
zh.decorator.AlwaysFail, type: "Action",
zh.decorator.AlwaysSuccess, desc: "返回一个随机数",
zh.decorator.Inverter, input: ["最小值?", "最大值?"],
zh.decorator.Once, args: [
zh.decorator.RepeatUntilFailure, {
zh.decorator.RepeatUntilSuccess, name: "min",
zh.decorator.Timeout, type: "float",
zh.decorator.Listen, desc: "最小值",
]); },
{
name: "max",
type: "float",
desc: "最大值",
},
{
name: "floor",
type: "boolean?",
desc: "是否向下取整",
},
],
output: ["随机数"],
},
{
name: "RandomIndex",
type: "Action",
desc: "随机返回输入的其中一个!",
input: ["输入目标"],
output: ["随机目标"],
doc: "+ 合法元素不包括 `undefined` 和 `null`\n+ 在输入数组中,随机返回其中一个\n+ 当输入数组为空时,或者没有合法元素,返回`失败`\n",
},
{
name: "RepeatUntilFailure",
type: "Decorator",
desc: "一直尝试直到子节点返回失败",
input: ["最大循环次数?"],
args: [
{
name: "maxLoop",
type: "int?",
desc: "最大循环次数",
},
],
doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 只有当子节点返回`失败`时,才返回`成功`,其它情况返回`运行中`状态\n+ 如果设定了尝试次数,超过指定次数则返回`失败`",
},
{
name: "RepeatUntilSuccess",
type: "Decorator",
desc: "一直尝试直到子节点返回成功",
input: ["最大循环次数?"],
args: [
{
name: "maxLoop",
type: "int?",
desc: "最大循环次数",
},
],
doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 只有当子节点返回`成功`时,才返回`成功`,其它情况返回`运行中`状态\n+ 如果设定了尝试次数,超过指定次数则返回`失败`",
},
{
name: "Selector",
type: "Composite",
desc: "选择执行",
doc: "+ 一直往下执行,直到有子节点返回`成功`则返回`成功`\n+ 若全部节点返回`失败`则返回`失败`",
},
{
name: "Sequence",
type: "Composite",
desc: "顺序执行",
doc: "+ 一直往下执行,只有当所有子节点都返回`成功`, 才返回`成功`\n+ 若子节点返回`失败`,则直接返回`失败`状态\n+ 其余情况返回`运行中`状态\n",
},
{
name: "Timeout",
type: "Decorator",
desc: "超时",
args: [
{
name: "time",
type: "float",
desc: "超时时间",
},
],
doc: "+ 只能有一个子节点,多个仅执行第一个\n+ 当子节点执行超时或返回`失败`时,返回`失败`\n+ 其余情况返回子节点的执行状态\n",
},
{
name: "Wait",
type: "Action",
desc: "等待",
input: ["等待时间?"],
args: [
{
name: "time",
type: "float",
desc: "等待时间",
},
{
name: "random",
type: "float?",
desc: "随机范围",
},
],
},
],
null,
2
);
}; };