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.
127 lines
2.7 KiB
127 lines
2.7 KiB
extends CharacterBody2D
|
|
|
|
class_name Player
|
|
|
|
const SPEED = 100.0
|
|
const JUMP_VELOCITY = 200.0
|
|
|
|
const STATES= [
|
|
"spawn",
|
|
"idle",
|
|
"walk",
|
|
"climb",
|
|
"jump",
|
|
"die"
|
|
]
|
|
|
|
var use_gravity = {
|
|
"climb": false,
|
|
"die": false,
|
|
"idle": true,
|
|
"jump": true,
|
|
"spawn": true,
|
|
"walk": true
|
|
}
|
|
|
|
# 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)
|
|
add_to_group("player")
|
|
|
|
func _enter_spawn():
|
|
$fsm.set_next_state("idle")
|
|
|
|
func on_enter_idle_state():
|
|
pass
|
|
|
|
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 != 0:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = 0
|
|
$fsm.set_next_state("idle")
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
$fsm.set_next_state("jump")
|
|
|
|
# detect collision with wall or ceiling to enter climb state
|
|
if is_on_wall() or is_on_ceiling():
|
|
velocity.y = 0
|
|
$fsm.set_next_state("climb")
|
|
|
|
func on_enter_jump_state():
|
|
if is_on_floor():
|
|
velocity.y = -JUMP_VELOCITY
|
|
|
|
func jump_state(_delta):
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
if(direction):
|
|
velocity.x = move_toward(velocity.x, direction * SPEED, SPEED)
|
|
|
|
if is_on_floor():
|
|
if direction:
|
|
$fsm.set_next_state("walk")
|
|
else:
|
|
velocity.x = 0
|
|
$fsm.set_next_state("idle")
|
|
|
|
if is_on_wall() or is_on_ceiling():
|
|
$fsm.set_next_state("climb")
|
|
|
|
func on_enter_climb_state():
|
|
$sprite.modulate = Color(1, 0, 0)
|
|
velocity = Vector2(0, 0)
|
|
|
|
func on_exit_climb_state():
|
|
$sprite.modulate = Color(1, 1, 1)
|
|
|
|
func climb_state(_delta):
|
|
if is_on_wall():
|
|
var direction = Input.get_axis("ui_up", "ui_down")
|
|
velocity = Vector2(0, direction * SPEED)
|
|
|
|
if is_on_ceiling():
|
|
var direction = Input.get_axis("ui_left", "ui_right")
|
|
velocity = Vector2(direction * SPEED, 0)
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_wall():
|
|
var wall_position = get_slide_collision(0).get_normal().x
|
|
var jump_normal = Vector2(wall_position, -0.5).normalized()
|
|
velocity = jump_normal * JUMP_VELOCITY
|
|
|
|
$fsm.set_next_state("jump")
|
|
if is_on_ceiling() && Input.is_action_just_pressed("ui_down"):
|
|
velocity.y = 0
|
|
$fsm.set_next_state("idle")
|
|
|
|
if !is_on_wall() && !is_on_ceiling():
|
|
$fsm.set_next_state("idle")
|
|
|
|
|
|
func after_state(delta):
|
|
move_and_slide()
|
|
|
|
if use_gravity[$fsm.current_state] == true:
|
|
velocity.y += gravity * delta
|
|
|
|
|
|
func die():
|
|
$fsm.set_next_state("die")
|
|
|
|
func on_enter_die_state():
|
|
velocity = Vector2(0, -500)
|
|
$shape.disabled = true
|
|
# $AnimationPlayer.play("die")
|
|
|