Skip to content

Commit

Permalink
Added canConverse property to PlayableCharacter
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Oct 25, 2024
1 parent 1063ea7 commit 61003d6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> = emptyList()
) : Character() {

Expand All @@ -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)
Expand All @@ -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<Item> = emptyList()
) : this(Identifier(identifier), Description(description), items)
) : this(Identifier(identifier), Description(description), canConverse, items)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class CharacterCommandInterpreter : Interpreter {

val commands = mutableListOf<CommandHelp>()

if (game.overworld.currentRegion?.currentRoom?.characters?.any() == true) {
if (game.player.canConverse && game.overworld.currentRegion?.currentRoom?.characters?.any() == true) {
commands.add(talkCommandHelp)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Binary file modified ktaf/src/test/resources/test.jar
Binary file not shown.

0 comments on commit 61003d6

Please sign in to comment.