Skip to content

Commit

Permalink
hit flash effect for enemies using shader
Browse files Browse the repository at this point in the history
utilized shader for flash effect with a uniform variable `lerp_percent`, being controlled from component scene to lerp the value for animation
  • Loading branch information
KartikWatts committed Jan 17, 2024
1 parent 54c3143 commit 7dd79a1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 41 deletions.
22 changes: 22 additions & 0 deletions 2d-survivors-course/scenes/components/hit_flash_component.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends Node

@export var health_component: HealthComponent
@export var sprite: Sprite2D
@export var hit_flash_material : ShaderMaterial

var hit_flash_tween: Tween

func _ready():
health_component.health_changed.connect(on_health_changed)
sprite.material = hit_flash_material


func on_health_changed():
if hit_flash_tween !=null and hit_flash_tween.is_valid():
hit_flash_tween.kill()

(sprite.material as ShaderMaterial).set_shader_parameter("lerp_percent", 1.0)

hit_flash_tween = create_tween()
hit_flash_tween.tween_property(sprite.material, "shader_parameter/lerp_percent", 0.0, .25)\
.set_ease(Tween.EASE_IN).set_trans(Tween.TRANS_CUBIC)
19 changes: 19 additions & 0 deletions 2d-survivors-course/scenes/components/hit_flash_component.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
shader_type canvas_item;

//void vertex() {
//// Called for every vertex the material is visible on.
//}

uniform float lerp_percent: hint_range(0.0, 1.0, 0.1);

void fragment() {
// Called for every pixel the material is visible on.
vec4 texture_color = texture(TEXTURE, UV);
vec4 final_color = mix(texture_color, vec4(1.0, 1.0, 1.0, texture_color.a), lerp_percent);
COLOR = final_color;
}

//void light() {
// Called for every pixel for every light affecting the CanvasItem.
// Uncomment to replace the default light processing function with this one.
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://y7fu5m6xj717"]

[ext_resource type="Script" path="res://scenes/components/hit_flash_component.gd" id="1_p1022"]
[ext_resource type="Material" uid="uid://cp2qrgnhwj747" path="res://scenes/components/hit_flash_component_material.tres" id="2_0ccf8"]

[node name="HitFlashComponent" type="Node"]
script = ExtResource("1_p1022")
hit_flash_material = ExtResource("2_0ccf8")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://cp2qrgnhwj747"]

[ext_resource type="Shader" path="res://scenes/components/hit_flash_component.gdshader" id="1_wsdh0"]

[resource]
resource_local_to_scene = true
shader = ExtResource("1_wsdh0")
shader_parameter/lerp_percent = 0.0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=3 uid="uid://dlsoablcbbnbn"]
[gd_scene load_steps=16 format=3 uid="uid://dlsoablcbbnbn"]

[ext_resource type="Script" path="res://scenes/game_object/basic_enemy/basic_enemy.gd" id="1_btoq0"]
[ext_resource type="Texture2D" uid="uid://c4wr81m8ifp8g" path="res://scenes/game_object/basic_enemy/basic_enemy.png" id="1_tp3eb"]
Expand All @@ -7,6 +7,8 @@
[ext_resource type="PackedScene" uid="uid://cjvydmwd1w4f6" path="res://scenes/components/hurt_box_component.tscn" id="4_dclov"]
[ext_resource type="PackedScene" uid="uid://ccr2pyjml5e3f" path="res://scenes/components/death_component.tscn" id="4_ghoxd"]
[ext_resource type="PackedScene" uid="uid://bq1vvw48qq8jf" path="res://scenes/components/velocity_component.tscn" id="4_quu7f"]
[ext_resource type="Shader" path="res://scenes/components/hit_flash_component.gdshader" id="6_6ykum"]
[ext_resource type="PackedScene" uid="uid://y7fu5m6xj717" path="res://scenes/components/hit_flash_component.tscn" id="8_7vpqb"]

[sub_resource type="Animation" id="Animation_u4rlb"]
length = 0.001
Expand Down Expand Up @@ -70,6 +72,11 @@ _data = {
"walk": SubResource("Animation_b3l7f")
}

