mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 13:11:33 +00:00
turn the ChunkID type alias into a real type
This commit is contained in:
@ -1,12 +1,6 @@
|
||||
import Foundation
|
||||
|
||||
public class World {
|
||||
public typealias ChunkID = SIMD3<Int>
|
||||
@inline(__always) public static func makeID<F: BinaryFloatingPoint>(position: SIMD3<F>) -> ChunkID {
|
||||
makeID(position: SIMD3(Int(floor(position.x)), Int(floor(position.y)), Int(floor(position.z))))
|
||||
}
|
||||
@inline(__always) public static func makeID(position: SIMD3<Int>) -> ChunkID { position &>> Chunk.shift }
|
||||
|
||||
private var _chunks: Dictionary<ChunkID, Chunk>
|
||||
private var _chunkDamage: Set<ChunkID>
|
||||
private var _generator: any WorldGenerator
|
||||
@ -21,24 +15,24 @@ public class World {
|
||||
}
|
||||
|
||||
func getBlock(at position: SIMD3<Int>) -> Block {
|
||||
return if let chunk = self._chunks[position &>> Chunk.shift] {
|
||||
if let chunk = self._chunks[ChunkID(fromPosition: position)] {
|
||||
chunk.getBlock(at: position)
|
||||
} else { Block(.air) }
|
||||
}
|
||||
|
||||
func setBlock(at position: SIMD3<Int>, type: BlockType) {
|
||||
// Find the chunk containing the block position
|
||||
let chunkID = position &>> Chunk.shift
|
||||
let chunkID = ChunkID(fromPosition: position)
|
||||
if let idx = self._chunks.index(forKey: chunkID) {
|
||||
// Set the block and mark the containing chunk for render update
|
||||
self._chunks.values[idx].setBlock(at: position, type: type)
|
||||
self._chunkDamage.insert(chunkID)
|
||||
|
||||
// Mark adjacent chunks for render update when placing along the chunk border
|
||||
let internalPos = position &- chunkID &<< Chunk.shift
|
||||
let internalPos = position &- chunkID.getPosition()
|
||||
for (i, ofs) in zip(internalPos.indices, [ SIMD3<Int>.X, .Y, .Z ]) {
|
||||
if internalPos[i] == 0 {
|
||||
let id = chunkID &- ofs
|
||||
let id = chunkID &- ChunkID(id: ofs)
|
||||
if let other = self._chunks[id],
|
||||
// optim: Damage adjacent chunk only if block is touching a solid
|
||||
case .solid = other.getBlock(internal: (internalPos &- ofs) & Chunk.mask).type
|
||||
@ -46,7 +40,7 @@ public class World {
|
||||
self._chunkDamage.insert(id)
|
||||
}
|
||||
} else if internalPos[i] == Chunk.size - 1 {
|
||||
let id = chunkID &+ ofs
|
||||
let id = chunkID &+ ChunkID(id: ofs)
|
||||
if let other = self._chunks[id],
|
||||
// optim: Damage adjacent chunk only if block is touching a solid
|
||||
case .solid = other.getBlock(internal: (internalPos &+ ofs) & Chunk.mask).type
|
||||
@ -62,7 +56,7 @@ public class World {
|
||||
self._chunks[chunkID]
|
||||
}
|
||||
|
||||
public func forEachChunk(_ body: @escaping (_ id: SIMD3<Int>, _ chunk: Chunk) throws -> Void) rethrows {
|
||||
public func forEachChunk(_ body: @escaping (_ id: ChunkID, _ chunk: Chunk) throws -> Void) rethrows {
|
||||
for i in self._chunks {
|
||||
try body(i.key, i.value)
|
||||
}
|
||||
@ -80,14 +74,14 @@ public class World {
|
||||
for z in 0..<depth {
|
||||
for y in 0..<height {
|
||||
for x in 0..<width {
|
||||
let chunkID = SIMD3(x, y, z) &- orig
|
||||
let chunkID = ChunkID(id: SIMD3(x, y, z) &- orig)
|
||||
self._chunkGeneration.generate(chunkID: chunkID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func generateSingleChunkUncommitted(chunkID: SIMD3<Int>) -> Chunk {
|
||||
func generateSingleChunkUncommitted(id chunkID: ChunkID) -> Chunk {
|
||||
self._generator.makeChunk(id: chunkID)
|
||||
}
|
||||
|
||||
@ -95,10 +89,10 @@ public class World {
|
||||
self._chunkGeneration.generateAdjacentIfNeeded(position: position)
|
||||
}
|
||||
|
||||
public func addChunk(chunkID: ChunkID, chunk: Chunk) {
|
||||
public func addChunk(id chunkID: ChunkID, chunk: Chunk) {
|
||||
self._chunks[chunkID] = chunk
|
||||
self._chunkDamage.insert(chunkID)
|
||||
for i: ChunkID in [ .X, .Y, .Z ] {
|
||||
for i in ChunkID.axes {
|
||||
for otherID in [ chunkID &- i, chunkID &+ i ] {
|
||||
if self._chunks.keys.contains(otherID) {
|
||||
self._chunkDamage.insert(otherID)
|
||||
|
Reference in New Issue
Block a user