Skip to content

Commit 1063ea7

Browse files
author
ben_singer
committed
Added ability to change player mid game
1 parent 6436bb9 commit 1063ea7

File tree

2 files changed

+41
-1
lines changed
  • ktaf/src
    • main/kotlin/com/github/benpollarduk/ktaf/logic
    • test/kotlin/com/github/benpollarduk/ktaf/logic

2 files changed

+41
-1
lines changed

ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/logic/Game.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import com.github.benpollarduk.ktaf.rendering.frames.Frame
3131
*/
3232
public class Game(
3333
public val information: GameInformation,
34-
public val player: PlayableCharacter,
34+
initialPlayer: PlayableCharacter,
3535
public val overworld: Overworld,
3636
private val completionCondition: EndCheck,
3737
private val gameOverCondition: EndCheck,
@@ -49,6 +49,12 @@ public class Game(
4949
information.author
5050
)
5151

52+
/**
53+
* The current [PlayableCharacter].
54+
*/
55+
public var player: PlayableCharacter
56+
private set
57+
5258
/**
5359
* The active [Converser].
5460
*/
@@ -65,6 +71,10 @@ public class Game(
6571
*/
6672
public var sceneMapKeyType: KeyType = KeyType.DYNAMIC
6773

74+
init {
75+
player = initialPlayer
76+
}
77+
6878
/**
6979
* Display a transition frame with a specified [title] and [message].
7080
*/
@@ -341,6 +351,13 @@ public class Game(
341351
state = GameState.FINISHED
342352
}
343353

354+
/**
355+
* Change to a specified [player].
356+
*/
357+
public fun changePlayer(player: PlayableCharacter) {
358+
this.player = player
359+
}
360+
344361
private fun getFallbackFrame(): Frame {
345362
return ioConfiguration.frameBuilders.transitionFrameBuilder.build(
346363
"Error",

ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/logic/GameTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,27 @@ class GameTest {
223223
game.displayTransition("Title", "Message")
224224
}
225225
}
226+
227+
@Test
228+
fun `given change player when player2 then player is player2`() {
229+
// Given
230+
val player = PlayableCharacter("TestPlayer", "")
231+
val player2 = PlayableCharacter("TestPlayer", "")
232+
val regionMaker = RegionMaker("TestRegion", "")
233+
regionMaker[0, 0, 0] = Room("TestRoom", "")
234+
val overworldMaker = OverworldMaker("TestOverworld", "", listOf(regionMaker))
235+
val game = Game(
236+
GameInformation("", "", "", ""),
237+
player,
238+
overworldMaker.make(),
239+
{ EndCheckResult.notEnded },
240+
{ EndCheckResult.notEnded }
241+
)
242+
243+
// When
244+
game.changePlayer(player2)
245+
246+
// Then
247+
Assertions.assertEquals(player2, game.player)
248+
}
226249
}

0 commit comments

Comments
 (0)