Conveyor sideloading
This commit is contained in:
parent
222960a824
commit
81451bd356
14 changed files with 243 additions and 17 deletions
8
scripts/autoloads/conveyor_manager.gd
Normal file
8
scripts/autoloads/conveyor_manager.gd
Normal file
|
@ -0,0 +1,8 @@
|
|||
extends Node
|
||||
|
||||
# TODO: Use this autoload for conveyor optimizations
|
||||
|
||||
var sync_time : float
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
sync_time = fposmod(sync_time+delta,1)
|
1
scripts/autoloads/conveyor_manager.gd.uid
Normal file
1
scripts/autoloads/conveyor_manager.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://damurnhtipmga
|
|
@ -4,3 +4,13 @@ extends Object
|
|||
class_name Globals
|
||||
|
||||
const GRID_SIZE : Vector2 = Vector2(16,16)
|
||||
|
||||
enum Sides {
|
||||
RIGHT,
|
||||
DOWN,
|
||||
LEFT,
|
||||
UP
|
||||
}
|
||||
|
||||
static func facing_difference(from : Structure.Facing, to : Structure.Facing) -> float:
|
||||
return Structure.facing_to_vector(from).angle_to(Structure.facing_to_vector(to))
|
||||
|
|
|
@ -45,9 +45,22 @@ func add(stack: Stack) -> Stack:
|
|||
stack_added.emit(stack,0)
|
||||
return internal_array[0].merge_stack(stack)
|
||||
|
||||
func add_from_side(stack : Stack, ang_diff : float) -> Stack:
|
||||
if abs(ang_diff) >= PI/4:
|
||||
if internal_array[capacity/2].amount != 0:
|
||||
return null
|
||||
stack_added.emit(stack,capacity/2)
|
||||
return internal_array[capacity/2].merge_stack(stack)
|
||||
return add(stack)
|
||||
|
||||
func can_add() -> bool:
|
||||
return internal_array[0].amount == 0
|
||||
|
||||
func can_add_from_side(ang_diff : float) -> bool:
|
||||
if abs(ang_diff) >= PI/4:
|
||||
return internal_array[capacity/2].amount == 0
|
||||
return can_add()
|
||||
|
||||
## Tries to take first item. Returns null if no items in inventory
|
||||
func pop() -> Stack:
|
||||
if internal_array[capacity-1].amount == 0:
|
||||
|
|
|
@ -16,7 +16,10 @@ func _input(event: InputEvent) -> void:
|
|||
else:
|
||||
if held_construction.try_place(zone):
|
||||
held_construction = null
|
||||
|
||||
if event.is_action_pressed("plc_rotate_up"):
|
||||
if held_construction != null:
|
||||
held_construction.cycle_up_facing()
|
||||
|
||||
if event.is_action_pressed("plc_cancel"):
|
||||
held_construction.queue_free()
|
||||
|
||||
|
|
|
@ -4,6 +4,16 @@ extends Node2D
|
|||
## Game object that interact with other structures in its grid space
|
||||
class_name Structure
|
||||
|
||||
enum Facing {
|
||||
UNDIRECTIONAL = 0,
|
||||
RIGHT = 1,
|
||||
DOWN = 2,
|
||||
LEFT = 3,
|
||||
UP = 4
|
||||
}
|
||||
|
||||
signal switched_facing(to: Facing)
|
||||
|
||||
## Dimensions of structure in grid tiles
|
||||
@export var dimensions : Rect2i = Rect2i(0,0,1,1):
|
||||
set(value):
|
||||
|
@ -15,6 +25,20 @@ class_name Structure
|
|||
|
||||
## Inventory of this structure
|
||||
@export var inventory : Inventory
|
||||
@export var facing : Facing
|
||||
|
||||
static func facing_to_vector(face : Facing) -> Vector2:
|
||||
match face:
|
||||
Facing.RIGHT:
|
||||
return Vector2.RIGHT
|
||||
Facing.LEFT:
|
||||
return Vector2.LEFT
|
||||
Facing.UP:
|
||||
return Vector2.UP
|
||||
Facing.DOWN:
|
||||
return Vector2.DOWN
|
||||
_:
|
||||
return Vector2.ZERO
|
||||
|
||||
## Debug draw of points
|
||||
func _draw() -> void:
|
||||
|
@ -58,3 +82,9 @@ func get_dimension_points() -> Array[Vector2]:
|
|||
for y in range(dimensions.size.y):
|
||||
result[x + y * dimensions.size.x] = (Vector2(x,y)*Globals.GRID_SIZE + Vector2(dimensions.position))
|
||||
return result
|
||||
|
||||
func cycle_up_facing() -> void:
|
||||
if facing == Facing.UNDIRECTIONAL:
|
||||
return
|
||||
facing = wrapi(facing+1,1,5) as Facing
|
||||
switched_facing.emit(facing)
|
||||
|
|
|
@ -1,20 +1,65 @@
|
|||
extends Node2D
|
||||
|
||||
@onready var inventory : ConveyorInventory = get_parent().inventory
|
||||
@onready var parent_structure : Structure = get_parent()
|
||||
@onready var inventory : ConveyorInventory = parent_structure.inventory
|
||||
@onready var animator : AnimationPlayer = $"../AnimationPlayer"
|
||||
|
||||
func _ready() -> void:
|
||||
sync_animations.call_deferred()
|
||||
|
||||
func sync_animations() -> void:
|
||||
animator.seek(ConveyorManager.sync_time,true)
|
||||
|
||||
func _draw() -> void:
|
||||
for i in range(inventory.capacity):
|
||||
if inventory.internal_array[i].amount > 0:
|
||||
var calculated_position = Vector2(-8,0)+Vector2(16/inventory.capacity*i+16/inventory.capacity*inventory.progress_array[i],0)
|
||||
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position - inventory.internal_array[i].held_item.preview.get_size()/2.0)
|
||||
var calculated_position = calculate_position(i) - inventory.internal_array[i].held_item.preview.get_size()/2.0
|
||||
draw_texture(inventory.internal_array[i].held_item.preview,calculated_position)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
inventory.advance(delta)
|
||||
queue_redraw()
|
||||
var right : Structure = get_parent().get_relative(Vector2.RIGHT)
|
||||
if right == null or right.inventory.can_add() == false:
|
||||
var next : Structure = get_next()
|
||||
if next == null or next.inventory.can_add_from_side(Globals.facing_difference(next.facing,parent_structure.facing)) == false:
|
||||
return
|
||||
var popped = inventory.pop()
|
||||
if popped == null:
|
||||
return
|
||||
right.inventory.add(popped)
|
||||
next.inventory.add_from_side(popped,Globals.facing_difference(next.facing,parent_structure.facing))
|
||||
|
||||
func calculate_position(index: int) -> Vector2:
|
||||
var indexed_part = 16.0 / inventory.capacity
|
||||
match parent_structure.facing:
|
||||
Structure.Facing.RIGHT:
|
||||
return Vector2.LEFT*8 + Vector2.RIGHT * indexed_part * (index + inventory.progress_array[index])
|
||||
Structure.Facing.DOWN:
|
||||
return Vector2.UP*8 + Vector2.DOWN * indexed_part * (index + inventory.progress_array[index])
|
||||
Structure.Facing.LEFT:
|
||||
return Vector2.RIGHT*8 + Vector2.LEFT * indexed_part * (index + inventory.progress_array[index])
|
||||
Structure.Facing.UP:
|
||||
return Vector2.DOWN*8 + Vector2.UP * indexed_part * (index + inventory.progress_array[index])
|
||||
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
|
||||
|
||||
func _on_conveyor_switched_facing(to: Structure.Facing) -> void:
|
||||
match to:
|
||||
Structure.Facing.RIGHT:
|
||||
animator.play("right")
|
||||
Structure.Facing.DOWN:
|
||||
animator.play("down")
|
||||
Structure.Facing.LEFT:
|
||||
animator.play("left")
|
||||
Structure.Facing.UP:
|
||||
animator.play("up")
|
||||
sync_animations()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue