added more tunable basic movement

This commit is contained in:
2024-09-07 12:08:47 -04:00
parent b726bcf4aa
commit 0f41e0d017
4 changed files with 69 additions and 18 deletions

View File

@@ -1,25 +1,53 @@
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const JUMP_VELOCITY = -600.0
const MAX_JUMP_DURATION = 0.3
const MIN_JUMP_DURATION = 0.1
const GRAVITY = 980
const FALL_GRAVITY_MULTIPLIER = 1.5
var jump_timer = 0.0
var is_jumping = false
func _physics_process(delta: float) -> void:
# Add the gravity.
func _physics_process(delta):
# Apply gravity
if not is_on_floor():
velocity += get_gravity() * delta
var gravity_multiplier = FALL_GRAVITY_MULTIPLIER if velocity.y > 0 else 1.0
velocity.y += GRAVITY * gravity_multiplier * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle Jump
if Input.is_action_just_pressed("jump") and is_on_floor():
start_jump()
if is_jumping:
jump_timer += delta
if Input.is_action_pressed("jump") and jump_timer < MAX_JUMP_DURATION:
continue_jump(delta)
else:
end_jump()
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
# Handle horizontal movement
var direction = Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func start_jump():
velocity.y = JUMP_VELOCITY
is_jumping = true
jump_timer = 0.0
func continue_jump(delta):
if jump_timer > MIN_JUMP_DURATION:
var t = (jump_timer - MIN_JUMP_DURATION) / (MAX_JUMP_DURATION - MIN_JUMP_DURATION)
var ease_factor = 1.0 - t * t # Quadratic easing
velocity.y += JUMP_VELOCITY * ease_factor * delta
func end_jump():
is_jumping = false
if velocity.y < 0:
velocity.y *= 0.5 # Cut the upward velocity to end the jump early if button is released