Broken pos
This commit is contained in:
parent
11d928c4f3
commit
3af13d0a8b
24 changed files with 304 additions and 146 deletions
|
@ -34,7 +34,7 @@ func deferred_init():
|
|||
## Finds first entry of item. Returns -1 if no item found
|
||||
func find(item : Item) -> int:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].held_item == item:
|
||||
if internal_array[i].can_be_merged(item):
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
@ -58,15 +58,15 @@ func add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
|||
return internal_array[capacity-1].merge_stack(stack)
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
func can_add(_item : Item = null) -> bool:
|
||||
return internal_array[0].amount == 0
|
||||
|
||||
func can_add_from_side(ang_diff : float) -> bool:
|
||||
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()
|
||||
return can_add(item)
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
|
@ -77,6 +77,13 @@ func pop() -> Stack:
|
|||
stack_taken.emit(internal_array[capacity-1], capacity-1)
|
||||
return internal_array[capacity-1].extract()
|
||||
|
||||
func peek() -> Stack:
|
||||
if internal_array[capacity-1].amount == 0:
|
||||
return null
|
||||
if progress_array[capacity-1] < pop_treshold:
|
||||
return null
|
||||
return internal_array[capacity-1].as_stack()
|
||||
|
||||
func advance(delta : float) -> void:
|
||||
var progress_flag : bool = false
|
||||
for i in range(capacity):
|
||||
|
|
94
scripts/inventory/in_out_inventory.gd
Normal file
94
scripts/inventory/in_out_inventory.gd
Normal file
|
@ -0,0 +1,94 @@
|
|||
@tool
|
||||
extends Inventory
|
||||
|
||||
class_name InOutInventory
|
||||
|
||||
@export var input_capacity : int
|
||||
@export var output_capacity : int
|
||||
|
||||
@export_storage var input_array : Array[InventorySlot]
|
||||
@export_storage var output_array : Array[InventorySlot]
|
||||
|
||||
func resize() -> void:
|
||||
input_array.resize(input_capacity)
|
||||
output_array.resize(output_capacity)
|
||||
for i in range(input_capacity):
|
||||
input_array[i] = InventorySlot.new()
|
||||
for i in range(output_capacity):
|
||||
output_array[i] = InventorySlot.new()
|
||||
|
||||
func _init() -> void:
|
||||
super()
|
||||
resize.call_deferred()
|
||||
|
||||
func add(stack : Stack) -> Stack:
|
||||
var found_index : int = find_input(stack.held_item)
|
||||
if found_index != -1:
|
||||
stack_added.emit(stack,found_index)
|
||||
stack = input_array[found_index].merge_stack(stack)
|
||||
if stack == null:
|
||||
return null
|
||||
for i in range(input_capacity):
|
||||
if input_array[i].can_be_merged(stack.held_item):
|
||||
stack_added.emit(stack,i)
|
||||
stack = input_array[i].merge_stack(stack)
|
||||
if stack == null:
|
||||
return null
|
||||
return stack
|
||||
|
||||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(stack)
|
||||
|
||||
func can_add(item : Item = null) -> bool:
|
||||
if item == null:
|
||||
for i in range(input_capacity):
|
||||
return input_array[i].amount == 0
|
||||
return false
|
||||
else:
|
||||
for i in range(input_capacity):
|
||||
print(input_array[i].filter)
|
||||
if input_array[i].can_be_merged(item):
|
||||
print(input_array[i].filter)
|
||||
return true
|
||||
#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:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item != null:
|
||||
var extracted = output_array[i].extract()
|
||||
stack_taken.emit(extracted,i)
|
||||
return extracted
|
||||
return null
|
||||
|
||||
func peek() -> Stack:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item != null:
|
||||
var peeked = output_array[i].as_stack()
|
||||
return peeked
|
||||
return null
|
||||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
func take(item: Item,amount: int) -> Stack:
|
||||
var found = find(item)
|
||||
if found == -1:
|
||||
return null
|
||||
var extracted = output_array[found].extract_stack(amount)
|
||||
stack_taken.emit(extracted,found)
|
||||
return extracted
|
||||
|
||||
func find_input(item : Item) -> int:
|
||||
for i in range(input_capacity):
|
||||
if input_array[i].held_item == item:
|
||||
return i
|
||||
return -1
|
||||
|
||||
func find(item : Item) -> int:
|
||||
for i in range(output_capacity):
|
||||
if output_array[i].held_item == item:
|
||||
return i
|
||||
return -1
|
1
scripts/inventory/in_out_inventory.gd.uid
Normal file
1
scripts/inventory/in_out_inventory.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://m6b6vawdqgqb
|
|
@ -18,14 +18,17 @@ func add_from_side(stack : Stack, ang_diff : float) -> Stack
|
|||
|
||||
## Returns if conditions of adding are met
|
||||
@abstract
|
||||
func can_add() -> bool
|
||||
func can_add(item : Item = null) -> bool
|
||||
@abstract
|
||||
func can_add_from_side(ang_diff : float) -> bool
|
||||
func can_add_from_side(ang_diff : float, item : Item = null) -> bool
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
@abstract
|
||||
func pop() -> Stack
|
||||
|
||||
@abstract
|
||||
func peek() -> Stack
|
||||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
@abstract
|
||||
func take(item : Item,amount: int) -> Stack
|
||||
|
|
|
@ -28,6 +28,8 @@ class_name InventorySlot
|
|||
get:
|
||||
return amount
|
||||
|
||||
@export_storage var filter : Comparable
|
||||
|
||||
## Get some amount of items from slot. Returns null if slot is empty
|
||||
func extract_stack(extract_amount: int) -> Stack:
|
||||
if amount == 0:
|
||||
|
@ -53,9 +55,26 @@ func extract() -> Stack:
|
|||
|
||||
return return_stack
|
||||
|
||||
func as_stack() -> Stack:
|
||||
if amount == 0:
|
||||
return null
|
||||
|
||||
var return_stack : Stack = Stack.new(held_item,amount)
|
||||
|
||||
return return_stack
|
||||
|
||||
func can_be_merged(item : Item) -> bool:
|
||||
if filter != null:
|
||||
return filter.is_equal(item)
|
||||
if amount == 0:
|
||||
return true
|
||||
return item == held_item
|
||||
|
||||
## Add provided stack's amount to this amount. Returns null if stack is empty or items arent matching
|
||||
func merge_stack(stack: Stack) -> Stack:
|
||||
if held_item == null:
|
||||
if filter != null and filter.is_equal(stack.held_item) == false:
|
||||
return stack
|
||||
held_item = stack.held_item
|
||||
amount = stack.amount
|
||||
return null
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
@tool
|
||||
|
||||
extends Inventory
|
||||
|
||||
## Base class for FIFO inventories.
|
||||
|
||||
class_name Queue
|
||||
|
||||
## Amount of stacks that can be held in storage
|
||||
@export var capacity : int:
|
||||
set(value):
|
||||
if value < 0:
|
||||
return
|
||||
if value == capacity:
|
||||
return
|
||||
capacity = value
|
||||
get:
|
||||
return capacity
|
||||
|
||||
## :3
|
||||
@export_storage var internal_array : Array[InventorySlot] = []
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
super()
|
||||
internal_array.resize(capacity)
|
||||
for i in range(capacity):
|
||||
internal_array[i] = InventorySlot.new()
|
||||
|
||||
## Finds first entry of item. Returns -1 if no item found
|
||||
func find(item : Item) -> int:
|
||||
for i in range(len(internal_array)):
|
||||
if internal_array[i].held_item == item:
|
||||
return i
|
||||
return -1
|
||||
|
||||
## Tries to add an item into inventory. Returns not stored stack of item.
|
||||
func add(stack: Stack) -> Stack:
|
||||
for i in range(len(internal_array)):
|
||||
if internal_array[i].held_item == null or internal_array[i].held_item == stack.held_item:
|
||||
stack_added.emit(stack,i)
|
||||
stack = internal_array[i].merge_stack(stack)
|
||||
if stack == null:
|
||||
return null
|
||||
return stack
|
||||
|
||||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].amount == 0:
|
||||
return true
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float) -> bool:
|
||||
return can_add()
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
if internal_array[0].held_item == null:
|
||||
return null
|
||||
var stack_to_return = internal_array[0].extract()
|
||||
stack_taken.emit(stack_to_return,0)
|
||||
sort()
|
||||
return stack_to_return
|
||||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
func take(item: Item,amount: int) -> Stack:
|
||||
var found = find(item)
|
||||
if found == -1:
|
||||
return null
|
||||
var stack_to_return = internal_array[found].extract_stack(amount)
|
||||
stack_taken.emit(stack_to_return,found)
|
||||
sort()
|
||||
return stack_to_return
|
||||
|
||||
func sort() -> void:
|
||||
for i in range(len(internal_array)-1):
|
||||
if internal_array[i] == null:
|
||||
internal_array[i].merge_stack(internal_array[i+1].extract())
|
|
@ -1 +0,0 @@
|
|||
uid://q2gbtqfi32d
|
|
@ -21,6 +21,9 @@ class_name Storage
|
|||
|
||||
func _init() -> void:
|
||||
super()
|
||||
deferred_init.call_deferred()
|
||||
|
||||
func deferred_init():
|
||||
internal_array.resize(capacity)
|
||||
for i in range(capacity):
|
||||
internal_array[i] = InventorySlot.new()
|
||||
|
@ -51,14 +54,20 @@ func add(stack: Stack) -> Stack:
|
|||
func add_from_side(stack : Stack, _ang_diff : float) -> Stack:
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
for i in range(capacity):
|
||||
if internal_array[i].amount == 0:
|
||||
return true
|
||||
func can_add(item : Item = null) -> bool:
|
||||
if item == null:
|
||||
for i in range(internal_array):
|
||||
return internal_array[i].amount == 0
|
||||
return false
|
||||
else:
|
||||
for i in range(internal_array):
|
||||
if internal_array[i].amount == 0:
|
||||
return (internal_array[i].filter != null and internal_array[i].filter.is_equal(item)) or internal_array[i].filter == null
|
||||
return internal_array[i].held_item.is_equal(item)
|
||||
return false
|
||||
|
||||
func can_add_from_side(_ang_diff : float) -> bool:
|
||||
return can_add()
|
||||
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:
|
||||
|
@ -69,6 +78,13 @@ func pop() -> Stack:
|
|||
return extracted
|
||||
return null
|
||||
|
||||
func peek() -> Stack:
|
||||
for i in range(len(internal_array)):
|
||||
if internal_array[i].held_item != null:
|
||||
var peeked = internal_array[i].as_stack()
|
||||
return peeked
|
||||
return null
|
||||
|
||||
## Tries to take certain item from inventory. Returns null if no item found
|
||||
func take(item: Item,amount: int) -> Stack:
|
||||
var found = find(item)
|
||||
|
|
|
@ -17,7 +17,6 @@ func _input(event: InputEvent) -> void:
|
|||
selected_prototype = null
|
||||
|
||||
if event.is_action_pressed("plc_place") and delete_mode:
|
||||
print(delete_mode)
|
||||
var zone : PlacementZone = try_get_zone(global_position)
|
||||
if zone != null and zone.grid_controller.is_point_occupied(global_position):
|
||||
zone.grid_controller.destroy_at(global_position)
|
||||
|
|
5
scripts/structure_behaviour.gd
Normal file
5
scripts/structure_behaviour.gd
Normal file
|
@ -0,0 +1,5 @@
|
|||
extends Node2D
|
||||
|
||||
class_name StructureBehaviour
|
||||
|
||||
@onready var structure_parent : Structure = get_parent()
|
1
scripts/structure_behaviour.gd.uid
Normal file
1
scripts/structure_behaviour.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://iidfsh0lirc2
|
19
scripts/structures/assembler.gd
Normal file
19
scripts/structures/assembler.gd
Normal file
|
@ -0,0 +1,19 @@
|
|||
extends StructureBehaviour
|
||||
|
||||
@export var selected_recipe : Recipe
|
||||
@onready var inventory : InOutInventory = get_parent().inventory
|
||||
|
||||
func _ready() -> void:
|
||||
await get_tree().process_frame
|
||||
switch_recipe(selected_recipe)
|
||||
await get_tree().process_frame
|
||||
for i in len(selected_recipe.ingridients):
|
||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
||||
print(inventory.input_array[i].filter)
|
||||
|
||||
func switch_recipe(recipe: Recipe) -> void:
|
||||
selected_recipe = recipe
|
||||
inventory.input_capacity = len(selected_recipe.ingridients)
|
||||
inventory.resize()
|
||||
for i in len(selected_recipe.ingridients):
|
||||
inventory.input_array[i].filter = selected_recipe.ingridients[i].item
|
1
scripts/structures/assembler.gd.uid
Normal file
1
scripts/structures/assembler.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://c7mx3uatj6ulm
|
|
@ -1,7 +1,6 @@
|
|||
extends Node2D
|
||||
extends StructureBehaviour
|
||||
|
||||
@onready var parent_structure : Structure = get_parent()
|
||||
@onready var inventory : BeltInventory = parent_structure.inventory
|
||||
@onready var inventory : BeltInventory = structure_parent.inventory
|
||||
@onready var animator : AnimationPlayer = $"../AnimationPlayer"
|
||||
|
||||
func _ready() -> void:
|
||||
|
@ -14,23 +13,22 @@ func _draw() -> void:
|
|||
for i in range(inventory.capacity):
|
||||
if inventory.internal_array[i].amount > 0:
|
||||
var calculated_position = calculate_position(i) - inventory.internal_array[i].held_item.preview.get_size()/2.0
|
||||
for j in range(inventory.internal_array[i].amount/10):
|
||||
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position + Vector2(0,-j))
|
||||
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
inventory.advance(delta)
|
||||
queue_redraw()
|
||||
var next : Structure = get_next()
|
||||
if next == null or next.inventory == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,parent_structure.facing)) == false:
|
||||
if next == null or next.inventory == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,structure_parent.facing),inventory.internal_array[inventory.capacity-1].held_item) == false:
|
||||
return
|
||||
var popped = inventory.pop()
|
||||
if popped == null:
|
||||
return
|
||||
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,parent_structure.facing))
|
||||
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,structure_parent.facing))
|
||||
|
||||
func calculate_position(index: int) -> Vector2:
|
||||
var indexed_part = 16.0 / inventory.capacity
|
||||
match parent_structure.facing:
|
||||
match structure_parent.facing:
|
||||
Structure.Facing.RIGHT:
|
||||
return Vector2.LEFT*8 + Vector2.RIGHT * indexed_part * (index + inventory.progress_array[index])
|
||||
Structure.Facing.DOWN:
|
||||
|
@ -42,16 +40,8 @@ func calculate_position(index: int) -> Vector2:
|
|||
return Vector2.ZERO
|
||||
|
||||
func get_next() -> Structure:
|
||||
match parent_structure.facing:
|
||||
Structure.Facing.RIGHT:
|
||||
return parent_structure.get_relative(Vector2(1,0))
|
||||
Structure.Facing.DOWN:
|
||||
return parent_structure.get_relative(Vector2(0,1))
|
||||
Structure.Facing.LEFT:
|
||||
return parent_structure.get_relative(Vector2(-1,0))
|
||||
Structure.Facing.UP:
|
||||
return parent_structure.get_relative(Vector2(0,-1))
|
||||
return null
|
||||
var faced_vector = Structure.facing_to_vector(structure_parent.facing)
|
||||
return structure_parent.get_relative(faced_vector)
|
||||
|
||||
func _on_conveyor_switched_facing(to: Structure.Facing) -> void:
|
||||
match to:
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
extends Node
|
||||
extends StructureBehaviour
|
||||
|
||||
const test_item :=preload("res://generic/items/dbg_item.tres")
|
||||
const inp1 := preload("res://generic/items/dbg_input1.tres")
|
||||
const inp2 := preload("res://generic/items/dbg_input2.tres")
|
||||
const out := preload("res://generic/items/dbg_output.tres")
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
for point in [Vector2(-1,0),Vector2(1,0),Vector2(0,1),Vector2(0,-1)]:
|
||||
var found_structure :Structure = get_parent().get_relative(point)
|
||||
if found_structure and found_structure.inventory:
|
||||
found_structure.inventory.add_from_side(Stack.new(test_item,randi_range(1,test_item.stack_size)),Structure.facing_to_vector(found_structure.facing).angle_to(point))
|
||||
try_add(Vector2.UP,Stack.new(inp1,1))
|
||||
try_add(Vector2.DOWN,Stack.new(inp2,1))
|
||||
try_add(Vector2.RIGHT,Stack.new(out,1))
|
||||
|
||||
func try_add(dir: Vector2,stack : Stack):
|
||||
var found = structure_parent.get_relative(dir)
|
||||
if found != null:
|
||||
found.inventory.add(stack)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue