1
Fork 0
mirror of https://github.com/RYGhub/the-cold-night.git synced 2024-11-21 20:24:20 +00:00

💥 Properly setup bullet collisions

This commit is contained in:
Steffo 2022-04-03 09:04:30 +02:00
parent f81acea7c8
commit 431255dd13
Signed by: steffo
GPG key ID: 6965406171929D01
7 changed files with 37 additions and 8 deletions

View file

@ -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]

21
src/entities/Bullet.gd Normal file
View file

@ -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()

View file

@ -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"]

View file

@ -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 )

View file

@ -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="."]

View file

@ -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)

View file

@ -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