From 431255dd13d40191001910d50ffafe2453242613 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 3 Apr 2022 09:04:30 +0200 Subject: [PATCH] :boom: Properly setup bullet collisions --- project.godot | 5 +++-- src/entities/Bullet.gd | 21 +++++++++++++++++++++ src/entities/Bullet.tscn | 10 +++++++--- src/entities/Enemy.tscn | 1 + src/entities/Player.tscn | 1 + src/entities/behaviours/BulletSpawn.gd | 1 + src/entities/behaviours/Ownership.gd | 6 +++--- 7 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/entities/Bullet.gd diff --git a/project.godot b/project.godot index 44eb47d..f6fad7d 100644 --- a/project.godot +++ b/project.godot @@ -84,8 +84,9 @@ player_shoot={ 2d_render/layer_1="Game" 2d_render/layer_2="UserInterface" 2d_physics/layer_1="Entities" -2d_physics/layer_2="Projectiles" -2d_physics/layer_3="UI" +2d_physics/layer_2="Damageable" +2d_physics/layer_3="Projectiles" +2d_physics/layer_4="UI" [physics] diff --git a/src/entities/Bullet.gd b/src/entities/Bullet.gd new file mode 100644 index 0000000..9e41186 --- /dev/null +++ b/src/entities/Bullet.gd @@ -0,0 +1,21 @@ +extends KinematicBody2D + + +export var damage: int = 1 + + +func _on_BulletMovement_hit(collision: KinematicCollision2D): + # Find the owner of this bullet + var bullet_owner = Ownership.get_value(self) + # Check if the bullet should do damage + var same_alliance = Alliance.compare(bullet_owner, collision.collider) + if same_alliance: + return + # Check if it is possible to damage the collider + var damageable = collision.collider.get_node("Damageable") + if damageable == null: + return + # Damage the collider! + damageable.health -= damage + # Alas, destroy self + queue_free() diff --git a/src/entities/Bullet.tscn b/src/entities/Bullet.tscn index d47abfa..c81ea27 100644 --- a/src/entities/Bullet.tscn +++ b/src/entities/Bullet.tscn @@ -1,18 +1,20 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://src/mechanics/White.png" type="Texture" id=1] [ext_resource path="res://src/entities/behaviours/BulletMovement.gd" type="Script" id=2] [ext_resource path="res://src/entities/behaviours/Ownership.tscn" type="PackedScene" id=3] +[ext_resource path="res://src/entities/Bullet.gd" type="Script" id=4] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 16, 8 ) [node name="Bullet" type="KinematicBody2D"] -collision_layer = 2 +collision_layer = 4 +collision_mask = 2 +script = ExtResource( 4 ) [node name="Shape" type="CollisionShape2D" parent="."] shape = SubResource( 1 ) -disabled = true [node name="Sprite" type="Sprite" parent="Shape"] scale = Vector2( 1, 0.5 ) @@ -22,3 +24,5 @@ texture = ExtResource( 1 ) script = ExtResource( 2 ) [node name="Ownership" parent="." instance=ExtResource( 3 )] + +[connection signal="hit" from="BulletMovement" to="." method="_on_BulletMovement_hit"] diff --git a/src/entities/Enemy.tscn b/src/entities/Enemy.tscn index 90874bc..434d2e7 100644 --- a/src/entities/Enemy.tscn +++ b/src/entities/Enemy.tscn @@ -10,6 +10,7 @@ extents = Vector2( 16, 16 ) [node name="Enemy" type="KinematicBody2D"] +collision_layer = 3 [node name="Shape" type="CollisionShape2D" parent="."] shape = SubResource( 1 ) diff --git a/src/entities/Player.tscn b/src/entities/Player.tscn index 88161e9..1e21e58 100644 --- a/src/entities/Player.tscn +++ b/src/entities/Player.tscn @@ -10,6 +10,7 @@ extents = Vector2( 16, 16 ) [node name="Player" type="KinematicBody2D"] +collision_layer = 3 collision/safe_margin = 0.02 [node name="Shape" type="CollisionShape2D" parent="."] diff --git a/src/entities/behaviours/BulletSpawn.gd b/src/entities/behaviours/BulletSpawn.gd index 02a7c9d..c41e4ad 100644 --- a/src/entities/behaviours/BulletSpawn.gd +++ b/src/entities/behaviours/BulletSpawn.gd @@ -19,3 +19,4 @@ func shoot(): var rotation = new_bullet.get_angle_to(source.get_global_mouse_position()) new_bullet.set_rotation(rotation) new_bullet.get_node("Ownership").entity_owner = source + new_bullet.add_collision_exception_with(source) diff --git a/src/entities/behaviours/Ownership.gd b/src/entities/behaviours/Ownership.gd index bf77084..0286a91 100644 --- a/src/entities/behaviours/Ownership.gd +++ b/src/entities/behaviours/Ownership.gd @@ -7,7 +7,7 @@ var entity_owner: Node = null static func get_value(first) -> Node: var first_node = first.get_node("Ownership") - var first_alliance = null + var first_eowner = null if first_node != null: - first_alliance = first_node.alliance - return first_alliance + first_eowner = first_node.entity_owner + return first_eowner