Files
fps_project_1/levels/switchcontroller.gd
2024-10-30 15:52:02 -05:00

61 lines
1.6 KiB
GDScript

extends Node
@export var door : Node
var switches = []
var switch_override : Node
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#Connect to signal bus
SignalBus.switch_changed.connect(_on_switch_changed)
#Collect child switches
for i in self.get_children():
if i.is_in_group("switch"):
if i.is_in_group("switch_override"):
switch_override = i
else:
switches.append(i)
print("switches " + str(switches))
print("override switches " + str(switch_override))
func _on_switch_changed() -> void:
var switches_needed = switches.size()
var switches_on = 0
for i in switches:
if i.switched_on:
switches_on += 1
#First check override switch and open or close door
if switch_override != null:
print("null check passed")
if switch_override.switched_on:
print("switched on")
if door.door_open == false:
door.open()
else:
if door.door_open == true:
door.close()
#Otherwise follow standard switches
elif switches_on == switches_needed:
print("switch check run with " + str(switches_on) + "switches on")
if door.door_open == false:
door.open()
else:
print("switch check run with " + str(switches_on) + "switches on")
if door.door_open == true:
door.close()
#Otherwise follow standard switches
elif switches_on == switches_needed:
print("switch check run with " + str(switches_on) + "switches on")
if door.door_open == false:
door.open()
else:
print("switch check run with " + str(switches_on) + "switches on")
if door.door_open == true:
door.close()