2024-08-13 08:38:21 +10:00
|
|
|
import simd
|
2024-08-18 18:16:27 -07:00
|
|
|
import Foundation
|
2024-08-13 08:38:21 +10:00
|
|
|
|
2024-08-16 00:27:35 +10:00
|
|
|
struct Box {
|
|
|
|
var geometry: AABB
|
2024-08-16 22:18:44 +10:00
|
|
|
var color: Color<Float16> = .white
|
2024-08-16 00:27:35 +10:00
|
|
|
}
|
|
|
|
|
2024-08-13 08:38:21 +10:00
|
|
|
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-20 01:03:58 +10:00
|
|
|
|
2024-08-18 18:37:32 -07:00
|
|
|
var boxes: [Box] = []
|
|
|
|
var chunk = Chunk(position: .zero)
|
|
|
|
|
2024-08-18 18:16:27 -07:00
|
|
|
init() {
|
2024-08-18 18:37:32 -07:00
|
|
|
player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5)
|
2024-08-20 01:03:58 +10:00
|
|
|
player.rotation = .init(.pi, 0)
|
|
|
|
|
2024-08-18 18:16:27 -07:00
|
|
|
let options: [BlockType] = [
|
|
|
|
.air,
|
|
|
|
.solid(.blue),
|
|
|
|
.air,
|
|
|
|
.solid(.red),
|
|
|
|
.air,
|
|
|
|
.solid(.green),
|
|
|
|
.air,
|
|
|
|
.solid(.white),
|
|
|
|
.air,
|
|
|
|
.solid(.cyan),
|
|
|
|
.air,
|
|
|
|
.solid(.yellow),
|
|
|
|
.air,
|
|
|
|
.solid(.magenta),
|
|
|
|
.air,
|
|
|
|
]
|
2024-08-20 01:03:58 +10:00
|
|
|
chunk.fill(allBy: { options[Int(arc4random_uniform(UInt32(options.count)))] })
|
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-20 02:17:39 +10:00
|
|
|
chunk.setBlockInternally(at: SIMD3(player.position + .down * 0.2), type: .air)
|
2024-08-20 01:03:58 +10:00
|
|
|
}
|
|
|
|
// Player reset
|
|
|
|
if pad.pressed(.back) {
|
|
|
|
player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5)
|
|
|
|
player.velocity = .zero
|
|
|
|
player.rotation = .init(.pi, 0)
|
2024-08-18 18:37:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
boxes = []
|
2024-08-18 18:16:27 -07:00
|
|
|
chunk.forEach { position, block in
|
|
|
|
if block.type == .air {
|
|
|
|
return
|
|
|
|
}
|
2024-08-20 01:03:58 +10:00
|
|
|
|
2024-08-18 18:16:27 -07:00
|
|
|
if case let .solid(color) = block.type {
|
2024-08-20 01:03:58 +10:00
|
|
|
boxes.append(Box(
|
|
|
|
geometry: .fromUnitCube(position: SIMD3<Float>(position) + 0.5, scale: .init(repeating: 0.5)),
|
|
|
|
color: color))
|
2024-08-18 18:16:27 -07:00
|
|
|
}
|
|
|
|
}
|
2024-08-20 01:03:58 +10:00
|
|
|
|
2024-08-16 00:27:35 +10:00
|
|
|
player.update(deltaTime: deltaTime, boxes: boxes)
|
2024-08-20 01:03:58 +10:00
|
|
|
camera.position = player.eyePosition
|
2024-08-20 02:17:39 +10:00
|
|
|
camera.rotation = player.eyeRotation
|
2024-08-13 08:38:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
func draw(_ renderer: Renderer, _ time: GameTime) {
|
|
|
|
let totalTime = Float(time.total.asFloat)
|
2024-08-19 00:05:53 +10:00
|
|
|
let cubeSpeedMul: Float = 0.1
|
2024-08-13 08:38:21 +10:00
|
|
|
|
2024-08-16 00:27:35 +10:00
|
|
|
var instances: [Instance] = boxes.map {
|
|
|
|
Instance(
|
|
|
|
position: $0.geometry.center,
|
2024-08-16 22:18:44 +10:00
|
|
|
scale: $0.geometry.size * 0.5,
|
|
|
|
color: $0.color)
|
2024-08-16 00:27:35 +10:00
|
|
|
}
|
|
|
|
instances.append(
|
2024-08-13 08:38:21 +10:00
|
|
|
Instance(
|
2024-08-18 18:16:27 -07:00
|
|
|
position: .init(0, sin(totalTime * 1.5 * cubeSpeedMul) * 0.5, 0) * 2,
|
2024-08-16 22:18:44 +10:00
|
|
|
scale: .init(repeating: 0.5),
|
2024-08-19 00:05:53 +10:00
|
|
|
rotation: .init(angle: totalTime * 3.0 * cubeSpeedMul, axis: .init(0, 1, 0)),
|
2024-08-16 22:18:44 +10:00
|
|
|
color: .init(r: 0.5, g: 0.5, b: 1).linear))
|
2024-08-13 08:38:21 +10:00
|
|
|
renderer.batch(instances: instances, camera: self.camera)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resize(_ size: Size<Int>) {
|
|
|
|
self.camera.setSize(size)
|
|
|
|
}
|
|
|
|
}
|