Skip to content

Commit

Permalink
Added aknowldgments for all actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_pollard committed Nov 5, 2023
1 parent 8458a28 commit 18171a0
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.benpollarduk.ktvn.console

import com.github.benpollarduk.ktvn.example.ExampleCreator
import com.github.benpollarduk.ktvn.logic.Flags
import com.github.benpollarduk.ktvn.logic.listeners.defaultProviders.ConsoleListenerProvider
import com.github.benpollarduk.ktvn.logic.listeners.console.ConsoleListenerProvider
import com.github.benpollarduk.ktvn.logic.structure.Chapter
import com.github.benpollarduk.ktvn.logic.structure.ChapterListener
import com.github.benpollarduk.ktvn.logic.structure.Scene
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ExampleCreator(private val listeners: ListenerProvider) {
private val narrator = Narrator(
listeners.narrates,
listeners.asks,
listeners.acknowledges,
listeners.speaksAcknowledgement,
listeners.answers
)

Expand All @@ -42,7 +42,8 @@ public class ExampleCreator(private val listeners: ListenerProvider) {
listeners.speaks,
listeners.emotes,
listeners.asks,
listeners.acknowledges,
listeners.speaksAcknowledgement,
listeners.emotesAcknowledgement,
listeners.answers
)
}
Expand All @@ -53,7 +54,9 @@ public class ExampleCreator(private val listeners: ListenerProvider) {
scene name "Introduction"
scene background EmptyBackground()
scene type SceneType.Narrative
scene layout createLayout { }
scene layout createLayout {
it setMoveAcknowledgments listeners.movesAcknowledgement
}
scene steps listOf(
next { narrator narrates "Many years have passed since Michel moved in to the mansion." },
next { narrator narrates "Although Michel has remained amicable, the witch, Morgana, has not." }
Expand All @@ -70,6 +73,7 @@ public class ExampleCreator(private val listeners: ListenerProvider) {
layout addLeftOf michel
layout addRightOf morgana
layout setMoves listeners.moves
layout setMoveAcknowledgments listeners.movesAcknowledgement
}
scene steps listOf(
next { scene.layout moveLeft michel },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import com.github.benpollarduk.ktvn.logic.listeners.Emotes
import com.github.benpollarduk.ktvn.logic.listeners.Speaks

/**
* Provides a character with a specified [name]. Listeners for [speaks], [emotes], [asks], [acknowledges] and
* [answers] must be specified.
* Provides a character with a specified [name]. Listeners for [speaks], [emotes], [asks], [speaksAcknowledgment],
* [emotesAcknowledgment] and [answers] must be specified.
*/
@Suppress("LongParameterList")
public class Character(
public val name: String,
private val speaks: Speaks,
private val emotes: Emotes,
private val asks: Asks,
private val acknowledges: Acknowledges,
private val speaksAcknowledgment: Acknowledges,
private val emotesAcknowledgment: Acknowledges,
private val answers: Answers
) {
/**
Expand All @@ -31,14 +33,14 @@ public class Character(
*/
public infix fun looks(emotion: Emotion) {
this.emotion = emotion
emotes(this, emotion)
emotes(this, emotion, emotesAcknowledgment)
}

/**
* Say a [line].
*/
public infix fun says(line: String) {
speaks(this, line, acknowledges)
speaks(this, line, speaksAcknowledgment)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import com.github.benpollarduk.ktvn.logic.listeners.Asks
import com.github.benpollarduk.ktvn.logic.listeners.Narrates

/**
* Provides a narrator. Listeners for [narrates], [asks], [acknowledges] and [answers] must be specified.
* Provides a narrator. Listeners for [narrates], [asks], [speaksAcknowledges] and [answers] must be specified.
*/
public class Narrator(
private val narrates: Narrates,
private val asks: Asks,
private val acknowledges: Acknowledges,
private val speaksAcknowledgment: Acknowledges,
private val answers: Answers
) {
/**
* Narrate a [line].
*/
public infix fun narrates(line: String) {
narrates(line, acknowledges)
narrates(line, speaksAcknowledgment)
}

/**
Expand Down
22 changes: 20 additions & 2 deletions ktvn/src/main/kotlin/com/github/benpollarduk/ktvn/layout/Layout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.benpollarduk.ktvn.layout.Positions.left
import com.github.benpollarduk.ktvn.layout.Positions.leftOf
import com.github.benpollarduk.ktvn.layout.Positions.right
import com.github.benpollarduk.ktvn.layout.Positions.rightOf
import com.github.benpollarduk.ktvn.logic.listeners.Acknowledges
import com.github.benpollarduk.ktvn.logic.listeners.Moves

/**
Expand All @@ -17,7 +18,17 @@ import com.github.benpollarduk.ktvn.logic.listeners.Moves
public class Layout private constructor(setup: (Layout) -> Unit) {
private val mutablePositions: MutableList<CharacterPosition> = mutableListOf()
private var moves: Moves = object : Moves {
override fun invoke(character: Character, fromPosition: Position, toPosition: Position) {
override fun invoke(
character: Character,
fromPosition: Position,
toPosition: Position,
acknowledgement: Acknowledges
) {
// nothing
}
}
private var moveAcknowledgement: Acknowledges = object : Acknowledges {
override fun waitFor() {
// nothing
}
}
Expand All @@ -42,7 +53,7 @@ public class Layout private constructor(setup: (Layout) -> Unit) {
val fromPosition = current?.position ?: left
mutablePositions.removeAll { it.character == character }
mutablePositions.add(CharacterPosition(character, position))
moves(character, fromPosition, position)
moves(character, fromPosition, position, moveAcknowledgement)
}

/**
Expand All @@ -52,6 +63,13 @@ public class Layout private constructor(setup: (Layout) -> Unit) {
this.moves = moves
}

/**
* Specify how move acknowledgments should be handled.
*/
public infix fun setMoveAcknowledgments(moveAcknowledgement: Acknowledges) {
this.moveAcknowledgement = moveAcknowledgement
}

/**
* Add a [character] at the left of position.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.github.benpollarduk.ktvn.characters.Emotion
*/
public interface Emotes {
/**
* Invoke the listener with a specified [character] and [emotion].
* Invoke the listener with a specified [character], [emotion] and [acknowledgement].
*/
public operator fun invoke(character: Character, emotion: Emotion)
public operator fun invoke(character: Character, emotion: Emotion, acknowledgement: Acknowledges)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ package com.github.benpollarduk.ktvn.logic.listeners
*/
public interface ListenerProvider {
/**
* Get the [Acknowledges] listener.
* Get the [Acknowledges] listener for emotes.
*/
public val acknowledges: Acknowledges
public val emotesAcknowledgement: Acknowledges

/**
* Get the [Acknowledges] listener for speaks.
*/
public val speaksAcknowledgement: Acknowledges

/**
* Get the [Acknowledges] listener for moves.
*/
public val movesAcknowledgement: Acknowledges

/**
* Get the [Answers] listener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import com.github.benpollarduk.ktvn.layout.Position
*/
public interface Moves {
/**
* Invoke the listener with a specified [character], [fromPosition] and [toPosition].
* Invoke the listener with a specified [character], [fromPosition], [toPosition] and [acknowledgement].
*/
public operator fun invoke(character: Character, fromPosition: Position, toPosition: Position)
public operator fun invoke(
character: Character,
fromPosition: Position,
toPosition: Position,
acknowledgement: Acknowledges
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.benpollarduk.ktvn.logic.listeners.defaultProviders
package com.github.benpollarduk.ktvn.logic.listeners.console

import com.github.benpollarduk.ktvn.characters.Character
import com.github.benpollarduk.ktvn.characters.Emotion
Expand Down Expand Up @@ -26,7 +26,19 @@ public object ConsoleListenerProvider : ListenerProvider {
print("\u001b[H\u001b[2J")
}

override val acknowledges: Acknowledges = object : Acknowledges {
override val emotesAcknowledgement: Acknowledges = object : Acknowledges {
override fun waitFor() {
// continue without acknowledgement
}
}

override val movesAcknowledgement: Acknowledges = object : Acknowledges {
override fun waitFor() {
// continue without acknowledgement
}
}

override val speaksAcknowledgement: Acknowledges = object : Acknowledges {
override fun waitFor() {
readln()
clear()
Expand Down Expand Up @@ -58,8 +70,9 @@ public object ConsoleListenerProvider : ListenerProvider {
}

override val emotes: Emotes = object : Emotes {
override fun invoke(character: Character, emotion: Emotion) {
override fun invoke(character: Character, emotion: Emotion, acknowledgement: Acknowledges) {
println("${character.name} looks $emotion.")
acknowledgement.waitFor()
}
}

Expand All @@ -71,8 +84,14 @@ public object ConsoleListenerProvider : ListenerProvider {
}

override val moves: Moves = object : Moves {
override fun invoke(character: Character, fromPosition: Position, toPosition: Position) {
println("${character.name} moves from $fromPosition to $toPosition.")
override fun invoke(
character: Character,
fromPosition: Position,
toPosition: Position,
acknowledgement: Acknowledges
) {
println("${character.name} moves from '$fromPosition' to '$toPosition'.")
acknowledgement.waitFor()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Chapter private constructor(setup: (Chapter) -> Unit) {
/**
* Get the name of this [Chapter].
*/
public var name: String = ""
public var name: String = "Chapter"
private set

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Scene private constructor(setup: (Scene) -> Unit) {
/**
* Get the name of this [Scene].
*/
public var name: String = ""
public var name: String = "Scene"
private set

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Choice private constructor(private val setup: (Choice) -> Unit) : S
private var script: (Flags) -> Answer = { answer { } }
private set

override var name: String = ""
override var name: String = "Choice"
private set

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Conditional private constructor(setup: (Conditional) -> Unit) : Ste
private var result: StepResult = StepResult.Continue
private set

override var name: String = ""
override var name: String = "Conditional"
private set

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class End private constructor(private val setup: (End) -> Unit) : Step {
private var ending: Int = 0
private set

override var name: String = ""
override var name: String = "End"
private set

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Then private constructor(private val setup: (Then) -> Unit) : Step
private var script: (Flags) -> Unit = { }
private set

override var name: String = ""
override var name: String = "Then"
private set

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CharacterTest {
}

private val emotes = object : Emotes {
override fun invoke(character: Character, emotion: Emotion) {
override fun invoke(character: Character, emotion: Emotion, acknowledgement: Acknowledges) {
// nothing
}
}
Expand Down Expand Up @@ -49,7 +49,7 @@ class CharacterTest {
@Test
fun `given a character named Test then name is assigned`() {
// Given
val character = Character("Test", speaks, emotes, asks, acknowledges, answers)
val character = Character("Test", speaks, emotes, asks, acknowledges, acknowledges, answers)

// Then
Assertions.assertEquals("Test", character.name)
Expand All @@ -58,7 +58,7 @@ class CharacterTest {
@Test
fun `given a character when assigning emotion then emotion is assigned`() {
// Given
val character = Character("", speaks, emotes, asks, acknowledges, answers)
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// Conditional
character looks happy
Expand Down

0 comments on commit 18171a0

Please sign in to comment.