crabs-game/scripts/GooEmitter.gd

70 lines
2.0 KiB
GDScript3
Raw Permalink Normal View History

extends Node2D
export(Array, Resource) var goo_particles = []
export(Vector2) var direction
export(int) var spawn_radius = 32
export(int) var spawn_rate = 1
export(int) var despawn_radius = 250
export(float) var movement_speed = 10
var goo_shader = preload('res://images/goo/goo.shader')
var delta_sum = 0
var time = 0
func _ready():
for particle in goo_particles:
particle.load_textures()
func emit():
var particle = goo_particles[rand_range(0, goo_particles.size())] as GooParticle
var instance = Particle.new()
instance.particle = particle
instance.material = ShaderMaterial.new()
instance.material.shader = goo_shader
instance.material.set_shader_param('particles_anim_h_frames', particle.columns)
instance.material.set_shader_param('particles_anim_v_frames', particle.rows)
instance.material.set_shader_param('particles_anim_loop', true)
instance.material.set_shader_param('frame', 0)
instance.texture = particle.texture
instance.normal_map = particle.normal_map
instance.shader_frame = 0
instance.position = Vector2(rand_range(0, spawn_radius), rand_range(0, spawn_radius))
instance.initial_y = instance.position.y
instance.initial_time = time
add_child(instance)
func _process(delta):
delta_sum += delta
time += delta
for child in get_children():
child.delta_sum += delta
while child.delta_sum >= 1.0 / child.particle.framerate:
child.delta_sum -= 1.0 / child.particle.framerate
child.shader_frame += 1
if child.shader_frame >= child.particle.frame_count:
child.shader_frame = 0
child.material.set_shader_param('frame', child.shader_frame)
child.position += direction * delta * movement_speed
child.position.y = child.initial_y + sin(time - child.initial_time) * 3
while delta_sum >= spawn_rate:
delta_sum -= spawn_rate
emit()
for child in get_children():
if child.position.length() > despawn_radius:
child.queue_free()
class Particle:
extends Sprite
var shader_frame = 0
var particle : GooParticle
var delta_sum : float = 0
var initial_y : int
var initial_time : float