move.gd 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 input_direction.is_zero_approx():
  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. owner.velocity = direction.normalized() * speed
  28. owner.move_and_slide()
  29. if owner.get_slide_collision_count() == 0:
  30. return
  31. return owner.get_slide_collision(0)