Skip to content

Commit

Permalink
26 Change resources from tags (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniil-shevtsov committed May 28, 2022
1 parent e86d3f4 commit 3bc69de
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import com.daniil.shevtsov.idle.feature.resource.domain.ResourceKey
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag
import com.daniil.shevtsov.idle.feature.tagsystem.domain.TagRelation

typealias ResourceChanges = Map<ResourceKey, Double>
typealias RatioChangeForTags = Map<List<Tag>, Double>
typealias RatioChanges = Map<RatioKey, RatioChangeForTags>

data class Action(
val id: Long,
val title: String,
val subtitle: String,
val resourceChanges: Map<ResourceKey, Double>,
val ratioChanges: Map<RatioKey, Double>,
val resourceChanges: ResourceChanges,
val ratioChanges: RatioChanges,
val tags: Map<TagRelation, List<Tag>>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ import com.daniil.shevtsov.idle.feature.action.presentation.ActionModel
import com.daniil.shevtsov.idle.feature.action.presentation.RatioChangeModel
import com.daniil.shevtsov.idle.feature.action.presentation.ResourceChangeModel
import com.daniil.shevtsov.idle.feature.ratio.domain.RatioKey
import com.daniil.shevtsov.idle.feature.resource.domain.ResourceKey
import com.daniil.shevtsov.idle.feature.ratio.domain.ratioChange
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag
import com.daniil.shevtsov.idle.feature.tagsystem.domain.TagRelation

fun ratioChanges(
vararg entries: Pair<RatioKey, Double>,
): RatioChanges = ratioChangesWithTags(
*entries.map { it.first to ratioChange(tags = emptyList(), change = it.second) }.toTypedArray()
)

fun ratioChangesWithTags(
vararg entries: Pair<RatioKey, RatioChangeForTags>,
): RatioChanges = entries.associate { it.first to it.second }

fun action(
id: Long = 0L,
title: String = "",
subtitle: String = "",
resourceChanges: Map<ResourceKey, Double> = mapOf(),
ratioChanges: Map<RatioKey, Double> = mapOf(),
resourceChanges: ResourceChanges = mapOf(),
ratioChanges: RatioChanges = mapOf(),
tags: Map<TagRelation, List<Tag>> = mapOf(),
) = Action(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun createAllActions() = listOf(
ResourceKey.Blood to -10.0,
ResourceKey.Prisoner to 1.0,
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.2,
),
tags = mapOf(
Expand All @@ -62,7 +62,7 @@ fun createAllActions() = listOf(
ResourceKey.Prisoner to -1.0,
ResourceKey.Remains to 1.0,
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.05,
),
tags = mapOf(
Expand Down Expand Up @@ -92,7 +92,7 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.Remains to -1.0,
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to -0.05,
),
tags = mapOf(
Expand All @@ -108,7 +108,7 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.Organs to 1.0,
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.1,
),
tags = mapOf(
Expand All @@ -125,7 +125,7 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.Remains to -1.0,
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to -0.1,
),
tags = mapOf(
Expand Down Expand Up @@ -172,7 +172,7 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.Money to 15.0
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.005,
),
tags = mapOf(
Expand All @@ -193,7 +193,7 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.Money to 40.0
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.015
),
tags = mapOf(
Expand Down Expand Up @@ -242,8 +242,8 @@ fun createAllActions() = listOf(
resourceChanges = mapOf(
ResourceKey.HumanFood to 1.0,
),
ratioChanges = mapOf(
RatioKey.Suspicion to 0.005,
ratioChanges = ratioChanges(
RatioKey.Suspicion to 0.005,
),
tags = mapOf(
TagRelation.RequiredAll to listOf(
Expand Down Expand Up @@ -295,7 +295,7 @@ fun createAllActions() = listOf(
Tags.Body.IronJaws,
)
),
ratioChanges = mapOf(
ratioChanges = ratioChanges(
RatioKey.Suspicion to -0.1,
)
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.daniil.shevtsov.idle.feature.main.domain

import com.daniil.shevtsov.idle.core.navigation.Screen
import com.daniil.shevtsov.idle.feature.action.domain.RatioChanges
import com.daniil.shevtsov.idle.feature.action.domain.ResourceChanges
import com.daniil.shevtsov.idle.feature.coreshell.domain.GameState
import com.daniil.shevtsov.idle.feature.drawer.presentation.DrawerViewAction
import com.daniil.shevtsov.idle.feature.main.presentation.MainViewAction
import com.daniil.shevtsov.idle.feature.ratio.domain.Ratio
import com.daniil.shevtsov.idle.feature.ratio.domain.RatioKey
import com.daniil.shevtsov.idle.feature.resource.domain.Resource
import com.daniil.shevtsov.idle.feature.resource.domain.ResourceKey
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag
import com.daniil.shevtsov.idle.feature.tagsystem.domain.TagRelation
import com.daniil.shevtsov.idle.feature.upgrade.domain.UpgradeStatus

Expand All @@ -34,16 +37,14 @@ fun mainFunctionalCore(
)
is MainViewAction.LocationSelectionExpandChange -> handleLocationSelectionExpandChange(
state = state,
viewAction = viewAction,
)
MainViewAction.Init -> state
}
return newState
}

fun handleLocationSelectionExpandChange(
state: GameState,
viewAction: MainViewAction.LocationSelectionExpandChange
state: GameState
): GameState {
return state.copy(
locationSelectionState = state.locationSelectionState.copy(
Expand Down Expand Up @@ -119,11 +120,13 @@ fun handleActionClicked(

val updatedRatios = applyRatioChanges(
currentRatios = state.ratios,
ratioChanges = selectedAction.ratioChanges
ratioChanges = selectedAction.ratioChanges,
tags = state.player.tags,
)

val newTags =
state.player.generalTags + selectedAction.tags[TagRelation.Provides].orEmpty() - selectedAction.tags[TagRelation.Removes].orEmpty()
.toSet()

return if (!hasInvalidChanges) {
state.copy(
Expand All @@ -133,7 +136,8 @@ fun handleActionClicked(
generalTags = newTags
),
currentScreen = when {
updatedRatios.find { it.key == RatioKey.Suspicion }?.value ?: 0.0 >= 1.0 -> Screen.FinishedGame
(updatedRatios.find { it.key == RatioKey.Suspicion }?.value
?: 0.0) >= 1.0 -> Screen.FinishedGame
else -> state.currentScreen
}
)
Expand All @@ -144,17 +148,22 @@ fun handleActionClicked(

private fun applyRatioChanges(
currentRatios: List<Ratio>,
ratioChanges: Map<RatioKey, Double>,
ratioChanges: RatioChanges,
tags: List<Tag>,
): List<Ratio> = currentRatios.map { ratio ->
when (val ratioChange = ratioChanges[ratio.key]) {
val ratioChange = ratioChanges[ratio.key]
?.minByOrNull { (matchedTags, _) ->
(tags - matchedTags.toSet()).size
}?.value
when (ratioChange) {
null -> ratio
else -> ratio.copy(value = ratio.value + ratioChange)
}
}

private fun applyResourceChanges(
currentResources: List<Resource>,
resourceChanges: Map<ResourceKey, Double>,
resourceChanges: ResourceChanges,
) = currentResources.map { resource ->
when (val resourceChange = resourceChanges[resource.key]) {
null -> resource
Expand All @@ -164,7 +173,7 @@ private fun applyResourceChanges(

private fun hasInvalidChanges(
currentResources: List<Resource>,
resourceChanges: Map<ResourceKey, Double>,
resourceChanges: ResourceChanges,
): Boolean = resourceChanges.any { (resourceKey, resourceChange) ->
val currentResourceValue = currentResources
.find { resource -> resource.key == resourceKey }!!.value
Expand Down Expand Up @@ -210,6 +219,7 @@ fun handleUpgradeSelected(
val updatedRatios = applyRatioChanges(
currentRatios = state.ratios,
ratioChanges = boughtUpgrade.ratioChanges,
tags = state.player.tags,
)

return state.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.daniil.shevtsov.idle.core.presentation.formatting.formatEnumName
import com.daniil.shevtsov.idle.core.presentation.formatting.formatRound
import com.daniil.shevtsov.idle.core.ui.Icons
import com.daniil.shevtsov.idle.feature.action.domain.Action
import com.daniil.shevtsov.idle.feature.action.domain.RatioChanges
import com.daniil.shevtsov.idle.feature.action.domain.ResourceChanges
import com.daniil.shevtsov.idle.feature.action.presentation.*
import com.daniil.shevtsov.idle.feature.coreshell.domain.GameState
import com.daniil.shevtsov.idle.feature.flavor.flavorMachine
Expand Down Expand Up @@ -102,7 +104,7 @@ private fun createMainViewState(state: GameState): MainViewState {
state.resources.find { it.key == ResourceKey.Blood }?.value ?: 0.0
),
resourceChanges = mapResourceChanges(resourceChanges),
ratioChanges = mapRatioChanges(ratioChanges),
ratioChanges = mapRatioChanges(ratioChanges, state.player.tags),
)
}
}
Expand Down Expand Up @@ -174,8 +176,8 @@ private fun createActionState(
}
.filter { action ->
action.resourceChanges.all { (resourceKey, resourceChange) ->
val currentResource = resources.find { it.key == resourceKey }!!.value
currentResource + resourceChange >= 0
val currentResource = resources.find { it.key == resourceKey }?.value
currentResource != null && currentResource + resourceChange >= 0
}
}
.map { action ->
Expand Down Expand Up @@ -207,7 +209,7 @@ private fun createActionState(
}
),
resourceChanges = mapResourceChanges(resourceChanges),
ratioChanges = mapRatioChanges(ratioChanges),
ratioChanges = mapRatioChanges(ratioChanges, state.player.tags),
isEnabled = isActive,
)
}
Expand All @@ -223,7 +225,7 @@ private fun createActionState(
)
}

private fun mapResourceChanges(resourceChanges: Map<ResourceKey, Double>) =
private fun mapResourceChanges(resourceChanges: ResourceChanges) =
resourceChanges.map { (resourceKey, changeValue) ->
val formattedValue =
("+".takeIf { changeValue > 0 } ?: "") + changeValue.formatRound(digits = 2)
Expand All @@ -233,8 +235,12 @@ private fun mapResourceChanges(resourceChanges: Map<ResourceKey, Double>) =
)
}

private fun mapRatioChanges(ratioChanges: Map<RatioKey, Double>) =
ratioChanges.map { (ratioKey, changeValue) ->
private fun mapRatioChanges(
ratioChanges: RatioChanges,
tags: List<Tag>
) =
ratioChanges.map { (ratioKey, tagChanges) ->
val changeValue = tagChanges[emptyList()] ?: 0.0
val formattedValue =
("+".takeIf { changeValue > 0 } ?: "") + (changeValue * 100)
.formatRound(digits = 2) + " %"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.daniil.shevtsov.idle.feature.ratio.domain

import com.daniil.shevtsov.idle.feature.action.domain.RatioChangeForTags
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag

fun ratio(
key: RatioKey = RatioKey.Mutanity,
title: String = "",
Expand All @@ -11,6 +14,6 @@ fun ratio(
)

fun ratioChange(
key: RatioKey = RatioKey.Mutanity,
tags: List<Tag> = emptyList(),
change: Double = 0.0,
) = key to change
): RatioChangeForTags = mapOf(tags to change)
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ fun Map<Tag, TagRelation>.requiredTags(): List<Tag> =

fun Map<Tag, TagRelation>.hasRequiredTag(tag: Tag): Boolean = requiredTags().contains(tag)

fun List<Tag>.satisfies(relation: TagRelation, tag: Tag): Boolean = satisfies(relation, listOf(tag))

fun List<Tag>.satisfies(relation: TagRelation, tags: List<Tag>): Boolean = when (relation) {
TagRelation.Provides -> false
TagRelation.Removes -> false
TagRelation.RequiresNone -> tags.none { tag -> tag in this }
TagRelation.RequiredAll -> tags.all { tag -> tag in this }
TagRelation.RequiredAny -> tags.any { tag -> tag in this }
}

Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ object Tags {
val Tech = Tag(name = "Tech")
}

object Appearance {
val Human = Tag(name = "Human Appearance")
val Monster = Tag(name = "Monster Appearance")
}

val Surgeon = Tag(name = "Surgeon", description = "You are very precise with the scalpel")
val SolitaryJob = Tag(name = "Solitary Job", description = "People don't pay attention as long as the job gets done")
val SolitaryJob = Tag(
name = "Solitary Job",
description = "People don't pay attention as long as the job gets done"
)
val CorpseAccess = Tag(name = "Corpse Access")
val MeatAccess = Tag(name = "Meat Access")
val LaborIntensive = Tag(name = "Labor intensive job")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.daniil.shevtsov.idle.feature.upgrade.domain

import com.daniil.shevtsov.idle.feature.ratio.domain.RatioKey
import com.daniil.shevtsov.idle.feature.resource.domain.ResourceKey
import com.daniil.shevtsov.idle.feature.action.domain.RatioChanges
import com.daniil.shevtsov.idle.feature.action.domain.ResourceChanges
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag
import com.daniil.shevtsov.idle.feature.tagsystem.domain.TagRelation

Expand All @@ -10,8 +10,8 @@ data class Upgrade(
val title: String,
val subtitle: String,
val price: Price,
val resourceChanges: Map<ResourceKey, Double>,
val ratioChanges: Map<RatioKey, Double>,
val resourceChanges: ResourceChanges,
val ratioChanges: RatioChanges,
val status: UpgradeStatus,
val tags: Map<TagRelation, List<Tag>>,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.daniil.shevtsov.idle.feature.upgrade.domain

import com.daniil.shevtsov.idle.feature.ratio.domain.RatioKey
import com.daniil.shevtsov.idle.feature.resource.domain.ResourceKey
import com.daniil.shevtsov.idle.feature.action.domain.RatioChanges
import com.daniil.shevtsov.idle.feature.action.domain.ResourceChanges
import com.daniil.shevtsov.idle.feature.tagsystem.domain.Tag
import com.daniil.shevtsov.idle.feature.tagsystem.domain.TagRelation

Expand All @@ -11,8 +11,8 @@ fun upgrade(
subtitle: String = "",
price: Double = 0.0,
status: UpgradeStatus = UpgradeStatus.NotBought,
resourceChanges: Map<ResourceKey, Double> = mapOf(),
ratioChanges: Map<RatioKey, Double> = mapOf(),
resourceChanges: ResourceChanges = mapOf(),
ratioChanges: RatioChanges = mapOf(),
tags: Map<TagRelation, List<Tag>> = mapOf(),
) = Upgrade(
id = id,
Expand Down
Loading

0 comments on commit 3bc69de

Please sign in to comment.