[sub_resource type="ShaderMaterial" id="ShaderMaterial_q55gy"]
resource_local_to_scene = true
shader = ExtResource("6_6ykum")
shader_parameter/lerp_percent = 0.0

[sub_resource type="CircleShape2D" id="CircleShape2D_jomo2"]
radius = 12.0

Expand All @@ -96,6 +103,11 @@ health_component = NodePath("../HealthComponent")

[node name="VelocityComponent" parent="." instance=ExtResource("4_quu7f")]

[node name="HitFlashComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("8_7vpqb")]
health_component = NodePath("../HealthComponent")
sprite = NodePath("../Visuals/Sprite2D")
hit_flash_material = SubResource("ShaderMaterial_q55gy")

[node name="DeathComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("4_ghoxd")]
health_component = NodePath("../HealthComponent")
sprite = NodePath("../Visuals/Sprite2D")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=3 uid="uid://kjyrfs3as7an"]
[gd_scene load_steps=16 format=3 uid="uid://kjyrfs3as7an"]

[ext_resource type="Texture2D" uid="uid://clwxylm8bs8xj" path="res://scenes/game_object/wizard_enemy/wizard_enemy.png" id="1_us2gk"]
[ext_resource type="Script" path="res://scenes/game_object/wizard_enemy/wizard_enemy.gd" id="1_yaq25"]
Expand All @@ -7,6 +7,47 @@
[ext_resource type="PackedScene" uid="uid://ccr2pyjml5e3f" path="res://scenes/components/death_component.tscn" id="2_xyxvg"]
[ext_resource type="PackedScene" uid="uid://buus3vhc8c05j" path="res://scenes/components/vial_drop_component.tscn" id="4_wjm1p"]
[ext_resource type="PackedScene" uid="uid://cjvydmwd1w4f6" path="res://scenes/components/hurt_box_component.tscn" id="4_wlk0n"]
[ext_resource type="PackedScene" uid="uid://y7fu5m6xj717" path="res://scenes/components/hit_flash_component.tscn" id="5_loq7b"]
[ext_resource type="Shader" path="res://scenes/components/hit_flash_component.gdshader" id="6_sy68i"]

[sub_resource type="Animation" id="Animation_qfuh7"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Visuals/Sprite2D:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Visuals/Sprite2D:rotation")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.0]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Visuals/Sprite2D:scale")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(1, 1)]
}

[sub_resource type="Animation" id="Animation_u4jge"]
resource_name = "walk"
Expand Down Expand Up @@ -67,51 +108,17 @@ tracks/3/keys = {
}]
}

[sub_resource type="Animation" id="Animation_qfuh7"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Visuals/Sprite2D:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(0, 0)]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Visuals/Sprite2D:rotation")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [0.0]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Visuals/Sprite2D:scale")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Vector2(1, 1)]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_p77v0"]
_data = {
"RESET": SubResource("Animation_qfuh7"),
"walk": SubResource("Animation_u4jge")
}

[sub_resource type="ShaderMaterial" id="ShaderMaterial_iram6"]
resource_local_to_scene = true
shader = ExtResource("6_sy68i")
shader_parameter/lerp_percent = 0.0

[sub_resource type="CircleShape2D" id="CircleShape2D_kgh57"]
radius = 8.0

Expand Down Expand Up @@ -139,6 +146,11 @@ acceleration = 20.0
[node name="VialDropComponent" parent="." node_paths=PackedStringArray("health_component") instance=ExtResource("4_wjm1p")]
health_component = NodePath("../HealthComponent")

[node name="HitFlashComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("5_loq7b")]
health_component = NodePath("../HealthComponent")
sprite = NodePath("../Visuals/Sprite2D")
hit_flash_material = SubResource("ShaderMaterial_iram6")

[node name="DeathComponent" parent="." node_paths=PackedStringArray("health_component", "sprite") instance=ExtResource("2_xyxvg")]
health_component = NodePath("../HealthComponent")
sprite = NodePath("../Visuals/Sprite2D")
Expand Down

0 comments on commit 7dd79a1

Please sign in to comment.