Good morning, afternoon or evening. I’m working on an Asteroids clone for the sake of practice and learning, trying to actually start the “just make games” phase.
Recently finished all the player systems, with the most recent being collision damage and respawning, then moved on to destroying asteroids. While destroying them is easy, I can’t figure out the whole logic and algorithm behind an asteroid instantiating another two on being destroyed.
I’ve been using composition (or, at least, a bare-bones, 100% inefficient version of it), I suppose, and it has been working so far, but now it feels like my brain just went jelly for the whole week and even though it sounds simple in practice, I just can’t get my head to work and solve this at all. There are three asteroid scenes, “asteroid_big”, “asteroid_medium” and “asteroid_small”. They are all CharacterBody2D and share the same script that makes it move forward.
extends CharacterBody2D # asteroid.gd
@export var SPEED : float = 200;
func _process(_delta: float) -> void:
velocity += transform.x * SPEED;
if velocity.length() >= SPEED:
velocity = velocity.limit_length(SPEED);
move_and_slide();
An Area2D called “component_damage” detects collisions, handles health and emits a signal for either collision or health depletion.
extends Area2D #component_damage.gd
@export var parent = Node2D;
@export var health : int;
signal has_collided
signal health_depleted
func contact_damage(_area: Area2D) -> void:
#print(str(parent.name) + " has collided with " + str(_area.name));
has_collided.emit();
deplete_health();
func deplete_health():
health -= 1;
#print(str(parent.name) + "'s HEALTH has been reduced to " + str(get_health_value()));
health_depleted.emit();
if health <= 0:
health = 0;
#print(str(parent.name) + "'s HEALTH is zero and cannot be depleted any further.");
func get_health_value():
return health;
I’ve had a handful of failed approaches that, at this point, makes me believe I might have to completely start over with the implementation of asteroids in general, and just the thought of having to do that deeply demotivates me and makes it harder and harder to even feel like giving another shot.
The closest I got to achieving even the most basic functionality of this feature was by loosely following a tutorial, which led to me trying a component_asteroid another time, setting an enum that stores different asteroid sizes and using a match statement to set the “next_asteroid” according to the asteroid’s size.
Since this was a component, since I knew I couldn’t reference/preload the path of the asteroid’s scene within its own script, I’d have to add a health variable to the asteroid script and set it to the same value as the health variable under component_damage every frame. With that, I managed to detect damage and instantiate the proper “next_asteroid”, but I had no idea how to proceed with either freeing the asteroid from the queue while maintaining the instantiated asteroids in the scene. With how different my implementation is compared to the tutorial, just following it would make my project even more confusing.
I apologize for this wall of text of a post, but I’m at a total loss here. This is the first time I actually managed to get anything beyond a somewhat functional character movement and I really want to finish this project before attempting a game of my own.
Here’s the remote repository of the project, in case the information I provided within the post isn’t enough. Please point me toward the right direction! I would prefer not to be given code, but receive detailed suggestions to how I could approach this feature. Once again, sorry for the wall of text and thank you in advance!