cosmic/scripts/Base/actions_menu.gd
2024-05-22 19:09:36 +03:00

82 lines
2.3 KiB
GDScript

extends Node
## Array of menu actions represented as buttons
@onready var actions: Array[Button] = [
$Action1,
$Action2,
$Action3,
$Action4,
$Action5,
$Action6,
$Action7,
$Action8
]
var base
## Script attached to transit buttons
const TRANSIT_BUTTON_SCRIPT = preload("res://scripts/Classes/Menu/transit_button.gd")
## Buy/Sell item trigger
const BUY_SELL_ITEM = "BUY_SELL_ITEM"
## Buy/Sell button placeholder
const BASE_BUY_SELL = "BASE_BUY_SELL"
## Null button ID
const NULL = "NULL"
## Menu which is loaded on ready
@export var menu: Menu = null
## Is base buying from player or vice versa
var buy: bool = false
## How many buy/sell options are available
var buy_sell_options: int = -1
## Which buy/sell tab should be opened (tab holds 6 items)
var buy_sell_tab: int = 0
func _ready():
load_menu()
get_tree().create_timer(0.05).timeout.connect(post_ready)
func post_ready():
base = get_parent().get_parent().get_parent().base
## Called when menu is changed
func load_menu():
var format = {}
# iterating through all actions
for i in range(len(menu.item_ids)):
match menu.item_ids[i]:
BASE_BUY_SELL:
if i <= buy_sell_options:
var list = base.items_on_buy if buy else base.items_on_sell
format["item_id"] = i + buy_sell_tab * 6
format["item_name"] = tr(list[i - 1 + buy_sell_tab * 6].name)
menu.item_ids[i] = BUY_SELL_ITEM
# disconnect previous action
if actions[i] is TransitButton:
actions[i].button_up.disconnect(transit_menu)
elif actions[i] is MenuAction:
actions[i].button_up.disconnect(actions[i].action)
# disconnect previous script
actions[i].set_script(null)
actions[i].text = tr(menu.item_ids[i]).format(format)
# assign new script
match menu.item_actions[i]:
Menu.Action.TransitAction:
actions[i].set_script(TRANSIT_BUTTON_SCRIPT)
Menu.Action.ButtonAction:
actions[i].set_script(menu.item_data[i].load_script())
Menu.Action.ComboAction:
actions[i].set_script(menu.item_data[i].load_script())
if "id" in actions[i]:
actions[i].id = i
if "dialogue" in actions[i]:
actions[i].dialogue = $"../../Dialogue"
## Called with transit_button, changes current menu
func transit_menu(id: int):
assert(id < len(menu.item_ids) and id >= 0)
var new_menu = menu.item_data[id].load_menu().duplicate()
menu = new_menu
load_menu()