TheFlipSide/Scripts/background_particles.gd

69 lines
2.7 KiB
GDScript

extends Node2D
@onready var up_material : ParticleProcessMaterial = load("res://Materials/Particles/up_particles.tres") #The process material for when gravity is upwards.
@onready var down_material : ParticleProcessMaterial = load("res://Materials/Particles/down_particles.tres") #The process material for when gravity is downwards.
@export var number_of_particles : int #The total number of particles we want emitting.
var particle_child : GPUParticles2D #The child node that's holding the emitting particles.
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
#Duplicate the process materials so we have a local copy.
#Done so we don't modify the process material attached to the player particle emitter.
up_material = up_material.duplicate()
down_material = down_material.duplicate()
reloadParticles()
func _physics_process(delta: float) -> void:
#Run reloadParticles() when gravity is changed.
if Input.is_action_just_pressed("flip"):
print("input detected")
reloadParticles()
#Detect gravity, and set the appropriate process material to the GPUParticles2D child node.
#If that child node does not exist, then create it, and then assign the process material.
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"):
#Create a new GPUParticles2D object.
var bgparticle := GPUParticles2D.new()
#Set number of particles, make them emit by default, make them top level to not follow camera, and set name.
bgparticle.amount = number_of_particles
bgparticle.emitting = true
bgparticle.top_level = true
bgparticle.name = "BGParticles"
#Add our created GPUParticles2D object as a child to this Node.
add_child(bgparticle)
#Set our particle_child variable equal to our newly created child Node so we can modify it later.
particle_child = get_node_or_null("BGParticles")
#Set up a termpoary material variable, 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 to a box, with an assignable size.
material.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_BOX
#Set the bounds of the emission box equivalent to the current screen size.
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
func _on_player_player_respawned() -> void:
reloadParticles()