Description:
Refactor dialog into separate Engine.
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r404:93578fea7b5a -

@@ -0,0 +1,122
1 using System;
2 using System.Text.RegularExpressions;
3 using System.Linq;
4
5 using Encompass;
6 using Ink.Runtime;
7 using TraceryNet;
8
9 using isometricparkfna.Messages;
10 using isometricparkfna.Components;
11
12 namespace isometricparkfna.Engines
13 {
14
15 [Receives(typeof(SetDialogMessage),
16 typeof(DialogChoiceMessage))]
17 [Reads(typeof(DialogComponent)) ]
18 [Writes( typeof(DialogComponent))]
19 class DialogEngine : Engine
20 {
21 public Story Story;
22 public Grammar Grammar;
23
24 public DialogEngine(Story story, Grammar grammar)
25 {
26 this.Story = story;
27 this.Grammar = grammar;
28 }
29
30 public override void Update(double dt)
31 {
32
33 foreach (ref readonly var dialogMessage in ReadMessages<SetDialogMessage>())
34 {
35 if (dialogMessage.newOption != null)
36 {
37 // SetComponent(dialogMessage.Entity,
38 // new DialogComponent {CurrentDialog = dialogMessage.newOption
39 // });
40 }
41 else
42 {
43 Destroy(dialogMessage.Entity);
44
45 }
46 }
47
48 foreach (ref readonly var choiceMessage in ReadMessages<DialogChoiceMessage>())
49 {
50
51 if (Story.currentChoices.Count > 0)
52 {
53 //Advance the story
54 Logging.Debug("Advancing story.");
55 Story.ChooseChoiceIndex(choiceMessage.Choice);
56
57 //Update the dialog component with the new speaker, dialog, and options:
58 var continuation = this.Grammar.Flatten(this.Story.ContinueMaximally());
59 var parts = Regex.Split(continuation, ":", 0);
60 var speaker = (parts.Length == 2) ? this.Grammar.Flatten(parts[0]) : "Dialog";
61 var dialog = (parts.Length == 2) ? this.Grammar.Flatten(parts[1]) : continuation;
62
63 SetComponent(choiceMessage.Entity, new DialogComponent {
64 CurrentDialog = dialog,
65 CurrentSpeaker = speaker,
66 Options = this.Story.currentChoices
67 .Select(option => option.text)
68 .ToList()});}
69 else
70 {
71 Logging.Debug(String.Format("Destroying Dialog (Entity ID {0}).", choiceMessage.Entity.ID));
72 Destroy(choiceMessage.Entity);
73
74 var lowestID = Int32.MaxValue;
75 Entity lowestEntity = default;
76
77
78 foreach(ref var entity in ReadEntities<DialogComponent>())
79 {
80 if ((entity.ID < lowestID)
81 //Don't try to use entity we just destroyed:
82 && (entity.ID != choiceMessage.Entity.ID)
83
84 && (!GetComponent<DialogComponent>(entity).Hydrated))
85 {
86 lowestID = entity.ID;
87 lowestEntity = entity;
88 Logging.Spy(lowestID, "ID");
89 }
90 }
91
92 if (lowestID != Int32.MaxValue
93 && EntityExists(lowestEntity))
94 {
95 Logging.Debug("Hydrating Dialog.");
96
97
98 var Knot = GetComponent<DialogComponent>(lowestEntity).Knot;
99
100 Story.ChoosePathString(Knot);
101 var continuation = this.Story.ContinueMaximally();
102 var parts = Regex.Split(continuation, ":", 0);
103 var speaker = (parts.Length == 2) ? this.Grammar.Flatten(parts[0]) : "Dialog";
104 var dialog = (parts.Length == 2) ? this.Grammar.Flatten(parts[1]) : continuation;
105 SetComponent(lowestEntity, new DialogComponent {
106 Knot = Knot,
107 CurrentDialog = dialog,
108 CurrentSpeaker = speaker,
109 Options = this.Story.currentChoices
110 .Select(option => option.text)
111 .ToList()});
112
113 }
114 }
115
116
117
118
119 }
120 }
121 }
122 }
@@ -1,9 +1,9
1
1
2 VAR GovernorOpinion = 0
2 VAR GovernorOpinion = 0
3
3
4 //-> IntroGovernor
4 //This is needed to make both including and for jumpting to dialog sections work:
5 //-> IntroAssistant
5 === placeholder ===
6 //This is a test.
6
7 -> END
7 -> END
8
8
9 === Once ===
9 === Once ===
@@ -30,7 +30,7
30
30
31
31
32 Governor: Ummm, yeah
32 Governor: Ummm, yeah
33 * * * \...
33 * * * [\...]
34 -> END
34 -> END
35 * * [Sounds good!]
35 * * [Sounds good!]
36 ~ GovernorOpinion = GovernorOpinion + 1
36 ~ GovernorOpinion = GovernorOpinion + 1
@@ -45,11 +45,11
45 === IntroAssistant ===
45 === IntroAssistant ===
46
46
47 \#assistantName\#: \#whatever\#
47 \#assistantName\#: \#whatever\#
48 * Hi
48 * [Hi]
49
49
50 \#assistantName\#: Bye
50 \#assistantName\#: Bye
51 -> END
51 -> END
52 * How are you?
52 * [How are you?]
53
53
54 \#assistantName\#: \#howdoing\#
54 \#assistantName\#: \#howdoing\#
55 -> END No newline at end of file
55 -> END
@@ -1,1 +1,1
1 {"inkVersion":20,"root":[["end",["done",{"#f":5,"#n":"g-0"}],null],"done",{"Once":[["^Once upon a time...","\n",["ev",{"^->":"Once.0.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":18},{"s":["^There were two choices.",{"->":"$r","var":true},null]}],["ev",{"^->":"Once.0.3.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-1","flg":18},{"s":["^There were four lines of content.",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"Once.0.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.2.s"},[{"#n":"$r2"}],"\n",{"->":".^.^.g-0"},{"#f":5}],"c-1":["ev",{"^->":"Once.0.c-1.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.3.s"},[{"#n":"$r2"}],"\n",{"->":".^.^.g-0"},{"#f":5}],"g-0":["^They lived happily ever after.","\n","end",{"#f":5}]}],{"#f":1}],"IntroGovernor":[["^Governor: Welcome to your new park, director! You can use the mouse or arrow keys to move around, and the plus and minus keys to zoom in and out. B opens the budget and F lets you adjust Forest Policy.","\n","ev","str","^Okay","/str","/ev",{"*":".^.c-0","flg":20},{"c-0":["\n","^Governor: Make sure that you keep visitors happy and the budget in the black! You're currently getting an annual grant out of my budget—it'd sure be nice if you park were self-sufficient so we could drop that expense!","\n",["ev","str","^And I need to keep the forest healthy, too, right?","/str","/ev",{"*":".^.c-0","flg":20},"ev","str","^Sounds good!","/str","/ev",{"*":".^.c-1","flg":20},{"c-0":["^ ","\n","ev",{"VAR?":"GovernorOpinion"},1,"-","/ev",{"VAR=":"GovernorOpinion","re":true},"^Governor: Ummm, yeah","\n",[["ev",{"^->":"IntroGovernor.0.c-0.3.c-0.10.0.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":18},{"s":["^...",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"IntroGovernor.0.c-0.3.c-0.10.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.0.s"},[{"#n":"$r2"}],"\n","end",{"#f":5}]}],{"#f":5}],"c-1":["\n","ev",{"VAR?":"GovernorOpinion"},1,"+","/ev",{"VAR=":"GovernorOpinion","re":true},"^Governor: I'll check in soon.","\n","end",{"#f":5}]}],{"#f":5}]}],{"#f":1}],"IntroAssistant":[["^#assistantName#: #whatever#","\n","ev","str","^Hi","/str","/ev",{"*":".^.c-0","flg":20},"ev","str","^How are you?","/str","/ev",{"*":".^.c-1","flg":20},{"c-0":["\n","^#assistantName#: Bye","\n","end",{"#f":5}],"c-1":["\n","^#assistantName#: #howdoing#","\n","end",{"#f":5}]}],{"#f":1}],"global decl":["ev",0,{"VAR=":"GovernorOpinion"},"/ev","end",null],"#f":1}],"listDefs":{}} No newline at end of file
1 {"inkVersion":20,"root":[[["done",{"#f":5,"#n":"g-0"}],null],"done",{"placeholder":["end",{"#f":1}],"Once":[["^Once upon a time...","\n",["ev",{"^->":"Once.0.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":18},{"s":["^There were two choices.",{"->":"$r","var":true},null]}],["ev",{"^->":"Once.0.3.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-1","flg":18},{"s":["^There were four lines of content.",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"Once.0.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.2.s"},[{"#n":"$r2"}],"\n",{"->":".^.^.g-0"},{"#f":5}],"c-1":["ev",{"^->":"Once.0.c-1.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.3.s"},[{"#n":"$r2"}],"\n",{"->":".^.^.g-0"},{"#f":5}],"g-0":["^They lived happily ever after.","\n","end",{"#f":5}]}],{"#f":1}],"IntroGovernor":[["^Governor: Welcome to your new park, director! You can use the mouse or arrow keys to move around, and the plus and minus keys to zoom in and out. B opens the budget and F lets you adjust Forest Policy.","\n","ev","str","^Okay","/str","/ev",{"*":".^.c-0","flg":20},{"c-0":["\n","^Governor: Make sure that you keep visitors happy and the budget in the black! You're currently getting an annual grant out of my budget—it'd sure be nice if you park were self-sufficient so we could drop that expense!","\n",["ev","str","^And I need to keep the forest healthy, too, right?","/str","/ev",{"*":".^.c-0","flg":20},"ev","str","^Sounds good!","/str","/ev",{"*":".^.c-1","flg":20},{"c-0":["^ ","\n","ev",{"VAR?":"GovernorOpinion"},1,"-","/ev",{"VAR=":"GovernorOpinion","re":true},"^Governor: Ummm, yeah","\n",["ev","str","^...","/str","/ev",{"*":".^.c-0","flg":20},{"c-0":["\n","end",{"#f":5}]}],{"#f":5}],"c-1":["\n","ev",{"VAR?":"GovernorOpinion"},1,"+","/ev",{"VAR=":"GovernorOpinion","re":true},"^Governor: I'll check in soon.","\n","end",{"#f":5}]}],{"#f":5}]}],{"#f":1}],"IntroAssistant":[["^#assistantName#: #whatever#","\n","ev","str","^Hi","/str","/ev",{"*":".^.c-0","flg":20},"ev","str","^How are you?","/str","/ev",{"*":".^.c-1","flg":20},{"c-0":["\n","^#assistantName#: Bye","\n","end",{"#f":5}],"c-1":["\n","^#assistantName#: #howdoing#","\n","end",{"#f":5}]}],{"#f":1}],"global decl":["ev",0,{"VAR=":"GovernorOpinion"},"/ev","end",null],"#f":1}],"listDefs":{}} No newline at end of file
@@ -26,17 +26,14
26 typeof(VisibilityComponent),
26 typeof(VisibilityComponent),
27 typeof(SelectedComponent))]
27 typeof(SelectedComponent))]
28 [Writes(typeof(VisibilityComponent),
28 [Writes(typeof(VisibilityComponent),
29 typeof(SelectedComponent),
29 typeof(SelectedComponent))]
30 typeof(DialogComponent))]
31 class UIEngine : Engine
30 class UIEngine : Engine
32 {
31 {
33 public Story Story;
32 public Story Story;
34 public Grammar grammar;
35
33
36 public UIEngine(Story story, Grammar grammar)
34 public UIEngine(Story story)
37 {
35 {
38 this.Story = story;
36 this.Story = story;
39 this.grammar = grammar;
40 }
37 }
41
38
42 public override void Update(double dt)
39 public override void Update(double dt)
@@ -101,93 +98,7
101 new SelectedComponent { selected = true });
98 new SelectedComponent { selected = true });
102 }
99 }
103
100
104 foreach (ref readonly var dialogMessage in ReadMessages<SetDialogMessage>())
105 {
106 if (dialogMessage.newOption != null)
107 {
108 // SetComponent(dialogMessage.Entity,
109 // new DialogComponent {CurrentDialog = dialogMessage.newOption
110 // });
111 }
112 else
113 {
114 Destroy(dialogMessage.Entity);
115
101
116 }
117 }
118
119 foreach (ref readonly var choiceMessage in ReadMessages<DialogChoiceMessage>())
120 {
121
122 if (Story.currentChoices.Count > 0)
123 {
124 //Advance the story
125 Logging.Debug("Advancing story.");
126 Story.ChooseChoiceIndex(choiceMessage.Choice);
127
128 //Update the dialog component with the new speaker, dialog, and options:
129 var continuation = this.Story.ContinueMaximally();
130 var parts = Regex.Split(continuation, ":", 0);
131 var speaker = (parts.Length == 2) ? this.grammar.Flatten(parts[0]) : "Dialog";
132 var dialog = (parts.Length == 2) ? this.grammar.Flatten(parts[1]) : continuation;
133
134 SetComponent(choiceMessage.Entity, new DialogComponent {
135 CurrentDialog = dialog,
136 CurrentSpeaker = speaker,
137 Options = this.Story.currentChoices
138 .Select(option => option.text)
139 .ToList()});}
140 else
141 {
142 Logging.Debug(String.Format("Destroying Dialog (Entity ID {0}).", choiceMessage.Entity.ID));
143 Destroy(choiceMessage.Entity);
144
145 var lowestID = Int32.MaxValue;
146 Entity lowestEntity = default;
147
148
149 foreach(ref var entity in ReadEntities<DialogComponent>())
150 {
151 if ((entity.ID < lowestID)
152 //Don't try to use entity we just destroyed:
153 && (entity.ID != choiceMessage.Entity.ID)
154
155 && (!GetComponent<DialogComponent>(entity).Hydrated))
156 {
157 lowestID = entity.ID;
158 lowestEntity = entity;
159 Logging.Spy(lowestID, "ID");
160 }
161 }
162
163 if (lowestID != Int32.MaxValue
164 && EntityExists(lowestEntity))
165 {
166 Logging.Debug("Hydrating Dialog.");
167
168
169 var Knot = GetComponent<DialogComponent>(lowestEntity).Knot;
170
171 Story.ChoosePathString(Knot);
172 var continuation = this.Story.ContinueMaximally();
173 var parts = Regex.Split(continuation, ":", 0);
174 var speaker = (parts.Length == 2) ? this.grammar.Flatten(parts[0]) : "Dialog";
175 var dialog = (parts.Length == 2) ? this.grammar.Flatten(parts[1]) : continuation;
176 SetComponent(lowestEntity, new DialogComponent {
177 Knot = Knot,
178 CurrentDialog = dialog,
179 CurrentSpeaker = speaker,
180 Options = this.Story.currentChoices
181 .Select(option => option.text)
182 .ToList()});
183
184 }
185 }
186
187
188
189
190 }
191 }
102 }
192 }
103 }
193 }
104 }
@@ -249,7 +249,8
249 Logging.Debug(this.Story.ContinueMaximally());
249 Logging.Debug(this.Story.ContinueMaximally());
250
250
251 WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm));
251 WorldBuilder.AddEngine(new InputEngine(Menu.MENU_BAR_HEIGHT, this.camera, gdm));
252 WorldBuilder.AddEngine(new UIEngine(this.Story, this.grammar));
252 WorldBuilder.AddEngine(new UIEngine(this.Story));
253 WorldBuilder.AddEngine(new DialogEngine(this.Story, this.grammar));
253
254
254 var gameBridgeEngine = new GameBridgeEngine(this);
255 var gameBridgeEngine = new GameBridgeEngine(this);
255
256
You need to be logged in to leave comments. Login now