Inventory context
This commit is contained in:
parent
d413c211b5
commit
fed57e38df
17 changed files with 128 additions and 108 deletions
|
@ -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)
|
||||
|
|
11
scripts/gui/construction_buttons_generator.gd
Normal file
11
scripts/gui/construction_buttons_generator.gd
Normal 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)
|
1
scripts/gui/construction_buttons_generator.gd.uid
Normal file
1
scripts/gui/construction_buttons_generator.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://bnjwg4rlcfd8k
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
13
scripts/inventory/inventory_context.gd
Normal file
13
scripts/inventory/inventory_context.gd
Normal 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
|
1
scripts/inventory/inventory_context.gd.uid
Normal file
1
scripts/inventory/inventory_context.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://d3ytipk4nt11d
|
27
scripts/inventory/splitter_inventory.gd
Normal file
27
scripts/inventory/splitter_inventory.gd
Normal 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()
|
1
scripts/inventory/splitter_inventory.gd.uid
Normal file
1
scripts/inventory/splitter_inventory.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://dlt3mbu6hk572
|
|
@ -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)):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue