I hate my code, but buy/sell is done
This commit is contained in:
parent
b68215a710
commit
1ea6c2d296
18 changed files with 208 additions and 64 deletions
|
|
@ -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)
|
||||
|
|
|
|||
41
scripts/Base/Menu/buy_sell_button.gd
Normal file
41
scripts/Base/Menu/buy_sell_button.gd
Normal 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()
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
7
scripts/Base/Menu/set_buy_sell_selected_item.gd
Normal file
7
scripts/Base/Menu/set_buy_sell_selected_item.gd
Normal 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)
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -123,16 +123,18 @@ func generate_prices():
|
|||
# gen prices for buyable items
|
||||
for i in range(len(items_on_buy)):
|
||||
var item = items_on_buy[i]
|
||||
var avg = (item.max_price + item.min_price) * 50
|
||||
if item in items_on_sell:
|
||||
# buy not higher than avg price
|
||||
buy_prices.append(randi_range(item.min_price * 100, (item.max_price + item.min_price) * 50) / 100.0)
|
||||
buy_prices.append(randi_range(item.min_price * 100, avg) / 100.0)
|
||||
else:
|
||||
buy_prices.append(randi_range(item.min_price * 100, item.max_price * 100) / 100.0)
|
||||
# gen prices for items in sell
|
||||
for i in range(len(items_on_sell)):
|
||||
var item = items_on_sell[i]
|
||||
var avg = (item.max_price + item.min_price) * 50
|
||||
if item in items_on_buy:
|
||||
# sell not lower than avg price
|
||||
sell_prices.append(randi_range((item.min_price + item.max_price) * 50, item.max_price * 100) / 100.0)
|
||||
sell_prices.append(randi_range(avg, item.max_price * 100) / 100.0)
|
||||
else:
|
||||
sell_prices.append(randi_range(item.min_price * 100, item.max_price * 100) / 100.0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue