full state machine
This commit is contained in:
1 parent
b8cdde4763
commit
edef8f7f8f
10 files changed
+379
-3
No files matched your search
+36
-2
@@ -17,7 +17,9 @@ var pushEnabled:bool = false
|
||||
@onready var right_spawn = $RightSpawn
|
||||
@onready var left_spawn = $LeftSpawn
|
||||
|
||||
var upJump:bool = false
|
||||
@onready var jump_buffer_timer: Timer = $JumpBufferTimer
|
||||
@onready var player_graphic: AnimatedSprite2D = $AnimatedSprite2D
|
||||
|
||||
#For State Machine.............t.w.
|
||||
enum State{IDLE, RUNNING, JUMP, FALLING}
|
||||
@@ -29,15 +31,38 @@ func _physics_process(delta):
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
update_states()
|
||||
update_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
#Create a State Machine.........t.w.
|
||||
func update_states()->void:
|
||||
match current_state:
|
||||
State.JUMP when velocity.y >0:
|
||||
State.IDLE when velocity.x !=0:
|
||||
current_state = State.RUNNING
|
||||
State.RUNNING:
|
||||
if velocity.x ==0:
|
||||
current_state = State.IDLE
|
||||
#jumping when reaching apex
|
||||
State.JUMP when velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
|
||||
State.FALLING when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
else:
|
||||
current_state = State.RUNNING
|
||||
func update_animation():
|
||||
match current_state:
|
||||
State.IDLE:
|
||||
player_graphic.play("idle")
|
||||
State.RUNNING:
|
||||
player_graphic.play("run")
|
||||
State.JUMP:
|
||||
if upJump:
|
||||
player_graphic.play("jump")
|
||||
State.FALLING:
|
||||
player_graphic.play("fall")
|
||||
|
||||
func handle_input()->void:
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
@@ -46,8 +71,10 @@ func handle_input()->void:
|
||||
direction = Input.get_axis("moveLeft", "moveRight")
|
||||
if direction <0:
|
||||
facing=FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
if direction >0:
|
||||
facing=FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
print("I want to shove")
|
||||
@@ -95,6 +122,7 @@ func handle_movement(_delta)->void:
|
||||
velocity.y = JUMP_VELOCITY
|
||||
#change state...........t.w.
|
||||
current_state = State.JUMP
|
||||
upJump = true
|
||||
jump_buffer_timer.stop()
|
||||
|
||||
func handle_collisions()->void:
|
||||
@@ -119,3 +147,9 @@ func handle_collisions()->void:
|
||||
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_state:
|
||||
State.JUMP:
|
||||
upJump=false
|
||||
Reference in new issue
Block a user