41 lines
1.0 KiB
GDScript
41 lines
1.0 KiB
GDScript
extends Node2D
|
|
@export var player: CharacterBody2D
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
%mouse_finder.position=get_global_mouse_position()
|
|
|
|
|
|
func _on_control_gui_input(event: InputEvent) -> void:
|
|
if event.is_action_pressed("mouse_right"):
|
|
if player:
|
|
player.sent_message("move",player.get_global_mouse_position())
|
|
|
|
|
|
if event.is_action_pressed("mouse_left"):
|
|
var arr=%mouse_finder.get_overlapping_bodies()
|
|
print(arr)
|
|
if arr.size()==0:
|
|
return
|
|
var target=get_closest_node(player,arr,Unit)
|
|
print(target)
|
|
if target!=null:
|
|
|
|
player.sent_message("attack",target)
|
|
pass
|
|
|
|
pass # Replace with function body.
|
|
|
|
func get_closest_node(self_node:Node2D,array:Array,target_class):
|
|
if array.size()==0:
|
|
return null
|
|
var length=999999999
|
|
var node=null
|
|
for i in range(0,array.size()):
|
|
print(is_instance_of(i,target_class))
|
|
if is_instance_of(array[i],target_class) and array[i]!=self_node:
|
|
var l=(array[i].global_position-self_node.global_position).length()
|
|
if l<length:
|
|
length=l
|
|
node=array[i]
|
|
return node
|