I hate my code, but buy/sell is done

This commit is contained in:
2ndbeam 2024-05-23 12:08:25 +03:00
commit 1ea6c2d296
18 changed files with 208 additions and 64 deletions

View file

@ -14,12 +14,18 @@ extends Node
var base
var player_ship
## 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"
## Button to buy amount of item
const BASE_BUY_ITEM = "BASE_BUY_ITEM"
## Button to sell amount of item
const BASE_SELL_ITEM = "BASE_SELL_ITEM"
## Null button ID
const NULL = "NULL"
@ -32,6 +38,10 @@ var buy: bool = false
var buy_sell_options: int = -1
## Which buy/sell tab should be opened (tab holds 6 items)
var buy_sell_tab: int = 0
## Which item is selected to buy/sell
var buy_sell_selected_item: int = -1
## How many items will be bought/sold
var buy_sell_amount = 0
func _ready():
load_menu()
@ -39,6 +49,7 @@ func _ready():
func post_ready():
base = get_parent().get_parent().get_parent().base
player_ship = base.player_ship
## Called when menu is changed
func load_menu():
@ -52,6 +63,11 @@ func load_menu():
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
BASE_BUY_ITEM:
format = get_buy_sell_button_format(buy_sell_selected_item, i)
BASE_SELL_ITEM:
format = get_buy_sell_button_format(buy_sell_selected_item, i)
actions[i].disabled = false
# disconnect previous action
if actions[i] is TransitButton:
actions[i].button_up.disconnect(transit_menu)
@ -81,3 +97,40 @@ func transit_menu(id: int):
var new_menu = menu.item_data[id].load_menu().duplicate()
menu = new_menu
load_menu()
func get_buy_sell_button_format(item_id: int, button_id: int) -> Dictionary:
var amount = 0
var prices = base.buy_prices if buy else base.sell_prices
var items = base.items_on_buy if buy else base.items_on_sell
var item_name = items[item_id].name
var price = prices[item_id]
match button_id:
1:
amount = 1
2:
amount = 5
3:
amount = 10
4:
amount = 50
5:
amount = 100
6:
if buy:
amount = roundi(player_ship.money / price)
elif item_name in player_ship.cargo:
amount = player_ship.cargo[item_name]
7:
if buy:
amount = roundi(player_ship.money / (price * 2))
elif item_name in player_ship.cargo:
amount = roundi(player_ship.cargo[item_name] / 2)
var total_cost = price * amount
var format = {
"amount": amount,
"total_cost": total_cost,
"item": item_name,
"item_name": tr(item_name),
"buy": buy
}
return format