Skip to content

Commit

Permalink
add shader to show that enemy got hit, also parameterize damage on enemy
Browse files Browse the repository at this point in the history
  • Loading branch information
mathew-tran committed Nov 30, 2023
1 parent a70b9f7 commit facb932
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
14 changes: 13 additions & 1 deletion entities/enemy.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
[gd_scene load_steps=4 format=3 uid="uid://32uqp2kt3dy4"]
[gd_scene load_steps=6 format=3 uid="uid://32uqp2kt3dy4"]

[ext_resource type="Script" path="res://scripts/enemy.gd" id="1_yqwu2"]
[ext_resource type="Texture2D" uid="uid://bivyscdevv7ye" path="res://art/enemy/enemy-01.png" id="2_jkjfl"]
[ext_resource type="Shader" path="res://shaders/hitflash.gdshader" id="2_qcvix"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_85vqh"]
resource_local_to_scene = true
shader = ExtResource("2_qcvix")
shader_parameter/flash_color = Color(1, 1, 1, 1)
shader_parameter/progress = 0.0

[sub_resource type="RectangleShape2D" id="RectangleShape2D_kt0x3"]

Expand All @@ -12,6 +19,7 @@ script = ExtResource("1_yqwu2")

[node name="Sprite2D" type="Sprite2D" parent="."]
texture_filter = 1
material = SubResource("ShaderMaterial_85vqh")
scale = Vector2(1.75, 1.75)
texture = ExtResource("2_jkjfl")

Expand All @@ -21,4 +29,8 @@ texture = ExtResource("2_jkjfl")
scale = Vector2(2.9641, 2.94208)
shape = SubResource("RectangleShape2D_kt0x3")

[node name="HitTimer" type="Timer" parent="."]
wait_time = 0.2
one_shot = true

[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"]
15 changes: 14 additions & 1 deletion scripts/enemy.gd
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
extends PathFollow2D

@export var speed: float # gets set in main.gd, so we dont really need to initialize any value here
@onready var sprite_reference = get_node("Sprite2D")
@onready var hit_timer = get_node("HitTimer")

var health = 2
var damage = 5

func _ready():
progress = 0

func _process(delta):
sprite_reference.material.set_shader_parameter("progress", get_hit_timer_progress())

func get_hit_timer_progress():
return hit_timer.time_left / hit_timer.wait_time

func _physics_process(delta):
if progress_ratio < 1.0:
progress += speed * delta
else:
eventmanager.broadcast_player_take_damage(1)
eventmanager.broadcast_player_take_damage(get_damage())
queue_free()

func get_damage():
return damage

func _on_area_2d_area_entered(area):
if area.is_in_group("projectile"):
hit_timer.start()
health -= area.get_damage()
area.queue_free() # destroy the projectile
if health == 0:
Expand Down
10 changes: 10 additions & 0 deletions shaders/hitflash.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
shader_type canvas_item;

uniform vec4 flash_color : source_color;
uniform float progress;

void fragment() {
vec4 color = texture(TEXTURE, UV);
color.rgb = mix(color, flash_color, progress).rgb;
COLOR = color;
}

0 comments on commit facb932

Please sign in to comment.