buffer jump input

This commit is contained in:
a dinosaur 2024-08-20 15:14:40 +10:00
parent bcc56956b3
commit b7dba429cf
2 changed files with 24 additions and 4 deletions

View File

@ -22,6 +22,7 @@ struct Player {
private var _rotation = SIMD2<Float>.zero
private var _onGround: Bool = false
private var _shouldJump: Optional<Float> = .none
public var position: SIMD3<Float> { get { self._position } set { self._position = newValue } }
public var velocity: SIMD3<Float> { get { self._velocity } set { self._velocity = newValue } }
@ -46,12 +47,23 @@ struct Player {
self._rotation.y = self._rotation.y.clamp(-.pi * 0.5, .pi * 0.5)
// Jumping
if self._onGround {
if pad.pressed(.east) {
self._velocity.y = Self.jumpVelocity
self._onGround = false
if pad.pressed(.east) {
self._shouldJump = 0.3
} else if self._shouldJump != .none {
if pad.down(.east) {
self._shouldJump! -= deltaTime
if self._shouldJump! <= 0.0 {
self._shouldJump = .none
}
} else {
self._shouldJump = .none
}
}
if self._onGround && self._shouldJump != .none {
self._velocity.y = Self.jumpVelocity
self._onGround = false
self._shouldJump = .none
}
// Movement (slower in air than on ground)
let movement = pad.leftStick.cardinalDeadzone(min: 0.1, max: 1)

View File

@ -0,0 +1,8 @@
//
// Random.swift
// voxelotl
//
// Created by a dinosaur on 20/8/2024.
//
import Foundation