Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- var rng = RandomNumberGenerator.new()
- var starfields = []
- var starfield = preload("res://src/bg/starfield.tscn")
- var starfield_matrix
- export var parallax_scale = 0.2
- export var min_stars = 5
- export var max_stars = 30
- onready var sky_matrix_node = $'canvas/sky_matrix'
- # Called when the node enters the scene tree for the first time.
- func _ready():
- create_starfields()
- sky_matrix_node.motion_scale = Vector2(parallax_scale, parallax_scale)
- # NOTE: here we force a swap of the starfields as sometimes the game starts
- # with an empty sky :/. I'm not sure if it's helping yet.
- pass
- func create_starfields():
- print("1. generating starfields")
- # if you want to make one starfield == the size of the screen:
- # - change all the 2's to 1's
- # and in starfield.gd, divide max_w/max_h by / 2
- self.starfield_matrix = [
- [-1, -1], [0, -1], [1, -1],
- [-1, 0], [0, 0], [1, 0 ],
- [-1, 1], [0, 1], [1, 1 ],
- ]
- for coords in starfield_matrix:
- var sf = create_starfield(coords)
- starfields.append(sf)
- func create_starfield(coords, num_stars = rand_range(min_stars, max_stars)):
- var sf = starfield.instance()
- var bg_size = get_viewport().get_visible_rect().size
- # NOTE: if we remove colours from starfield_matrix remember to update this.
- sf.init([coords[0], coords[1]], bg_size, num_stars)
- sky_matrix_node.call_deferred("add_child", sf)
- sf.connect("swap_starfields", self, "_on_swap_starfields")
- return sf
- func get_next_matrix(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 next_matrix = get_next_matrix(coords)
- # loop through existing starfields and keep them or discard them.
- for sf in self.starfields:
- # we need to check new coords against existing coords, so let's fetch 'em here.'
- if sf:
- existing_coords.append(sf.coords)
- # check if the current sf is around the new coord field we're in.
- if sf.coords in next_matrix:
- newStarfieldMatrix.append(sf)
- else:
- starfields_out_of_view.append(sf)
- # create the new starfields we need (or ... move them?)
- for coord in next_matrix:
- if not coord in existing_coords:
- var new_sf = create_starfield(coord)
- newStarfieldMatrix.append(new_sf)
- self.starfields = newStarfieldMatrix
- for n in starfields_out_of_view:
- n.remove_npc()
- n.queue_free()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement