145 lines
4.3 KiB
GDScript
145 lines
4.3 KiB
GDScript
extends VBoxContainer
|
|
|
|
signal buttonPressed(item)
|
|
signal fiveButtonPressed(item)
|
|
signal allButtonPressed(item)
|
|
signal itemClicked(item)
|
|
|
|
var displayScene = preload("res://UI/Shopping/ItemShopDisplay.tscn")
|
|
var inventory
|
|
|
|
var positiveOptions = true
|
|
var negativeOptions = false
|
|
var fiveButtons = false
|
|
var allButtons = false
|
|
|
|
var quantitiesShown = true
|
|
|
|
func _ready():
|
|
inventory = Inventory.new()
|
|
|
|
func set_title(title):
|
|
$Title.text = title
|
|
$Title.visible = true
|
|
|
|
func disconnect_signals():
|
|
for display in %Items.get_children():
|
|
var connections = display.buttonPressed.get_connections()
|
|
for connection in connections:
|
|
display.buttonPressed.disconnect(connection["callable"])
|
|
|
|
connections = display.itemClicked.get_connections()
|
|
for connection in connections:
|
|
display.itemClicked.disconnect(connection["callable"])
|
|
|
|
connections = display.fiveButtonPressed.get_connections()
|
|
for connection in connections:
|
|
display.fiveButtonPressed.disconnect(connection["callable"])
|
|
|
|
connections = display.allButtonPressed.get_connections()
|
|
for connection in connections:
|
|
display.allButtonPressed.disconnect(connection["callable"])
|
|
|
|
func update_display():
|
|
disconnect_signals()
|
|
|
|
var displaysMade = %Items.get_children().size()
|
|
for i in range(inventory.items.size()):
|
|
if i >= displaysMade:
|
|
var newDisplay = displayScene.instantiate()
|
|
%Items.add_child(newDisplay)
|
|
|
|
setup_new_display(newDisplay)
|
|
|
|
var currentDisplay = %Items.get_child(i)
|
|
currentDisplay.set_sprite(inventory.items[i].get_sprite())
|
|
currentDisplay.set_item_name(inventory.items[i].get_name())
|
|
currentDisplay.set_quantity(inventory.quantities[i])
|
|
|
|
currentDisplay.buttonPressed.connect(button_pressed.bind(inventory.items[i]))
|
|
currentDisplay.fiveButtonPressed.connect(five_button_pressed.bind(inventory.items[i]))
|
|
currentDisplay.allButtonPressed.connect(all_button_pressed.bind(inventory.items[i]))
|
|
currentDisplay.itemClicked.connect(item_clicked.bind(inventory.items[i]))
|
|
currentDisplay.visible = true
|
|
|
|
for i in range(inventory.items.size(), displaysMade):
|
|
%Items.get_child(i).visible = false
|
|
|
|
func remove_item(item, quantity):
|
|
var originalInvSize = inventory.items.size()
|
|
var displaysMade = %Items.get_children().size()
|
|
for i in range(originalInvSize):
|
|
var currentInvItem = inventory.items[originalInvSize - i - 1]
|
|
if currentInvItem.equals(item):
|
|
var newQuantity = inventory.quantities[originalInvSize - i - 1] - quantity
|
|
var currentDisplay = %Items.get_child(originalInvSize - i - 1)
|
|
if newQuantity <= 0:
|
|
%Items.remove_child(currentDisplay)
|
|
currentDisplay.queue_free()
|
|
else:
|
|
currentDisplay.set_quantity(newQuantity)
|
|
break
|
|
|
|
func add_item(item, quantity):
|
|
var itemFound = false
|
|
|
|
var originalInvSize = inventory.items.size()
|
|
var displaysMade = %Items.get_children().size()
|
|
for i in range(originalInvSize):
|
|
var currentInvItem = inventory.items[originalInvSize - i - 1]
|
|
if currentInvItem.equals(item):
|
|
var newQuantity = inventory.quantities[originalInvSize - i - 1] + quantity
|
|
if (originalInvSize - i - 1) < displaysMade:
|
|
var currentDisplay = %Items.get_child(originalInvSize - i - 1)
|
|
currentDisplay.set_quantity(newQuantity)
|
|
itemFound = true
|
|
else:
|
|
quantity = newQuantity
|
|
break
|
|
|
|
if !itemFound:
|
|
var newDisplay = displayScene.instantiate()
|
|
%Items.add_child(newDisplay)
|
|
setup_new_display(newDisplay)
|
|
|
|
newDisplay.set_sprite(item.get_sprite())
|
|
newDisplay.set_item_name(item.get_name())
|
|
newDisplay.set_quantity(quantity)
|
|
|
|
newDisplay.buttonPressed.connect(button_pressed.bind(item))
|
|
newDisplay.fiveButtonPressed.connect(five_button_pressed.bind(item))
|
|
newDisplay.allButtonPressed.connect(all_button_pressed.bind(item))
|
|
newDisplay.itemClicked.connect(item_clicked.bind(item))
|
|
|
|
func setup_new_display(newDisplay):
|
|
if fiveButtons:
|
|
newDisplay.show_five_button()
|
|
|
|
if allButtons:
|
|
newDisplay.show_all_button()
|
|
|
|
if !quantitiesShown:
|
|
newDisplay.hide_quantity()
|
|
|
|
if negativeOptions:
|
|
newDisplay.show_negatives()
|
|
else:
|
|
newDisplay.hide_negatives()
|
|
|
|
if positiveOptions:
|
|
newDisplay.show_positives()
|
|
else:
|
|
newDisplay.hide_positives()
|
|
|
|
func button_pressed(item):
|
|
buttonPressed.emit(item)
|
|
|
|
func five_button_pressed(item):
|
|
fiveButtonPressed.emit(item)
|
|
|
|
func all_button_pressed(item):
|
|
allButtonPressed.emit(item)
|
|
|
|
func item_clicked(item):
|
|
itemClicked.emit(item)
|