extends Area2D

class_name ProbeElectric

const center = Vector2.ONE * 16

var neighbours = []

var network: PowerNetwork = null

master func _ready():
	if network == null:
		network = PowerNetwork.new()
		network.add_node(self)
		$"/root/scene/systems".add_child(network, true)

master func _physics_process(_delta):
	if PowerNetwork.DEBUG:
		update()

func _draw():
	if PowerNetwork.DEBUG:
		draw_circle(center, 4, network.debugColor)
		for neighbour in neighbours:
			var delta = (neighbour.global_position - global_position) / global_scale
			draw_line(center, delta + center, network.debugColor, 2)

master func _got_neighbour(area: Area2D):
	if area == self:
		return
	if area.network == null:
		area.network = network
		network.rpc("add_node", area)
	elif area.network != network:
		# Merge networks
		network.rpc("join", area.network)
	neighbours.push_back(area)
