Skip to content

Commit

Permalink
Simplify Snake rendering by using patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
korge-game-engine committed Jun 22, 2024
1 parent 7b49342 commit c8cae4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 55 deletions.
59 changes: 18 additions & 41 deletions src/snake/model/Snake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import korlibs.datastructure.*
import korlibs.image.tiles.*
import korlibs.math.geom.*


data class Snake(val list: List<PointInt>, val length: Int) {
val headPos get() = list.last()

Expand All @@ -27,58 +26,36 @@ data class Snake(val list: List<PointInt>, val length: Int) {

fun render(map: TileMapData, intMap: IntIArray2) {
//println("-----")
for ((index, point) in list.withIndex()) {
for (point in list) {
intMap[point] = SNAKE
}

for ((index, point) in list.withIndex()) {
val isTail = index == 0
val isHead = index == list.size - 1
val prevPoint = list.getOrElse(index - 1) { point }
val nextPoint = list.getOrElse(index + 1) { point }
val dir = SnakeDirection.fromPoints(prevPoint, point)
val ndir = SnakeDirection.fromPoints(point, nextPoint)
val turned = dir.isHorizontal != ndir.isHorizontal

//println("Dir: $dir : $ndir")

val finalTile = when {
isHead || isTail || !turned -> {
val tile = Tile(
when {
isHead -> 4
isTail -> 1
else -> 2
}
val tile = when {
isHead -> SnakeHeadProvider.get(
SimpleTileSpec(
left = dir == SnakeDirection.LEFT,
up = dir == SnakeDirection.UP,
right = dir == SnakeDirection.RIGHT,
down = dir == SnakeDirection.DOWN,
)
when (if (isTail) ndir else dir) {
SnakeDirection.UP -> tile.rotatedLeft()
SnakeDirection.DOWN -> tile.rotatedRight()
SnakeDirection.LEFT -> tile.flippedX()
SnakeDirection.RIGHT -> tile
SnakeDirection.NONE -> tile
}
}
else -> {
//
val tile = Tile(3)
when {
dir == SnakeDirection.UP && ndir == SnakeDirection.RIGHT -> tile.flippedX()
dir == SnakeDirection.LEFT && ndir == SnakeDirection.DOWN -> tile.flippedX()

dir == SnakeDirection.RIGHT && ndir == SnakeDirection.UP -> tile.rotatedRight(1)
dir == SnakeDirection.DOWN && ndir == SnakeDirection.LEFT -> tile.rotatedRight(1)

dir == SnakeDirection.DOWN && ndir == SnakeDirection.RIGHT -> tile.rotatedRight(2)
dir == SnakeDirection.LEFT && ndir == SnakeDirection.UP -> tile.rotatedRight(2)

else -> tile
}
}
)
else -> SnakeProvider.get(
SimpleTileSpec(
left = dir == SnakeDirection.RIGHT || ndir == SnakeDirection.LEFT,
up = dir == SnakeDirection.DOWN || ndir == SnakeDirection.UP,
right = dir == SnakeDirection.LEFT || ndir == SnakeDirection.RIGHT,
down = dir == SnakeDirection.UP || ndir == SnakeDirection.DOWN,
)
)
}

//map[point.x, point.y] = finalTile
map.push(point.x, point.y, finalTile)
map.push(point.x, point.y, tile)
}
}
}
27 changes: 27 additions & 0 deletions src/snake/model/Tiles.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
package snake.model

import korlibs.image.tiles.*

val EMPTY = 0
val APPLE = 1
val WALL = 2
val SNAKE = 3

object AppleProvider : ISimpleTileProvider by (SimpleTileProvider(value = APPLE).also {
it.rule(SimpleRule(Tile(12)))
})

object WallProvider : ISimpleTileProvider by (SimpleTileProvider(value = WALL).also {
it.rule(SimpleRule(Tile(16)))
it.rule(SimpleRule(Tile(17), right = true))
it.rule(SimpleRule(Tile(18), left = true, right = true))
it.rule(SimpleRule(Tile(19), left = true, down = true))
it.rule(SimpleRule(Tile(20), left = true, up = true, down = true))
it.rule(SimpleRule(Tile(21), left = true, up = true, down = true, right = true))
})

object SnakeProvider : ISimpleTileProvider by (SimpleTileProvider(value = SNAKE).also {
it.rule(SimpleRule(Tile(0)))
it.rule(SimpleRule(Tile(1), right = true))
it.rule(SimpleRule(Tile(2), left = true, right = true))
it.rule(SimpleRule(Tile(3), left = true, down = true))
})

object SnakeHeadProvider : ISimpleTileProvider by (SimpleTileProvider(value = SNAKE).also {
it.rule(SimpleRule(Tile(0)))
it.rule(SimpleRule(Tile(4), right = true))
})
15 changes: 1 addition & 14 deletions src/snake/scene/IngameScene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class IngameScene : PixelatedScene(32 * 16, 32 * 16, sceneSmoothing = true), Sta
snake = snake.copy(length = snake.length + 1)
gameInfo = gameInfo.withIncrementedScore(+1)
}
headValue == WALL || headValue == SNAKE -> {
headValue != EMPTY -> {
change(::gameOver)
return
}
Expand All @@ -155,16 +155,3 @@ class IngameScene : PixelatedScene(32 * 16, 32 * 16, sceneSmoothing = true), Sta
//println(intMap.base)
}
}

object AppleProvider : ISimpleTileProvider by (SimpleTileProvider(value = APPLE).also {
it.rule(SimpleRule(Tile(12)))
})

object WallProvider : ISimpleTileProvider by (SimpleTileProvider(value = WALL).also {
it.rule(SimpleRule(Tile(16)))
it.rule(SimpleRule(Tile(17), right = true))
it.rule(SimpleRule(Tile(18), left = true, right = true))
it.rule(SimpleRule(Tile(19), left = true, down = true))
it.rule(SimpleRule(Tile(20), left = true, up = true, down = true))
it.rule(SimpleRule(Tile(21), left = true, up = true, down = true, right = true))
})
1 change: 1 addition & 0 deletions test/snake/scene/SimpleTileProviderTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snake.scene

import korlibs.image.tiles.*
import snake.model.*
import kotlin.test.*

class SimpleTileProviderTest {
Expand Down

0 comments on commit c8cae4b

Please sign in to comment.