Files
voxelotl-engine/Sources/Voxelotl/Game.swift

96 lines
2.4 KiB
Swift
Raw Normal View History

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
struct Instance {
let position: SIMD3<Float>
let scale: SIMD3<Float>
let rotation: simd_quatf
let color: Color<Float16>
init(
position: SIMD3<Float> = .zero,
scale: SIMD3<Float> = .one,
rotation: simd_quatf = .identity,
color: Color<Float16> = .white
) {
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-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-20 03:21:55 +10:00
let colors: [Color<UInt8>] = [
.white,
.red, .blue, .green,
.magenta, .yellow, .cyan
2024-08-18 18:16:27 -07:00
]
2024-08-20 03:21:55 +10:00
chunk.fill(allBy: {
if (arc4random() & 0x1) == 0x1 {
.solid(colors[Int(arc4random_uniform(UInt32(colors.count)))])
} 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-20 03:21:55 +10:00
chunk.setBlock(at: SIMD3(player.position + .down * 0.2), type: .air)
2024-08-20 01:03:58 +10:00
}
// Player reset
if pad.pressed(.back) {
2024-08-20 03:21:55 +10:00
player.position = .init(repeating: 0.5) + .init(0, Float(Chunk.chunkSize), 0)
2024-08-20 01:03:58 +10:00
player.velocity = .zero
player.rotation = .init(.pi, 0)
2024-08-18 18:37:32 -07:00
}
}
2024-08-20 01:03:58 +10:00
2024-08-20 03:21:55 +10:00
player.update(deltaTime: deltaTime, chunk: chunk)
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-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),
color: Color<Float16>(color).linear)
} else { nil }
}
2024-08-13 08:38:21 +10:00
renderer.batch(instances: instances, camera: self.camera)
}
func resize(_ size: Size<Int>) {
self.camera.setSize(size)
}
}