Skip to content

Commit

Permalink
bad crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
EclipsedMango committed Jul 4, 2024
1 parent e20013a commit c17d6b7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 22 deletions.
2 changes: 0 additions & 2 deletions Scenes/Crafting.gd

This file was deleted.

9 changes: 5 additions & 4 deletions Scenes/inventory.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[ext_resource type="Script" path="res://Scripts/Inventory.gd" id="1_hpfk4"]
[ext_resource type="Texture2D" uid="uid://crurudbckpe4j" path="res://Assets/Art/Textures/inventory.png" id="1_qivim"]
[ext_resource type="PackedScene" uid="uid://cccy3p17tx25c" path="res://Scenes/ui_item_stack.tscn" id="2_mmi5o"]
[ext_resource type="Script" path="res://Scripts/Crafting.gd" id="5_k523s"]
[ext_resource type="PackedScene" uid="uid://d3jmireffqxgp" path="res://Scenes/ui_recipe.tscn" id="5_lxt81"]
[ext_resource type="Script" path="res://Scenes/Crafting.gd" id="5_osfj6"]
[ext_resource type="Texture2D" uid="uid://b438nicufrs6d" path="res://Assets/Art/Textures/hotbar.png" id="5_rj0ue"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_anabp"]
Expand Down Expand Up @@ -301,7 +301,7 @@ offset_top = 738.0
offset_right = -419.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("5_osfj6")
script = ExtResource("5_k523s")

[node name="ScrollContainer" type="ScrollContainer" parent="Menu/Crafting"]
layout_mode = 1
Expand All @@ -311,10 +311,11 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="HBoxContainer" type="HBoxContainer" parent="Menu/Crafting/ScrollContainer"]
[node name="RecipeContainer" type="HBoxContainer" parent="Menu/Crafting/ScrollContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="UiRecipe" parent="Menu/Crafting/ScrollContainer/HBoxContainer" instance=ExtResource("5_lxt81")]
[node name="UiRecipe" parent="Menu/Crafting/ScrollContainer/RecipeContainer" instance=ExtResource("5_lxt81")]
layout_mode = 2
16 changes: 3 additions & 13 deletions Scenes/ui_recipe.tscn
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
[gd_scene load_steps=3 format=3 uid="uid://d3jmireffqxgp"]
[gd_scene load_steps=2 format=3 uid="uid://d3jmireffqxgp"]

[ext_resource type="PackedScene" uid="uid://cccy3p17tx25c" path="res://Scenes/ui_item_stack.tscn" id="1_kr5gx"]
[ext_resource type="PackedScene" uid="uid://du08juske0mo1" path="res://Scenes/ui_ingredient_stack.tscn" id="2_kyq4o"]

[node name="UiRecipe" type="VBoxContainer"]

[node name="hallo" parent="." instance=ExtResource("1_kr5gx")]
[node name="UiResult" parent="." instance=ExtResource("1_kr5gx")]
custom_minimum_size = Vector2(100, 100)
layout_mode = 2
size_flags_vertical = 1

[node name="HFlowContainer" type="HFlowContainer" parent="."]
[node name="IngredientContainer" type="HFlowContainer" parent="."]
layout_mode = 2
size_flags_vertical = 3
alignment = 1

[node name="hallo" parent="HFlowContainer" instance=ExtResource("2_kyq4o")]
layout_mode = 2

[node name="hallo3" parent="HFlowContainer" instance=ExtResource("2_kyq4o")]
layout_mode = 2

[node name="hallo2" parent="HFlowContainer" instance=ExtResource("2_kyq4o")]
layout_mode = 2
46 changes: 46 additions & 0 deletions Scripts/Crafting.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extends Control

@onready var inventory: Inventory = $"../.."
@onready var recipe_container: Control = %RecipeContainer

var ui_recipe_res = preload("res://Scenes/ui_recipe.tscn")
var ui_ingredient_res = preload("res://Scenes/ui_ingredient_stack.tscn")

func _ready() -> void:
inventory.inv_updated.connect(_inv_updated)

func _inv_updated() -> void:
for child in recipe_container.get_children():
child.queue_free()

for recipe: Dictionary in Recipes.recipes:
if _has_items(recipe):
var ui_recipe: Control = ui_recipe_res.instantiate()
var ui_result = ui_recipe.get_node("UiResult")
inventory._update_ui_item(ui_result, recipe.result)
ui_result.get_node("Button").pressed.connect(_recipe_pressed.bind(recipe))

var ingredient_container = ui_recipe.get_node("IngredientContainer")

for ingredient: ItemStack in recipe.ingredients:
var ui_ingredient: Control = ui_ingredient_res.instantiate()
inventory._update_ui_item(ui_ingredient, ingredient)
ingredient_container.add_child(ui_ingredient)

add_child(ui_recipe)

func _recipe_pressed(recipe: Dictionary) -> void:
if inventory.cursor_item != null:
return

inventory.cursor_item = ItemStack.new(recipe.result.type, recipe.result.amount)
inventory._update_ui_item(inventory.ui_cursor_item, inventory.cursor_item)
for ingredient: ItemStack in recipe.ingredients:
inventory.remove_items(ingredient.type, ingredient.amount)

func _has_items(recipe: Dictionary) -> bool:
for ingredient: ItemStack in recipe.ingredients:
if inventory.count_items(ingredient.type, ingredient.amount) < ingredient.amount:
return false

return true
40 changes: 37 additions & 3 deletions Scripts/Inventory.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class_name Inventory
extends Control

signal inv_updated

@onready var grid_container: GridContainer = %InventoryGrid
@onready var grid_container_2: GridContainer = %InventoryHotbarGrid
@onready var ui_cursor_item: MarginContainer = %CursorItem
Expand Down Expand Up @@ -87,11 +89,13 @@ func _clicked_item(index: int) -> void:
items[index] = null
add_item(item)
_update_ui_slot(index)
inv_updated.emit()
return

if add_item(item, 27):
items[index] = null
_update_ui_slot(index)
inv_updated.emit()

return

Expand All @@ -104,9 +108,11 @@ func _clicked_item(index: int) -> void:

_update_ui_slot(index)
_update_ui_item(ui_cursor_item, cursor_item)
inv_updated.emit()

func _right_clicked_item(index: int) -> void:
_try_split_stack(index)
inv_updated.emit()

func _update_ui_slot(index: int) -> void:
_update_ui_item(_get_ui_item(index), items[index])
Expand All @@ -116,17 +122,17 @@ func _update_ui_slot(index: int) -> void:
_update_ui_item(hotbar_item, items[index])

func _update_ui_item(ui_item: Node, item: ItemStack) -> void:
var item_label: Label = ui_item.get_node("ItemName")
#var item_label: Label = ui_item.get_node("ItemName")
var item_count: Label = ui_item.get_node("ItemCount")
var texture_rect: TextureRect = ui_item.get_node("TextureRect")

if item == null:
item_label.text = ""
#item_label.text = ""
item_count.text = ""
texture_rect.visible = false
return

item_label.text = ItemStack.type_names[item.type]
#item_label.text = ItemStack.type_names[item.type]
item_count.text = str(item.amount)
texture_rect.visible = true
texture_rect.texture = ItemStack.type_icons[item.type]
Expand All @@ -151,6 +157,7 @@ func add_item(item: ItemStack, start_index: int = 0) -> bool:

_update_ui_slot(slot)

inv_updated.emit()
return true

func _find_first_slot(type: ItemStack.ItemType, start_index: int = 0) -> int:
Expand Down Expand Up @@ -191,3 +198,30 @@ func _swap_with_cursor(index: int) -> void:
var temp = cursor_item
cursor_item = items[index]
items[index] = temp

func count_items(type: ItemStack.ItemType, max_count: int) -> int:
var count: int = 0
for item in items:
if item != null && item.type == type:
count += item.amount
if count >= max_count:
return count
return count

func remove_items(type: ItemStack.ItemType, amount: int) -> void:
for i in range(inv_size):
if items[i] != null && items[i].type == type:
if items[i].amount < amount:
amount -= items[i].amount
items[i] = null
_update_ui_slot(i)
continue

items[i].amount -= amount
if items[i].amount == 0:
items[i] = null
_update_ui_slot(i)
inv_updated.emit()
return
printerr("failed to remove items. how the fuck did you get here?")
inv_updated.emit()
12 changes: 12 additions & 0 deletions Scripts/Recipes.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class_name Recipes
extends RefCounted

static var recipes: Array = [
{"result": ItemStack.new(ItemStack.ItemType.Pickaxe), "ingredients": [
ItemStack.new(ItemStack.ItemType.Wood, 5),
ItemStack.new(ItemStack.ItemType.Rock, 3)
]},
{"result": ItemStack.new(ItemStack.ItemType.Rock), "ingredients": [
ItemStack.new(ItemStack.ItemType.Wood, 1),
]},
]

0 comments on commit c17d6b7

Please sign in to comment.