starcheese/main.gd

81 lines
2.3 KiB
GDScript3
Raw Normal View History

2024-10-03 18:12:33 +01:00
extends Node
func _init() -> void:
var node = WeaponsSystem.new()
node.set_name("WeaponsSystem")
add_child(node)
node = DefaultEventBus.new()
node.set_name("MainEventBus")
add_child(node)
node = EventScope.new()
node.set_name("InputEventScope")
add_child(node)
node = EventScope.new()
node.set_name("DamageEventScope")
add_child(node)
node = EventScope.new()
node.set_name("DestroyedEventScope")
add_child(node)
# Instantiate and add the RewardSpawner
node = RewardSpawner.new()
node.set_name("RewardSpawner")
add_child(node)
2024-10-03 18:12:33 +01:00
# 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 actions = [
"ui_fire",
"ui_up",
"ui_down",
"ui_left",
"ui_right",
]
for action in actions:
if Input.is_action_pressed(action):
get_node("/root/Main/MainEventBus").publish(get_node("/root/Main/InputEventScope"), KeyboardInputEvent.new(action, true))
elif Input.is_action_just_released(action):
get_node("/root/Main/MainEventBus").publish(get_node("/root/Main/InputEventScope"), KeyboardInputEvent.new(action, false))
pass
# Spawn function and random position logic are placed here
func spawn_asteroid():
var asteroid_scene = preload("res://asteroid.tscn")
var asteroid_instance = asteroid_scene.instantiate()
# Generate random positions within a defined area
var spawn_position = get_random_position()
# Check that it's not too close to the player or other asteroids
while !is_valid_position(spawn_position):
spawn_position = get_random_position()
asteroid_instance.global_position = spawn_position
add_child(asteroid_instance)
func get_random_position() -> Vector2:
var screen_size = get_viewport().size
return Vector2(randf_range(0, screen_size.x), randf_range(0, screen_size.y))
func is_valid_position(spawn_position: Vector2) -> bool:
var player = get_node("spaceship") # Adjust the path to your player node
var player_distance = player.global_position.distance_to(spawn_position)
if player_distance < 200:
return false
var asteroids = get_tree().get_nodes_in_group("asteroids")
for asteroid in asteroids:
if asteroid.global_position.distance_to(spawn_position) < 150:
return false
return true