蓝图组合
将可复用的逻辑封装为片段:
import { createFragment } from '@esengine/blueprint';
const healthFragment = createFragment('HealthSystem', { inputs: [ { name: 'damage', type: 'number', internalNodeId: 'input1', internalPinName: 'value' } ], outputs: [ { name: 'isDead', type: 'boolean', internalNodeId: 'output1', internalPinName: 'value' } ], graph: { nodes: [...], connections: [...], variables: [...] }});import { createComposer, FragmentRegistry } from '@esengine/blueprint';
// 注册片段FragmentRegistry.instance.register('health', healthFragment);FragmentRegistry.instance.register('movement', movementFragment);
// 创建组合器const composer = createComposer('PlayerBlueprint');
// 添加片段到槽位composer.addFragment(healthFragment, 'slot1', { position: { x: 0, y: 0 } });composer.addFragment(movementFragment, 'slot2', { position: { x: 400, y: 0 } });
// 连接槽位composer.connect('slot1', 'onDeath', 'slot2', 'disable');
// 验证const validation = composer.validate();if (!validation.isValid) { console.error(validation.errors);}
// 编译成蓝图const blueprint = composer.compile();import { FragmentRegistry } from '@esengine/blueprint';
const registry = FragmentRegistry.instance;
// 注册片段registry.register('health', healthFragment);
// 获取片段const fragment = registry.get('health');
// 获取所有片段const allFragments = registry.getAll();
// 按类别获取const combatFragments = registry.getByCategory('combat');定义触发条件
Section titled “定义触发条件”import { TriggerCondition, TriggerDispatcher } from '@esengine/blueprint';
const lowHealthCondition: TriggerCondition = { type: 'comparison', left: { type: 'variable', name: 'health' }, operator: '<', right: { type: 'constant', value: 20 }};使用触发器分发器
Section titled “使用触发器分发器”const dispatcher = new TriggerDispatcher();
// 注册触发器dispatcher.register('lowHealth', lowHealthCondition, (context) => { context.triggerEvent('OnLowHealth');});
// 每帧评估dispatcher.evaluate(context);const complexCondition: TriggerCondition = { type: 'and', conditions: [ { type: 'comparison', left: { type: 'variable', name: 'health' }, operator: '<', right: { type: 'constant', value: 50 } }, { type: 'comparison', left: { type: 'variable', name: 'inCombat' }, operator: '==', right: { type: 'constant', value: true } } ]};片段最佳实践
Section titled “片段最佳实践”- 单一职责 - 每个片段只做一件事
- 清晰接口 - 输入输出引脚命名明确
- 文档注释 - 为片段添加描述和使用示例
- 版本控制 - 更新片段时注意向后兼容