From f57f85612e19e5ce99ce426834e3122548d14f1e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 4 Apr 2022 00:17:48 +0200 Subject: [PATCH] :wrench: Remove projectiles when their shape is off-screen --- project.godot | 6 ++++++ .../spawning/FreeWhenShapeIsOffScreen.gd | 16 ++++++++++++++++ .../spawning/FreeWhenShapeIsOffScreen.tscn | 8 ++++++++ src/behaviours/spawning/SpawnOnShoot.gd | 2 +- src/entities/bullets/Arrow.tscn | 5 ++++- src/entities/bullets/ArrowAlternative.tscn | 5 ++++- 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd create mode 100644 src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn diff --git a/project.godot b/project.godot index 8417ace..daf47e4 100644 --- a/project.godot +++ b/project.godot @@ -59,6 +59,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://src/behaviours/graphics/FourSidedSprite.gd" }, { +"base": "VisibilityNotifier2D", +"class": "FreeWhenShapeIsOffScreen", +"language": "GDScript", +"path": "res://src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd" +}, { "base": "Node", "class": "Ownership", "language": "GDScript", @@ -115,6 +120,7 @@ _global_script_class_icons={ "FireExtinguisherEnemy": "", "Flammable": "", "FourSidedSprite": "", +"FreeWhenShapeIsOffScreen": "", "Ownership": "", "PlayerMovement": "", "RNG": "", diff --git a/src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd b/src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd new file mode 100644 index 0000000..493591d --- /dev/null +++ b/src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd @@ -0,0 +1,16 @@ +extends VisibilityNotifier2D +class_name FreeWhenShapeIsOffScreen + + +onready var shape: Node = get_parent() +onready var target: Node = shape.get_parent() + + +func _ready(): + # This is gonna break. + var ext = shape.shape.extents + rect = Rect2(-ext.x, -ext.y, ext.x * 2, ext.y * 2) + + +func freedom(): + target.queue_free() diff --git a/src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn b/src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn new file mode 100644 index 0000000..cfcbc35 --- /dev/null +++ b/src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://src/behaviours/spawning/FreeWhenShapeIsOffScreen.gd" type="Script" id=1] + +[node name="FreeWhenShapeIsOffScreen" type="VisibilityNotifier2D"] +script = ExtResource( 1 ) + +[connection signal="screen_exited" from="." to="." method="freedom"] diff --git a/src/behaviours/spawning/SpawnOnShoot.gd b/src/behaviours/spawning/SpawnOnShoot.gd index ff124a1..4ed64ac 100644 --- a/src/behaviours/spawning/SpawnOnShoot.gd +++ b/src/behaviours/spawning/SpawnOnShoot.gd @@ -23,9 +23,9 @@ func get_cooldown() -> float: func shoot(target): var node = bullet.instance() + container.add_child(node) node.set_position(global_position) node.set_rotation(node.get_angle_to(target)) - container.add_child(node) node.get_node("Ownership").entity_owner = parent node.add_collision_exception_with(parent) emit_signal("shot") diff --git a/src/entities/bullets/Arrow.tscn b/src/entities/bullets/Arrow.tscn index 7d87965..421bb5c 100644 --- a/src/entities/bullets/Arrow.tscn +++ b/src/entities/bullets/Arrow.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://src/entities/bullets/AbstractBullet.tscn" type="PackedScene" id=1] [ext_resource path="res://src/entities/bullets/Arrow.png" type="Texture" id=2] +[ext_resource path="res://src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn" type="PackedScene" id=3] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 16, 6 ) @@ -14,5 +15,7 @@ shape = SubResource( 1 ) [node name="Sprite" type="Sprite" parent="Shape" index="0"] texture = ExtResource( 2 ) +[node name="FreeWhenShapeIsOffScreen" parent="Shape" index="1" instance=ExtResource( 3 )] + [node name="BulletMovement" parent="." index="1"] movement_per_second = 400.0 diff --git a/src/entities/bullets/ArrowAlternative.tscn b/src/entities/bullets/ArrowAlternative.tscn index 14c810f..0d7a587 100644 --- a/src/entities/bullets/ArrowAlternative.tscn +++ b/src/entities/bullets/ArrowAlternative.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://src/entities/bullets/AbstractBullet.tscn" type="PackedScene" id=1] [ext_resource path="res://src/entities/bullets/ArrowAlternative.png" type="Texture" id=2] [ext_resource path="res://src/entities/bullets/ArrowAlternative.gd" type="Script" id=3] [ext_resource path="res://src/behaviours/damage/Flammable.tscn" type="PackedScene" id=4] [ext_resource path="res://src/mechanics/Light.png" type="Texture" id=5] +[ext_resource path="res://src/behaviours/spawning/FreeWhenShapeIsOffScreen.tscn" type="PackedScene" id=6] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 6, 1.5 ) @@ -18,6 +19,8 @@ shape = SubResource( 1 ) [node name="Sprite" type="Sprite" parent="Shape" index="0"] texture = ExtResource( 2 ) +[node name="FreeWhenShapeIsOffScreen" parent="Shape" index="1" instance=ExtResource( 6 )] + [node name="BulletMovement" parent="." index="1"] movement_per_second = 400.0