cosmic/scripts/Base/Menu/buy_sell_button.gd
2024-05-23 12:08:25 +03:00

41 lines
1.1 KiB
GDScript

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()