Files
vr-project/scripts/bee_swarm.gd

56 lines
1.5 KiB
GDScript

@tool
class_name BeeSwarm
extends Node3D
@export var bees_count: int = 10:
set(count):
bees_count = count
if Engine.is_editor_hint():
_spawn_bees()
@export var spawn_radius: float = 5.0:
set(radius):
spawn_radius = radius
if Engine.is_editor_hint():
_update_collision()
_spawn_bees()
const BEE_INSTANCE = preload("res://scenes/bee.tscn")
@onready var collision_shape: CollisionShape3D = $CollisionShape3D
func _ready() -> void:
_update_collision()
_spawn_bees()
func _update_collision() -> void:
var shape: SphereShape3D = SphereShape3D.new()
shape.radius = spawn_radius
collision_shape.shape = shape
func _spawn_bees() -> void:
_destroy_bees()
for i in range(bees_count):
var bee: Bee = BEE_INSTANCE.instantiate()
bee.position = get_random_point()
if not Engine.is_editor_hint():
bee.swarm = self
#bee.rotate_x(randf_range(deg_to_rad(-180), deg_to_rad(180)))
bee.rotate_y(randf_range(deg_to_rad(-180), deg_to_rad(180)))
bee.rotate_z(randf_range(deg_to_rad(-15), deg_to_rad(15)))
add_child(bee)
func get_random_point() -> Vector3:
var theta: float = randf_range(deg_to_rad(-180.0), deg_to_rad(180.0))
var phi: float = randf_range(deg_to_rad(-180.0), deg_to_rad(180.0))
var radius: float = randf_range(0.01, spawn_radius)
var x = radius * sin(phi) * cos(theta)
var y = radius * sin(phi) * sin(theta)
var z = radius * cos(phi)
return Vector3(x, y, z)
func _destroy_bees() -> void:
for child in get_children():
if child is Bee:
child.free()