Inventory context

This commit is contained in:
Rendo 2025-10-16 22:05:24 +05:00
commit fed57e38df
17 changed files with 128 additions and 108 deletions

View file

@ -1,12 +1,9 @@
[gd_scene load_steps=11 format=3 uid="uid://u7thalwj5742"]
[gd_scene load_steps=8 format=3 uid="uid://u7thalwj5742"]
[ext_resource type="PackedScene" uid="uid://b7du4vul4bmpq" path="res://scenes/player_platform.tscn" id="1_4ia00"]
[ext_resource type="Script" uid="uid://db37xmo8ygwhg" path="res://scripts/gui/construction_button.gd" id="2_27lod"]
[ext_resource type="Resource" uid="uid://dj524jjal1f0n" path="res://generic/prototypes/belt.tres" id="3_2crwe"]
[ext_resource type="Script" uid="uid://bnjwg4rlcfd8k" path="res://scripts/gui/construction_buttons_generator.gd" id="3_i7yte"]
[ext_resource type="Script" uid="uid://b4nkk0ndqcto" path="res://scripts/placement_manager.gd" id="4_i7yte"]
[ext_resource type="PackedScene" uid="uid://bac552xgua68e" path="res://scenes/other_platform.tscn" id="5_mnxrb"]
[ext_resource type="Resource" uid="uid://yn1iesx30nfu" path="res://generic/prototypes/dbg_assembler.tres" id="5_ymyc2"]
[ext_resource type="Resource" uid="uid://jhe8wtjoo6c6" path="res://generic/prototypes/dbg_item_deposit.tres" id="6_bq5r8"]
[sub_resource type="Animation" id="Animation_bq5r8"]
length = 0.001
@ -65,53 +62,14 @@ offset_top = -0.01600647
grow_horizontal = 2
grow_vertical = 0
[node name="ConveyorButton" type="Button" parent="GUI/Panel"]
[node name="HBoxContainer" type="HBoxContainer" parent="GUI/Panel"]
layout_mode = 1
anchors_preset = -1
anchor_left = 0.008
anchor_top = 0.086
anchor_right = 0.1
anchor_bottom = 0.88600004
offset_left = -0.096000195
offset_top = -0.01999998
offset_right = -0.20000076
offset_bottom = -0.020004272
size_flags_horizontal = 3
theme_type_variation = &"CRT"
icon_alignment = 1
expand_icon = true
script = ExtResource("2_27lod")
constructible = ExtResource("3_2crwe")
[node name="AssemblerButton" type="Button" parent="GUI/Panel"]
layout_mode = 1
anchors_preset = -1
anchor_left = 0.107421875
anchor_top = 0.08571429
anchor_right = 0.19921875
anchor_bottom = 0.8857143
size_flags_horizontal = 3
theme_type_variation = &"CRT"
icon_alignment = 1
expand_icon = true
script = ExtResource("2_27lod")
constructible = ExtResource("5_ymyc2")
metadata/_edit_use_anchors_ = true
[node name="SpawnerButton" type="Button" parent="GUI/Panel"]
layout_mode = 1
anchors_preset = -1
anchor_left = 0.20898438
anchor_top = 0.08571429
anchor_right = 0.30078125
anchor_bottom = 0.8857143
size_flags_horizontal = 3
theme_type_variation = &"CRT"
icon_alignment = 1
expand_icon = true
script = ExtResource("2_27lod")
constructible = ExtResource("6_bq5r8")
metadata/_edit_use_anchors_ = true
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("3_i7yte")
[node name="Placer" type="Node2D" parent="."]
script = ExtResource("4_i7yte")

View file

@ -0,0 +1,17 @@
[gd_scene load_steps=2 format=3 uid="uid://dwq8u8ptmcegk"]
[ext_resource type="Script" uid="uid://db37xmo8ygwhg" path="res://scripts/gui/construction_button.gd" id="1_unpdg"]
[node name="ConstructionButton" type="AspectRatioContainer"]
offset_right = 12.0
offset_bottom = 9.0
size_flags_horizontal = 3
size_flags_vertical = 3
theme_type_variation = &"CRT"
[node name="Button" type="Button" parent="."]
layout_mode = 2
theme_type_variation = &"CRT"
icon_alignment = 1
expand_icon = true
script = ExtResource("1_unpdg")

View file

