move.gd 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. extends "on_ground.gd"
  2. @export var max_walk_speed: float = 450
  3. @export var max_run_speed: float = 700
  4. func enter():
  5. speed = 0.0
  6. velocity = Vector2()
  7. var input_direction = get_input_direction()
  8. update_look_direction(input_direction)
  9. owner.get_node(^"AnimationPlayer").play("walk")
  10. func handle_input(event):
  11. return super.handle_input(event)
  12. func update(_delta):
  13. var input_direction = get_input_direction()
  14. if not input_direction:
  15. emit_signal("finished", "idle")
  16. update_look_direction(input_direction)
  17. if Input.is_action_pressed("run"):
  18. speed = max_run_speed
  19. else:
  20. speed = max_walk_speed
  21. var collision_info = move(speed, input_direction)
  22. if not collision_info:
  23. return
  24. if speed == max_run_speed and collision_info.collider.is_in_group("environment"):
  25. return null
  26. func move(speed, direction):
  27. velocity = direction.normalized() * speed
  28. # TODO: This information should be set to the CharacterBody properties instead of arguments.
  29. owner.move_and_slide(velocity, Vector2(), 5, 2)
  30. if owner.get_slide_count() == 0:
  31. return
  32. return owner.get_slide_collision(0)