spawn and reset on top of the chunk

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

View File

@ -30,13 +30,14 @@ class Game: GameDelegate {
var camera = Camera(fov: 60, size: .one, range: 0.06...900)
var player = Player()
var projection: matrix_float4x4 = .identity
var boxes: [Box] = []
var chunk = Chunk(position: .zero)
init() {
player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5)
player.rotation = .init(.pi, 0)
let options: [BlockType] = [
.air,
.solid(.blue),
@ -54,10 +55,9 @@ class Game: GameDelegate {
.solid(.magenta),
.air,
]
chunk
.fill(allBy: { options[Int(arc4random_uniform(UInt32(options.count)))] })
chunk.fill(allBy: { options[Int(arc4random_uniform(UInt32(options.count)))] })
}
func fixedUpdate(_ time: GameTime) {
}
@ -71,8 +71,13 @@ class Game: GameDelegate {
if let pad = GameController.current?.state {
if pad.pressed(.south) {
chunk
.setBlockInternally(at: SIMD3(player.position - SIMD3(0, 2, 0)), type: .air)
chunk.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 = []
@ -80,21 +85,16 @@ class Game: GameDelegate {
if block.type == .air {
return
}
if case let .solid(color) = block.type {
boxes
.append(
Box(
geometry:
.fromUnitCube(position: SIMD3<Float>(position) + 0.5, scale: .init(repeating: 0.5)),
color: color
)
)
boxes.append(Box(
geometry: .fromUnitCube(position: SIMD3<Float>(position) + 0.5, scale: .init(repeating: 0.5)),
color: color))
}
}
player.update(deltaTime: deltaTime, boxes: boxes)
camera.position = player.position
camera.position = player.eyePosition
camera.rotation =
simd_quatf(angle: player.rotation.y, axis: .init(1, 0, 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
public var position: SIMD3<Float> {
get { self._position + .init(0, Self.eyeLevel, 0) } set { self._position = newValue }
}
public var rotation: SIMD2<Float> { self._rotation }
public var position: SIMD3<Float> { get { self._position } 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 eyePosition: SIMD3<Float> { self._position + .init(0, Self.eyeLevel, 0) }
mutating func update(deltaTime: Float, boxes: [Box]) {
if let pad = GameController.current?.state {
@ -56,17 +57,9 @@ struct Player {
self._onGround = false
}
}
// Flying and unflying
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
@ -119,11 +112,11 @@ struct Player {
self._velocity.x = 0
self._velocity.z = 0
}
if self._velocity.x > 10 {
self._velocity.x = 10
}
if self._velocity.y > 10 {
self._velocity.y = 10
}