Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- var velocity: Vector2 = Vector2.ZERO
- var speed = 400
- var in_attack_range = false
- var path : Array = []
- var nav: Navigation2D = null
- var target_loc = null
- var threshold = 1
- func _ready():
- yield(get_tree(), "idle_frame")
- var tree = get_tree()
- if tree.has_group("LevelNavigation"):
- nav = tree.get_nodes_in_group("LevelNavigation")[0]
- var timer = 0
- func _physics_process(delta):
- if in_attack_range and !path.empty():
- _navigate()
- if Input.is_action_just_pressed("LMB"):
- target_loc = get_global_mouse_position()
- _attack_range_transition()
- _generate_path()
- func _generate_path():
- if nav != null and target_loc != null:
- path = nav.get_simple_path(global_position, target_loc, false)
- print(path)
- func _navigate():
- var direction = global_position.direction_to(path[0])
- velocity = direction * speed
- velocity = move_and_slide(velocity)
- if global_position.distance_to(path[0]) < threshold:
- path.pop_front()
- func _attack_range_transition():
- if in_attack_range == false:
- in_attack_range = true
- print("on")
- else:
- in_attack_range = false
- print("off")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement