I don’t wanna sound picky but we don’t really need a variable for the platforms count, we already instantiate them in Platforms_Parent node so we keep track of that with
$Platforms_Parent.get_child_count()
I went a little too far and handled infinite spawn, destroying old platforms, and I guess handling colliding platforms but that never happen :
func _process(_delta):
var platforms_count = platforms_parent.get_child_count()
if platforms_count > 4 and not is_level_generating:
var trigger_platform = platforms_parent.get_child(platforms_count - 5)
if player.global_position.y < trigger_platform.global_position.y:
generate_level(false)
func generate_level(generate_ground:bool):
is_level_generating = true
if generate_ground:
var loc_x = 0
var platform_size_x = 136
for i in range(4):
create_platform(Vector2(loc_x, viewport_size.y - 64))
loc_x += platform_size_x
var active_platforms = platforms_parent.get_child_count()
print("Generating level...")
# get the last active platform position.y as level current height or 0 if there is no
var level_height = viewport_size.y
if active_platforms > 0:
var last_platform_index = active_platforms - 1
level_height = platforms_parent.get_child(last_platform_index).global_position.y
# if active platforms number is greater than level_size we remove the old
if active_platforms > level_size:
var platforms_to_erase = platforms_parent.get_children().slice(0, active_platforms - 10)
print(str(platforms_to_erase.size()) + " platforms to erase...")
for platform in platforms_to_erase:
platform.queue_free()
print("Current platforms count : " + str(platforms_parent.get_child_count()))
# creating new platforms
print("Creating new platforms...")
var loc_x = 0
var platform_size_x = 136
var random = RandomNumberGenerator.new()
random.randomize()
var loc_y = -randi_range(min_distance_jumps, max_distance_jumps) + level_height
for i in range(level_size):
loc_x = randi() % int(viewport_size.x - platform_size_x)
var platform = create_platform(Vector2(loc_x, loc_y))
loc_y -= randi_range(min_distance_jumps, max_distance_jumps)
is_level_generating = false
print("Current platforms count : " + str(platforms_parent.get_child_count()))
I’m still eager to see you’re solution and improve