Skip to content

Commit

Permalink
[#21] Improve exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed Apr 20, 2020
1 parent 34ce074 commit fea2a81
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/app/demo/MeleeCombatDemo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app.demo

import game.CannotTargetSelf
import game.GameRenderer
import game.NoActionPointsException
import game.OutOfRangeException
import game.action.*
import game.component.*
Expand All @@ -18,7 +19,6 @@ import game.rpg.character.skill.Skill
import game.rpg.character.skill.SkillManager
import game.rpg.character.skill.SkillUsage
import game.rpg.check.Checker
import game.rpg.time.NoActionPointsException
import game.rpg.time.TimeSystem
import game.rpg.time.TurnData
import javafx.application.Application
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/game/Exceptions.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package game

class CannotTargetSelf : Exception()
class OutOfRangeException : Exception()
data class CannotTargetSelf(val entity: Int) : Exception()
data class NoActionPointsException(val entity: Int) : Exception()
data class NoMovementPointsException(val entity: Int) : Exception()
data class OutOfRangeException(val distance: Int) : Exception()
2 changes: 1 addition & 1 deletion src/main/kotlin/game/reducer/action/MoveReducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private fun move(
val newBody = updateBody(body, walkable.position)
val newBodyStorage = bodyStorage.updateAndRemove(mapOf(entity to newBody))

val newTurnData = turnData.reduceMovementPoints()
val newTurnData = turnData.reduceMovementPoints(entity)

return state.copy(listOf(newBodyStorage), listOf(newMap, newTurnData))
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/game/reducer/action/UseAbilityReducer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ val USE_ABILITY_REDUCER: Reducer<Action, EcsState> = a@{ state, action ->
val target =
map.entities[action.position] ?: throw IllegalStateException("No target at position ${action.position}")

if (action.entity == target) throw CannotTargetSelf()
if (action.entity == target) throw CannotTargetSelf(target)

val (entityStatistics, targetStatistics) = state.getStorage<Statistics>().getList(action.entity, target)

Expand All @@ -59,7 +59,7 @@ val USE_ABILITY_REDUCER: Reducer<Action, EcsState> = a@{ state, action ->
is MeleeAttack -> {
val distance = calculateDistanceToPosition(map.size, body, action.position)

if (distance > ability.reach) throw OutOfRangeException()
if (distance > ability.reach) throw OutOfRangeException(distance)

val attackRank = entityStatistics.getRank(ability.skill)
val defenseRank = targetStatistics.getRank(defense.skill)
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/game/rpg/time/NoActionPointsException.kt

This file was deleted.

6 changes: 4 additions & 2 deletions src/main/kotlin/game/rpg/time/TurnData.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package game.rpg.time

import game.NoActionPointsException
import game.NoMovementPointsException
import game.component.Combat
import game.component.Statistics
import game.rpg.character.skill.Skill
Expand All @@ -16,8 +18,8 @@ data class TurnData(

fun isFinished() = movementPoints <= 0 && actionPoints <= 0

fun reduceMovementPoints(): TurnData {
require(movementPoints > 0) { "Can not reduce movement points below 0!" }
fun reduceMovementPoints(entity: Int): TurnData {
if (movementPoints <= 0) throw NoMovementPointsException(entity)
return copy(movementPoints = movementPoints - 1)
}

Expand Down
14 changes: 10 additions & 4 deletions src/test/kotlin/game/rpg/time/TurnDataTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package game.rpg.time

import assertk.assertThat
import assertk.assertions.isEqualTo
import game.NoActionPointsException
import game.NoMovementPointsException
import game.component.Combat
import game.component.Statistics
import game.rpg.character.skill.Skill
Expand All @@ -16,6 +18,8 @@ import kotlin.test.assertTrue

class TurnDataTest {

private val entity = 99

@Test
fun `Test constructor`() {
assertThat(TurnData(6, 2)).isEqualTo(TurnData(6, 6, 2, 2))
Expand Down Expand Up @@ -52,22 +56,24 @@ class TurnDataTest {

@Test
fun `Reduce movement points`() {
val data = TurnData(6, 1).reduceMovementPoints()
val data = TurnData(6, 1).reduceMovementPoints(entity)
assertThat(data).isEqualTo(TurnData(5, 6, 1, 1))
}

@Test
fun `Reduce 0 movement points`() {
assertFailsWith<IllegalArgumentException> { TurnData(0, 1).reduceMovementPoints() }
val exception = assertFailsWith<NoMovementPointsException> {
TurnData(0, 1).reduceMovementPoints(entity)
}

assertThat(exception).isEqualTo(NoMovementPointsException(entity))
}

}

@Nested
inner class ReduceActionPoints {

val entity = 99

@Test
fun `Reduce action points`() {
val data = TurnData(6, 2).reduceActionPoints(entity)
Expand Down

0 comments on commit fea2a81

Please sign in to comment.