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

@ -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)):