-
Notifications
You must be signed in to change notification settings - Fork 1
/
obstacle_generator.gd
38 lines (30 loc) · 1.17 KB
/
obstacle_generator.gd
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
extends Node
@export var n_obstacles: int = 40
func _ready() -> void:
var rock_scene = preload("res://rock.tscn")
var anthill = get_node("../Anthill")
var anthill_position = anthill.position
var viewport_size = get_viewport().get_visible_rect().size
var placed_positions: Array = []
randomize()
for i in range(n_obstacles):
var rock_instance: Node = rock_scene.instantiate()
var random_position: Vector2 = generate_random_position(anthill_position, viewport_size, placed_positions)
rock_instance.position = random_position
placed_positions.append(random_position)
add_child(rock_instance)
rock_instance.add_to_group("rocks")
func generate_random_position(anthill_position: Vector2, viewport_size: Vector2, placed_positions: Array) -> Vector2:
var random_position: Vector2
while true:
random_position = Vector2(randf() * viewport_size.x, randf() * viewport_size.y)
var valid_position = true
if random_position.distance_to(anthill_position) < 128:
valid_position = false
for placed_position in placed_positions:
if random_position.distance_to(placed_position) < 96:
valid_position = false
break
if valid_position:
break
return random_position