rotation rework, deletion of complex objects

This commit is contained in:
Rendo 2025-10-16 19:47:52 +05:00
commit 8dcd7725a9
15 changed files with 103 additions and 147 deletions

View file

@ -11,6 +11,3 @@ enum Sides {
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))

View file

@ -42,8 +42,13 @@ func destroy_at(point : Vector2) -> void:
var index = building_zone.indexify_global_point(point)
if index == -1:
return
structures[index].queue_free()
structures[index] = null
var found: Structure = structures[index]
for dim_point in found.get_dimension_points():
structures[building_zone.indexify_global_point(found.global_position+dim_point)] = null
found.queue_free()
func is_point_occupied(point : Vector2) -> bool:
return get_at(point) != null

View file

@ -30,18 +30,18 @@ func _input(event: InputEvent) -> void:
if event.is_action_pressed("plc_place"):
var zone = try_get_zone(held_construction.global_position)
if zone != null and held_construction.try_place(zone):
var facing = held_construction.facing
var direction = held_construction.direction
held_construction = selected_prototype.scene.instantiate()
add_child(held_construction)
held_construction.set_facing(facing)
held_construction.set_direction(direction)
if event.is_action_pressed("plc_rotate_up"):
if held_construction != null:
held_construction.cycle_up_facing()
held_construction.increment_direction()
if event.is_action_pressed("plc_rotate_down"):
if held_construction != null:
held_construction.cycle_down_facing()
held_construction.decrement_direction()
if event.is_action_pressed("plc_cancel"):
held_construction.queue_free()

View file

@ -4,15 +4,7 @@ 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)
signal changed_direction(to: float,max_directions : int)
## Dimensions of structure in grid tiles
@export var dimensions : Rect2i = Rect2i(0,0,1,1):
@ -25,20 +17,8 @@ signal switched_facing(to: Facing)
## 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
@export var direction : float
@export var maximum_directions : int
## Debug draw of points
func _draw() -> void:
@ -83,16 +63,21 @@ func get_dimension_points() -> Array[Vector2]:
result[x + y * dimensions.size.x] = (Vector2(x,y)*Globals.GRID_SIZE + Vector2(dimensions.position))
return result
func set_facing(to : Facing) -> void:
facing = to
switched_facing.emit(facing)
func set_direction(to : float) -> void:
direction = to
changed_direction.emit(direction,maximum_directions)
func cycle_up_facing() -> void:
if facing == Facing.UNDIRECTIONAL:
return
set_facing(wrapi(facing+1,1,5))
func increment_direction() -> void:
set_direction(wrapf(direction + TAU/maximum_directions,0,TAU))
func cycle_down_facing() -> void:
if facing == Facing.UNDIRECTIONAL:
return
set_facing(wrapi(facing-1,1,5))
func decrement_direction() -> void:
set_direction(wrapf(direction - TAU/maximum_directions,0,TAU))
func direction_vector() -> Vector2:
return Vector2.from_angle(direction)
func direction_to(structure: Structure):
return (structure.global_position-global_position).angle()
func direction_difference(structure: Structure):
return direction-structure.direction

View file

@ -1,5 +1,7 @@
@abstract
extends Node2D
class_name StructureBehaviour
@onready var structure_parent : Structure = get_parent()
@onready var inventory : Inventory = structure_parent.inventory

View file

@ -1,7 +1,6 @@
extends StructureBehaviour
@export var selected_recipe : Recipe
@onready var inventory : InOutInventory = get_parent().inventory
func _ready() -> void:
inventory.stack_added.connect(check_for_recipe)
@ -23,13 +22,12 @@ func _process(_delta: float) -> void:
var output : Structure = get_output_structure()
if output == null:
return
var ang_diff = Globals.facing_difference(output.facing,structure_parent.facing)
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))
func get_output_structure() -> Structure:
var facing_direction = Structure.facing_to_vector(structure_parent.facing)
var rotated = Vector2(0.5,1.5).rotated(-Vector2.DOWN.angle_to(facing_direction))
var rotated = Vector2(1.5,-0.5).rotated(structure_parent.direction)
return structure_parent.get_relative(rotated+Vector2(0.5,0.5))
func check_for_recipe(_stack : Stack, _position : int) -> void:

View file

@ -1,14 +1,7 @@
extends StructureBehaviour
@onready var inventory : BeltInventory = structure_parent.inventory
@onready var animator : AnimationPlayer = $"../AnimationPlayer"
func _ready() -> void:
sync_animations.call_deferred()
func sync_animations() -> void:
animator.seek(BeltManager.sync_time,true)
func _draw() -> void:
for i in range(inventory.capacity):
if inventory.internal_array[i].amount > 0:
@ -23,41 +16,20 @@ func _process(delta: float) -> void:
func calculate_position(index: int) -> Vector2:
var indexed_part = 16.0 / inventory.capacity
match structure_parent.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
return -structure_parent.direction_vector()*8 + structure_parent.direction_vector() * indexed_part * (index + inventory.progress_array[index])
func get_next() -> Structure:
var faced_vector = Structure.facing_to_vector(structure_parent.facing)
return structure_parent.get_relative(faced_vector)
return structure_parent.get_relative(structure_parent.direction_vector())
func try_transfer(structure : Structure) -> void:
if structure == null or inventory.internal_array[inventory.capacity-1].amount == 0:
return
var last_slot = inventory.internal_array[inventory.capacity-1]
var ang_diff = Globals.facing_difference(structure_parent.facing,structure.facing)
var ang_diff = structure_parent.direction_difference(structure)
if structure.inventory.can_add_from_side(ang_diff,last_slot.held_item) == false:
return
last_slot.merge_stack(structure.inventory.add_from_side(last_slot.extract(),ang_diff))
if last_slot.amount == 0:
inventory.sort()
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()

View file

@ -0,0 +1,4 @@
extends AnimationPlayer
func _ready() -> void:
seek.call_deferred(BeltManager.sync_time, true)

View file

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

View file

@ -0,0 +1,12 @@
extends Sprite2D
@export var textures : Array[Texture]
@export var structure : Structure
func _ready() -> void:
structure.changed_direction.connect(on_direction_changed)
func on_direction_changed(to: float, max_directions: int) -> void:
var indexed_direction = to*max_directions/TAU
texture = textures[indexed_direction]

View file

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