File size: 4,404 Bytes
d4e5991 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | @tool
class_name BoxSpawner
extends ResizableNode3D
## The box scene to spawn (must be a Box-derived PackedScene).
@export var scene: PackedScene
## When enabled, stops spawning new boxes.
@export var disable: bool = false:
set(value):
if value == disable:
return
disable = value
if is_inside_tree():
_change_texture()
if not disable:
_reset_spawn_cycle()
## The color applied to spawned boxes.
@export var box_color: Color = Color.WHITE:
set(value):
box_color = value
## Enable random sizing for spawned boxes within min/max range.
@export var random_size: bool = false
## Minimum size for randomly sized boxes (X, Y, Z dimensions).
@export var random_size_min: Vector3 = Vector3(0.5, 0.5, 0.5)
## Maximum size for randomly sized boxes (X, Y, Z dimensions).
@export var random_size_max: Vector3 = Vector3(1, 1, 1)
## Initial velocity applied to spawned boxes.
@export var initial_linear_velocity: Vector3 = Vector3.ZERO
## Number of boxes spawned per minute (0-1000).
@export var boxes_per_minute: int = 45:
set(value):
value = clamp(value, 0, 1000)
boxes_per_minute = value
## When true, boxes spawn at a fixed rate. When false, spawn times vary randomly.
@export var fixed_rate: bool = true
## Optional conveyor reference. Spawning pauses when conveyor speed is zero.
@export var conveyor: Node3D = null:
set(value):
conveyor = value
if not value:
_conveyor_stopped = false
var _scan_interval: float = 0.0
var _conveyor_stopped: bool = false
var _next_spawn_time: float = 0.0
var _spawn_counter: int = 0
var _first_spawn_done: bool = false
@onready var _preview_mesh: MeshInstance3D = $MeshInstance3D
@onready var disabled_box_texture: MeshInstance3D = $MeshInstance3D2
@onready var _preview_collision: CollisionShape3D = $Area3D/CollisionShape3D
func _init() -> void:
super._init()
size_default = Vector3.ONE
func _enter_tree() -> void:
super._enter_tree()
_reset_spawn_cycle()
func _ready() -> void:
EditorInterface.simulation_started.connect(_on_simulation_started)
EditorInterface.simulation_stopped.connect(_on_simulation_ended)
_on_size_changed()
_change_texture()
func _on_size_changed() -> void:
if is_instance_valid(_preview_mesh):
_preview_mesh.scale = size * 0.5
if is_instance_valid(disabled_box_texture):
disabled_box_texture.scale = size * 0.501
if is_instance_valid(_preview_collision):
var box_shape := _preview_collision.shape as BoxShape3D
if box_shape:
box_shape.size = size
func _physics_process(delta: float) -> void:
if conveyor and EditorInterface.is_simulation_running() and &"speed" in conveyor:
_conveyor_stopped = conveyor.speed == 0
if disable or _conveyor_stopped or not EditorInterface.is_simulation_running():
return
_scan_interval += delta
if not _first_spawn_done:
_spawn_box()
_first_spawn_done = true
_spawn_counter += 1
_scan_interval = 0.0
if fixed_rate:
var time_between: float = 60.0 / float(boxes_per_minute)
if _scan_interval >= time_between:
_spawn_box()
_scan_interval -= time_between
else:
if _scan_interval >= _next_spawn_time:
_spawn_box()
_spawn_counter += 1
if _spawn_counter >= boxes_per_minute:
_reset_spawn_cycle()
else:
_next_spawn_time = _scan_interval + (60.0 / boxes_per_minute) * randf_range(0.5, 1.5)
func _spawn_box() -> void:
var box := scene.instantiate() as Box
if random_size:
var x := randf_range(random_size_min.x, random_size_max.x)
var y := randf_range(random_size_min.y, random_size_max.y)
var z := randf_range(random_size_min.z, random_size_max.z)
box.size = Vector3(x, y, z)
else:
box.size = size
box.rotation = rotation
box.position = position
box.initial_linear_velocity = initial_linear_velocity
box.color = box_color
box.instanced = true
add_child(box, true)
box.owner = get_tree().edited_scene_root
func _reset_spawn_cycle() -> void:
_scan_interval = 0.0
_spawn_counter = 0
_first_spawn_done = false
_next_spawn_time = (60.0 / boxes_per_minute) * randf_range(0.5, 1.5)
func _change_texture() -> void:
if not is_inside_tree():
return
disabled_box_texture.visible = disable
func use() -> void:
disable = not disable
func _on_simulation_started() -> void:
if conveyor and &"speed" not in conveyor:
push_warning("Conveyor reference in " + name + " does not have a speed property")
_reset_spawn_cycle()
func _on_simulation_ended() -> void:
_conveyor_stopped = false
|