+45
-14
lines changedFilter options
+45
-14
lines changed Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import com.almasb.fxgl.core.EngineService
10
10
import com.almasb.fxgl.core.asset.AssetLoaderService
11
11
import com.almasb.fxgl.core.collection.PropertyMap
12
12
import com.almasb.fxgl.core.util.EmptyRunnable
13
+
import com.almasb.fxgl.cutscene.dialogue.DialogueContext
13
14
import com.almasb.fxgl.cutscene.dialogue.DialogueGraph
14
15
import com.almasb.fxgl.cutscene.dialogue.DialogueScene
15
16
import com.almasb.fxgl.cutscene.dialogue.FunctionCallHandler
@@ -35,17 +36,26 @@ class CutsceneService : EngineService() {
35
36
scene.start(cutscene)
36
37
}
37
38
38
-
@JvmOverloads fun startDialogueScene(dialogueGraph: DialogueGraph, functionHandler: FunctionCallHandler = EmptyFunctionCallHandler, onFinished: Runnable = EmptyRunnable) {
39
+
@JvmOverloads fun startDialogueScene(
40
+
dialogueGraph: DialogueGraph,
41
+
context: DialogueContext = TemporaryContext(),
42
+
functionHandler: FunctionCallHandler = EmptyFunctionCallHandler,
43
+
onFinished: Runnable = EmptyRunnable) {
44
+
39
45
dialogueScene.gameVars = gameVars ?: throw IllegalStateException("Cannot start dialogue scene. The game is not initialized yet.")
40
46
dialogueScene.assetLoader = assetLoader
41
-
dialogueScene.start(dialogueGraph, functionHandler, onFinished)
47
+
dialogueScene.start(dialogueGraph, context, functionHandler, onFinished)
42
48
}
43
49
44
50
override fun onGameReady(vars: PropertyMap) {
45
51
gameVars = vars
46
52
}
47
53
}
48
54
55
+
private class TemporaryContext : DialogueContext {
56
+
override fun properties(): PropertyMap = PropertyMap()
57
+
}
58
+
49
59
private object EmptyFunctionCallHandler : FunctionCallHandler {
50
60
private val log = Logger.get(javaClass)
51
61
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
6
6
7
7
package com.almasb.fxgl.cutscene.dialogue
8
8
9
+
import com.almasb.fxgl.core.collection.PropertyMap
9
10
import com.almasb.fxgl.cutscene.dialogue.DialogueNodeType.*
10
11
import javafx.beans.property.SimpleStringProperty
11
12
import javafx.beans.property.StringProperty
@@ -24,6 +25,18 @@ fun interface FunctionCallHandler {
24
25
fun handle(functionName: String, args: Array<String>): Any
25
26
}
26
27
28
+
/**
29
+
* The context in which a dialogue is running.
30
+
* For example, a single NPC could be a context.
31
+
*/
32
+
fun interface DialogueContext {
33
+
34
+
/**
35
+
* @return property map that is local to the dialogue context
36
+
*/
37
+
fun properties(): PropertyMap
38
+
}
39
+
27
40
/* NODES */
28
41
29
42
sealed class DialogueNode(
Original file line number Diff line number Diff line change
@@ -171,11 +171,11 @@ class DialogueScene(private val sceneService: SceneService) : SubScene() {
171
171
animation2.startReverse()
172
172
}
173
173
174
-
fun start(dialogueGraph: DialogueGraph, functionHandler: FunctionCallHandler, onFinished: Runnable) {
174
+
fun start(dialogueGraph: DialogueGraph, context: DialogueContext, functionHandler: FunctionCallHandler, onFinished: Runnable) {
175
175
graph = dialogueGraph.copy()
176
176
this.functionHandler = functionHandler
177
177
this.onFinished = onFinished
178
-
dialogueScriptRunner = DialogueScriptRunner(gameVars, functionHandler)
178
+
dialogueScriptRunner = DialogueScriptRunner(gameVars, context.properties(), functionHandler)
179
179
180
180
// while graph has subdialogue nodes, expand
181
181
while (graph.nodes.any { it.value.type == SUBDIALOGUE }) {
Original file line number Diff line number Diff line change
@@ -14,14 +14,21 @@ import com.almasb.fxgl.logging.Logger
14
14
* @author Almas Baimagambetov (almaslvl@gmail.com)
15
15
*/
16
16
internal class DialogueScriptRunner(
17
+
18
+
/**
19
+
* Variables global to the game.
20
+
*/
17
21
private val gameVars: PropertyMap,
22
+
23
+
/**
24
+
* Variables local to the dialogue context.
25
+
*/
26
+
private val localVars: PropertyMap,
18
27
private val functionHandler: FunctionCallHandler
19
28
) {
20
29
21
30
private val log = Logger.get<DialogueScriptRunner>()
22
31
23
-
private val localVars = hashMapOf<String, Any>()
24
-
25
32
/**
26
33
* Given a [line], this function replaces all variables with their values.
27
34
*/
@@ -43,8 +50,8 @@ internal class DialogueScriptRunner(
43
50
var result = line
44
51
45
52
varNames.forEach {
46
-
if (it in localVars) {
47
-
val value = localVars[it]
53
+
if (localVars.exists(it)) {
54
+
val value = localVars.getValue<Any>(it)
48
55
result = result.replace("\$$it", value.toString())
49
56
50
57
} else if (gameVars.exists(it)) {
@@ -112,8 +119,8 @@ internal class DialogueScriptRunner(
112
119
113
120
if (tokens.size == 1) {
114
121
// check if this is a boolean variable -- the only valid variable to use here
115
-
if (funcName in localVars) {
116
-
return localVars[funcName]!!
122
+
if (localVars.exists(funcName)) {
123
+
return localVars.getValue(funcName)
117
124
}
118
125
119
126
if (gameVars.exists(funcName)) {
@@ -131,12 +138,13 @@ internal class DialogueScriptRunner(
131
138
val varName = line.substringBefore('=').trim()
132
139
val varValue = line.substringAfter('=').trim().toTypedValue()
133
140
134
-
if (varName in localVars) {
135
-
localVars[varName] = varValue
141
+
if (localVars.exists(varName)) {
142
+
localVars.setValue(varName, varValue)
136
143
} else if (gameVars.exists(varName)) {
137
144
gameVars.setValue(varName, varValue)
138
145
} else {
139
-
localVars[varName] = varValue
146
+
// does not exist anywhere, create locally
147
+
localVars.setValue(varName, varValue)
140
148
}
141
149
}
142
150
}
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ class DialogueScriptRunnerTest {
31
31
map.setValue("isAlive", true)
32
32
map.setValue("isSleeping", false)
33
33
34
-
runner = DialogueScriptRunner(map) { funcName, args ->
34
+
runner = DialogueScriptRunner(map, PropertyMap()) { funcName, args ->
35
35
36
36
var result: Any = ""
37
37
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4