Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- onready var Anim = $AnimatedSprite3D
- var velocity = Vector3()
- var is_walk = false
- var current_state := 0
- enum {WALK, ATTACK, IDLE}
- var enter_state := true
- func _physics_process(delta):
- match current_state:
- WALK:
- _walk_state(delta)
- ATTACK:
- _attack_state(delta)
- IDLE:
- _idle_state(delta)
- # state functions
- func _walk_state(_delta):
- Anim.play("walk")
- _move()
- _move_and_slide()
- _set_state(_check_walk_state())
- func _attack_state(_delta):
- Anim.play("punch")
- velocity.x = 0
- _move_and_slide()
- func _idle_state(_delta):
- Anim.play("idle")
- velocity.x = 0
- _move_and_slide()
- _set_state(_check_idle_state())
- # check functions
- func _check_idle_state():
- var new_state = current_state
- if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right"):
- new_state = WALK
- elif Input.is_action_just_pressed("punch"):
- new_state = ATTACK
- return new_state
- func _check_walk_state():
- var new_state = current_state
- if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right"):
- new_state = WALK
- elif Input.is_action_just_pressed("punch"):
- new_state = ATTACK
- return new_state
- # helpers
- func _move_and_slide():
- velocity = move_and_slide(velocity, Vector3.UP)
- func _move():
- if Input.is_action_pressed("move_left"):
- velocity.x = -100
- $AnimatedSprite3D.flip_h = true
- elif Input.is_action_pressed("move_right"):
- velocity.x = 100
- $AnimatedSprite3D.flip_h = false
- else:
- velocity.x = 0
- Anim.play("idle")
- func _set_state(new_state):
- if new_state != current_state:
- enter_state = true
- current_state = new_state
- func _on_AnimatedSprite3D_animation_finished():
- var anim_name = $AnimatedSprite3D.animation
- if anim_name == "punch":
- _set_state(IDLE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement