Files

86 lines
2.3 KiB
GDScript

extends Control
@export var slotScene : PackedScene
@export var HotbarContainer : HBoxContainer
@export var hotbar : HotbarResource
func updateHotbar(hotbar: HotbarResource):
for child in HotbarContainer.get_children():
child.queue_free()
for index in hotbar.slots.size():
var item = hotbar.slots[index]
var slotInstance = slotScene.instantiate()
HotbarContainer.add_child(slotInstance)
if item:
var is_selected = (MainGame.selectedSlot == index)
if is_selected:
MainGame.currentHeldItemID = item.ID
slotInstance.setItem(item, is_selected)
func givePlayerItem(ID:String):
var segments = ID.split(":")
for index in hotbar.slots.size():
var item = hotbar.slots[index]
if item.ID == "base:air":
hotbar.slots[index] = load("res://GameShit/ItemHandlers/{namespace}/{id}.tres".format({"namespace": segments[0], "id": segments[1]}))
updateHotbar(hotbar)
return
func removePlayerHeldItem():
hotbar.slots[MainGame.selectedSlot] = load("res://GameShit/ItemHandlers/base/air.tres")
updateHotbar(hotbar)
func _ready() -> void:
## connect the InventoryBus signal for the shit
InventoryBus.givePlayerItem.connect(givePlayerItem)
InventoryBus.removeHeldItem.connect(removePlayerHeldItem)
## throw in some default slots
if hotbar.slots.is_empty():
hotbar.slots.clear()
for i in range(0,12):
hotbar.slots.append(load("res://GameShit/ItemHandlers/base/air.tres"))
## give the player the default tools
givePlayerItem("base:hammer")
givePlayerItem("base:interact")
updateHotbar(hotbar)
func _process(delta: float) -> void:
if Input.is_key_pressed(KEY_1):
MainGame.selectedSlot = 0
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_2):
MainGame.selectedSlot = 1
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_3):
MainGame.selectedSlot = 2
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_4):
MainGame.selectedSlot = 3
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_5):
MainGame.selectedSlot = 4
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_6):
MainGame.selectedSlot = 5
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_7):
MainGame.selectedSlot = 6
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_8):
MainGame.selectedSlot = 7
updateHotbar(hotbar)
elif Input.is_key_pressed(KEY_9):
MainGame.selectedSlot = 8
updateHotbar(hotbar)