From 2060719f4ec41eb30af8187996702bf1d8287a22 Mon Sep 17 00:00:00 2001 From: luke358 Date: Mon, 18 May 2026 14:55:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0Hitbox=E5=92=8CHurtbo?= =?UTF-8?q?x=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=95=8C=E4=BA=BA=E5=92=8C?= =?UTF-8?q?=E7=8E=A9=E5=AE=B6=E4=B9=8B=E9=97=B4=E7=9A=84=E7=A2=B0=E6=92=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.godot | 4 ++ scenes/components/hitbox_component.gd | 45 ++++++++++++++++++++ scenes/components/hitbox_component.gd.uid | 1 + scenes/components/hitbox_component.tscn | 8 ++++ scenes/components/hurtbox_component.gd | 19 +++++++++ scenes/components/hurtbox_component.gd.uid | 1 + scenes/components/hurtbox_component.tscn | 8 ++++ scenes/unit/enemy/enemy_chaser_slow.tscn | 15 ++++++- scenes/unit/players/player_well_rounded.tscn | 16 ++++++- scenes/unit/unit.tscn | 3 ++ 10 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 scenes/components/hitbox_component.gd create mode 100644 scenes/components/hitbox_component.gd.uid create mode 100644 scenes/components/hitbox_component.tscn create mode 100644 scenes/components/hurtbox_component.gd create mode 100644 scenes/components/hurtbox_component.gd.uid create mode 100644 scenes/components/hurtbox_component.tscn diff --git a/project.godot b/project.godot index 6feeb98..41772e4 100644 --- a/project.godot +++ b/project.godot @@ -57,6 +57,10 @@ dash={ 2d_physics/layer_1="Player" 2d_physics/layer_2="Enemy" +2d_physics/layer_3="HitboxEnemy" +2d_physics/layer_4="HurtboxEnemy" +2d_physics/layer_5="HitboxPlayer" +2d_physics/layer_6="HurtboxPlayer" [physics] diff --git a/scenes/components/hitbox_component.gd b/scenes/components/hitbox_component.gd new file mode 100644 index 0000000..d767e09 --- /dev/null +++ b/scenes/components/hitbox_component.gd @@ -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) + diff --git a/scenes/components/hitbox_component.gd.uid b/scenes/components/hitbox_component.gd.uid new file mode 100644 index 0000000..ce5494f --- /dev/null +++ b/scenes/components/hitbox_component.gd.uid @@ -0,0 +1 @@ +uid://blhk17rquv0cp diff --git a/scenes/components/hitbox_component.tscn b/scenes/components/hitbox_component.tscn new file mode 100644 index 0000000..62d0302 --- /dev/null +++ b/scenes/components/hitbox_component.tscn @@ -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"] diff --git a/scenes/components/hurtbox_component.gd b/scenes/components/hurtbox_component.gd new file mode 100644 index 0000000..f4ef2b9 --- /dev/null +++ b/scenes/components/hurtbox_component.gd @@ -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) diff --git a/scenes/components/hurtbox_component.gd.uid b/scenes/components/hurtbox_component.gd.uid new file mode 100644 index 0000000..cb529c0 --- /dev/null +++ b/scenes/components/hurtbox_component.gd.uid @@ -0,0 +1 @@ +uid://c7k3ou0kpgbcs diff --git a/scenes/components/hurtbox_component.tscn b/scenes/components/hurtbox_component.tscn new file mode 100644 index 0000000..d1a8129 --- /dev/null +++ b/scenes/components/hurtbox_component.tscn @@ -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"] diff --git a/scenes/unit/enemy/enemy_chaser_slow.tscn b/scenes/unit/enemy/enemy_chaser_slow.tscn index 3e9bad0..1679911 100644 --- a/scenes/unit/enemy/enemy_chaser_slow.tscn +++ b/scenes/unit/enemy/enemy_chaser_slow.tscn @@ -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) diff --git a/scenes/unit/players/player_well_rounded.tscn b/scenes/unit/players/player_well_rounded.tscn index 80a54be..f854644 100644 --- a/scenes/unit/players/player_well_rounded.tscn +++ b/scenes/unit/players/player_well_rounded.tscn @@ -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"] diff --git a/scenes/unit/unit.tscn b/scenes/unit/unit.tscn index ee29a29..2f671a7 100644 --- a/scenes/unit/unit.tscn +++ b/scenes/unit/unit.tscn @@ -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")]