TheFlipSide/Scripts/background_particles.gd

48 lines
1.7 KiB
GDScript

extends Node2D
@export var number_of_particles : int
var particle_child : GPUParticles2D
var up_material : ParticleProcessMaterial = load("res://Materials/Particles/up_particles_bg.tres")
var down_material : ParticleProcessMaterial = load("res://Materials/Particles/down_particles_bg.tres")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
up_material = up_material.duplicate()
down_material = down_material.duplicate()
reloadParticles()
func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("flip"):
print("input detected")
reloadParticles()
func reloadParticles() -> void:
#If the particle node does not already exist, do the initial setup, and add it as a child node.
if !has_node("BGParticles"):
var bgparticle := GPUParticles2D.new()
bgparticle.amount = number_of_particles
bgparticle.emitting = true
bgparticle.top_level = true
bgparticle.name = "BGParticles"
add_child(bgparticle)
particle_child = get_node_or_null("BGParticles")
#Set up the material we want to set the particle node to, and set up a screen size variable.
var material : ParticleProcessMaterial
var size : Vector2 = get_viewport_rect().size
#Simple gravity direction detection.
if al_globals.y_gravity > 0:
material = up_material
elif al_globals.y_gravity < 0:
material = down_material
#Set the emission shape and the size, relative to size of screen.
material.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_BOX
material.emission_box_extents = Vector3(size.x, size.y, 0)
#Set the material of the particle child object to the created material.
particle_child.process_material = material