35 lines
835 B
GDScript
35 lines
835 B
GDScript
extends CharacterBody2D
|
|
|
|
const SPEED = 400.0
|
|
const ACCEL = 7.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
const FALL_SPEED = -JUMP_VELOCITY * 2
|
|
var falling = false
|
|
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
|
|
var direction = Vector2.ZERO
|
|
|
|
func _physics_process(delta):
|
|
if not is_on_floor():
|
|
var lowGrav = velocity.y < 0 and Input.is_action_pressed("jump")
|
|
|
|
velocity.y += gravity * delta / gen.boolToNumb(lowGrav, 1)
|
|
else:
|
|
falling = false
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
if Input.is_action_just_pressed("down") and not falling:
|
|
falling = true
|
|
velocity.y = clamp(velocity.y + FALL_SPEED, -INF, FALL_SPEED)
|
|
|
|
direction = round(Input.get_axis("left", "right"))
|
|
|
|
velocity.x = lerp(velocity.x, direction * SPEED, delta * ACCEL)
|
|
|
|
move_and_slide()
|