spawn and reset on top of the chunk

This commit is contained in:
2024-08-20 01:03:58 +10:00
parent 8ec9138b51
commit 2c0b83e671
2 changed files with 26 additions and 33 deletions

View File

@ -34,9 +34,10 @@ class Game: GameDelegate {
var boxes: [Box] = [] var boxes: [Box] = []
var chunk = Chunk(position: .zero) var chunk = Chunk(position: .zero)
init() { init() {
player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5) player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5)
player.rotation = .init(.pi, 0)
let options: [BlockType] = [ let options: [BlockType] = [
.air, .air,
.solid(.blue), .solid(.blue),
@ -54,8 +55,7 @@ class Game: GameDelegate {
.solid(.magenta), .solid(.magenta),
.air, .air,
] ]
chunk chunk.fill(allBy: { options[Int(arc4random_uniform(UInt32(options.count)))] })
.fill(allBy: { options[Int(arc4random_uniform(UInt32(options.count)))] })
} }
func fixedUpdate(_ time: GameTime) { func fixedUpdate(_ time: GameTime) {
@ -71,8 +71,13 @@ class Game: GameDelegate {
if let pad = GameController.current?.state { if let pad = GameController.current?.state {
if pad.pressed(.south) { if pad.pressed(.south) {
chunk chunk.setBlockInternally(at: SIMD3(player.position - SIMD3(0, 2, 0)), type: .air)
.setBlockInternally(at: SIMD3(player.position - SIMD3(0, 2, 0)), type: .air) }
// 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)
} }
} }
boxes = [] boxes = []
@ -82,19 +87,14 @@ class Game: GameDelegate {
} }
if case let .solid(color) = block.type { if case let .solid(color) = block.type {
boxes boxes.append(Box(
.append( geometry: .fromUnitCube(position: SIMD3<Float>(position) + 0.5, scale: .init(repeating: 0.5)),
Box( color: color))
geometry:
.fromUnitCube(position: SIMD3<Float>(position) + 0.5, scale: .init(repeating: 0.5)),
color: color
)
)
} }
} }
player.update(deltaTime: deltaTime, boxes: boxes) player.update(deltaTime: deltaTime, boxes: boxes)
camera.position = player.position camera.position = player.eyePosition
camera.rotation = camera.rotation =
simd_quatf(angle: player.rotation.y, axis: .init(1, 0, 0)) * simd_quatf(angle: player.rotation.y, axis: .init(1, 0, 0)) *
simd_quatf(angle: player.rotation.x, axis: .init(0, 1, 0)) simd_quatf(angle: player.rotation.x, axis: .init(0, 1, 0))

View File

@ -20,10 +20,11 @@ struct Player {
private var _onGround: Bool = false private var _onGround: Bool = false
public var position: SIMD3<Float> { public var position: SIMD3<Float> { get { self._position } set { self._position = newValue } }
get { self._position + .init(0, Self.eyeLevel, 0) } set { self._position = newValue } public var velocity: SIMD3<Float> { get { self._velocity } set { self._velocity = newValue } }
} public var rotation: SIMD2<Float> { get { self._rotation } set { self._rotation = newValue } }
public var rotation: SIMD2<Float> { self._rotation }
public var eyePosition: SIMD3<Float> { self._position + .init(0, Self.eyeLevel, 0) }
mutating func update(deltaTime: Float, boxes: [Box]) { mutating func update(deltaTime: Float, boxes: [Box]) {
if let pad = GameController.current?.state { if let pad = GameController.current?.state {
@ -59,14 +60,6 @@ struct Player {
// Flying and unflying // Flying and unflying
self._velocity.y += (pad.rightTrigger - pad.leftTrigger) * 36 * deltaTime self._velocity.y += (pad.rightTrigger - pad.leftTrigger) * 36 * deltaTime
// Reset
if pad.pressed(.back) {
self._position = .zero
self._velocity = .zero
self._rotation = .zero
self._onGround = false
}
} }
// Apply gravity // Apply gravity