Skip to content

Commit

Permalink
Dynamically add padding around level layouts so every level has the s…
Browse files Browse the repository at this point in the history
…ame size
  • Loading branch information
haukesomm committed Jan 4, 2024
1 parent b37a313 commit 66e550f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 108 deletions.
21 changes: 18 additions & 3 deletions core/src/commonMain/kotlin/de/haukesomm/sokoban/core/Level.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class LevelDescription(
* The layout string consists of a series of characters that represent the tiles of the level.
*
* The layout string has to have [width] * [height] characters and must only use the characters defined in the
* [characterMap].
* [characterMap]. Lines must have an equal length and be delimited by line breaks.
*
* Here is an example of a layout string using the [CharacterMap.default] character map. It defines a level with a
* width of 8 and a height of 5. It contains a player (`@`), a box (`$`), and a target (`.`).
Expand All @@ -33,11 +33,12 @@ data class LevelDescription(
data class Level(
val id: String,
val name: String,
val width: Int,
val height: Int,
val layoutString: String,
val characterMap: CharacterMap = CharacterMap.default
) {
private val lines by lazy {
layoutString.lines()
}

/**
* The normalized layout string is the layout string without any whitespace
Expand All @@ -46,4 +47,18 @@ data class Level(
val normalizedLayoutString: String by lazy {
layoutString.replace("[ \r\n]+".toRegex(), "")
}

/**
* Horizontal number of layout characters.
*/
val width: Int by lazy {
lines.firstOrNull()?.length ?: 0
}

/**
* Vertical number of layout characters.
*/
val height: Int by lazy {
lines.size
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.haukesomm.sokoban.core

import de.haukesomm.sokoban.core.levels.BundledLevelRepository
import de.haukesomm.sokoban.core.levels.PaddingLevelRepositoryDecorator
import de.haukesomm.sokoban.core.moving.MoveRule
import de.haukesomm.sokoban.core.moving.MoveServiceImpl
import de.haukesomm.sokoban.core.moving.rules.*
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic

/**
Expand Down Expand Up @@ -88,12 +90,18 @@ object SokobanGameFactory {
* Additional behavior can be added by passing additional [ConfigurationOption]s. A list of available
* options can be found in [configurationOptions].
*/
fun withMinimalConfiguration(additional: Collection<ConfigurationOption>): SokobanGame {
@JvmStatic
@JvmOverloads
fun withMinimalConfiguration(additional: Collection<ConfigurationOption> = emptySet()): SokobanGame {
val combinedConfig = additional.fold(ConfigurationContext()) { scope, config ->
config.applyTo(scope)
}
return SokobanGame(
BundledLevelRepository(),
PaddingLevelRepositoryDecorator(
BundledLevelRepository(),
minWidth = 20,
minHeight = 16
),
MoveServiceImpl(
ConditionalMoveRule(
condition = AggregatingMoveRule(
Expand Down
Loading

0 comments on commit 66e550f

Please sign in to comment.