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
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue