62 lines
1.2 KiB
GDScript
62 lines
1.2 KiB
GDScript
extends CharacterBody2D
|
|
|
|
var gravity = 800
|
|
const SPEED = 60.0
|
|
const JUMP_VELOCITY = -400.0
|
|
@onready var sprite = $Sprites
|
|
|
|
var land_time = 0
|
|
var jumping = false
|
|
var side = 1
|
|
|
|
func _physics_process(delta):
|
|
if not is_on_floor():
|
|
velocity.y += gravity * delta
|
|
elif jumping:
|
|
jumping = false
|
|
sprite.play("Land")
|
|
land_time = 0.33
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
sprite.play("Jump")
|
|
jumping = true
|
|
sprite.pause()
|
|
sprite.set_frame_and_progress(0, 0)
|
|
|
|
if jumping:
|
|
if velocity.y > 50:
|
|
sprite.set_frame_and_progress(2,0)
|
|
elif velocity.y > -150:
|
|
sprite.set_frame_and_progress(1, 0)
|
|
|
|
var speed = SPEED
|
|
if Input.is_key_pressed(KEY_SHIFT):
|
|
speed *= 3
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
if direction:
|
|
velocity.x = direction * speed
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, speed)
|
|
if direction != 0:
|
|
sprite.flip_h = direction == -1
|
|
|
|
|
|
|
|
if land_time >= 0:
|
|
land_time -= delta
|
|
|
|
# If shift left/right then 2 * speed, play run
|
|
if land_time <= 0 and not jumping:
|
|
if velocity.x != 0:
|
|
if Input.is_key_pressed(KEY_SHIFT):
|
|
sprite.play("Run")
|
|
else:
|
|
sprite.play("Walk")
|
|
else:
|
|
sprite.play("Idle")
|
|
|
|
|
|
|
|
move_and_slide()
|