feat: 添加Hitbox和Hurtbox,添加敌人和玩家之间的碰撞

This commit is contained in:
luke358
2026-05-18 14:55:43 +08:00
parent 67ea3f2d42
commit 2060719f4e
10 changed files with 117 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
extends Area2D
class_name HitboxComponent
signal on_hit_hurtbox(hurtbox: HurtboxComponent)
var damage := 1.0
var critical := false
var knockback_power := 0.0
var source: Node2D
func enable() -> void:
set_deferred("monitoring", true)
set_deferred("monitorable", true)
func disable() -> void:
set_deferred("monitoring", false)
set_deferred("monitorable", false)
func setup(damage: float, critical: bool, knockback: float, source: Node2D) -> void:
self.damage = damage
self.critical = critical
knockback_power = knockback
self.source = source
# 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
func _on_area_entered(area: Area2D) -> void:
if area is HurtboxComponent:
on_hit_hurtbox.emit(area)
print(area.owner.name)
@@ -0,0 +1 @@
uid://blhk17rquv0cp
+8
View File
@@ -0,0 +1,8 @@
[gd_scene format=3 uid="uid://c0fyx8gj5uexl"]
[ext_resource type="Script" uid="uid://blhk17rquv0cp" path="res://scenes/components/hitbox_component.gd" id="1_okvui"]
[node name="HitboxComponent" type="Area2D" unique_id=151235566]
script = ExtResource("1_okvui")
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
+19
View File
@@ -0,0 +1,19 @@
extends Area2D
class_name HurtboxComponent
signal on_damaged(hitbox: HitboxComponent)
# 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
func _on_area_entered(area: Area2D) -> void:
if area is HitboxComponent:
on_damaged.emit(area)
@@ -0,0 +1 @@
uid://c7k3ou0kpgbcs
+8
View File
@@ -0,0 +1,8 @@
[gd_scene format=3 uid="uid://bkyyic3okyjxx"]
[ext_resource type="Script" uid="uid://c7k3ou0kpgbcs" path="res://scenes/components/hurtbox_component.gd" id="1_trd8p"]
[node name="HurtboxComponent" type="Area2D" unique_id=1155790880]
script = ExtResource("1_trd8p")
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
+14 -1
View File
@@ -4,13 +4,17 @@
[ext_resource type="Script" uid="uid://b5j2awtdd1a4a" path="res://scenes/unit/enemy/enemy.gd" id="2_lmqbh"]
[ext_resource type="Texture2D" uid="uid://d1ou504pylric" path="res://assets/sprites/Enemies/Enemy_1.png" id="2_oo7ju"]
[ext_resource type="Resource" uid="uid://bn4dskgwnahaq" path="res://resources/units/enemies/stats_enemy_chaser_slow.tres" id="3_lmqbh"]
[ext_resource type="PackedScene" uid="uid://c0fyx8gj5uexl" path="res://scenes/components/hitbox_component.tscn" id="5_e15k4"]
[sub_resource type="CircleShape2D" id="CircleShape2D_e15k4"]
radius = 31.016125
radius = 4.0
[sub_resource type="CircleShape2D" id="CircleShape2D_lmqbh"]
radius = 150.0
[sub_resource type="CircleShape2D" id="CircleShape2D_421q1"]
radius = 40.0
[node name="EnemyChaserSlow" unique_id=2092979720 instance=ExtResource("1_dd0vi")]
collision_layer = 2
script = ExtResource("2_lmqbh")
@@ -32,3 +36,12 @@ collision_mask = 2
[node name="CollisionShape2D" type="CollisionShape2D" parent="VisionArea" index="0" unique_id=715489053]
position = Vector2(0, -25)
shape = SubResource("CircleShape2D_lmqbh")
[node name="HitboxComponent" parent="." index="5" unique_id=151235566 instance=ExtResource("5_e15k4")]
collision_layer = 4
collision_mask = 32
[node name="CollisionShape2D" type="CollisionShape2D" parent="HitboxComponent" index="0" unique_id=906401181]
position = Vector2(0, -30)
shape = SubResource("CircleShape2D_421q1")
debug_color = Color(0.95670325, 0.056883797, 0.5071302, 0.41960785)
+14 -2
View File
@@ -11,7 +11,10 @@ _data = [Vector2(0, 0), 0.0, 0.0, 0, 0, Vector2(1, 1), 0.0, 0.0, 0, 0]
point_count = 2
[sub_resource type="CircleShape2D" id="CircleShape2D_etcc1"]
radius = 30.0
radius = 2.236068
[sub_resource type="CircleShape2D" id="CircleShape2D_ht2l1"]
radius = 33.0
[node name="PlayerWellRounded" unique_id=2022554550 instance=ExtResource("1_wkafa")]
collision_mask = 2
@@ -38,7 +41,7 @@ z_index = 1
texture = ExtResource("2_etcc1")
[node name="CollisionShape2D" parent="." index="1" unique_id=139786467]
visible = false
z_index = 1
position = Vector2(0, -31)
shape = SubResource("CircleShape2D_etcc1")
@@ -51,5 +54,14 @@ one_shot = true
[node name="TrailTimer" type="Timer" parent="." index="5" unique_id=2006934730]
unique_name_in_owner = true
[node name="HurtboxComponent" parent="." index="6" unique_id=1155790880]
collision_layer = 32
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="HurtboxComponent" index="0" unique_id=1411044109]
z_index = 1
position = Vector2(0, -32)
shape = SubResource("CircleShape2D_ht2l1")
[connection signal="timeout" from="DashTimer" to="." method="_on_dash_timer_timeout"]
[connection signal="timeout" from="TrailTimer" to="Visuals/Trail" method="_on_trail_timer_timeout"]
+3
View File
@@ -2,6 +2,7 @@
[ext_resource type="Texture2D" uid="uid://devt2xbk78rej" path="res://assets/sprites/shadow.png" id="1_fvc8g"]
[ext_resource type="Script" uid="uid://cwf3afjy6t6rg" path="res://scenes/unit/unit.gd" id="1_vh40f"]
[ext_resource type="PackedScene" uid="uid://bkyyic3okyjxx" path="res://scenes/components/hurtbox_component.tscn" id="3_vh40f"]
[sub_resource type="Animation" id="Animation_bhlit"]
length = 0.001
@@ -244,3 +245,5 @@ position = Vector2(0, -61)
[node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=619019243]
libraries/ = SubResource("AnimationLibrary_w7udy")
[node name="HurtboxComponent" parent="." unique_id=1155790880 instance=ExtResource("3_vh40f")]