ld48/src/ui/EncounterChoice.gd

92 lines
2.1 KiB
GDScript3

extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
signal encounter_choice_changed
var choices = 3
var level = 0
var buttonNames = [
"Option0",
"Option1",
"Option2",
]
# Called when the node enters the scene tree for the first time.
func _ready():
# Bind button callbacks
for n in buttonNames:
var button = self.find_node(n)
button.connect("toggled", self, "_on_option_button_pressed", [n])
func initialize(l = null, nOptions = null):
self.level = l
if nOptions != null:
self.choices = nOptions
var x = 0
while x < 3:
self.find_node("Option" + str(x)).visible = x < nOptions
x += 1
func activate_options():
if self.choices > 1:
for c in self.get_children():
if c.visible:
c.set_disabled(false)
func active_visited():
var i = self.get_active_option_index()
if i == null:
print(self.get_name() + " has no active option index")
return false
if self.level == null:
print(self.get_name() + " does not have self.level set")
return false
print("Level " + str(self.level) + " is visited: " + str(GameState.encounters[self.level][i]['visited']))
return GameState.encounters[self.level][i]['visited']
func disable_options():
for c in self.get_children():
if c.visible:
c.set_disabled(true)
func get_active_option_index():
var x = 0
for c in self.get_children():
if not c.is_visible():
x += 1
continue
if c.is_pressed():
return x
else:
x += 1
return null
func get_active_position():
var i = get_active_option_index()
if i == null:
return null
return get_node("Option" + str(i)).get_global_position()
func has_active():
var active = false
for n in buttonNames:
active = active or (self.get_node(n).is_pressed() and self.get_node(n).is_visible())
return active
func _on_option_button_pressed(state, buttonName):
for n in buttonNames:
if n != buttonName:
if state:
self.find_node(n).set_pressed(false)
emit_signal("encounter_choice_changed")
#for n in buttonNames:
# if self.find_node(n).is_pressed():
# print("Button" + n + " is pressed")
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass