You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
extends CharacterBody2D
|
|
|
|
|
|
const SPEED = 300.0
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
const STATES= [
|
|
"spawn",
|
|
"idle",
|
|
"walk",
|
|
"jump",
|
|
"die"
|
|
]
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
|
|
func _ready():
|
|
$fsm.set_states(STATES)
|
|
|
|
func _enter_spawn():
|
|
$fsm.set_next_state("idle")
|
|
|
|
func on_enter_idle_state():
|
|
velocity.x = 0.0
|
|
|
|
func idle_state(_delta):
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
if direction:
|
|
$fsm.set_next_state("walk")
|
|
elif Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
$fsm.set_next_state("jump")
|
|
|
|
func walk_state(_delta):
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
$fsm.set_next_state("idle")
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
$fsm.set_next_state("jump")
|
|
|
|
func on_enter_jump_state():
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
func jump_state(_delta):
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
velocity.x = move_toward(velocity.x, direction * SPEED, SPEED)
|
|
|
|
if is_on_floor():
|
|
if direction:
|
|
$fsm.set_next_state("walk")
|
|
else:
|
|
$fsm.set_next_state("idle")
|
|
|
|
func after_state(delta):
|
|
if $fsm.current_state in ["idle", "walk"]:
|
|
move_and_slide()
|
|
|
|
if not is_on_floor():
|
|
velocity.y += gravity * delta
|
|
|