Splitters part 1

This commit is contained in:
Rendo 2025-10-16 23:29:49 +05:00
commit 3c0777f4fd
9 changed files with 150 additions and 2 deletions

View file

@ -0,0 +1,15 @@
extends Node
@export var structure : Structure
@export var sprite : Sprite2D
@onready var initial_dimensions : Rect2i = structure.dimensions
@onready var initial_offset : Vector2 = sprite.offset
func _ready() -> void:
structure.changed_direction.connect(on_changed_direction)
func on_changed_direction(to: float,max_directions : int):
var calculated_size = Vector2(initial_dimensions.size).rotated(to).abs().ceil()
structure.dimensions = Rect2i(initial_dimensions.position,calculated_size)
sprite.offset = initial_offset.rotated(to).abs()

View file

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

View file

@ -2,6 +2,8 @@
extends Inventory
class_name SplitterInventory
@export var capacity : int:
set(value):
if value < 0:
@ -25,3 +27,48 @@ func deferred_init():
for i in range(capacity):
upper_array[i] = InventorySlot.new()
down_array[i] = InventorySlot.new()
func add(stack : Stack, context: InventoryContext = null) -> Stack:
if context == null:
return stack
if context.position == context.target.global_position:
return add_to_array(stack,upper_array,context)
return add_to_array(stack,down_array,context)
func add_to_array(stack: Stack, array: Array[InventorySlot], context: InventoryContext = null) -> Stack:
for i in range(len(array)):
if array[i].held_item == null or array[i].held_item == stack.held_item:
stack_added.emit(stack,i)
array[i].merge_stack(stack)
if stack.is_valid() == false:
return null
return stack
func can_add(item : Item = null, context: InventoryContext = null) -> bool:
if context == null:
return false
if item == null:
for i in range(capacity):
if upper_array[i].amount == 0 or down_array[i].amount == 0:
return true
else:
for i in range(capacity):
if upper_array[i].amount == 0:
return upper_array[i].can_be_merged(item)
if down_array[i].amount ==0:
return down_array[i].can_be_merged(item)
return false
func pop() -> Stack:
return null
func peek() -> Stack:
return null
## Tries to take certain item from inventory. Returns null if no item found
func take(item : Item,amount: int) -> Stack:
return null
## Finds first entry of item. Returns -1 if no item found
func find(item : Item) -> int:
return -1

View file

@ -31,9 +31,12 @@ func _draw() -> void:
## dv : Vector2 - get position in tiles [br]
func get_relative(dv : Vector2) -> Structure:
if get_parent() is GridController:
return get_parent().get_at(global_position+dv*Globals.GRID_SIZE)
return get_parent().get_at(relative_tile_as_global(dv))
return null
func relative_tile_as_global(dv: Vector2) -> Vector2:
return global_position+dv*Globals.GRID_SIZE
## Check structure is in zone and does not collide with any other structures [br]
## Returns true if structure is in zone and does not collide with any other structure [br]
## Returns false if structure is not in zone or does not collide with any other structure

View file

@ -24,7 +24,7 @@ func _process(_delta: float) -> void:
var output : Structure = get_output_structure()
if output == null:
return
var transfer_context : InventoryContext = InventoryContext.new(structure_parent,output,get_output_position())
var transfer_context : InventoryContext = InventoryContext.new(structure_parent,output,structure_parent.relative_tile_as_global(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))

View file

@ -0,0 +1 @@
extends StructureBehaviour

View file

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