KStarbound/src/main/kotlin/ru/dbotthepony/kstarbound/world/CellView.kt
2023-09-03 13:48:40 +07:00

70 lines
2.2 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ru.dbotthepony.kstarbound.world
import ru.dbotthepony.kstarbound.world.api.BackgroundView
import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZE
import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZE_FF
import ru.dbotthepony.kstarbound.world.api.ForegroundView
import ru.dbotthepony.kstarbound.world.api.IChunk
import ru.dbotthepony.kstarbound.world.api.IChunkCell
/**
* Предоставляет доступ к чанку и его соседям
*
* В основном для использования в местах, где нужен не мир, а определённый чанк мира,
* и при этом координаты проверяются относительно чанка и могут спокойно выйти за его пределы,
* с желанием получить тайл из соседнего чанка
*/
class CellView(
override val pos: ChunkPos,
val center: IChunk?,
val right: IChunk?,
val top: IChunk?,
val topRight: IChunk?,
val topLeft: IChunk?,
val left: IChunk?,
val bottom: IChunk?,
val bottomLeft: IChunk?,
val bottomRight: IChunk?,
) : IChunk {
val backgroundView = BackgroundView(this)
val foregroundView = ForegroundView(this)
override fun getCell(x: Int, y: Int): IChunkCell {
if (x in 0 ..CHUNK_SIZE_FF) {
if (y in 0 ..CHUNK_SIZE_FF) {
return center?.getCell(x, y) ?: IChunkCell.Companion
}
if (y < 0) {
return bottom?.getCell(x, y + CHUNK_SIZE) ?: IChunkCell.Companion
} else {
return top?.getCell(x, y - CHUNK_SIZE) ?: IChunkCell.Companion
}
}
if (x < 0) {
if (y in 0 ..CHUNK_SIZE_FF) {
return left?.getCell(x + CHUNK_SIZE, y) ?: IChunkCell.Companion
}
if (y < 0) {
return bottomLeft?.getCell(x + CHUNK_SIZE, y + CHUNK_SIZE) ?: IChunkCell.Companion
} else {
return topLeft?.getCell(x + CHUNK_SIZE, y - CHUNK_SIZE) ?: IChunkCell.Companion
}
} else {
if (y in 0 ..CHUNK_SIZE_FF) {
return right?.getCell(x - CHUNK_SIZE, y) ?: IChunkCell.Companion
}
if (y < 0) {
return bottomRight?.getCell(x - CHUNK_SIZE, y + CHUNK_SIZE) ?: IChunkCell.Companion
} else {
return topRight?.getCell(x - CHUNK_SIZE, y - CHUNK_SIZE) ?: IChunkCell.Companion
}
}
}
}