Skip to content

Commit

Permalink
Add implementation of MutableResourceMap #313
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Oct 12, 2022
1 parent 35c05c9 commit f878504
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ interface MutableResourceMap: ResourceMap {
/**
* Moves all resources that are possible to move. Ignores any modifiers on this object.
*/
fun payTo(resources: ResourceMap, destination: ResourceMap): Boolean
fun payTo(resources: ResourceMap, destination: MutableResourceMap): Boolean
}

// move (values) to (other)
// "For the next 5 turns, you always have one wildcard to spend" --> How to move it? Probably don't force it to be moved
// "For the next 5 turns, you always have one wildcard to spend" --> Doesn't make sense if it would be moved, as it doesn't really exist.
// "For the next 5 turns, cards you buy costs one less of any color" --> Makes a lot more sense, has the same effect.

// Resource aliases (not immediate problem)

Expand All @@ -46,7 +46,7 @@ interface MutableResourceMap: ResourceMap {

class ResourceMapImpl(
private val resources: MutableMap<GameResource, ResourceEntryImpl> = mutableMapOf()
): ResourceMap {
): MutableResourceMap, ResourceMap {

override fun get(resource: GameResource): Int? = resources[resource]?.value
override fun getOrDefault(resource: GameResource): Int = resources[resource]?.value ?: resource.defaultValue()
Expand Down Expand Up @@ -92,5 +92,40 @@ class ResourceMapImpl(

override fun unaryMinus(): ResourceMap = this.map { it.resource to -it.value }

private inline fun enforceResource(resource: GameResource): ResourceEntryImpl = this.resources.getOrPut(resource) { ResourceEntryImpl(resource, resource.defaultValue()) }

override fun set(resource: GameResource, value: Int) {
enforceResource(resource).value = value
}

override fun plusAssign(other: ResourceMap) {
other.entries().forEach {
this.enforceResource(it.resource) += it.value
}
}

override fun minusAssign(other: ResourceMap) {
other.entries().forEach {
this.enforceResource(it.resource) -= it.value
}
}

override fun timesAssign(value: Int) {
entries().map { it as MutableResourceEntry }.forEach {
it.value *= value
}
}

override fun clear(resource: GameResource) {
this.resources.remove(resource)
}

override fun payTo(resources: ResourceMap, destination: MutableResourceMap): Boolean {
if (!this.has(resources)) return false
this -= resources
destination += resources
return true
}

}

0 comments on commit f878504

Please sign in to comment.