Advertisement
Guest User

Untitled

a guest
Apr 9th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. var velocity: Vector2 = Vector2.ZERO
  4. var speed = 400
  5.  
  6. var in_attack_range = false
  7.  
  8. var path : Array = []
  9. var nav: Navigation2D = null
  10. var target_loc = null
  11. var threshold = 1
  12.  
  13. func _ready():
  14. yield(get_tree(), "idle_frame")
  15. var tree = get_tree()
  16. if tree.has_group("LevelNavigation"):
  17. nav = tree.get_nodes_in_group("LevelNavigation")[0]
  18.  
  19. var timer = 0
  20.  
  21. func _physics_process(delta):
  22.  
  23. if in_attack_range and !path.empty():
  24. _navigate()
  25.  
  26. if Input.is_action_just_pressed("LMB"):
  27. target_loc = get_global_mouse_position()
  28. _attack_range_transition()
  29. _generate_path()
  30.  
  31. func _generate_path():
  32. if nav != null and target_loc != null:
  33. path = nav.get_simple_path(global_position, target_loc, false)
  34. print(path)
  35.  
  36. func _navigate():
  37. var direction = global_position.direction_to(path[0])
  38. velocity = direction * speed
  39. velocity = move_and_slide(velocity)
  40. if global_position.distance_to(path[0]) < threshold:
  41. path.pop_front()
  42.  
  43. func _attack_range_transition():
  44. if in_attack_range == false:
  45. in_attack_range = true
  46. print("on")
  47. else:
  48. in_attack_range = false
  49. print("off")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement