From b7dba429cfc5706751e4b228df9f174fff5ec14c Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Tue, 20 Aug 2024 15:14:40 +1000 Subject: [PATCH] buffer jump input --- Sources/Voxelotl/Player.swift | 20 ++++++++++++++++---- Sources/Voxelotl/Random.swift | 8 ++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 Sources/Voxelotl/Random.swift diff --git a/Sources/Voxelotl/Player.swift b/Sources/Voxelotl/Player.swift index dc8ba28..a8cdf26 100644 --- a/Sources/Voxelotl/Player.swift +++ b/Sources/Voxelotl/Player.swift @@ -22,6 +22,7 @@ struct Player { private var _rotation = SIMD2.zero private var _onGround: Bool = false + private var _shouldJump: Optional = .none public var position: SIMD3 { get { self._position } set { self._position = newValue } } public var velocity: SIMD3 { 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) diff --git a/Sources/Voxelotl/Random.swift b/Sources/Voxelotl/Random.swift new file mode 100644 index 0000000..45ede1a --- /dev/null +++ b/Sources/Voxelotl/Random.swift @@ -0,0 +1,8 @@ +// +// Random.swift +// voxelotl +// +// Created by a dinosaur on 20/8/2024. +// + +import Foundation