Visitor #2 - Sky generation

Tagged: visitor gamedev

Generating an infinite sky

Tonight's post is brought to you by Teen Daze's new album and some old Bob Ross episodes.

I've been putting off working on Visitor┬║ as I have been struggling with figuring out how to create an infinite background of stars that the player moves over. Tonight I managed to find a temporary solution that will work so that I can move on to more important things.

Here's how it works:

  • The sky is made of starfields

  • Each starfield is made of stars.

  • The player starts in a matrix of 3x3 starfields.

  • Each starfield has a collision shape of its entire size; when a player enters that shape, we emit a signal for the sky to re-arrange the starfields.

  • The sky class looks at the coords of the starfield the player entered, and determines how many of the existing starfields properly surround it and how many will need to be created,

  • Then we create a new starfield for the lacking matrix environs

  • Then we clear the old starfields.

Here's some messy code that handles the swapping of the sky:

func get_surrounding_coords(coords):
	var x = coords[0]
	var y = coords[1]
	return [
		[x - 1, y - 1], [x, y - 1], [x + 1, y - 1],
		[x - 1, y    ],	[x, y],     [x + 1, y    ],
		[x - 1, y + 1],	[x, y + 1],	[x + 1, y + 1]

# when entering a new starfield
# check and see if is a need to create new ones by comparing the new matrix
# to the old one; if so - make new fields, if not, don't
# FIXME: instead of creating and queue_free'ing starfields, we could
# just move the ones that are out of sight, into sight.
func _on_swap_starfields(coords):
	var existing_coords = []
	var starfields_out_of_view = []
	var newStarfieldMatrix = []
	# matrix of coordinates for the newly entered field.
	var surrounding_coords = get_surrounding_coords(coords)
	# loop through existing starfields
	for sf in self.starfields:
		# check if the current sf is around the new coord field we're in.
		if sf.coords in surrounding_coords:

	# create the new starfields we need (or ... move them?)
	for coord in surrounding_coords:
		if not coord in existing_coords:
			var new_sf = create_starfield(coord)
	self.starfields = newStarfieldMatrix
	for n in starfields_out_of_view:

I'm struggling here because I tried a more efficient implementation that reuses the previous out-of-sight starfield rather than creating a new one:

# this was hopefully going to work, but it's not.
# func _on_swap_starfields(coords):
var newStarfieldMatrix = []
var new_matrix = get_surrounding_coords(coords)
var in_view = []
var out_of_view = []
var to_be_made = []

for i in self.starfield_matrix.size():
    var sf = self.starfields[i]
    var sf_coord = sf.coords
    if sf_coord in new_matrix:
        var new_coord = new_matrix[i]
self.starfields = newStarfieldMatrix

But I'm getting stuck somewhere in here - I'm hoping I can return to it soon enough (or maybe not - I should move on!).

I have to keep reminding myself that I've allotted three months to work on this project as my main focus. Not a lot of time.

What's next?

My original mvp list wasn't set to be in a specific order. I think next I'll need to start trying to figure out the concept of consumption, which is a key component (although not as important as NPC conversation / interaction).

Much of Visitor is inspired by osmos, a game I love that I discovered in high school. However, rather than emitting / jettisoning your contents to propel you, you will only collect and grow, moving on your own set of invisible propulsion.

A quick guess at what I need to do...

  • Create a new node/class for "light"

  • Light needs to be able to spawn anywhere on the screen, of varying sizes.

  • Light needs to be able to shrink/be absorbed.

  • Similar to stars, light will be scattered throughout the sky, (but will appear different than stars)

  • it will need to have a collision shape as well to detect when the player overlaps the light.

  • If/When the player overlaps a frame of light, it will shrink in size until it disappears, at which point it will queue_free from the scene.

  • I'd also like the light to hover and move a bit in an idle state.

See ya' next time.