Minor cleanups
This commit is contained in:
parent
1e1c5a83c1
commit
fb44d77353
@ -2,6 +2,7 @@ package ru.dbotthepony.kstarbound.client
|
|||||||
|
|
||||||
import ru.dbotthepony.kstarbound.client.render.LayeredRenderer
|
import ru.dbotthepony.kstarbound.client.render.LayeredRenderer
|
||||||
import ru.dbotthepony.kstarbound.math.encasingChunkPosAABB
|
import ru.dbotthepony.kstarbound.math.encasingChunkPosAABB
|
||||||
|
import ru.dbotthepony.kstarbound.math.encasingIntAABB
|
||||||
import ru.dbotthepony.kstarbound.world.*
|
import ru.dbotthepony.kstarbound.world.*
|
||||||
import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZEf
|
import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZEf
|
||||||
import ru.dbotthepony.kstarbound.world.entities.Entity
|
import ru.dbotthepony.kstarbound.world.entities.Entity
|
||||||
@ -32,7 +33,7 @@ class ClientWorld(
|
|||||||
size: AABB,
|
size: AABB,
|
||||||
layers: LayeredRenderer
|
layers: LayeredRenderer
|
||||||
) {
|
) {
|
||||||
for (chunk in collectPositionAware(size.encasingChunkPosAABB())) {
|
for (chunk in collectWithPosition(size.encasingChunkPosAABB())) {
|
||||||
val (x, y) = chunk.first
|
val (x, y) = chunk.first
|
||||||
|
|
||||||
val renderer = chunk.second.Renderer(
|
val renderer = chunk.second.Renderer(
|
||||||
|
@ -61,6 +61,20 @@ abstract class Chunk<WorldType : World<WorldType, This>, This : Chunk<WorldType,
|
|||||||
return cells[x, y]
|
return cells[x, y]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dimensions of this chunk with accounting for world boundaries
|
||||||
|
val inWorldWidth: Int
|
||||||
|
val inWorldHeight: Int
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (world.size == null) {
|
||||||
|
inWorldWidth = CHUNK_SIZE
|
||||||
|
inWorldHeight = CHUNK_SIZE
|
||||||
|
} else {
|
||||||
|
inWorldWidth = (world.size.x - pos.tileX).coerceAtMost(CHUNK_SIZE)
|
||||||
|
inWorldHeight = (world.size.y - pos.tileY).coerceAtMost(CHUNK_SIZE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// local cells' tile access
|
// local cells' tile access
|
||||||
val localBackgroundView = TileView.Background(this)
|
val localBackgroundView = TileView.Background(this)
|
||||||
val localForegroundView = TileView.Foreground(this)
|
val localForegroundView = TileView.Foreground(this)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.kstarbound.world
|
package ru.dbotthepony.kstarbound.world
|
||||||
|
|
||||||
|
import com.google.common.base.Predicate
|
||||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap
|
||||||
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
|
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
|
||||||
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet
|
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet
|
||||||
@ -158,8 +159,14 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
abstract operator fun get(x: Int, y: Int): ChunkType?
|
abstract operator fun get(x: Int, y: Int): ChunkType?
|
||||||
operator fun get(pos: ChunkPos) = get(pos.x, pos.y)
|
operator fun get(pos: ChunkPos) = get(pos.x, pos.y)
|
||||||
|
|
||||||
|
open fun chunkFromCell(x: Int, y: Int): ChunkType? {
|
||||||
|
return get(ChunkPos.component(x), ChunkPos.component(y))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun chunkFromCell(pos: IStruct2i) = chunkFromCell(pos.component1(), pos.component2())
|
||||||
|
|
||||||
override fun getCell(x: Int, y: Int): IChunkCell {
|
override fun getCell(x: Int, y: Int): IChunkCell {
|
||||||
return get(ChunkPos.component(x), ChunkPos.component(y))?.getCell(x and CHUNK_SIZE_MASK, y and CHUNK_SIZE_MASK) ?: IChunkCell.Companion
|
return chunkFromCell(x, y)?.getCell(x and CHUNK_SIZE_MASK, y and CHUNK_SIZE_MASK) ?: IChunkCell.Companion
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract operator fun set(x: Int, y: Int, chunk: ChunkType)
|
abstract operator fun set(x: Int, y: Int, chunk: ChunkType)
|
||||||
@ -235,6 +242,12 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
return map[this.x.chunk(x), this.y.chunk(y)]
|
return map[this.x.chunk(x), this.y.chunk(y)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun chunkFromCell(x: Int, y: Int): ChunkType? {
|
||||||
|
val ix = this.x.cell(x)
|
||||||
|
val iy = this.y.cell(y)
|
||||||
|
return get(ix ushr CHUNK_SIZE_BITS, iy ushr CHUNK_SIZE_BITS)
|
||||||
|
}
|
||||||
|
|
||||||
override fun getCell(x: Int, y: Int): IChunkCell {
|
override fun getCell(x: Int, y: Int): IChunkCell {
|
||||||
val ix = this.x.cell(x)
|
val ix = this.x.cell(x)
|
||||||
val iy = this.y.cell(y)
|
val iy = this.y.cell(y)
|
||||||
@ -422,31 +435,7 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
/**
|
/**
|
||||||
* Возвращает все чанки, которые пересекаются с заданным [boundingBox]
|
* Возвращает все чанки, которые пересекаются с заданным [boundingBox]
|
||||||
*/
|
*/
|
||||||
fun collect(boundingBox: AABBi): List<ChunkType> {
|
fun collectWithPosition(boundingBox: AABBi): List<Pair<ChunkPos, ChunkType>> {
|
||||||
val output = ArrayList<ChunkType>()
|
|
||||||
|
|
||||||
if (
|
|
||||||
chunkMap.x.cellOffset(boundingBox.mins.x) == chunkMap.x.cellOffset(boundingBox.maxs.x) &&
|
|
||||||
chunkMap.y.cellOffset(boundingBox.mins.y) == chunkMap.y.cellOffset(boundingBox.maxs.y)
|
|
||||||
) {
|
|
||||||
for (pos in boundingBox.chunkPositions) {
|
|
||||||
val chunk = chunkMap[pos]
|
|
||||||
|
|
||||||
if (chunk != null && (loopX || chunkMap.x.inBoundsChunk(pos.x)) && (loopY || chunkMap.y.inBoundsChunk(pos.y))) {
|
|
||||||
output.add(chunk)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Возвращает все чанки, которые пересекаются с заданным [boundingBox]
|
|
||||||
*/
|
|
||||||
fun collectPositionAware(boundingBox: AABBi): List<Pair<ChunkPos, ChunkType>> {
|
|
||||||
val output = ArrayList<Pair<ChunkPos, ChunkType>>()
|
val output = ArrayList<Pair<ChunkPos, ChunkType>>()
|
||||||
|
|
||||||
for (pos in boundingBox.chunkPositions) {
|
for (pos in boundingBox.chunkPositions) {
|
||||||
@ -460,37 +449,18 @@ abstract class World<This : World<This, ChunkType>, ChunkType : Chunk<This, Chun
|
|||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
fun testSpace(aabb: AABB, predicate: Predicate<IChunkCell> = Predicate { it.foreground.material != null }): Boolean {
|
||||||
* Возвращает, застрянет ли сущность будет с коллизией [worldaabb]
|
val tiles = aabb.encasingIntAABB()
|
||||||
*/
|
|
||||||
fun isSpaceEmptyFromTiles(worldaabb: AABB): Boolean {
|
|
||||||
val collected = collect(worldaabb.encasingChunkPosAABB())
|
|
||||||
.map { it.collisionLayers() }
|
|
||||||
.flatten()
|
|
||||||
.sortedWith { o1, o2 ->
|
|
||||||
val a = o1.distance(worldaabb)
|
|
||||||
val b = o2.distance(worldaabb)
|
|
||||||
|
|
||||||
if (a == b)
|
for (x in tiles.mins.x .. tiles.maxs.x) {
|
||||||
return@sortedWith 0
|
for (y in tiles.mins.y .. tiles.maxs.y) {
|
||||||
|
if (predicate.test(chunkMap.getCell(x, y))) {
|
||||||
if (a > b)
|
return true
|
||||||
return@sortedWith 1
|
}
|
||||||
|
|
||||||
return@sortedWith -1
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collected.isEmpty()) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
for (aabb in collected) {
|
|
||||||
if (aabb.intersectWeak(worldaabb)) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Свет
|
// Свет
|
||||||
|
@ -51,7 +51,7 @@ class PlayerMovementController(entity: PlayerEntity) : WalkableMovementControlle
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun canUnDuck(): Boolean {
|
override fun canUnDuck(): Boolean {
|
||||||
return world.isSpaceEmptyFromTiles(STANDING_AABB + position)
|
return !world.testSpace(STANDING_AABB + position)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
Loading…
Reference in New Issue
Block a user