Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node2D
- signal swap_starfields(matrix_pos)
- var rng = RandomNumberGenerator.new()
- var star = preload("res://src/bg/star.tscn")
- var light = preload("res://src/light/light.tscn")
- var npc = preload("res://src/npc/NPC.tscn")
- onready var screen_size = get_viewport_rect().size
- onready var Game = $"/root/Game" # NOTE: IS THIS BAD?
- var current_npc
- var starfield_npc = null
- var num_stars = 100
- var width
- var height
- var coords = [] # the location in the night sky
- onready var max_w = screen_size.x / 2
- onready var max_h = screen_size.y / 2
- onready var is_foreground = get_scene_parent() == "sky_fg"
- # Called when the node enters the scene tree for the first time.
- func _ready():
- # rng.randomize() # this not working?
- create_stars()
- if coords != [0, 0] and is_foreground:
- spawn_npc()
- create_light()
- set_initial_pos()
- # we need to determine if the starfield is in the background or
- # fg, (because we might have several sky matrixes- and not all of them
- # have to spawn stuff.)
- func get_scene_parent():
- var scene_name = get_parent().get_parent().get_parent().name
- assert(scene_name == "sky_fg" || scene_name == "sky_bg", "The starfield parent scene has been renamed from sky_fg or sky_bg")
- return scene_name
- func rand_col():
- rng.randomize()
- var r = rng.randf_range(0.5, 1.0)
- var g = rng.randf_range(0.5, 1.0)
- var b = rng.randf_range(0.5, 1.0)
- return Color(r, g, b)
- func create_light():
- var percent = randf()
- if percent > 0.20:
- var l = light.instance()
- l.init(Util.rand_pos(0, max_w, 0, max_h))
- add_child(l)
- func spawn_npc():
- var percent = randf()
- if percent > 0.5:
- current_npc = Game.npc_get_next()
- if current_npc:
- var sf_pos = self.position
- var npc_pos = Util.rand_pos(
- sf_pos.x, sf_pos.x + max_w,
- sf_pos.y, sf_pos.y + max_h)
- npc_pos.x += max_w
- current_npc.set_position(npc_pos)
- func remove_npc():
- if current_npc:
- Game.npc_remove_from_tree(current_npc)
- func create_stars():
- # for some reason this has to be divided by two?
- for _i in range(num_stars):
- var s = star.instance()
- s.init(Util.rand_pos(0, max_w, 0, max_h))
- add_child(s)
- func set_initial_pos():
- position.x -= screen_size.x / 2
- position.y -= screen_size.y / 2
- func set_pos_by_coords():
- var x_coord = coords[0]
- var y_coord = coords[1]
- self.position.x = width * x_coord
- self.position.y = height * y_coord
- func init(_coords, bg_size, _num_stars: int):
- self.num_stars = _num_stars
- self.coords = _coords
- self.width = bg_size.x
- self.height = bg_size.y
- set_pos_by_coords()
- func _on_starfieldArea2D_area_entered(area: Area2D):
- if area.get_name() == "PlayerCenterArea":
- # only swap the starfields once game has started / player is moving
- if area.get_parent().has_started_moving:
- emit_signal("swap_starfields", self.coords)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement