Advertisement
Guest User

Untitled

a guest
Mar 30th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node2D
  2. var rng = RandomNumberGenerator.new()
  3. var starfields = []
  4. var starfield = preload("res://src/bg/starfield.tscn")
  5. var starfield_matrix
  6. export var parallax_scale = 0.2
  7. export var min_stars = 5
  8. export var max_stars = 30
  9. onready var sky_matrix_node = $'canvas/sky_matrix'
  10.  
  11.  
  12. # Called when the node enters the scene tree for the first time.
  13. func _ready():
  14.     create_starfields()
  15.     sky_matrix_node.motion_scale = Vector2(parallax_scale, parallax_scale)
  16.     # NOTE: here we force a swap of the starfields as sometimes the game starts
  17.     # with an empty sky :/. I'm not sure if it's helping yet.
  18.     pass
  19.  
  20. func create_starfields():
  21.     print("1. generating starfields")
  22.     # if you want to make one starfield == the size of the screen:
  23.     # - change all the 2's to 1's
  24.     # and in starfield.gd, divide max_w/max_h by / 2
  25.     self.starfield_matrix = [
  26.         [-1, -1], [0, -1], [1, -1],
  27.         [-1, 0],  [0, 0],  [1, 0 ],
  28.         [-1, 1],  [0, 1],  [1, 1 ],
  29.         ]
  30.  
  31.     for coords in starfield_matrix:
  32.         var sf = create_starfield(coords)
  33.         starfields.append(sf)
  34.  
  35. func create_starfield(coords, num_stars = rand_range(min_stars, max_stars)):
  36.     var sf = starfield.instance()
  37.     var bg_size = get_viewport().get_visible_rect().size
  38.     # NOTE: if we remove colours from starfield_matrix remember to update this.
  39.     sf.init([coords[0], coords[1]], bg_size, num_stars)
  40.     sky_matrix_node.call_deferred("add_child", sf)
  41.     sf.connect("swap_starfields", self, "_on_swap_starfields")
  42.     return sf
  43.  
  44. func get_next_matrix(coords):
  45.     var x = coords[0]
  46.     var y = coords[1]
  47.     return [
  48.         [x - 1, y - 1], [x, y - 1], [x + 1, y - 1],
  49.         [x - 1, y    ], [x, y],     [x + 1, y    ],
  50.         [x - 1, y + 1], [x, y + 1], [x + 1, y + 1]
  51.         ]
  52.  
  53. # when entering a new starfield
  54. # check and see if is a need to create new ones by comparing the new matrix
  55. # to the old one; if so - make new fields, if not, don't
  56. #
  57. # FIXME: instead of creating and queue_free'ing starfields, we could
  58. # just move the ones that are out of sight, into sight.
  59. #
  60. func _on_swap_starfields(coords):
  61.     var existing_coords = []
  62.     var starfields_out_of_view = []
  63.     var newStarfieldMatrix = []
  64.     # matrix of coordinates for the newly entered field.
  65.     var next_matrix = get_next_matrix(coords)
  66.     # loop through existing starfields and keep them or discard them.
  67.     for sf in self.starfields:
  68.         # we need to check new coords against existing coords, so let's fetch 'em here.'
  69.         if sf:
  70.             existing_coords.append(sf.coords)
  71.             # check if the current sf is around the new coord field we're in.
  72.             if sf.coords in next_matrix:
  73.                 newStarfieldMatrix.append(sf)
  74.             else:
  75.                 starfields_out_of_view.append(sf)
  76.  
  77.     # create the new starfields we need (or ... move them?)
  78.     for coord in next_matrix:
  79.         if not coord in existing_coords:
  80.             var new_sf = create_starfield(coord)
  81.             newStarfieldMatrix.append(new_sf)
  82.     self.starfields = newStarfieldMatrix
  83.     for n in starfields_out_of_view:
  84.         n.remove_npc()
  85.         n.queue_free()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement