extends Node2D
class_name LightSemaphore

@export var synchronizedSemaphore : LightSemaphore = null
@export var hasOwnTimerEnabled = true
@onready var lights := [$Sempahore/green, $Sempahore/yellow, $Sempahore/red ];
@export var waitTicks := [5, 1, 6];
@export var light = 0;
var tickCounter = 0

func _ready() -> void:
	$brakeZone.get_child(1).disabled = !light;
	for i in range (lights.size()):
		lights[i].visible = i == light
	$Timer.wait_time = 1;

func tick():
	tickCounter+=1
	if tickCounter == waitTicks[light]:
		light += 1;
		if light > 2:
			light = 0;
		tickCounter = 0;
	$brakeZone.get_child(1).disabled = !light;
	for i in range (lights.size()):
		lights[i].visible = i == light
	if synchronizedSemaphore:
		synchronizedSemaphore.tick();

func _on_timer_timeout() -> void:
	if hasOwnTimerEnabled:
		$Timer.start()
		tick();
