extends Node2D @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 var current_time := 50.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 # Replace with function body. # 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 if current_time >= 2.0: current_time = 0 spawn() func spawn_duplicate(asteroid: Node2D): var dup = asteroid.duplicate(DUPLICATE_USE_INSTANTIATION) dup.currently_in = true dup.position = (asteroid.position - Vector2(320, 320)).rotated(PI) + Vector2(320, 320) 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)) + Vector2(320, 320) new.global_position = new_global_position new.direction = (player.global_position - new_global_position).normalized() 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)