Skip to content

Commit

Permalink
Added ability to change player mid game
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Oct 23, 2024
1 parent 6436bb9 commit 1063ea7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/logic/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.github.benpollarduk.ktaf.rendering.frames.Frame
*/
public class Game(
public val information: GameInformation,
public val player: PlayableCharacter,
initialPlayer: PlayableCharacter,
public val overworld: Overworld,
private val completionCondition: EndCheck,
private val gameOverCondition: EndCheck,
Expand All @@ -49,6 +49,12 @@ public class Game(
information.author
)

/**
* The current [PlayableCharacter].
*/
public var player: PlayableCharacter
private set

/**
* The active [Converser].
*/
Expand All @@ -65,6 +71,10 @@ public class Game(
*/
public var sceneMapKeyType: KeyType = KeyType.DYNAMIC

init {
player = initialPlayer
}

/**
* Display a transition frame with a specified [title] and [message].
*/
Expand Down Expand Up @@ -341,6 +351,13 @@ public class Game(
state = GameState.FINISHED
}

/**
* Change to a specified [player].
*/
public fun changePlayer(player: PlayableCharacter) {
this.player = player
}

private fun getFallbackFrame(): Frame {
return ioConfiguration.frameBuilders.transitionFrameBuilder.build(
"Error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,27 @@ class GameTest {
game.displayTransition("Title", "Message")
}
}

@Test
fun `given change player when player2 then player is player2`() {
// Given
val player = PlayableCharacter("TestPlayer", "")
val player2 = PlayableCharacter("TestPlayer", "")
val regionMaker = RegionMaker("TestRegion", "")
regionMaker[0, 0, 0] = Room("TestRoom", "")
val overworldMaker = OverworldMaker("TestOverworld", "", listOf(regionMaker))
val game = Game(
GameInformation("", "", "", ""),
player,
overworldMaker.make(),
{ EndCheckResult.notEnded },
{ EndCheckResult.notEnded }
)

// When
game.changePlayer(player2)

// Then
Assertions.assertEquals(player2, game.player)
}
}

0 comments on commit 1063ea7

Please sign in to comment.