From 66452f1072738a2bfaee66b8550be5ac37d9f598 Mon Sep 17 00:00:00 2001 From: luke358 Date: Mon, 18 May 2026 16:10:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BA=B2=E9=81=BF=E6=95=8C=E4=BA=BA?= =?UTF-8?q?=E6=94=BB=E5=87=BB=E5=8A=9F=E8=83=BD=20fix:=20=E6=95=8C?= =?UTF-8?q?=E4=BA=BA=E5=88=9D=E5=A7=8B=E5=8C=96=E8=A1=80=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autoloads/global.gd | 18 ++++++++++-------- effect/flash.gdshader | 6 ++++++ effect/flash.gdshader.uid | 1 + effect/flash_material.tres | 6 ++++++ scenes/unit/enemy/enemy.gd | 2 +- scenes/unit/unit.gd | 17 ++++++++++++++++- scenes/unit/unit.tscn | 5 +++++ 7 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 effect/flash.gdshader create mode 100644 effect/flash.gdshader.uid create mode 100644 effect/flash_material.tres diff --git a/autoloads/global.gd b/autoloads/global.gd index 533ec33..c167777 100644 --- a/autoloads/global.gd +++ b/autoloads/global.gd @@ -1,13 +1,15 @@ extends Node +const FLASH_MATERIAL = preload("uid://cwtrdmkrsmw23") var player: Player -# Called when the node enters the scene tree for the first time. -func _ready() -> void: - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: - pass +# 尝试阻止敌人攻击,获取闪避, unit_stats block_chance 越大 +# 成功率越高 +func get_chance_success(chance: float) -> bool: + var random := randf_range(0, 1.0) + + if random < chance: + return true + + return false diff --git a/effect/flash.gdshader b/effect/flash.gdshader new file mode 100644 index 0000000..20a2e5a --- /dev/null +++ b/effect/flash.gdshader @@ -0,0 +1,6 @@ +shader_type canvas_item; + +void fragment() { + COLOR = vec4(1.0,1.0,1.0,texture(TEXTURE, UV).a); + +} diff --git a/effect/flash.gdshader.uid b/effect/flash.gdshader.uid new file mode 100644 index 0000000..a0262b2 --- /dev/null +++ b/effect/flash.gdshader.uid @@ -0,0 +1 @@ +uid://dmnmstd7ptupl diff --git a/effect/flash_material.tres b/effect/flash_material.tres new file mode 100644 index 0000000..e2a26c5 --- /dev/null +++ b/effect/flash_material.tres @@ -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") diff --git a/scenes/unit/enemy/enemy.gd b/scenes/unit/enemy/enemy.gd index 6f49c7d..5c8d4e3 100644 --- a/scenes/unit/enemy/enemy.gd +++ b/scenes/unit/enemy/enemy.gd @@ -10,7 +10,7 @@ var can_move := true # Called when the node enters the scene tree for the first time. func _ready() -> void: - pass # Replace with function body. + super._ready() # Called every frame. 'delta' is the elapsed time since the previous frame. diff --git a/scenes/unit/unit.gd b/scenes/unit/unit.gd index c3284d6..812d5ab 100644 --- a/scenes/unit/unit.gd +++ b/scenes/unit/unit.gd @@ -7,15 +7,30 @@ class_name Unit @onready var sprite: Sprite2D = %Sprite @onready var anim_player: AnimationPlayer = $AnimationPlayer @onready var health_component: HealthComponent = $HealthComponent +@onready var flash_timer: Timer = $FlashTimer func _ready() -> void: - print(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: if health_component.current_health <=0: 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) print("%s: %d" % [name, health_component.current_health] ) + + +func _on_flash_timer_timeout() -> void: + sprite.material = null diff --git a/scenes/unit/unit.tscn b/scenes/unit/unit.tscn index 90734e9..6f6fed6 100644 --- a/scenes/unit/unit.tscn +++ b/scenes/unit/unit.tscn @@ -258,5 +258,10 @@ offset_top = -90.0 offset_right = 50.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_health_changed" from="HealthComponent" to="HealthBar" method="_on_health_component_on_health_changed"] +[connection signal="timeout" from="FlashTimer" to="." method="_on_flash_timer_timeout"]