101 lines
2.0 KiB
GDScript
101 lines
2.0 KiB
GDScript
extends CharacterBody2D
|
|
|
|
var gravity = 800
|
|
const SPEED = 200.0
|
|
const JUMP_VELOCITY = -400.0
|
|
@onready var sprite = $Sprites
|
|
|
|
var land_time = 0
|
|
var jumping = false
|
|
var side = 1
|
|
var double_jump = false
|
|
|
|
enum State {
|
|
Idle,
|
|
Run,
|
|
Walk,
|
|
Jump,
|
|
AirSpin,
|
|
Landing
|
|
}
|
|
|
|
enum JumpState {
|
|
}
|
|
|
|
func _play_anim(state: State, velocity: Vector2):
|
|
match state:
|
|
State.Idle:
|
|
sprite.play("Idle")
|
|
State.Run:
|
|
sprite.play("Run")
|
|
State.Walk:
|
|
sprite.play("Walk")
|
|
State.Jump:
|
|
sprite.pause()
|
|
sprite.set_frame_and_progress(0, 0)
|
|
if velocity.y > 50:
|
|
sprite.set_frame_and_progress(2,0)
|
|
elif velocity.y > -150:
|
|
sprite.set_frame_and_progress(1, 0)
|
|
State.AirSpin:
|
|
pass
|
|
State.Landing:
|
|
sprite.play("Land")
|
|
|
|
func _process_velocity(state: State, delta):
|
|
pass
|
|
|
|
func _physics_process(delta):
|
|
if not is_on_floor():
|
|
velocity.y += gravity * delta
|
|
elif jumping:
|
|
jumping = false
|
|
sprite.play("Land")
|
|
double_jump = false
|
|
land_time = 0.16
|
|
|
|
if Input.is_action_just_pressed("ui_accept"):
|
|
if is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
sprite.play("Jump")
|
|
jumping = true
|
|
sprite.pause()
|
|
sprite.set_frame_and_progress(0, 0)
|
|
elif not double_jump:
|
|
sprite.play("AirSpin")
|
|
double_jump = true
|
|
var boost = 0 if velocity.y > 0 else velocity.y / 3
|
|
velocity.y = JUMP_VELOCITY / 1.4 + boost
|
|
|
|
if jumping and not double_jump:
|
|
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("left", "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("Walk")
|
|
else:
|
|
sprite.play("Run")
|
|
else:
|
|
sprite.play("Idle")
|
|
|
|
move_and_slide()
|