Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- enum ORIENTATION {
- UP,
- DOWN,
- LEFT,
- RIGHT
- }
- enum STATES {
- WALK,
- RUN,
- IDLE
- }
- var current_state = null
- var orientation = DOWN
- var input_direction = Vector2()
- var last_direction = Vector2(1, 0)
- const MAX_WALK_SPEED = 400
- const MAX_RUN_SPEED = 700
- var speed = 0.0
- var max_speed = 0.0
- var velocity = Vector2()
- func change_direction(direction):
- if direction.y == -1:
- orientation = UP
- if direction.y == 1:
- orientation = DOWN
- if direction.x == 1:
- orientation = RIGHT
- if direction.x == -1:
- orientation = LEFT
- change_state(current_state)
- func change_state(state):
- current_state = state
- match current_state:
- IDLE:
- match orientation:
- UP:
- $AnimationPlayer.play('idle up')
- DOWN:
- $AnimationPlayer.play('idle down')
- LEFT:
- $AnimationPlayer.play("idle left")
- RIGHT:
- $AnimationPlayer.play("idle right")
- WALK:
- match orientation:
- UP:
- $AnimationPlayer.play('walk up')
- DOWN:
- $AnimationPlayer.play('walk down')
- LEFT:
- $AnimationPlayer.play("walk left")
- RIGHT:
- $AnimationPlayer.play("walk right")
- RUN:
- match orientation:
- UP:
- $AnimationPlayer.play('walk up')
- DOWN:
- $AnimationPlayer.play('walk down')
- LEFT:
- $AnimationPlayer.play("walk left")
- RIGHT:
- $AnimationPlayer.play("walk right")
- func _physics_process(delta):
- update_direction()
- move(delta)
- func update_direction():
- if input_direction and last_direction != input_direction:
- last_direction = input_direction
- change_direction(input_direction)
- func move(delta):
- if input_direction:
- if speed != max_speed:
- speed = max_speed
- change_state(WALK)
- $Label.text = "WALK"
- else:
- speed = 0
- change_state(IDLE)
- $Label.text = "IDLE"
- velocity = input_direction.normalized() * speed
- move_and_slide(velocity)
Add Comment
Please, Sign In to add comment