Craft
This commit is contained in:
parent
3af13d0a8b
commit
5da9e11f36
5 changed files with 48 additions and 26 deletions
|
@ -10,6 +10,7 @@
|
||||||
resource_local_to_scene = true
|
resource_local_to_scene = true
|
||||||
script = ExtResource("3_ruvuk")
|
script = ExtResource("3_ruvuk")
|
||||||
capacity = 4
|
capacity = 4
|
||||||
|
progress_speed = 10.0
|
||||||
pop_treshold = 0.95
|
pop_treshold = 0.95
|
||||||
internal_array = Array[ExtResource("2_54w8r")]([Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
internal_array = Array[ExtResource("2_54w8r")]([Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
||||||
, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
, Object(RefCounted,"script":ExtResource("2_54w8r"),"held_item":null,"amount":0,"filter":null)
|
||||||
|
|
|
@ -8,9 +8,13 @@
|
||||||
[ext_resource type="Resource" uid="uid://d2lbc1qqkayaa" path="res://generic/recipes/test_recipe.tres" id="6_wqoim"]
|
[ext_resource type="Resource" uid="uid://d2lbc1qqkayaa" path="res://generic/recipes/test_recipe.tres" id="6_wqoim"]
|
||||||
|
|
||||||
[sub_resource type="Resource" id="Resource_kno0u"]
|
[sub_resource type="Resource" id="Resource_kno0u"]
|
||||||
|
resource_local_to_scene = true
|
||||||
script = ExtResource("3_wqoim")
|
script = ExtResource("3_wqoim")
|
||||||
input_capacity = 2
|
input_capacity = 2
|
||||||
output_capacity = 1
|
output_capacity = 1
|
||||||
|
input_array = Array[ExtResource("2_p1cyh")]([Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
|
||||||
|
, Object(RefCounted,"script":ExtResource("2_p1cyh"),"held_item":null,"amount":0,"filter":null)
|
||||||
|
])
|
||||||
metadata/_custom_type_script = "uid://m6b6vawdqgqb"
|
metadata/_custom_type_script = "uid://m6b6vawdqgqb"
|
||||||
|
|
||||||
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]
|
[sub_resource type="AtlasTexture" id="AtlasTexture_xh4eg"]
|
||||||
|
@ -21,6 +25,7 @@ region = Rect2(32, 0, 32, 32)
|
||||||
script = ExtResource("1_k5y3y")
|
script = ExtResource("1_k5y3y")
|
||||||
dimensions = Rect2i(0, 0, 2, 2)
|
dimensions = Rect2i(0, 0, 2, 2)
|
||||||
inventory = SubResource("Resource_kno0u")
|
inventory = SubResource("Resource_kno0u")
|
||||||
|
facing = 2
|
||||||
|
|
||||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||||
texture = SubResource("AtlasTexture_xh4eg")
|
texture = SubResource("AtlasTexture_xh4eg")
|
||||||
|
|
|
@ -7,15 +7,13 @@ class_name InOutInventory
|
||||||
@export var output_capacity : int
|
@export var output_capacity : int
|
||||||
|
|
||||||
@export_storage var input_array : Array[InventorySlot]
|
@export_storage var input_array : Array[InventorySlot]
|
||||||
@export_storage var output_array : Array[InventorySlot]
|
@export_storage var output_slot : InventorySlot
|
||||||
|
|
||||||
func resize() -> void:
|
func resize() -> void:
|
||||||
input_array.resize(input_capacity)
|
input_array.resize(input_capacity)
|
||||||
output_array.resize(output_capacity)
|
|
||||||
for i in range(input_capacity):
|
for i in range(input_capacity):
|
||||||
input_array[i] = InventorySlot.new()
|
input_array[i] = InventorySlot.new()
|
||||||
for i in range(output_capacity):
|
output_slot = InventorySlot.new()
|
||||||
output_array[i] = InventorySlot.new()
|
|
||||||
|
|
||||||
func _init() -> void:
|
func _init() -> void:
|
||||||
super()
|
super()
|
||||||
|
@ -46,9 +44,7 @@ func can_add(item : Item = null) -> bool:
|
||||||
return false
|
return false
|
||||||
else:
|
else:
|
||||||
for i in range(input_capacity):
|
for i in range(input_capacity):
|
||||||
print(input_array[i].filter)
|
|
||||||
if input_array[i].can_be_merged(item):
|
if input_array[i].can_be_merged(item):
|
||||||
print(input_array[i].filter)
|
|
||||||
return true
|
return true
|
||||||
#return true
|
#return true
|
||||||
return false
|
return false
|
||||||
|
@ -58,27 +54,21 @@ func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
|
||||||
|
|
||||||
## Tries to take first item. Returns null if no items in inventory
|
## Tries to take first item. Returns null if no items in inventory
|
||||||
func pop() -> Stack:
|
func pop() -> Stack:
|
||||||
for i in range(output_capacity):
|
if output_slot.held_item != null:
|
||||||
if output_array[i].held_item != null:
|
var extracted = output_slot.extract()
|
||||||
var extracted = output_array[i].extract()
|
stack_taken.emit(extracted,0)
|
||||||
stack_taken.emit(extracted,i)
|
return extracted
|
||||||
return extracted
|
|
||||||
return null
|
return null
|
||||||
|
|
||||||
func peek() -> Stack:
|
func peek() -> Stack:
|
||||||
for i in range(output_capacity):
|
if output_slot.held_item != null:
|
||||||
if output_array[i].held_item != null:
|
var peeked = output_slot.as_stack()
|
||||||
var peeked = output_array[i].as_stack()
|
return peeked
|
||||||
return peeked
|
|
||||||
return null
|
return null
|
||||||
|
|
||||||
## Tries to take certain item from inventory. Returns null if no item found
|
## Tries to take certain item from inventory. Returns null if no item found
|
||||||
func take(item: Item,amount: int) -> Stack:
|
func take(item: Item,amount: int) -> Stack:
|
||||||
var found = find(item)
|
var extracted = output_slot.extract_stack(amount)
|
||||||
if found == -1:
|
|
||||||
return null
|
|
||||||
var extracted = output_array[found].extract_stack(amount)
|
|
||||||
stack_taken.emit(extracted,found)
|
|
||||||
return extracted
|
return extracted
|
||||||
|
|
||||||
func find_input(item : Item) -> int:
|
func find_input(item : Item) -> int:
|
||||||
|
@ -88,7 +78,6 @@ func find_input(item : Item) -> int:
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
func find(item : Item) -> int:
|
func find(item : Item) -> int:
|
||||||
for i in range(output_capacity):
|
if output_slot.held_item == item:
|
||||||
if output_array[i].held_item == item:
|
return 0
|
||||||
return i
|
|
||||||
return -1
|
return -1
|
||||||
|
|
|
@ -72,6 +72,8 @@ func can_be_merged(item : Item) -> bool:
|
||||||
|
|
||||||
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
||||||
func merge_stack(stack: Stack) -> Stack:
|
func merge_stack(stack: Stack) -> Stack:
|
||||||
|
if stack == null or stack.held_item == null or stack.amount == 0:
|
||||||
|
return stack
|
||||||
if held_item == null:
|
if held_item == null:
|
||||||
if filter != null and filter.is_equal(stack.held_item) == false:
|
if filter != null and filter.is_equal(stack.held_item) == false:
|
||||||
return stack
|
return stack
|
||||||
|
|
|
@ -4,16 +4,41 @@ extends StructureBehaviour
|
||||||
@onready var inventory : InOutInventory = get_parent().inventory
|
@onready var inventory : InOutInventory = get_parent().inventory
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
|
inventory.stack_added.connect(check_for_recipe)
|
||||||
await get_tree().process_frame
|
await get_tree().process_frame
|
||||||
switch_recipe(selected_recipe)
|
switch_recipe(selected_recipe)
|
||||||
await get_tree().process_frame
|
|
||||||
for i in len(selected_recipe.ingridients):
|
for i in len(selected_recipe.ingridients):
|
||||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||||
print(inventory.input_array[i].filter)
|
|
||||||
|
|
||||||
func switch_recipe(recipe: Recipe) -> void:
|
func switch_recipe(recipe: Recipe) -> void:
|
||||||
selected_recipe = recipe
|
selected_recipe = recipe
|
||||||
inventory.input_capacity = len(selected_recipe.ingridients)
|
inventory.input_capacity = len(selected_recipe.ingridients)
|
||||||
inventory.resize()
|
inventory.resize()
|
||||||
for i in len(selected_recipe.ingridients):
|
for i in range(len(selected_recipe.ingridients)):
|
||||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||||
|
|
||||||
|
func _process(_delta: float) -> void:
|
||||||
|
if inventory.output_slot.amount <= 0:
|
||||||
|
return
|
||||||
|
var output : Structure = get_output_structure()
|
||||||
|
if output == null:
|
||||||
|
return
|
||||||
|
var ang_diff = Globals.facing_difference(output.facing,structure_parent.facing)
|
||||||
|
if output.inventory.can_add_from_side(ang_diff):
|
||||||
|
inventory.output_slot.merge_stack(output.inventory.add_from_side(inventory.output_slot.extract(),ang_diff))
|
||||||
|
|
||||||
|
func get_output_structure() -> Structure:
|
||||||
|
var facing_direction = Structure.facing_to_vector(structure_parent.facing)
|
||||||
|
var rotated = Vector2(0.5,1.5).rotated(-Vector2.DOWN.angle_to(facing_direction))
|
||||||
|
return structure_parent.get_relative(rotated+Vector2(0.5,0.5))
|
||||||
|
|
||||||
|
func check_for_recipe(_stack : Stack, _position : int) -> void:
|
||||||
|
for i in range(len(selected_recipe.ingridients)):
|
||||||
|
if inventory.input_array[i].amount < selected_recipe.ingridients[i].amount:
|
||||||
|
return
|
||||||
|
craft()
|
||||||
|
|
||||||
|
func craft() -> void:
|
||||||
|
for i in range(len(selected_recipe.ingridients)):
|
||||||
|
inventory.input_array[i].extract_stack(selected_recipe.ingridients[i].amount)
|
||||||
|
inventory.output_slot.merge_stack(Stack.new(selected_recipe.result.item,selected_recipe.result.amount))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue