From cbe36a2e8d57e3d760f36a00e3f858985f1690e2 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Sun, 10 Jan 2021 15:24:23 +0100 Subject: [PATCH] Fix GunShot deallocation Now bullets are deallocated once they hit a wall --- assets/misc/SimpleGunShot.tscn | 31 +++++++++++++++++++++++++------ src/misc/SimpleGunShot.gd | 5 +++++ src/misc/SimpleGunShotHitbox.gd | 4 ---- 3 files changed, 30 insertions(+), 10 deletions(-) delete mode 100644 src/misc/SimpleGunShotHitbox.gd diff --git a/assets/misc/SimpleGunShot.tscn b/assets/misc/SimpleGunShot.tscn index 7277969..36a2b1a 100644 --- a/assets/misc/SimpleGunShot.tscn +++ b/assets/misc/SimpleGunShot.tscn @@ -1,16 +1,25 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://assets/misc/SimpleGunShot.png" type="Texture" id=1] [ext_resource path="res://assets/misc/Hitbox.tscn" type="PackedScene" id=2] [ext_resource path="res://src/misc/SimpleGunShot.gd" type="Script" id=3] -[ext_resource path="res://src/misc/SimpleGunShotHitbox.gd" type="Script" id=4] +[ext_resource path="res://assets/misc/Hurtbox.tscn" type="PackedScene" id=5] + +[sub_resource type="GDScript" id=3] +script/source = "extends Area2D + +func _on_Hitbox_area_entered(area): + print(\"hit\") + queue_free() # remove myself +" [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 1.00781, 1.99957 ) +[sub_resource type="RectangleShape2D" id=2] +extents = Vector2( 1, 2 ) + [node name="SimpleGunShot" type="KinematicBody2D"] -visible = false -collision_layer = 0 collision_mask = 20 script = ExtResource( 3 ) @@ -18,10 +27,20 @@ script = ExtResource( 3 ) texture = ExtResource( 1 ) [node name="GunShotHitbox" parent="." instance=ExtResource( 2 )] -script = ExtResource( 4 ) +visible = false +collision_mask = 20 +script = SubResource( 3 ) [node name="CollisionShape2D" parent="GunShotHitbox" index="0"] shape = SubResource( 1 ) -[connection signal="area_entered" from="GunShotHitbox" to="GunShotHitbox" method="_on_Hitbox_area_entered"] + +[node name="GunShotHurtbox" parent="." instance=ExtResource( 5 )] +collision_layer = 0 +collision_mask = 4 + +[node name="CollisionShape2D2" type="CollisionShape2D" parent="GunShotHurtbox"] +shape = SubResource( 2 ) +[connection signal="body_entered" from="GunShotHitbox" to="." method="_on_GunShotHitbox_body_entered"] +[connection signal="body_entered" from="GunShotHurtbox" to="." method="_on_Hurtbox_body_entered"] [editable path="GunShotHitbox"] diff --git a/src/misc/SimpleGunShot.gd b/src/misc/SimpleGunShot.gd index acccfac..a89ef8b 100644 --- a/src/misc/SimpleGunShot.gd +++ b/src/misc/SimpleGunShot.gd @@ -9,3 +9,8 @@ func _ready(): func _physics_process(delta): move_and_collide(Vector2(0, SPEED * -1)) + +# decrease enemy or player's life +func _on_GunShotHitbox_body_entered(body: Node): + # TODO body's hp + queue_free() diff --git a/src/misc/SimpleGunShotHitbox.gd b/src/misc/SimpleGunShotHitbox.gd deleted file mode 100644 index 7133596..0000000 --- a/src/misc/SimpleGunShotHitbox.gd +++ /dev/null @@ -1,4 +0,0 @@ -extends Area2D - -func _on_Hitbox_area_entered(area): - queue_free()