Skip to content

Commit

Permalink
feat: dialogue nodes can now be copied
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Dec 2, 2023
1 parent 93fa54e commit 4b9214b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.almasb.fxgl.cutscene.dialogue

import com.almasb.fxgl.core.Copyable
import com.almasb.fxgl.core.collection.PropertyMap
import com.almasb.fxgl.cutscene.dialogue.DialogueNodeType.*
import javafx.beans.property.SimpleStringProperty
Expand Down Expand Up @@ -37,7 +38,7 @@ fun interface DialogueContext {
sealed class DialogueNode(
val type: DialogueNodeType,
text: String
) {
) : Copyable<DialogueNode> {

val textProperty: StringProperty = SimpleStringProperty(text)

Expand All @@ -54,17 +55,29 @@ sealed class DialogueNode(
}
}

class StartNode(text: String) : DialogueNode(START, text)
class StartNode(text: String) : DialogueNode(START, text) {
override fun copy(): StartNode = StartNode(text)
}

class EndNode(text: String) : DialogueNode(END, text)
class EndNode(text: String) : DialogueNode(END, text) {
override fun copy(): EndNode = EndNode(text)
}

class TextNode(text: String) : DialogueNode(TEXT, text)
class TextNode(text: String) : DialogueNode(TEXT, text) {
override fun copy(): TextNode = TextNode(text)
}

class SubDialogueNode(text: String) : DialogueNode(SUBDIALOGUE, text)
class SubDialogueNode(text: String) : DialogueNode(SUBDIALOGUE, text) {
override fun copy(): SubDialogueNode = SubDialogueNode(text)
}

class FunctionNode(text: String) : DialogueNode(FUNCTION, text)
class FunctionNode(text: String) : DialogueNode(FUNCTION, text) {
override fun copy(): FunctionNode = FunctionNode(text)
}

class BranchNode(text: String) : DialogueNode(BRANCH, text)
class BranchNode(text: String) : DialogueNode(BRANCH, text) {
override fun copy(): BranchNode = BranchNode(text)
}

class ChoiceNode(text: String) : DialogueNode(CHOICE, text) {

Expand All @@ -87,6 +100,17 @@ class ChoiceNode(text: String) : DialogueNode(CHOICE, text) {
*/
val lastOptionID: Int
get() = options.keys.maxOrNull() ?: -1

override fun copy(): ChoiceNode {
val copy = ChoiceNode(text)
options.forEach { (k, v) ->
copy.options[k] = SimpleStringProperty(v.value)
}
conditions.forEach { (k, v) ->
copy.conditions[k] = SimpleStringProperty(v.value)
}
return copy
}
}

/* EDGES */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package com.almasb.fxgl.cutscene.dialogue

import com.almasb.fxgl.cutscene.dialogue.DialogueNodeType.*
import javafx.beans.property.SimpleStringProperty
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -185,6 +185,53 @@ class DialogueGraphTest {
assertTrue(graph.containsNode(node1))
}

@Test
fun `Copy nodes`() {
listOf(
StartNode("TestText"),
EndNode("TestText"),
TextNode("TestText"),
SubDialogueNode("TestText"),
FunctionNode("TestText"),
BranchNode("TestText"),
ChoiceNode("TestText")
).forEach {
val copy = it.copy()
assertThat(it.text, `is`(copy.text))
assertThat(it.type, `is`(copy.type))
}
}

@Test
fun `Copy choice options`() {
val choice = ChoiceNode("Choice")
choice.options[0] = SimpleStringProperty("Choice A")
choice.options[1] = SimpleStringProperty("Choice B")
choice.options[2] = SimpleStringProperty("Choice C")

choice.conditions[0] = SimpleStringProperty("Condition A")
choice.conditions[1] = SimpleStringProperty("Condition B")
choice.conditions[2] = SimpleStringProperty("Condition C")

val copy = choice.copy()

assertThat(choice.lastOptionID, `is`(copy.lastOptionID))

// StringProperty has to be a deep copy, not shallow
assertThat(choice.options, `is`(not(copy.options)))
assertThat(choice.conditions, `is`(not(copy.conditions)))
assertThat(choice.options.size, `is`(copy.options.size))
assertThat(choice.conditions.size, `is`(copy.conditions.size))

choice.options.forEach { (k, v) ->
assertThat(v.value, `is`(copy.options[k]!!.value))
}

choice.conditions.forEach { (k, v) ->
assertThat(v.value, `is`(copy.conditions[k]!!.value))
}
}

@Test
fun `Copy`() {
val node1 = ChoiceNode("")
Expand Down

0 comments on commit 4b9214b

Please sign in to comment.