46 lines
1.4 KiB
GDScript
46 lines
1.4 KiB
GDScript
extends Node
|
|
|
|
@onready var screen_size = Vector2(ProjectSettings.get("display/window/size/viewport_width"), ProjectSettings.get("display/window/size/viewport_height"))
|
|
|
|
# Tower variables
|
|
@export var Tower = preload("res://Tower.tscn"); # Tower scene
|
|
@export var tower_density = 250 # How often a tower should be added
|
|
var last_built = 300 # Helper variable to adhear the tower density
|
|
var towers = []
|
|
|
|
signal spawn # A tower is spawned in
|
|
|
|
# Build a single tower
|
|
func build_tower():
|
|
var tower = Tower.instantiate()
|
|
tower.connect("exit", Callable(self, "_on_Tower_exit"))
|
|
|
|
var pos = Vector2(last_built, (randf() * (screen_size.y * 0.7)) + screen_size.y * 0.15) # The player's movement vector.
|
|
|
|
tower.position = pos
|
|
towers.append(tower)
|
|
|
|
emit_signal("spawn", tower.get_global_transform().get_origin().x)
|
|
add_child(tower)
|
|
last_built += tower_density
|
|
|
|
# Build all towers
|
|
func build_all_towers():
|
|
get_tree().call_group("tower", "queue_free")
|
|
last_built = 300
|
|
while last_built < screen_size.x * 3:
|
|
build_tower()
|
|
|
|
func _ready():
|
|
# Connect signals
|
|
var ui = get_node("UI")
|
|
var player = get_node("Player")
|
|
player.connect("start", Callable(self, "build_all_towers"))
|
|
player.connect("start", Callable(ui, "_on_Player_start"))
|
|
player.connect("hit", Callable(ui, "_on_Player_hit"))
|
|
player.connect("passed", Callable(ui, "_on_Player_passed"))
|
|
connect("spawn", Callable(player, "_on_Tower_spawn"))
|
|
randomize()
|
|
|
|
func _on_Tower_exit():
|
|
build_tower()
|