Skip to content

Commit

Permalink
Merge pull request #17 from benpollarduk/updating-examine
Browse files Browse the repository at this point in the history
Added ExaminationResult and ExaminationScene
  • Loading branch information
benpollarduk authored Oct 22, 2024
2 parents 878cc17 + 20ffb61 commit 6436bb9
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public interface Examinable : PlayerVisible {
public val attributes: AttributeManager

/**
* Examine this object to obtain an [ExaminationResult].
* Examine this object to obtain an [ExaminationResult]. The [scene] that the examination occurs in must be
* specified.
*/
public fun examine(): ExaminationResult
public fun examine(scene: ExaminationScene): ExaminationResult
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public abstract class ExaminableObject : Examinable {
* Provides a callback for handling examination of this object.
*/
public var examination: Examination = {
var description = it.description.getDescription()
var description = it.examinable.description.getDescription()

if (it.commands.any()) {
if (it.examinable.commands.any()) {
if (description != "") {
description += " "
}

val newline = NEWLINE
description += "$newline$newline${it.identifier.name} provides the following commands: "
description += "$newline$newline${it.examinable.identifier.name} provides the following commands: "

it.commands.forEach { command ->
it.examinable.commands.forEach { command ->
description += "$newline \"${command.commandHelp.command}\" - " +
"${command.commandHelp.description.removeSentenceEnd()}, "
}
Expand All @@ -39,15 +39,15 @@ public abstract class ExaminableObject : Examinable {
}

if (description == "") {
description = it.identifier.name
description = it.examinable.identifier.name
}

if (description == "") {
description = it::class.simpleName.toString()
description = it.examinable::class.simpleName.toString()
}

if (attributes.count > 0) {
description += "\n\n" + StringUtilities.getAttributesAsString(it.attributes.toMap())
description += "\n\n" + StringUtilities.getAttributesAsString(it.examinable.attributes.toMap())
}

ExaminationResult(description)
Expand All @@ -66,8 +66,8 @@ public abstract class ExaminableObject : Examinable {

override var commands: List<CustomCommand> = emptyList()

override fun examine(): ExaminationResult {
return examination(this)
override fun examine(scene: ExaminationScene): ExaminationResult {
return examination(ExaminationRequest(this, scene))
}

override var isPlayerVisible: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.benpollarduk.ktaf.assets

/**
* Provides a lambda signature for a examining the [examinable] that must return a [ExaminationResult].
* Provides a lambda signature for handling an [examinationRequest] that must return a [ExaminationResult].
*/
public typealias Examination = (examinable: Examinable) -> ExaminationResult
public typealias Examination = (examinationRequest: ExaminationRequest) -> ExaminationResult
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.benpollarduk.ktaf.assets

/**
* Represents a request to examine an [examinable] in a [scene].
*/
public data class ExaminationRequest(
public val examinable: Examinable,
public val scene: ExaminationScene
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.benpollarduk.ktaf.assets

import com.github.benpollarduk.ktaf.assets.characters.Character
import com.github.benpollarduk.ktaf.assets.characters.PlayableCharacter
import com.github.benpollarduk.ktaf.assets.locations.Room

/**
* Represents a scene that an examination occurs in with a [examiner] and a [room].
*/
public data class ExaminationScene(
val examiner: Character,
val room: Room
) {
public companion object {
/**
* Provides a value representing no scene.
*/
public val noScene: ExaminationScene = ExaminationScene(
PlayableCharacter("", ""),
Room("", "")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.benpollarduk.ktaf.assets.locations
import com.github.benpollarduk.ktaf.assets.Description
import com.github.benpollarduk.ktaf.assets.ExaminableObject
import com.github.benpollarduk.ktaf.assets.ExaminationResult
import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Identifier
import com.github.benpollarduk.ktaf.extensions.equalsExaminable

Expand All @@ -14,7 +15,7 @@ public class Overworld(
override var description: Description
) : ExaminableObject() {
/**
* Provides an overworld which is a container of [Region]. The overwold has an [identifier] and a [description].
* Provides an overlord which is a container of [Region]. The overwold has an [identifier] and a [description].
*/
public constructor(
identifier: String,
Expand Down Expand Up @@ -83,7 +84,7 @@ public class Overworld(
return true
}

override fun examine(): ExaminationResult {
override fun examine(scene: ExaminationScene): ExaminationResult {
return ExaminationResult(description.getDescription())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.benpollarduk.ktaf.assets.locations

import com.github.benpollarduk.ktaf.assets.ExaminationResult
import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Identifier
import com.github.benpollarduk.ktaf.extensions.equalsExaminable
import com.github.benpollarduk.ktaf.utilities.RegionMaker
Expand Down Expand Up @@ -191,7 +192,7 @@ public class Region(
return jumpToRoom(room)
}

override fun examine(): ExaminationResult {
override fun examine(scene: ExaminationScene): ExaminationResult {
return ExaminationResult("$Identifier: ${description.getDescription()}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.benpollarduk.ktaf.assets.ConditionalDescription
import com.github.benpollarduk.ktaf.assets.Description
import com.github.benpollarduk.ktaf.assets.ExaminableObject
import com.github.benpollarduk.ktaf.assets.ExaminationResult
import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Identifier
import com.github.benpollarduk.ktaf.assets.Item
import com.github.benpollarduk.ktaf.assets.characters.Character
Expand Down Expand Up @@ -311,7 +312,7 @@ public class Room(
this.description = description
}

override fun examine(): ExaminationResult {
override fun examine(scene: ExaminationScene): ExaminationResult {
val visibleItems = items.filter { it.isPlayerVisible }

if (visibleItems.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.benpollarduk.ktaf.commands.game

import com.github.benpollarduk.ktaf.assets.Examinable
import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.interaction.Reaction
import com.github.benpollarduk.ktaf.assets.interaction.ReactionResult
import com.github.benpollarduk.ktaf.assets.locations.Room
import com.github.benpollarduk.ktaf.commands.Command
import com.github.benpollarduk.ktaf.logic.Game

Expand All @@ -15,6 +17,14 @@ internal class Examine(private val examinable: Examinable?) : Command {
return Reaction(ReactionResult.ERROR, "You must specify an examinable.")
}

return Reaction(ReactionResult.OK, examinable.examine().description)
return Reaction(
ReactionResult.OK,
examinable.examine(
ExaminationScene(
game.player,
game.overworld.currentRegion?.currentRoom ?: Room("", "")
)
).description
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.benpollarduk.ktaf.rendering.frames.ansi

import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Size
import com.github.benpollarduk.ktaf.assets.characters.PlayableCharacter
import com.github.benpollarduk.ktaf.assets.locations.Region
Expand Down Expand Up @@ -89,7 +90,7 @@ public class AnsiSceneFrameBuilder(
)

var extendedDescription: String = if (room.items.any()) {
room.examine().description.ensureFinishedSentence()
room.examine(ExaminationScene(playableCharacter, room)).description.ensureFinishedSentence()
} else {
"There are no items in this area."
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.benpollarduk.ktaf.rendering.frames.html

import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Item
import com.github.benpollarduk.ktaf.assets.Size
import com.github.benpollarduk.ktaf.assets.attributes.Attribute
Expand Down Expand Up @@ -76,7 +77,7 @@ public class HtmlSceneFrameBuilder(
htmlPageBuilder.p(room.description.getDescription().ensureFinishedSentence())

var extendedDescription: String = if (room.items.any()) {
room.examine().description.ensureFinishedSentence()
room.examine(ExaminationScene(playableCharacter, room)).description.ensureFinishedSentence()
} else {
"There are no items in this area."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ExaminableObjectTest {
val examinable = Item("Test", "ABC")

// When
val result = examinable.examine()
val result = examinable.examine(ExaminationScene.noScene)

// Then
Assertions.assertTrue(result.description.contains("ABC"))
Expand All @@ -26,7 +26,7 @@ class ExaminableObjectTest {
val examinable = Item("Test", "")

// When
val result = examinable.examine()
val result = examinable.examine(ExaminationScene.noScene)

// Then
Assertions.assertTrue(result.description.contains("Test"))
Expand All @@ -38,7 +38,7 @@ class ExaminableObjectTest {
val examinable = Item("", "")

// When
val result = examinable.examine()
val result = examinable.examine(ExaminationScene.noScene)

// Then
Assertions.assertTrue(result.description.contains("Item"))
Expand All @@ -55,7 +55,7 @@ class ExaminableObjectTest {
)

// When
val result = examinable.examine()
val result = examinable.examine(ExaminationScene.noScene)

// Then
Assertions.assertTrue(result.description.contains("ABC"))
Expand All @@ -68,7 +68,7 @@ class ExaminableObjectTest {
examinable.attributes.add("attribute", 0)

// When
val result = examinable.examine()
val result = examinable.examine(ExaminationScene.noScene)

// Then
Assertions.assertTrue(result.description.contains("attribute"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.benpollarduk.ktaf.assets.characters

import com.github.benpollarduk.ktaf.assets.Examination
import com.github.benpollarduk.ktaf.assets.ExaminationResult
import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Item
import com.github.benpollarduk.ktaf.assets.interaction.Interaction
import com.github.benpollarduk.ktaf.assets.interaction.InteractionEffect
Expand Down Expand Up @@ -95,7 +96,7 @@ class NonPlayableCharacterTest {
)

// When
val result = character.examine()
val result = character.examine(ExaminationScene.noScene)

// Then
Assertions.assertEquals("ABC", result.description)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.benpollarduk.ktaf.assets.location

import com.github.benpollarduk.ktaf.assets.ExaminationScene
import com.github.benpollarduk.ktaf.assets.Identifier
import com.github.benpollarduk.ktaf.assets.Item
import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter
Expand Down Expand Up @@ -223,7 +224,7 @@ class RoomTest {
room.addItem(item)

// When
val result = room.examine().description
val result = room.examine(ExaminationScene.noScene).description

// Then
Assertions.assertTrue(result.contains("Item"))
Expand All @@ -239,7 +240,7 @@ class RoomTest {
room.addItem(item2)

// When
val result = room.examine().description
val result = room.examine(ExaminationScene.noScene).description

// Then
Assertions.assertTrue(result.contains("Item 1"))
Expand Down Expand Up @@ -416,7 +417,7 @@ class RoomTest {
val room = Room("Room", "Room description")

// When
val result = room.examine()
val result = room.examine(ExaminationScene.noScene)

// Then
Assertions.assertEquals("There is nothing to examine.", result.description)
Expand Down

0 comments on commit 6436bb9

Please sign in to comment.