@ -0,0 +1,10 @@ | |||||
[gd_resource type="Resource" script_class="InventoryResource" load_steps=3 format=3 uid="uid://cnpw7y1csu774"] | |||||
[ext_resource type="Script" uid="uid://becun6dj78v8d" path="res://resources/inventory_resource.gd" id="1_o2th4"] | |||||
[ext_resource type="Script" uid="uid://bdx4q355l5ugl" path="res://resources/item_resource.gd" id="1_udg6i"] | |||||
[resource] | |||||
script = ExtResource("1_o2th4") | |||||
inventory = Array[ExtResource("1_udg6i")]([]) | |||||
max_stack_size = 999 | |||||
metadata/_custom_type_script = "uid://becun6dj78v8d" |
@ -0,0 +1,61 @@ | |||||
[gd_resource type="Resource" script_class="InventoryResource" load_steps=13 format=3 uid="uid://blfp6tiir282o"] | |||||
[ext_resource type="Script" uid="uid://becun6dj78v8d" path="res://resources/inventory_resource.gd" id="1_4v6mg"] | |||||
[ext_resource type="Script" uid="uid://bdx4q355l5ugl" path="res://resources/item_resource.gd" id="1_gg8jx"] | |||||
[ext_resource type="Material" uid="uid://uex0dq00xomt" path="res://assets/materials/dirt.tres" id="2_8w148"] | |||||
[ext_resource type="Script" uid="uid://dwrmy4mx0mw18" path="res://resources/block_resource.gd" id="3_vgvac"] | |||||
[ext_resource type="Material" uid="uid://cx7m27qa4ds4s" path="res://assets/materials/stone.tres" id="4_7yuhn"] | |||||
[ext_resource type="Material" uid="uid://bnqiumcb3ixan" path="res://assets/materials/wood_ends.tres" id="5_viap3"] | |||||
[ext_resource type="Material" uid="uid://h0naiw6swfkl" path="res://assets/materials/wood_side.tres" id="6_wqat2"] | |||||
[ext_resource type="Material" uid="uid://d15n1p3spu3jg" path="res://assets/materials/leaves.tres" id="7_xd1nd"] | |||||
[sub_resource type="Resource" id="Resource_jaam2"] | |||||
script = ExtResource("3_vgvac") | |||||
material_east = ExtResource("2_8w148") | |||||
id = "001" | |||||
name = "Dirt" | |||||
amount = 100 | |||||
description = "Block of dirt" | |||||
item_texture = "uid://li36txj7oweq" | |||||
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" | |||||
[sub_resource type="Resource" id="Resource_4kby3"] | |||||
script = ExtResource("3_vgvac") | |||||
material_east = ExtResource("4_7yuhn") | |||||
id = "002" | |||||
name = "Stone" | |||||
amount = 100 | |||||
description = "Block of stone" | |||||
item_texture = "uid://ct1iawpfkdf5l" | |||||
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" | |||||
[sub_resource type="Resource" id="Resource_byct5"] | |||||
script = ExtResource("3_vgvac") | |||||
material_east = ExtResource("6_wqat2") | |||||
material_west = ExtResource("6_wqat2") | |||||
material_north = ExtResource("6_wqat2") | |||||
material_south = ExtResource("6_wqat2") | |||||
material_top = ExtResource("5_viap3") | |||||
material_bottom = ExtResource("5_viap3") | |||||
id = "003" | |||||
name = "Wood" | |||||
amount = 100 | |||||
description = "Wood log" | |||||
item_texture = "uid://0mw651622h01" | |||||
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" | |||||
[sub_resource type="Resource" id="Resource_qycqj"] | |||||
script = ExtResource("3_vgvac") | |||||
material_east = ExtResource("7_xd1nd") | |||||
id = "004" | |||||
name = "Leaves" | |||||
amount = 100 | |||||
description = "Tree leaves" | |||||
item_texture = "uid://goygbpyqhych" | |||||
metadata/_custom_type_script = "uid://dwrmy4mx0mw18" | |||||
[resource] | |||||
script = ExtResource("1_4v6mg") | |||||
inventory = Array[ExtResource("1_gg8jx")]([SubResource("Resource_jaam2"), SubResource("Resource_4kby3"), SubResource("Resource_byct5"), SubResource("Resource_qycqj")]) | |||||
max_stack_size = 999 | |||||
metadata/_custom_type_script = "uid://becun6dj78v8d" |
@ -0,0 +1,6 @@ | |||||
class_name InventoryResource | |||||
extends Resource | |||||
@export var inventory: Array[ItemResource] = [] | |||||
@export var max_stack_size: int = 999 |
@ -0,0 +1 @@ | |||||
uid://becun6dj78v8d |
@ -0,0 +1,69 @@ | |||||
class_name Inventory | |||||
extends Control | |||||
@export var inventory_resource: InventoryResource | |||||
@export var item_rect_scene: PackedScene | |||||
@export var grid_container: GridContainer | |||||
func _input(event: InputEvent) -> void: | |||||
if event.is_action_pressed("open_inventory"): | |||||
toggle_inventory() | |||||
func _ready() -> void: | |||||
InventoryManager.item_picked_up.connect(add_item.bind()) | |||||
InventoryManager.item_dropped.connect(subtract_item.bind()) | |||||
for item_resource: ItemResource in inventory_resource.inventory: | |||||
add_item(item_resource) | |||||
func add_item(item_resource: ItemResource, amount: int = 1) -> void: | |||||
var item_rect: InventoryItemRect = find_item_rect(item_resource) | |||||
if item_rect != null: | |||||
if item_rect.item_resource.amount + item_resource.amount >= inventory_resource.max_stack_size: | |||||
item_rect.on_stack_full(true) | |||||
return | |||||
item_rect.item_resource.amount += item_resource.amount | |||||
item_rect.update_rect() | |||||
else: | |||||
item_rect = item_rect_scene.instantiate() | |||||
grid_container.add_child(item_rect) | |||||
item_rect.item_resource = item_resource | |||||
item_rect.update_rect() | |||||
func find_item_rect(item_resource: ItemResource) -> InventoryItemRect: | |||||
var rect: InventoryItemRect = null | |||||
for container: InventoryItemRect in grid_container.get_children(): | |||||
if container.item_resource.id == item_resource.id: | |||||
rect = container | |||||
break | |||||
return rect | |||||
func subtract_item(item_resource: ItemResource) -> void: | |||||
var item_rect: InventoryItemRect = find_item_rect(item_resource) | |||||
if item_rect != null: | |||||
if item_rect.item_resource.amount >= inventory_resource.max_stack_size: | |||||
item_rect.on_stack_full(false) | |||||
item_rect.item_resource.amount -= item_resource.amount | |||||
item_rect.update_rect() | |||||
if item_rect.item_resource.amount < 1: # Empty stack | |||||
item_rect.queue_free() | |||||
else: | |||||
push_error("Attempting to subtract amount (" + str(item_resource.amount) + " from nonexistent inventory item (" + str(item_resource.name) + ")") | |||||
func toggle_inventory() -> void: | |||||
visible = not visible | |||||
if visible: | |||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE | |||||
InventoryManager.inventory_opened.emit() | |||||
get_tree().paused = true | |||||
else: | |||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED | |||||
InventoryManager.inventory_closed.emit() | |||||
get_tree().paused = false |
@ -0,0 +1 @@ | |||||
uid://dybecq130mxhn |
@ -0,0 +1,60 @@ | |||||
[gd_scene load_steps=4 format=3 uid="uid://dcr25y1lw4wjp"] | |||||
[ext_resource type="Script" uid="uid://dybecq130mxhn" path="res://scenes/ui/inventory/inventory.gd" id="1_s6ek7"] | |||||
[ext_resource type="Resource" uid="uid://cnpw7y1csu774" path="res://resources/inventory/player_inventory_empty.tres" id="2_avmd0"] | |||||
[ext_resource type="PackedScene" uid="uid://boueuk2hnfvg" path="res://scenes/ui/inventory/item_rect.tscn" id="3_xeaml"] | |||||
[node name="Inventory" type="Control" node_paths=PackedStringArray("grid_container")] | |||||
process_mode = 3 | |||||
custom_minimum_size = Vector2(860, 400) | |||||
layout_mode = 3 | |||||
anchors_preset = 8 | |||||
anchor_left = 0.5 | |||||
anchor_top = 0.5 | |||||
anchor_right = 0.5 | |||||
anchor_bottom = 0.5 | |||||
offset_left = -410.0 | |||||
offset_top = -200.0 | |||||
offset_right = 410.0 | |||||
offset_bottom = 200.0 | |||||
grow_horizontal = 2 | |||||
grow_vertical = 2 | |||||
script = ExtResource("1_s6ek7") | |||||
inventory_resource = ExtResource("2_avmd0") | |||||
item_rect_scene = ExtResource("3_xeaml") | |||||
grid_container = NodePath("Background/MarginContainer/VBoxContainer/GridContainer") | |||||
[node name="Background" type="Panel" parent="."] | |||||
layout_mode = 1 | |||||
anchors_preset = 15 | |||||
anchor_right = 1.0 | |||||
anchor_bottom = 1.0 | |||||
grow_horizontal = 2 | |||||
grow_vertical = 2 | |||||
[node name="MarginContainer" type="MarginContainer" parent="Background"] | |||||
layout_mode = 1 | |||||
anchors_preset = 15 | |||||
anchor_right = 1.0 | |||||
anchor_bottom = 1.0 | |||||
grow_horizontal = 2 | |||||
grow_vertical = 2 | |||||
theme_override_constants/margin_left = 20 | |||||
theme_override_constants/margin_top = 20 | |||||
theme_override_constants/margin_right = 20 | |||||
theme_override_constants/margin_bottom = 20 | |||||
[node name="VBoxContainer" type="VBoxContainer" parent="Background/MarginContainer"] | |||||
layout_mode = 2 | |||||
[node name="Label" type="Label" parent="Background/MarginContainer/VBoxContainer"] | |||||
layout_mode = 2 | |||||
theme_type_variation = &"HeaderLarge" | |||||
text = "Inventory" | |||||
[node name="GridContainer" type="GridContainer" parent="Background/MarginContainer/VBoxContainer"] | |||||
layout_mode = 2 | |||||
size_flags_vertical = 3 | |||||
theme_override_constants/h_separation = 20 | |||||
theme_override_constants/v_separation = 20 | |||||
columns = 10 |
@ -0,0 +1,19 @@ | |||||
class_name InventoryItemRect | |||||
extends Panel | |||||
@export var amount_label: Label | |||||
@export var item_resource: ItemResource | |||||
@export var item_texture: TextureRect | |||||
func update_rect() -> void: | |||||
item_texture.texture = load(item_resource.item_texture) | |||||
amount_label.text = "x" + str(item_resource.amount) | |||||
tooltip_text = item_resource.name + "\n" + item_resource.description | |||||
func on_stack_full(is_full: bool) -> void: | |||||
if is_full: | |||||
amount_label.add_theme_color_override("font_color", Color(1, 0, 0)) | |||||
else: | |||||
amount_label.add_theme_color_override("font_color", Color(1, 1, 1)) |
@ -0,0 +1 @@ | |||||
uid://cknl6i0jce5jr |
@ -0,0 +1,41 @@ | |||||
[gd_scene load_steps=4 format=3 uid="uid://boueuk2hnfvg"] | |||||
[ext_resource type="Texture2D" uid="uid://dknv7amroftm8" path="res://icon.svg" id="1_o0kom"] | |||||
[ext_resource type="Script" uid="uid://cknl6i0jce5jr" path="res://scenes/ui/inventory/item_rect.gd" id="1_oderi"] | |||||
[sub_resource type="LabelSettings" id="LabelSettings_oderi"] | |||||
outline_size = 3 | |||||
outline_color = Color(0, 0, 0, 1) | |||||
[node name="ItemRect" type="Panel" node_paths=PackedStringArray("amount_label", "item_texture")] | |||||
custom_minimum_size = Vector2(64, 64) | |||||
offset_right = 64.0 | |||||
offset_bottom = 64.0 | |||||
script = ExtResource("1_oderi") | |||||
amount_label = NodePath("AmountLabel") | |||||
item_texture = NodePath("ItemTexture") | |||||
metadata/_edit_use_anchors_ = true | |||||
[node name="ItemTexture" type="TextureRect" parent="."] | |||||
texture_filter = 1 | |||||
layout_mode = 0 | |||||
offset_right = 64.0 | |||||
offset_bottom = 64.0 | |||||
texture = ExtResource("1_o0kom") | |||||
expand_mode = 1 | |||||
stretch_mode = 4 | |||||
[node name="AmountLabel" 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 = -40.0 | |||||
offset_top = -23.0 | |||||
offset_right = -3.0 | |||||
grow_horizontal = 0 | |||||
grow_vertical = 0 | |||||
text = "x100" | |||||
label_settings = SubResource("LabelSettings_oderi") |