From 3b844531a2f9ad8037c9666cfff00916b5321cf9 Mon Sep 17 00:00:00 2001 From: jbion Date: Sun, 8 Jul 2018 13:30:41 +0200 Subject: Kotlin mig: wonders package --- .../game/data/definitions/WonderDefinition.kt | 9 +---- .../org/luxons/sevenwonders/game/wonders/Wonder.kt | 45 ++++++++++++++++++++++ .../sevenwonders/game/wonders/WonderStage.kt | 30 +++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt create mode 100644 game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt (limited to 'game-engine/src/main/kotlin/org/luxons') diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt index 558f16e8..013a3d9c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt @@ -9,13 +9,8 @@ internal data class WonderDefinition( private val sides: Map ) { fun create(settings: Settings): Wonder { - val wonder = Wonder() - wonder.name = name - val wonderSideDef = sides[settings.pickWonderSide()]!! - wonder.initialResource = wonderSideDef.initialResource - wonder.stages = wonderSideDef.createStages() - wonder.image = wonderSideDef.image - return wonder + val stages = wonderSideDef.createStages() + return Wonder(name, wonderSideDef.initialResource, stages, wonderSideDef.image) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt new file mode 100644 index 00000000..beb87580 --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt @@ -0,0 +1,45 @@ +package org.luxons.sevenwonders.game.wonders + +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.ResourceType + +class Wonder( + val name: String, + val initialResource: ResourceType, + val stages: List, + val image: String +) { + val nbBuiltStages: Int + get() = stages.filter { it.isBuilt }.count() + + private val nextStage: WonderStage + get() { + val nextLevel = nbBuiltStages + if (nextLevel == stages.size) { + throw IllegalStateException("This wonder has already reached its maximum level") + } + return stages[nextLevel] + } + + private val lastBuiltStage: WonderStage + get() { + val lastLevel = nbBuiltStages - 1 + return stages[lastLevel] + } + + fun isNextStageBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean = + nbBuiltStages < stages.size && nextStage.isBuildable(table, playerIndex, boughtResources) + + fun buildLevel(cardBack: CardBack) = nextStage.build(cardBack) + + fun activateLastBuiltStage(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) = + lastBuiltStage.activate(table, playerIndex, boughtResources) + + fun computePoints(table: Table, playerIndex: Int): Int = stages + .filter { it.isBuilt } + .flatMap { c -> c.effects } + .map { e -> e.computePoints(table, playerIndex) } + .sum() +} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt new file mode 100644 index 00000000..6aad135d --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt @@ -0,0 +1,30 @@ +package org.luxons.sevenwonders.game.wonders + +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.cards.Requirements +import org.luxons.sevenwonders.game.effects.Effect +import org.luxons.sevenwonders.game.resources.ResourceTransactions + +class WonderStage(val requirements: Requirements, val effects: List) { + + var cardBack: CardBack? = null + private set + + val isBuilt: Boolean + get() = cardBack != null + + fun isBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean { + val board = table.getBoard(playerIndex) + return requirements.areMetWithHelpBy(board, boughtResources) + } + + internal fun build(cardBack: CardBack) { + this.cardBack = cardBack + } + + internal fun activate(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) { + effects.forEach { e -> e.apply(table, playerIndex) } + requirements.pay(table, playerIndex, boughtResources) + } +} -- cgit