|
| 1 | +/** |
| 2 | + * AVG 对话树 — 节点解析器 |
| 3 | + * |
| 4 | + * 解析对话树节点,处理类型分发和执行动作。 |
| 5 | + */ |
| 6 | + |
| 7 | +import type { |
| 8 | + DialogueNode, |
| 9 | + DialogueAction, |
| 10 | + DialogueState, |
| 11 | + DialogueHistoryEntry, |
| 12 | +} from '../../../../models/avg/dialogueTree'; |
| 13 | +import { ConditionEvaluator } from './conditionEvaluator'; |
| 14 | +import type { GameContext } from './conditionEvaluator'; |
| 15 | + |
| 16 | +export interface NodeResolveResult { |
| 17 | + node: DialogueNode; |
| 18 | + state: DialogueState; |
| 19 | + actions: DialogueAction[]; |
| 20 | + availableChoices: { id: string; text: string; consequenceHint?: string }[]; |
| 21 | + historyEntry: DialogueHistoryEntry; |
| 22 | +} |
| 23 | + |
| 24 | +export class NodeResolver { |
| 25 | + private evaluator: ConditionEvaluator; |
| 26 | + |
| 27 | + constructor(context: GameContext) { |
| 28 | + this.evaluator = new ConditionEvaluator(context); |
| 29 | + } |
| 30 | + |
| 31 | + updateContext(context: Partial<GameContext>): void { |
| 32 | + this.evaluator.updateContext(context); |
| 33 | + } |
| 34 | + |
| 35 | + resolve( |
| 36 | + treeNodes: DialogueNode[], |
| 37 | + state: DialogueState, |
| 38 | + choiceId?: string |
| 39 | + ): NodeResolveResult | null { |
| 40 | + const nodeMap = this._buildNodeMap(treeNodes); |
| 41 | + const currentNode = nodeMap.get(state.currentNodeId); |
| 42 | + if (!currentNode) return null; |
| 43 | + |
| 44 | + if (currentNode.condition && !this.evaluator.evaluate(currentNode.condition)) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + const newState = { ...state, visitedNodeIds: [...state.visitedNodeIds, currentNode.id] }; |
| 49 | + let nextNodeId: string | undefined; |
| 50 | + |
| 51 | + if (currentNode.type === 'choice' && currentNode.choices) { |
| 52 | + const availableChoices = this.evaluator.evaluateChoices(currentNode.choices); |
| 53 | + |
| 54 | + if (choiceId) { |
| 55 | + const chosen = availableChoices.find((c) => c.id === choiceId); |
| 56 | + if (chosen) { |
| 57 | + nextNodeId = chosen.targetNodeId; |
| 58 | + this._executeActions(chosen.actions); |
| 59 | + newState.history = [ |
| 60 | + ...state.history, |
| 61 | + this._makeHistoryEntry(currentNode, choiceId), |
| 62 | + ]; |
| 63 | + } else { |
| 64 | + return null; |
| 65 | + } |
| 66 | + } else { |
| 67 | + newState.history = [ |
| 68 | + ...state.history, |
| 69 | + this._makeHistoryEntry(currentNode, undefined), |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + const simplifiedChoices = availableChoices.map((c) => ({ |
| 74 | + id: c.id, |
| 75 | + text: c.text, |
| 76 | + consequenceHint: c.consequenceHint, |
| 77 | + })); |
| 78 | + |
| 79 | + newState.currentNodeId = nextNodeId ?? currentNode.id; |
| 80 | + |
| 81 | + return { |
| 82 | + node: currentNode, |
| 83 | + state: newState, |
| 84 | + actions: choiceId ? (currentNode.choices?.find((c) => c.id === choiceId)?.actions ?? []) : [], |
| 85 | + availableChoices: simplifiedChoices, |
| 86 | + historyEntry: this._makeHistoryEntry(currentNode, choiceId), |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + if (currentNode.type === 'condition' && currentNode.choices) { |
| 91 | + const availableChoices = this.evaluator.evaluateChoices(currentNode.choices); |
| 92 | + if (availableChoices.length > 0) { |
| 93 | + const first = availableChoices[0]; |
| 94 | + nextNodeId = first.targetNodeId; |
| 95 | + this._executeActions(first.actions); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (currentNode.type === 'action') { |
| 100 | + this._executeActions(currentNode.actions); |
| 101 | + } |
| 102 | + |
| 103 | + if (currentNode.type === 'jump' && currentNode.nextNodeId) { |
| 104 | + nextNodeId = currentNode.nextNodeId; |
| 105 | + } |
| 106 | + |
| 107 | + if (!nextNodeId && currentNode.nextNodeId) { |
| 108 | + nextNodeId = currentNode.nextNodeId; |
| 109 | + } |
| 110 | + |
| 111 | + if (nextNodeId) { |
| 112 | + newState.currentNodeId = nextNodeId; |
| 113 | + } else if (!nextNodeId && !currentNode.choices && !currentNode.nextNodeId) { |
| 114 | + newState.isComplete = true; |
| 115 | + } |
| 116 | + |
| 117 | + this._executeActions(currentNode.actions); |
| 118 | + |
| 119 | + return { |
| 120 | + node: currentNode, |
| 121 | + state: newState, |
| 122 | + actions: currentNode.actions, |
| 123 | + availableChoices: currentNode.choices |
| 124 | + ? this.evaluator.evaluateChoices(currentNode.choices).map((c) => ({ |
| 125 | + id: c.id, |
| 126 | + text: c.text, |
| 127 | + consequenceHint: c.consequenceHint, |
| 128 | + })) |
| 129 | + : [], |
| 130 | + historyEntry: this._makeHistoryEntry(currentNode, choiceId), |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + private _buildNodeMap(nodes: DialogueNode[]): Map<string, DialogueNode> { |
| 135 | + const map = new Map<string, DialogueNode>(); |
| 136 | + nodes.forEach((node) => map.set(node.id, node)); |
| 137 | + return map; |
| 138 | + } |
| 139 | + |
| 140 | + private _executeActions(actions: DialogueAction[]): void { |
| 141 | + for (const action of actions) { |
| 142 | + this.evaluator.updateContext(this._actionToContext(action)); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private _actionToContext(action: DialogueAction): Partial<GameContext> { |
| 147 | + switch (action.type) { |
| 148 | + case 'intimacy_change': |
| 149 | + return { intimacy: { [action.target]: action.value as number } }; |
| 150 | + case 'flag_set': |
| 151 | + return { flags: { [action.target]: action.value as boolean } }; |
| 152 | + case 'item_change': |
| 153 | + return { items: [action.value as string] }; |
| 154 | + case 'task_update': |
| 155 | + return { tasks: { [action.target]: action.value as string } }; |
| 156 | + default: |
| 157 | + return {}; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private _makeHistoryEntry(node: DialogueNode, chosenChoiceId?: string): DialogueHistoryEntry { |
| 162 | + return { |
| 163 | + nodeId: node.id, |
| 164 | + speaker: node.speaker, |
| 165 | + text: node.text, |
| 166 | + chosenChoiceId, |
| 167 | + timestamp: Date.now(), |
| 168 | + }; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +export function createNodeResolver(context: GameContext): NodeResolver { |
| 173 | + return new NodeResolver(context); |
| 174 | +} |
0 commit comments