Files
Azzteroid/script/spawner.gd
2025-09-07 23:13:51 +02:00

83 lines
2.6 KiB
GDScript

extends Node2D
class_name Spawner
@export var asteroidx1 : PackedScene
@export var asteroidx2 : PackedScene
@export var asteroidx3 : PackedScene
@export var asteroidx4 : PackedScene
@export var player : CharacterBody2D
@export var distance:= 600.0
@export var delay_spawn := 3.0
@export var load_screen:= false
var current_time := 0.0
var global_time := 0.0
signal add_collision
var list_of_collision_1 : Array[Node]
var list_of_collision_2 : Array[Node]
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var tmp : Vector2
for i in range(list_of_collision_1.size()):
if list_of_collision_1[i] and list_of_collision_2[i]:
var normal_vec = (list_of_collision_1[i].position - list_of_collision_2[i].position).normalized()
tmp = list_of_collision_1[i].direction
list_of_collision_1[i].direction = (list_of_collision_2[i].direction + normal_vec).normalized()
list_of_collision_2[i].direction = (tmp - normal_vec).normalized()
list_of_collision_1 = []
list_of_collision_2 = []
current_time += delta
global_time += delta
if current_time >= clampf(delay_spawn * (1- (global_time / 200)), 0.5, 100) and ((load_screen and get_children().size() < 10) or not load_screen):
current_time = 0
spawn()
func spawn_duplicate(asteroid: Node2D):
var dup = asteroid.duplicate(DUPLICATE_USE_INSTANTIATION)
dup.type = asteroid.type
dup.is_in = false
var new_pos = (asteroid.position ).rotated(PI)
dup.SPEED = asteroid.SPEED
dup.position = new_pos
dup.rotation = asteroid.rotation
dup.direction = asteroid.direction
call_deferred("add_child", dup)
func spawn():
var choice = randi_range(1, 4)
var new
if choice == 1:
new = asteroidx1.instantiate()
new.type = 1
elif choice == 2:
new = asteroidx2.instantiate()
new.type = 2
elif choice == 3:
new = asteroidx3.instantiate()
new.type = 3
elif choice == 4:
new = asteroidx4.instantiate()
new.type = 4
var new_global_position = Vector2(distance,0).rotated(randf_range(0.0, 360))
new.global_position = new_global_position
new.direction = (player.global_position - new_global_position).normalized()
new.SPEED = randf() * 50 + 50 * ( 1 + (global_time / 200))
add_child(new)
func _add_collision(body1 : Node, body2: Node) -> void:
for i in range(list_of_collision_1.size()):
if list_of_collision_1[i] == body1 and list_of_collision_2[i]== body2:
return
if list_of_collision_1[i] == body2 and list_of_collision_2[i] == body1:
return
list_of_collision_1.append(body1)
list_of_collision_2.append(body2)