2024-08-13 08:38:21 +10:00
|
|
|
import simd
|
|
|
|
|
|
|
|
struct Instance {
|
2024-08-16 00:27:35 +10:00
|
|
|
let position: SIMD3<Float>
|
|
|
|
let scale: SIMD3<Float>
|
|
|
|
let rotation: simd_quatf
|
2024-08-16 22:18:44 +10:00
|
|
|
let color: Color<Float16>
|
2024-08-16 00:27:35 +10:00
|
|
|
|
|
|
|
init(
|
|
|
|
position: SIMD3<Float> = .zero,
|
|
|
|
scale: SIMD3<Float> = .one,
|
|
|
|
rotation: simd_quatf = .identity,
|
2024-08-16 22:18:44 +10:00
|
|
|
color: Color<Float16> = .white
|
2024-08-16 00:27:35 +10:00
|
|
|
) {
|
|
|
|
self.position = position
|
|
|
|
self.scale = scale
|
|
|
|
self.rotation = rotation
|
|
|
|
self.color = color
|
|
|
|
}
|
2024-08-13 08:38:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
class Game: GameDelegate {
|
|
|
|
private var fpsCalculator = FPSCalculator()
|
2024-08-18 18:16:27 -07:00
|
|
|
var camera = Camera(fov: 60, size: .one, range: 0.06...900)
|
2024-08-13 08:38:21 +10:00
|
|
|
var player = Player()
|
|
|
|
var projection: matrix_float4x4 = .identity
|
2024-08-18 18:37:32 -07:00
|
|
|
var chunk = Chunk(position: .zero)
|
|
|
|
|
2024-08-18 18:16:27 -07:00
|
|
|
init() {
|
2024-08-22 03:37:29 +10:00
|
|
|
self.player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5)
|
|
|
|
self.player.rotation = .init(.pi, 0)
|
|
|
|
self.generateWorld()
|
|
|
|
}
|
2024-08-20 01:03:58 +10:00
|
|
|
|
2024-08-22 03:37:29 +10:00
|
|
|
private func generateWorld() {
|
|
|
|
var random = DarwinRandom(seed: Arc4Random.instance.next(in: DarwinRandom.max))
|
|
|
|
self.chunk.fill(allBy: {
|
2024-08-22 03:09:53 +10:00
|
|
|
if (random.next() & 0x1) == 0x1 {
|
2024-08-22 03:49:50 +10:00
|
|
|
.solid(.init(rgb888: UInt32(random.next(in: 0..<0xFFFFFF+1))).linear)
|
2024-08-20 03:21:55 +10:00
|
|
|
} else {
|
|
|
|
.air
|
|
|
|
}
|
|
|
|
})
|
2024-08-18 18:37:32 -07:00
|
|
|
}
|
2024-08-20 01:03:58 +10:00
|
|
|
|
2024-08-18 18:37:32 -07:00
|
|
|
func fixedUpdate(_ time: GameTime) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(_ time: GameTime) {
|
|
|
|
fpsCalculator.frame(deltaTime: time.delta) { fps in
|
|
|
|
print("FPS: \(fps)")
|
|
|
|
}
|
|
|
|
|
|
|
|
let deltaTime = min(Float(time.delta.asFloat), 1.0 / 15)
|
|
|
|
|
|
|
|
if let pad = GameController.current?.state {
|
2024-08-20 02:17:39 +10:00
|
|
|
// Delete block underneath player
|
2024-08-18 18:37:32 -07:00
|
|
|
if pad.pressed(.south) {
|
2024-08-22 03:37:29 +10:00
|
|
|
self.chunk.setBlock(at: SIMD3(player.position + .down * 0.2), type: .air)
|
2024-08-20 01:03:58 +10:00
|
|
|
}
|
2024-08-22 03:37:29 +10:00
|
|
|
|
2024-08-20 01:03:58 +10:00
|
|
|
// Player reset
|
|
|
|
if pad.pressed(.back) {
|
2024-08-22 03:37:29 +10:00
|
|
|
self.player.position = .init(repeating: 0.5) + .init(0, Float(Chunk.chunkSize), 0)
|
|
|
|
self.player.velocity = .zero
|
|
|
|
self.player.rotation = .init(.pi, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regenerate
|
|
|
|
if pad.pressed(.start) {
|
|
|
|
self.generateWorld()
|
2024-08-18 18:37:32 -07:00
|
|
|
}
|
|
|
|
}
|
2024-08-20 01:03:58 +10:00
|
|
|
|
2024-08-22 03:37:29 +10:00
|
|
|
self.player.update(deltaTime: deltaTime, chunk: chunk)
|
|
|
|
self.camera.position = player.eyePosition
|
|
|
|
self.camera.rotation = player.eyeRotation
|
2024-08-13 08:38:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func draw(_ renderer: Renderer, _ time: GameTime) {
|
2024-08-20 03:21:55 +10:00
|
|
|
let instances = chunk.compactMap { block, position in
|
|
|
|
if case let .solid(color) = block.type {
|
|
|
|
Instance(
|
|
|
|
position: SIMD3<Float>(position) + 0.5,
|
|
|
|
scale: .init(repeating: 0.5),
|
2024-08-22 03:49:50 +10:00
|
|
|
color: color)
|
2024-08-20 03:21:55 +10:00
|
|
|
} else { nil }
|
2024-08-16 00:27:35 +10:00
|
|
|
}
|
2024-08-22 03:09:53 +10:00
|
|
|
if !instances.isEmpty {
|
|
|
|
renderer.batch(instances: instances, camera: self.camera)
|
|
|
|
}
|
2024-08-13 08:38:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func resize(_ size: Size<Int>) {
|
|
|
|
self.camera.setSize(size)
|
|
|
|
}
|
|
|
|
}
|