@ -1,9 +1,13 @@
extends Button
@export var constructible : Prototype
class_name ConstructionButton
func _ready() -> void:
icon = constructible.preview
var prototype : Prototype:
set(value):
prototype = value
icon = prototype.preview
get:
return prototype
func _pressed() -> void:
GuiEventBus.construction_selected.emit(constructible)
GuiEventBus.construction_selected.emit(prototype)

View file

@ -0,0 +1,11 @@
extends Node
const button = preload("res://scenes/gui/construction_button.tscn")
func _ready() -> void:
for file in ResourceLoader.list_directory("res://generic/prototypes/"):
var btn = button.instantiate()
var loaded_prototype : Prototype = ResourceLoader.load("res://generic/prototypes/"+file)
btn.get_node("Button").prototype = loaded_prototype
add_child(btn)

View file

@ -0,0 +1 @@
uid://bnjwg4rlcfd8k

View file

@ -2,5 +2,5 @@ extends Node
@warning_ignore_start("unused_signal")
signal construction_selected(constructible : Prototype)
signal construction_placed(constructible : Prototype)
signal construction_selected(prototype : Prototype)
signal construction_placed(prototype : Prototype)

View file

@ -39,38 +39,37 @@ func find(item : Item) -> int:
return -1
## Tries to add an item into inventory. Returns not stored stack of item.
func add(stack: Stack) -> Stack:
func add(stack: Stack, context: InventoryContext = null) -> Stack:
if context != null:
var ang_diff = (context.position - context.source.global_position).angle()-context.target.direction
if is_equal_approx(abs(ang_diff),PI/2):
if internal_array[capacity/2].amount != 0:
return null
stack_added.emit(stack,capacity/2)
internal_array[capacity/2].merge_stack(stack)
return stack
elif is_equal_approx(abs(ang_diff), PI):
if internal_array[capacity-1].amount != 0:
return null
stack_added.emit(stack,capacity-1)
internal_array[capacity-1].merge_stack(stack)
return stack
if internal_array[0].amount != 0:
return null
stack_added.emit(stack,0)
internal_array[0].merge_stack(stack)
return stack
func add_from_side(stack : Stack, ang_diff : float) -> Stack:
if is_equal_approx(abs(ang_diff),PI/2):
if internal_array[capacity/2].amount != 0:
return null
stack_added.emit(stack,capacity/2)
internal_array[capacity/2].merge_stack(stack)
return stack
elif is_equal_approx(abs(ang_diff), PI):
if internal_array[capacity-1].amount != 0:
return null
stack_added.emit(stack,capacity-1)
internal_array[capacity-1].merge_stack(stack)
return stack
return add(stack)
func can_add(_item : Item = null) -> bool:
func can_add(_item : Item = null, context : InventoryContext = null) -> bool:
if context != null:
var ang_diff = (context.position - context.source.global_position).angle()-context.target.direction
if is_equal_approx(abs(ang_diff),PI/2):
return internal_array[capacity/2].amount == 0
elif is_equal_approx(abs(ang_diff),PI):
return internal_array[capacity-1].amount == 0
return internal_array[0].amount == 0
func can_add_from_side(ang_diff : float, item : Item = null) -> bool:
if is_equal_approx(abs(ang_diff),PI/2):
return internal_array[capacity/2].amount == 0
elif is_equal_approx(abs(ang_diff),PI):
return internal_array[capacity-1].amount == 0
return can_add(item)
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:
if internal_array[capacity-1].amount == 0:

View file

@ -19,7 +19,7 @@ func _init() -> void:
super()
resize.call_deferred()
func add(stack : Stack) -> Stack:
func add(stack : Stack, _context: InventoryContext = null) -> Stack:
for i in range(input_capacity):
if input_array[i].can_be_merged(stack.held_item):
stack_added.emit(stack,i)
@ -28,10 +28,7 @@ func add(stack : Stack) -> Stack:
return null
return stack
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
return add(stack)
func can_add(item : Item = null) -> bool:
func can_add(item : Item = null, _context: InventoryContext = null) -> bool:
if item == null:
return false
else:
@ -40,8 +37,6 @@ func can_add(item : Item = null) -> bool:
return true
return false
func can_add_from_side(_ang_diff : float,item: Item = null) -> bool:
return can_add(item)
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:

View file

