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

@ -45,6 +45,7 @@ func get_buy_sell_list(buy: bool) -> String:
total += item_description(buy, item) + "\n"
return total
## Returns amount of items in buy/sell list
func get_buy_sell_len(buy: bool) -> int:
var list = items_on_buy if buy else items_on_sell
return len(list)

View file

@ -0,0 +1,41 @@
extends MessageSenderAction
## How many items will be bought/sold
var amount: int
## How much money will be received/spent
var total_cost: float
## Item name which will be added to cargo
var item_name: String
var buy: bool
@onready var player_ship: PlayerShip = get_tree().current_scene.player_ship
func _ready():
amount = format["amount"]
total_cost = format["total_cost"]
item_name = format["item_name"]
buy = format["buy"]
if amount < 1:
disabled = true
elif !buy and player_ship.money < total_cost:
disabled = true
elif buy and item_name in player_ship.cargo:
if player_ship.cargo[item_name] < amount:
disabled = true
elif buy and !(item_name in player_ship.cargo):
disabled = true
super._ready()
func action():
get_parent().buy_sell_amount = amount
if !buy and player_ship.money >= total_cost:
player_ship.money -= total_cost
if item_name in player_ship.cargo:
player_ship.cargo[item_name] += amount
else:
player_ship.cargo[item_name] = amount
elif buy and player_ship.cargo[item_name] >= amount:
player_ship.cargo[item_name] -= amount
player_ship.money += total_cost
super.action()

View file

@ -0,0 +1,7 @@
extends Button
@onready var menu = $"../../.."
func _on_button_up():
menu.base.dock_leave()
menu.queue_free()

View file

@ -4,6 +4,10 @@ extends NinePatchRect
const BUY_FETCH = "BASE_FETCH_BUY"
## This message will trigger buy/sell node to fetch sell prices
const SELL_FETCH = "BASE_FETCH_SELL"
## This message will trigger dialogue to get selected item data
const BUY_ITEM = "BASE_BUY_ITEM"
## This message will trigger dialogue to get selected item data
const SELL_ITEM = "BASE_SELL_ITEM"
@onready var dialogue = $DialogueView
@onready var buy_sell = $"../../BuySell"
@ -24,7 +28,18 @@ func send_message(msg: Message):
format["buy_list"] = buy_sell.get_buy_sell_list(true)
actions_menu.buy = true
actions_menu.buy_sell_options = buy_sell.get_buy_sell_len(true)
BUY_ITEM:
var list = buy_sell.items_on_sell
var id = actions_menu.buy_sell_selected_item
var item_name = list[id].name
format["amount"] = actions_menu.buy_sell_amount
format["item_name"] = item_name
SELL_ITEM:
var list = buy_sell.items_on_buy
var id = actions_menu.buy_sell_selected_item
var item_name = list[id].name
format["amount"] = actions_menu.buy_sell_amount
format["item_name"] = item_name
var new_msg = tr(msg.fact + "_RECEIVED").format(format)
var old_len = len(dialogue.get_parsed_text())
dialogue.append_text(new_msg)

View file

@ -0,0 +1,7 @@
extends MenuAction
@onready var menu = get_parent()
func action():
menu.buy_sell_selected_item = id - 1 + menu.buy_sell_tab * 6
get_parent().transit_menu(id)