feat: 躲避敌人攻击功能

fix: 敌人初始化血条
This commit is contained in:
luke358
2026-05-18 16:10:19 +08:00
parent b6bf8ef5ba
commit 66452f1072
7 changed files with 45 additions and 10 deletions
+10 -8
View File
@@ -1,13 +1,15 @@
extends Node extends Node
const FLASH_MATERIAL = preload("uid://cwtrdmkrsmw23")
var player: Player var player: Player
# Called when the node enters the scene tree for the first time. # 尝试阻止敌人攻击,获取闪避, unit_stats block_chance 越大
func _ready() -> void: # 成功率越高
pass # Replace with function body. func get_chance_success(chance: float) -> bool:
var random := randf_range(0, 1.0)
# Called every frame. 'delta' is the elapsed time since the previous frame. if random < chance:
func _process(delta: float) -> void: return true
pass
return false
+6
View File
@@ -0,0 +1,6 @@
shader_type canvas_item;
void fragment() {
COLOR = vec4(1.0,1.0,1.0,texture(TEXTURE, UV).a);
}
+1
View File
@@ -0,0 +1 @@
uid://dmnmstd7ptupl
+6
View File
@@ -0,0 +1,6 @@
[gd_resource type="ShaderMaterial" format=3 uid="uid://cwtrdmkrsmw23"]
[ext_resource type="Shader" uid="uid://dmnmstd7ptupl" path="res://effect/flash.gdshader" id="1_dhio8"]
[resource]
shader = ExtResource("1_dhio8")
+1 -1
View File
@@ -10,7 +10,7 @@ var can_move := true
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
pass # Replace with function body. super._ready()
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
+16 -1
View File
@@ -7,15 +7,30 @@ class_name Unit
@onready var sprite: Sprite2D = %Sprite @onready var sprite: Sprite2D = %Sprite
@onready var anim_player: AnimationPlayer = $AnimationPlayer @onready var anim_player: AnimationPlayer = $AnimationPlayer
@onready var health_component: HealthComponent = $HealthComponent @onready var health_component: HealthComponent = $HealthComponent
@onready var flash_timer: Timer = $FlashTimer
func _ready() -> void: func _ready() -> void:
print(stats)
health_component.setup(stats) health_component.setup(stats)
func set_flash_material() -> void:
sprite.material = Global.FLASH_MATERIAL
flash_timer.start()
func _on_hurtbox_component_on_damaged(hitbox: HitboxComponent) -> void: func _on_hurtbox_component_on_damaged(hitbox: HitboxComponent) -> void:
if health_component.current_health <=0: if health_component.current_health <=0:
return return
var blocked := Global.get_chance_success(stats.block_chance / 100)
if blocked:
print("Blocked!")
return
set_flash_material()
health_component.take_damage(hitbox.damage) health_component.take_damage(hitbox.damage)
print("%s: %d" % [name, health_component.current_health] ) print("%s: %d" % [name, health_component.current_health] )
func _on_flash_timer_timeout() -> void:
sprite.material = null
+5
View File
@@ -258,5 +258,10 @@ offset_top = -90.0
offset_right = 50.0 offset_right = 50.0
offset_bottom = -70.0 offset_bottom = -70.0
[node name="FlashTimer" type="Timer" parent="." unique_id=2034069027]
wait_time = 0.2
one_shot = true
[connection signal="on_damaged" from="HurtboxComponent" to="." method="_on_hurtbox_component_on_damaged"] [connection signal="on_damaged" from="HurtboxComponent" to="." method="_on_hurtbox_component_on_damaged"]
[connection signal="on_health_changed" from="HealthComponent" to="HealthBar" method="_on_health_component_on_health_changed"] [connection signal="on_health_changed" from="HealthComponent" to="HealthBar" method="_on_health_component_on_health_changed"]
[connection signal="timeout" from="FlashTimer" to="." method="_on_flash_timer_timeout"]