@ -12,15 +12,11 @@ func _init() -> void:
## Tries to add an item into inventory. Returns not stored stack of item.
@abstract
func add(stack : Stack) -> Stack
@abstract
func add_from_side(stack : Stack, ang_diff : float) -> Stack
func add(stack : Stack, context: InventoryContext = null) -> Stack
## Returns if conditions of adding are met
@abstract
func can_add(item : Item = null) -> bool
@abstract
func can_add_from_side(ang_diff : float, item : Item = null) -> bool
func can_add(item : Item = null, context: InventoryContext = null) -> bool
## Tries to take first item. Returns null if no items in inventory
@abstract

View file

@ -0,0 +1,13 @@
extends RefCounted
class_name InventoryContext
func _init(_source: Structure, _target: Structure,_position: Vector2) -> void:
self.source = _source
self.target = _target
self.position = _position
var source : Structure
var target : Structure
var position : Vector2

View file

@ -0,0 +1 @@
uid://d3ytipk4nt11d

View file

@ -0,0 +1,27 @@
@tool
extends Inventory
@export var capacity : int:
set(value):
if value < 0:
return
if value == capacity:
return
capacity = value
get:
return capacity
@export_storage var upper_array : Array[InventorySlot]
@export_storage var down_array : Array[InventorySlot]
func _init() -> void:
super()
deferred_init.call_deferred()
func deferred_init():
upper_array.resize(capacity)
down_array.resize(capacity)
for i in range(capacity):
upper_array[i] = InventorySlot.new()
down_array[i] = InventorySlot.new()

View file

@ -0,0 +1 @@
uid://dlt3mbu6hk572

View file

@ -36,7 +36,7 @@ func find(item : Item) -> int:
return -1
## Tries to add an item into inventory. Returns not stored stack of item.
func add(stack: Stack) -> Stack:
func add(stack: Stack, _context: InventoryContext = null) -> Stack:
var found_index : int = find(stack.held_item)
if found_index != -1:
stack_added.emit(stack,found_index)
@ -51,10 +51,7 @@ func add(stack: Stack) -> Stack:
return null
return stack
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
return add(stack)
func can_add(item : Item = null) -> bool:
func can_add(item : Item = null, _context: InventoryContext = null) -> bool:
if item == null:
for i in range(internal_array):
return internal_array[i].amount == 0
@ -66,9 +63,6 @@ func can_add(item : Item = null) -> bool:
return internal_array[i].held_item.is_equal(item)
return false
func can_add_from_side(_ang_diff : float,item : Item = null) -> bool:
return can_add(item)
## Tries to take first item. Returns null if no items in inventory
func pop() -> Stack:
for i in range(len(internal_array)):

View file

@ -24,14 +24,17 @@ func _process(_delta: float) -> void:
var output : Structure = get_output_structure()
if output == null:
return
var ang_diff = output.direction_difference(structure_parent)
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))
var transfer_context : InventoryContext = InventoryContext.new(structure_parent,output,get_output_position())
if output.inventory.can_add(inventory.output_slot.held_item,transfer_context):
inventory.output_slot.merge_stack(output.inventory.add(inventory.output_slot.extract(),transfer_context))
func get_output_structure() -> Structure:
var rotated = Vector2(1.5,-0.5).rotated(structure_parent.direction)
return structure_parent.get_relative(rotated+Vector2(0.5,0.5))
func get_output_position() -> Vector2:
return to_global(Vector2(1.5,-0.5).rotated(structure_parent.direction)+Vector2(0.5,0.5))
func check_for_recipe(_stack : Stack, _position : int) -> void:
if inventory.output_slot.held_item != null and inventory.output_slot.amount == inventory.output_slot.held_item.stack_size:
return

View file

@ -26,10 +26,10 @@ func try_transfer(structure : Structure) -> void:
if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0 or inventory.progress_array[inventory.capacity-1] < inventory.pop_treshold:
return
var last_slot = inventory.internal_array[inventory.capacity-1]
var ang_diff = structure_parent.direction_difference(structure)
if structure.inventory.can_add_from_side(ang_diff,last_slot.held_item) == false:
var transfer_context : InventoryContext = InventoryContext.new(structure_parent,structure,to_global(structure_parent.direction_vector()))
if structure.inventory.can_add(last_slot.held_item,transfer_context) == false:
return
last_slot.merge_stack(structure.inventory.add_from_side(last_slot.extract(),ang_diff))
last_slot.merge_stack(structure.inventory.add(last_slot.extract(),transfer_context))
if last_slot.amount == 0:
inventory.sort()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Before After
Before After