Skip to content

Commit 4d29586

Browse files
committed
Add hit flash
1 parent 726671d commit 4d29586

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

game/ship_skeleton/cell/cell.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var _related_movemenet_collision_shape: CollisionShape2D
1515
@onready var hurtbox_2d: ComponentHurtbox2D = %Hurtbox2D
1616
@onready var health_component: ComponentHealth = %Health
1717
@onready var sprite_2d: Sprite2D = %Sprite2D
18+
@onready var animation_player: AnimationPlayer = %AnimationPlayer
1819

1920

2021
func _ready() -> void:
@@ -65,4 +66,5 @@ func _on_health_depleted() -> void:
6566
child_cell.health_component.current_health = 0
6667

6768
func _on_hit(hitbox: ComponentHitbox2D) -> void:
69+
animation_player.play(&"hit")
6870
health_component.take_damage(hitbox.damage)

game/ship_skeleton/cell/cell.gdshader

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
shader_type canvas_item;
22

33
uniform float hp_ratio = 1.0;
4+
uniform bool is_hit_flash_active = false;
45

56
void vertex() {
67
// Called for every vertex the material is visible on.
@@ -10,17 +11,19 @@ void fragment() {
1011
// Called for every pixel the material is visible on.
1112
vec4 tex_color = texture(TEXTURE, UV);
1213

13-
float dist = distance(UV, vec2(0.5));
14+
if (is_hit_flash_active) {
15+
tex_color = vec4(1,1,1, tex_color.a);
16+
} else {
17+
float dist = distance(UV, vec2(0.5));
1418

15-
if (tex_color.a > 0.0) {
16-
if (dist < 0.5 * hp_ratio) {
17-
tex_color.a = 1.0;
18-
} else {
19-
tex_color.a = 0.3;
19+
if (tex_color.a > 0.0) {
20+
if (dist < 0.5 * hp_ratio) {
21+
tex_color.a = 1.0;
22+
} else {
23+
tex_color.a = 0.3;
24+
}
2025
}
2126
}
22-
//tex_color.a = mix(tex_color.a, 1.0, boost_factor);
23-
2427
COLOR = tex_color;
2528
}
2629

game/ship_skeleton/cell/cell.tscn

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,48 @@
1010
resource_local_to_scene = true
1111
shader = ExtResource("2_mnm1f")
1212
shader_parameter/hp_ratio = 1.0
13+
shader_parameter/is_hit_flash_active = false
1314

1415
[sub_resource type="CircleShape2D" id="CircleShape2D_ruldg"]
1516
radius = 25.0
1617

18+
[sub_resource type="Animation" id="Animation_mnm1f"]
19+
length = 0.001
20+
tracks/0/type = "value"
21+
tracks/0/imported = false
22+
tracks/0/enabled = true
23+
tracks/0/path = NodePath("Sprite2D:material:shader_parameter/is_hit_flash_active")
24+
tracks/0/interp = 1
25+
tracks/0/loop_wrap = true
26+
tracks/0/keys = {
27+
"times": PackedFloat32Array(0),
28+
"transitions": PackedFloat32Array(1),
29+
"update": 1,
30+
"values": [false]
31+
}
32+
33+
[sub_resource type="Animation" id="Animation_0jsgn"]
34+
resource_name = "hit"
35+
length = 0.1
36+
tracks/0/type = "value"
37+
tracks/0/imported = false
38+
tracks/0/enabled = true
39+
tracks/0/path = NodePath("Sprite2D:material:shader_parameter/is_hit_flash_active")
40+
tracks/0/interp = 1
41+
tracks/0/loop_wrap = true
42+
tracks/0/keys = {
43+
"times": PackedFloat32Array(0, 0.10000001),
44+
"transitions": PackedFloat32Array(1, 1),
45+
"update": 1,
46+
"values": [true, false]
47+
}
48+
49+
[sub_resource type="AnimationLibrary" id="AnimationLibrary_0xnlj"]
50+
_data = {
51+
&"RESET": SubResource("Animation_mnm1f"),
52+
&"hit": SubResource("Animation_0jsgn")
53+
}
54+
1755
[node name="Cell" type="Node2D" unique_id=1435320636]
1856
script = ExtResource("1_cjhyy")
1957
repair_rate = 1.0
@@ -36,4 +74,8 @@ shape = SubResource("CircleShape2D_ruldg")
3674
unique_name_in_owner = true
3775
max_health = 15.0
3876

77+
[node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=1733586492]
78+
unique_name_in_owner = true
79+
libraries/ = SubResource("AnimationLibrary_0xnlj")
80+
3981
[editable path="Hurtbox2D"]

0 commit comments

Comments
 (0)