-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
160 lines (149 loc) · 4 KB
/
main.go
File metadata and controls
160 lines (149 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"log"
"conditional-narrative-engine/narrative"
)
func main() {
// Define narrative nodes and choices
nodes := []narrative.Node{
{
ID: "start",
Text: "You wake up in a mysterious forest. A path splits into two directions.",
Choices: []narrative.Choice{
{
ID: "go_left",
Text: "Go left, deeper into the forest.",
NextNodeID: "forest_left",
Events: []narrative.Event{ // Example event: add item
{
Type: narrative.InventoryEvent,
Payload: map[string]interface{}{
"action": "add",
"item": "rusty_sword",
"quantity": 1,
},
},
{
Type: narrative.QuestEvent,
Payload: map[string]interface{}{
"quest_id": "find_town",
"state": "started",
},
},
},
},
{
ID: "go_right",
Text: "Go right, towards a glimmer of light.",
NextNodeID: "forest_right",
Events: []narrative.Event{ // Example event: dialogue
{
Type: narrative.DialogueEvent,
Payload: map[string]interface{}{
"speaker": "narrator",
"message": "You feel a strange presence.",
},
},
},
},
},
},
{
ID: "forest_left",
Text: "You encounter a grumpy troll demanding a toll.",
Choices: []narrative.Choice{
{
ID: "fight_troll",
Text: "Fight the troll.",
NextNodeID: "troll_battle",
Conditions: []narrative.Condition{ // Example condition (not yet processed by engine)
{
Type: narrative.HasItemCondition,
Payload: map[string]interface{}{
"item": "rusty_sword",
},
},
},
Events: []narrative.Event{
{
Type: narrative.QuestEvent,
Payload: map[string]interface{}{
"quest_id": "defeat_troll",
"state": "active",
},
},
},
},
{
ID: "run_away",
Text: "Run away!",
NextNodeID: "start", // Back to start for now
},
},
},
{
ID: "forest_right",
Text: "You find a hidden glade with a magical spring.",
Choices: []narrative.Choice{},
},
{
ID: "troll_battle",
Text: "You bravely faced the troll! What will you do next?",
Choices: []narrative.Choice{
{
ID: "collect_loot",
Text: "Collect troll loot.",
NextNodeID: "forest_left", // Go back for simplicity
Events: []narrative.Event{
{
Type: narrative.InventoryEvent,
Payload: map[string]interface{}{
"action": "add",
"item": "troll_tooth",
"quantity": 3,
},
},
},
},
},
},
}
// Create a new narrative engine
engine, err := narrative.NewEngine(nodes, "start")
if err != nil {
log.Fatalf("Failed to create narrative engine: %v", err)
}
fmt.Println("--- Narrative Start ---")
// Game loop simulation
for {
currentNode := engine.GetCurrentNode()
fmt.Printf("\nNode: %s\nText: %s\n", currentNode.ID, currentNode.Text)
if len(currentNode.Choices) == 0 {
fmt.Println("End of this narrative path.")
break
}
fmt.Println("Choices:")
for _, choice := range currentNode.Choices {
fmt.Printf(" [%s] %s\n", choice.ID, choice.Text)
}
fmt.Print("Enter your choice ID: ")
var choiceID string
fmt.Scanln(&choiceID)
// Choose a path and get triggered events
_, triggeredEvents, err := engine.Choose(choiceID)
if err != nil {
fmt.Printf("Error choosing: %v. Please try again.\n", err)
continue
}
// Process triggered events (in a real game, this would dispatch to specific handlers)
if len(triggeredEvents) > 0 {
fmt.Println("--- Events Triggered ---")
for _, event := range triggeredEvents {
fmt.Printf(" Type: %s, Payload: %v\n", event.Type, event.Payload)
}
fmt.Println("------------------------")
}
}
fmt.Println("\n--- Narrative End ---")
}