diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacter.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacter.kt index 9754993..327828b 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacter.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacter.kt @@ -6,13 +6,16 @@ import com.github.benpollarduk.ktaf.assets.Item import com.github.benpollarduk.ktaf.assets.interaction.InteractWithItem import com.github.benpollarduk.ktaf.assets.interaction.InteractionEffect import com.github.benpollarduk.ktaf.assets.interaction.InteractionResult +import com.github.benpollarduk.ktaf.conversations.Converser /** * A playable character with the specified [identifier] and [description] and [items]. + * If the playable character can converse with a [Converser] [canConverse] should be set to true, else false. */ public class PlayableCharacter( override var identifier: Identifier, override var description: Description, + public val canConverse: Boolean = true, items: List = emptyList() ) : Character() { @@ -21,7 +24,7 @@ public class PlayableCharacter( } /** - * Trigger this [Player] to use the specified [item] on the specified [target]. + * Trigger this [PlayableCharacter] to use the specified [item] on the specified [target]. */ public fun useItem(item: Item, target: InteractWithItem): InteractionResult { val result = target.interact(item) @@ -42,10 +45,12 @@ public class PlayableCharacter( /** * A playable character with the specified [identifier] and [description] and [items]. + * If the playable character can converse with a [Converser] [canConverse] should be set to true, else false. */ public constructor( identifier: String, description: String, + canConverse: Boolean = true, items: List = emptyList() - ) : this(Identifier(identifier), Description(description), items) + ) : this(Identifier(identifier), Description(description), canConverse, items) } diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/commands/game/Talk.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/commands/game/Talk.kt index 3059f3c..d5a635a 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/commands/game/Talk.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/commands/game/Talk.kt @@ -12,6 +12,10 @@ import com.github.benpollarduk.ktaf.logic.Game */ internal class Talk(private val converser: Converser?) : Command { override fun invoke(game: Game): Reaction { + if (!game.player.canConverse) { + return Reaction(ReactionResult.ERROR, "Can't converse.") + } + if (converser == null) { return Reaction(ReactionResult.ERROR, "Can't converse.") } diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/interpretation/CharacterCommandInterpreter.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/interpretation/CharacterCommandInterpreter.kt index d849315..ad0646f 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/interpretation/CharacterCommandInterpreter.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/interpretation/CharacterCommandInterpreter.kt @@ -51,7 +51,7 @@ public class CharacterCommandInterpreter : Interpreter { val commands = mutableListOf() - if (game.overworld.currentRegion?.currentRoom?.characters?.any() == true) { + if (game.player.canConverse && game.overworld.currentRegion?.currentRoom?.characters?.any() == true) { commands.add(talkCommandHelp) } diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacterTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacterTest.kt index 83cdbbe..c077c67 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacterTest.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/assets/characters/PlayableCharacterTest.kt @@ -87,7 +87,7 @@ class PlayableCharacterTest { @Test fun `given a default instance when finding a visible item it has then instance is returned`() { // Given - val character = PlayableCharacter("", "", listOf(Item("Test", ""))) + val character = PlayableCharacter("", "", true, listOf(Item("Test", ""))) // When val result = character.findItem("Test") @@ -102,7 +102,7 @@ class PlayableCharacterTest { val item = Item("Test", "").apply { isPlayerVisible = false } - val character = PlayableCharacter("", "", listOf(item)) + val character = PlayableCharacter("", "", true, listOf(item)) // When val result = character.findItem("Test") @@ -117,7 +117,7 @@ class PlayableCharacterTest { val item = Item("Test", "").apply { isPlayerVisible = false } - val character = PlayableCharacter("", "", listOf(item)) + val character = PlayableCharacter("", "", true, listOf(item)) // When val result = character.findItem("Test", true) @@ -144,7 +144,7 @@ class PlayableCharacterTest { fun `given a default instance when giving an item it has then true is returned`() { // Given val item = Item("", "") - val character = PlayableCharacter("", "", listOf(item)) + val character = PlayableCharacter("", "", true, listOf(item)) val otherCharacter = NonPlayableCharacter("", "") // When @@ -158,7 +158,7 @@ class PlayableCharacterTest { fun `given a default instance when giving an item it has then item is dequired`() { // Given val item = Item("Test", "") - val character = PlayableCharacter("", "", listOf(item)) + val character = PlayableCharacter("", "", true, listOf(item)) val otherCharacter = NonPlayableCharacter("", "") // When @@ -173,7 +173,7 @@ class PlayableCharacterTest { fun `given a default instance when giving an item it has then other character has item`() { // Given val item = Item("Test", "") - val character = PlayableCharacter("", "", listOf(item)) + val character = PlayableCharacter("", "", true, listOf(item)) val otherCharacter = NonPlayableCharacter("", "") // When diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/game/TalkTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/game/TalkTest.kt index 85883bd..a91b41d 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/game/TalkTest.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/game/TalkTest.kt @@ -1,6 +1,7 @@ package com.github.benpollarduk.ktaf.command.game import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter +import com.github.benpollarduk.ktaf.assets.characters.PlayableCharacter import com.github.benpollarduk.ktaf.assets.interaction.ReactionResult import com.github.benpollarduk.ktaf.commands.game.Talk import com.github.benpollarduk.ktaf.conversations.Conversation @@ -22,6 +23,22 @@ class TalkTest { Assertions.assertEquals(ReactionResult.ERROR, result.result) } + @Test + fun `given player that can't converse when invoke then return error`() { + // Given + val converser = NonPlayableCharacter("", "") + val command = Talk(converser) + val game = GameTestHelper.getBlankGame().also { + it.changePlayer(PlayableCharacter("", "", false)) + } + + // When + val result = command.invoke(game) + + // Then + Assertions.assertEquals(ReactionResult.ERROR, result.result) + } + @Test fun `given dead converser when invoke then return error`() { // Given diff --git a/ktaf/src/test/resources/test.jar b/ktaf/src/test/resources/test.jar index a96644f..2735372 100644 Binary files a/ktaf/src/test/resources/test.jar and b/ktaf/src/test/resources/test.jar differ