From 0ddfa9d86643cdd9808dacedfb656497ce7958d8 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 19 Aug 2020 16:57:52 +0200 Subject: [PATCH] oil_tanker: add new GooParticle emitter --- images/goo/BigGoo1.tres | 10 +++++- images/goo/BigGoo2.tres | 11 +++++++ images/goo/goo.shader | 17 ++++++---- images/goo/goo1.tres | 11 +++++++ images/goo/goo2.tres | 11 +++++++ images/goo/goo3.tres | 11 +++++++ images/goo/goo4.tres | 11 +++++++ images/goo/goo5.tres | 11 +++++++ images/goo/goo6.tres | 11 +++++++ images/goo/goo7.tres | 11 +++++++ images/goo/goo8.tres | 11 +++++++ project.godot | 6 ++++ scenes/oil_tanker.tscn | 38 ++++++++++++++++++++-- scripts/GooEmitter.gd | 70 +++++++++++++++++++++++++++++++++++++++++ scripts/GooParticle.gd | 21 +++++++++++++ 15 files changed, 252 insertions(+), 9 deletions(-) create mode 100644 images/goo/BigGoo2.tres create mode 100644 images/goo/goo1.tres create mode 100644 images/goo/goo2.tres create mode 100644 images/goo/goo3.tres create mode 100644 images/goo/goo4.tres create mode 100644 images/goo/goo5.tres create mode 100644 images/goo/goo6.tres create mode 100644 images/goo/goo7.tres create mode 100644 images/goo/goo8.tres create mode 100644 scripts/GooEmitter.gd create mode 100644 scripts/GooParticle.gd diff --git a/images/goo/BigGoo1.tres b/images/goo/BigGoo1.tres index 6642b70..b666f81 100644 --- a/images/goo/BigGoo1.tres +++ b/images/goo/BigGoo1.tres @@ -1,3 +1,11 @@ -[gd_resource type="AnimatedTexture" format=2] +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] [resource] +script = ExtResource( 1 ) +name = "BigGoo1" +columns = 10 +rows = 11 +skip_x_last_frames = 9 +framerate = 5 diff --git a/images/goo/BigGoo2.tres b/images/goo/BigGoo2.tres new file mode 100644 index 0000000..f7eda1b --- /dev/null +++ b/images/goo/BigGoo2.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "BigGoo2" +columns = 8 +rows = 1 +skip_x_last_frames = 0 +framerate = 5 diff --git a/images/goo/goo.shader b/images/goo/goo.shader index 945be01..a19daa8 100644 --- a/images/goo/goo.shader +++ b/images/goo/goo.shader @@ -3,17 +3,22 @@ render_mode blend_mix; uniform int particles_anim_h_frames; uniform int particles_anim_v_frames; uniform bool particles_anim_loop; +uniform int frame; void vertex() { float h_frames = float(particles_anim_h_frames); float v_frames = float(particles_anim_v_frames); VERTEX.xy /= vec2(h_frames, v_frames); float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames); - float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames)); - if (!particles_anim_loop) { - particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0); - } else { - particle_frame = mod(particle_frame, particle_total_frames); - } UV /= vec2(h_frames, v_frames); +// float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames)); + +// if (!particles_anim_loop) { +// particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0); +// } else { +// particle_frame = mod(particle_frame, particle_total_frames); +// } + float particle_frame = float(frame); + + UV /= vec2(h_frames, v_frames); UV += vec2(mod(particle_frame, h_frames) / h_frames, floor(particle_frame / h_frames) / v_frames); } diff --git a/images/goo/goo1.tres b/images/goo/goo1.tres new file mode 100644 index 0000000..8f2d9de --- /dev/null +++ b/images/goo/goo1.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo1" +columns = 4 +rows = 1 +skip_x_last_frames = 0 +framerate = 4 diff --git a/images/goo/goo2.tres b/images/goo/goo2.tres new file mode 100644 index 0000000..109f3e1 --- /dev/null +++ b/images/goo/goo2.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo2" +columns = 4 +rows = 1 +skip_x_last_frames = 0 +framerate = 4 diff --git a/images/goo/goo3.tres b/images/goo/goo3.tres new file mode 100644 index 0000000..e22e007 --- /dev/null +++ b/images/goo/goo3.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo3" +columns = 4 +rows = 1 +skip_x_last_frames = 0 +framerate = 4 diff --git a/images/goo/goo4.tres b/images/goo/goo4.tres new file mode 100644 index 0000000..1397338 --- /dev/null +++ b/images/goo/goo4.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo4" +columns = 1 +rows = 1 +skip_x_last_frames = 0 +framerate = 1 diff --git a/images/goo/goo5.tres b/images/goo/goo5.tres new file mode 100644 index 0000000..f3dc6bb --- /dev/null +++ b/images/goo/goo5.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo5" +columns = 1 +rows = 1 +skip_x_last_frames = 0 +framerate = 1 diff --git a/images/goo/goo6.tres b/images/goo/goo6.tres new file mode 100644 index 0000000..c39b6ba --- /dev/null +++ b/images/goo/goo6.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo6" +columns = 1 +rows = 1 +skip_x_last_frames = 0 +framerate = 1 diff --git a/images/goo/goo7.tres b/images/goo/goo7.tres new file mode 100644 index 0000000..77ad2bd --- /dev/null +++ b/images/goo/goo7.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo7" +columns = 1 +rows = 1 +skip_x_last_frames = 0 +framerate = 1 diff --git a/images/goo/goo8.tres b/images/goo/goo8.tres new file mode 100644 index 0000000..f38da69 --- /dev/null +++ b/images/goo/goo8.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" load_steps=2 format=2] + +[ext_resource path="res://scripts/GooParticle.gd" type="Script" id=1] + +[resource] +script = ExtResource( 1 ) +name = "goo8" +columns = 1 +rows = 1 +skip_x_last_frames = 0 +framerate = 1 diff --git a/project.godot b/project.godot index 82983d9..7e3b126 100644 --- a/project.godot +++ b/project.godot @@ -24,6 +24,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://scripts/game1/Flatfish.gd" }, { +"base": "Resource", +"class": "GooParticle", +"language": "GDScript", +"path": "res://scripts/GooParticle.gd" +}, { "base": "Node", "class": "IntroScript", "language": "GDScript", @@ -38,6 +43,7 @@ _global_script_class_icons={ "DialogueAction": "", "DialogueBox": "", "Flatfish": "", +"GooParticle": "", "IntroScript": "", "LogEntity": "" } diff --git a/scenes/oil_tanker.tscn b/scenes/oil_tanker.tscn index ea24d9b..79b6fe6 100644 --- a/scenes/oil_tanker.tscn +++ b/scenes/oil_tanker.tscn @@ -1,10 +1,24 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=28 format=2] [ext_resource path="res://scripts/oil_tanker.gd" type="Script" id=1] [ext_resource path="res://images/oil_tanker/OilTanker.tres" type="SpriteFrames" id=2] [ext_resource path="res://images/oil_tanker/oilTankerInside_Background.png" type="Texture" id=3] [ext_resource path="res://scenes/SidePlayer.tscn" type="PackedScene" id=4] [ext_resource path="res://images/oil_tanker/oilTankerInside_Foreground.png" type="Texture" id=5] +[ext_resource path="res://scripts/GooEmitter.gd" type="Script" id=6] +[ext_resource path="res://images/goo/goo.shader" type="Shader" id=7] +[ext_resource path="res://images/goo/BigGoo1.tres" type="Resource" id=8] +[ext_resource path="res://images/goo/BigGoo1.png" type="Texture" id=9] +[ext_resource path="res://images/goo/BigGoo1_Outlines.png" type="Texture" id=10] +[ext_resource path="res://images/goo/BigGoo2.tres" type="Resource" id=11] +[ext_resource path="res://images/goo/goo5.tres" type="Resource" id=12] +[ext_resource path="res://images/goo/goo8.tres" type="Resource" id=13] +[ext_resource path="res://images/goo/goo4.tres" type="Resource" id=14] +[ext_resource path="res://images/goo/goo2.tres" type="Resource" id=15] +[ext_resource path="res://images/goo/goo7.tres" type="Resource" id=16] +[ext_resource path="res://images/goo/goo6.tres" type="Resource" id=17] +[ext_resource path="res://images/goo/goo1.tres" type="Resource" id=18] +[ext_resource path="res://images/goo/goo3.tres" type="Resource" id=19] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 10, 27.9493 ) @@ -27,6 +41,13 @@ b = Vector2( -129.208, -5.53812 ) [sub_resource type="SegmentShape2D" id=7] b = Vector2( -87.4343, 42.6051 ) +[sub_resource type="ShaderMaterial" id=10] +shader = ExtResource( 7 ) +shader_param/particles_anim_h_frames = 10 +shader_param/particles_anim_v_frames = 11 +shader_param/particles_anim_loop = true +shader_param/frame = null + [node name="Node2D" type="Node2D"] script = ExtResource( 1 ) @@ -37,6 +58,13 @@ centered = false [node name="Player" parent="InsideOilTanker" instance=ExtResource( 4 )] position = Vector2( 192, 64.4552 ) +[node name="GooEmitter" type="Node2D" parent="InsideOilTanker"] +position = Vector2( -43.5548, 47.7155 ) +script = ExtResource( 6 ) +goo_particles = [ ExtResource( 8 ), ExtResource( 11 ), ExtResource( 18 ), ExtResource( 15 ), ExtResource( 19 ), ExtResource( 14 ), ExtResource( 12 ), ExtResource( 17 ), ExtResource( 16 ), ExtResource( 13 ) ] +direction = Vector2( 1, 0 ) +spawn_rate = 2 + [node name="Foreground" type="Sprite" parent="InsideOilTanker"] texture = ExtResource( 5 ) centered = false @@ -86,6 +114,12 @@ position = Vector2( 204.978, 72.7017 ) shape = SubResource( 7 ) [node name="OilTanker" type="AnimatedSprite" parent="."] -visible = false frames = ExtResource( 2 ) centered = false + +[node name="GooExample" type="Sprite" parent="."] +visible = false +material = SubResource( 10 ) +texture = ExtResource( 9 ) +normal_map = ExtResource( 10 ) +centered = false diff --git a/scripts/GooEmitter.gd b/scripts/GooEmitter.gd new file mode 100644 index 0000000..1bb8e2c --- /dev/null +++ b/scripts/GooEmitter.gd @@ -0,0 +1,70 @@ +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 + print(particle.name) + + 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 diff --git a/scripts/GooParticle.gd b/scripts/GooParticle.gd new file mode 100644 index 0000000..117664e --- /dev/null +++ b/scripts/GooParticle.gd @@ -0,0 +1,21 @@ +extends Resource + +class_name GooParticle + +export var name : String +export var columns : int = 1 +export var rows : int = 1 +export var skip_x_last_frames : int = 0 +export var framerate = 5 + +var texture : Texture +var normal_map : Texture +var frame_count : int + +var loaded = false +func load_textures(): + if !loaded: + loaded = true + texture = load('res://images/goo/' + name + '.png') + normal_map = load('res://images/goo/' + name + '_Outlines.png') + frame_count = columns * rows - skip_x_last_frames