Initial commit
This commit is contained in:
81
Inventory/Inventory.gd
Normal file
81
Inventory/Inventory.gd
Normal file
@@ -0,0 +1,81 @@
|
||||
class_name Inventory
|
||||
extends Node
|
||||
|
||||
var slots : Array[InventorySlot]
|
||||
@onready var window : Panel = get_node("InventoryWindow")
|
||||
@onready var info_text : Label = get_node("InventoryWindow/InfoText")
|
||||
|
||||
@export var starter_items : Array[Item]
|
||||
|
||||
func _ready() -> void:
|
||||
toggle_window(false)
|
||||
|
||||
for child in get_node("InventoryWindow/SlotContainer").get_children():
|
||||
slots.append(child)
|
||||
child.set_item(null)
|
||||
child.inventory = self
|
||||
|
||||
for item in starter_items:
|
||||
add_item(item)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if Input.is_action_just_pressed("inventory"):
|
||||
toggle_window(!window.visible)
|
||||
|
||||
func toggle_window(open : bool):
|
||||
window.visible = open
|
||||
|
||||
if open:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
else:
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
func on_give_player_item (item : Item, amount : int):
|
||||
for i in range(amount):
|
||||
add_item(item)
|
||||
|
||||
func add_item (item : Item):
|
||||
var slot = get_slot_to_add(item)
|
||||
|
||||
if slot == null:
|
||||
return
|
||||
|
||||
if slot.item == null:
|
||||
slot.set_item(item)
|
||||
elif slot.item == item:
|
||||
slot.add_item()
|
||||
|
||||
func remove_item (item : Item):
|
||||
var slot = get_slot_to_remove(item)
|
||||
|
||||
if slot == null or slot.item == null:
|
||||
return
|
||||
|
||||
slot.remove_item()
|
||||
|
||||
func get_slot_to_add (item : Item) -> InventorySlot:
|
||||
for slot in slots:
|
||||
if slot.item == item and slot.quantity < item.max_stack_size:
|
||||
return slot
|
||||
|
||||
for slot in slots:
|
||||
if slot.item == null:
|
||||
return slot
|
||||
|
||||
return null
|
||||
|
||||
func get_slot_to_remove (item : Item) -> InventorySlot:
|
||||
for slot in slots:
|
||||
if slot.item == item:
|
||||
return slot
|
||||
|
||||
return null
|
||||
|
||||
func get_number_of_item (item : Item) -> int:
|
||||
var total = 0
|
||||
|
||||
for slot in slots:
|
||||
if slot.item == item:
|
||||
total += slot.quantity
|
||||
|
||||
return total
|
||||
1
Inventory/Inventory.gd.uid
Normal file
1
Inventory/Inventory.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bgp8sred771bp
|
||||
58
Inventory/InventorySlot.gd
Normal file
58
Inventory/InventorySlot.gd
Normal file
@@ -0,0 +1,58 @@
|
||||
class_name InventorySlot
|
||||
extends Node
|
||||
|
||||
var item : Item
|
||||
var quantity : int
|
||||
@onready var icon : TextureRect = get_node("Icon")
|
||||
@onready var quantity_text : Label = get_node("QuanityText")
|
||||
var inventory : Inventory
|
||||
|
||||
func set_item (new_item : Item):
|
||||
item = new_item
|
||||
quantity = 1
|
||||
|
||||
if item == null:
|
||||
icon.visible = false
|
||||
else:
|
||||
icon.visible = true
|
||||
icon.texture = item.icon
|
||||
|
||||
update_quantity_text()
|
||||
|
||||
func add_item ():
|
||||
quantity += 1
|
||||
update_quantity_text()
|
||||
|
||||
func remove_item ():
|
||||
quantity -= 1
|
||||
update_quantity_text()
|
||||
|
||||
if quantity == 0:
|
||||
set_item(null)
|
||||
|
||||
func update_quantity_text():
|
||||
if quantity <= 1:
|
||||
quantity_text.text = ""
|
||||
else:
|
||||
quantity_text.text = str(quantity)
|
||||
|
||||
func _on_mouse_entered() -> void:
|
||||
if item == null:
|
||||
inventory.info_text.text = ""
|
||||
else:
|
||||
inventory.info_text.text = item.display_name
|
||||
|
||||
func _on_mouse_exited() -> void:
|
||||
inventory.info_text.text = ""
|
||||
|
||||
func _on_pressed():
|
||||
if item == null:
|
||||
return
|
||||
|
||||
var remove_after_use = item._on_use(inventory.get_parent())
|
||||
|
||||
if remove_after_use:
|
||||
remove_item()
|
||||
|
||||
func drop_item():
|
||||
pass
|
||||
1
Inventory/InventorySlot.gd.uid
Normal file
1
Inventory/InventorySlot.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://daq5s125ae4of
|
||||
51
Inventory/inventory_slot.tscn
Normal file
51
Inventory/inventory_slot.tscn
Normal file
@@ -0,0 +1,51 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://c7i5cai8ud4lr"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://daq5s125ae4of" path="res://Inventory/InventorySlot.gd" id="1_gpqwp"]
|
||||
[ext_resource type="Texture2D" uid="uid://ccjtljhtdisln" path="res://Items/Icons/Wood.png" id="2_ded5v"]
|
||||
|
||||
[sub_resource type="LabelSettings" id="LabelSettings_0eju6"]
|
||||
font_size = 32
|
||||
shadow_color = Color(0, 0, 0, 0.75)
|
||||
|
||||
[node name="InventorySlot" type="Button"]
|
||||
custom_minimum_size = Vector2(96, 96)
|
||||
script = ExtResource("1_gpqwp")
|
||||
|
||||
[node name="Icon" type="TextureRect" parent="."]
|
||||
custom_minimum_size = Vector2(80, 80)
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -40.0
|
||||
offset_top = -40.0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
mouse_filter = 2
|
||||
texture = ExtResource("2_ded5v")
|
||||
expand_mode = 1
|
||||
|
||||
[node name="QuanityText" type="Label" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 3
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -45.0
|
||||
offset_top = -23.0
|
||||
offset_right = -5.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
text = "42"
|
||||
label_settings = SubResource("LabelSettings_0eju6")
|
||||
horizontal_alignment = 2
|
||||
vertical_alignment = 2
|
||||
|
||||
[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"]
|
||||
[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"]
|
||||
[connection signal="pressed" from="." to="." method="_on_pressed"]
|
||||
Reference in New Issue
Block a user