From fb44d77353577d46414a505a32c3425a5562b17d Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 4 Sep 2023 22:07:29 +0700 Subject: [PATCH] Minor cleanups --- .../kstarbound/client/ClientWorld.kt | 3 +- .../ru/dbotthepony/kstarbound/world/Chunk.kt | 14 ++++ .../ru/dbotthepony/kstarbound/world/World.kt | 76 ++++++------------- .../kstarbound/world/entities/PlayerEntity.kt | 2 +- 4 files changed, 40 insertions(+), 55 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientWorld.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientWorld.kt index 503e734d..8b8f9593 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientWorld.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/ClientWorld.kt @@ -2,6 +2,7 @@ package ru.dbotthepony.kstarbound.client import ru.dbotthepony.kstarbound.client.render.LayeredRenderer import ru.dbotthepony.kstarbound.math.encasingChunkPosAABB +import ru.dbotthepony.kstarbound.math.encasingIntAABB import ru.dbotthepony.kstarbound.world.* import ru.dbotthepony.kstarbound.world.api.CHUNK_SIZEf import ru.dbotthepony.kstarbound.world.entities.Entity @@ -32,7 +33,7 @@ class ClientWorld( size: AABB, layers: LayeredRenderer ) { - for (chunk in collectPositionAware(size.encasingChunkPosAABB())) { + for (chunk in collectWithPosition(size.encasingChunkPosAABB())) { val (x, y) = chunk.first val renderer = chunk.second.Renderer( diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt index c47bd346..be757259 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt @@ -61,6 +61,20 @@ abstract class Chunk, This : Chunk, ChunkType : Chunk, ChunkType : Chunk, ChunkType : Chunk { - val output = ArrayList() - - 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> { + fun collectWithPosition(boundingBox: AABBi): List> { val output = ArrayList>() for (pos in boundingBox.chunkPositions) { @@ -460,37 +449,18 @@ abstract class World, ChunkType : Chunk - val a = o1.distance(worldaabb) - val b = o2.distance(worldaabb) + fun testSpace(aabb: AABB, predicate: Predicate = Predicate { it.foreground.material != null }): Boolean { + val tiles = aabb.encasingIntAABB() - if (a == b) - return@sortedWith 0 - - if (a > b) - return@sortedWith 1 - - return@sortedWith -1 - } - - if (collected.isEmpty()) { - return true - } - - for (aabb in collected) { - if (aabb.intersectWeak(worldaabb)) { - return false + for (x in tiles.mins.x .. tiles.maxs.x) { + for (y in tiles.mins.y .. tiles.maxs.y) { + if (predicate.test(chunkMap.getCell(x, y))) { + return true + } } } - return true + return false } // Свет diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PlayerEntity.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PlayerEntity.kt index 67283829..55b29b32 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PlayerEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/entities/PlayerEntity.kt @@ -51,7 +51,7 @@ class PlayerMovementController(entity: PlayerEntity) : WalkableMovementControlle } override fun canUnDuck(): Boolean { - return world.isSpaceEmptyFromTiles(STANDING_AABB + position) + return !world.testSpace(STANDING_AABB + position) } companion object {