From 903c69e61b6cb300bbb13465b644ec4b8c5bab4a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 13 Oct 2023 23:51:55 +0200 Subject: [PATCH] Color red entities that would be deleted on placement --- interface/ghost/ghost.gd | 27 ++++++++++++++++++++++++--- interface/ghost/ghost.tscn | 8 ++++---- interface/ghost/overlap_freer.gd | 26 ++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/interface/ghost/ghost.gd b/interface/ghost/ghost.gd index 2430625..62ebcf6 100644 --- a/interface/ghost/ghost.gd +++ b/interface/ghost/ghost.gd @@ -57,8 +57,6 @@ func _ready(): ## Update the value of [can_place]. func update_state(): - overlap_checker.update_is_overlapping_with() - placeable_area_checker.update_is_overlapping_with() # DIRTY HACK: Relies on the placeable area being perfectly surrounded by solid bodies. can_place = overlap_checker.is_overlapping_with == null and placeable_area_checker.is_overlapping_with != null @@ -103,8 +101,31 @@ func materialize() -> Node: func _on_moved(_from, _to): + overlap_checker.update_is_overlapping_with() + overlap_freer.update_is_overlapping_with() + placeable_area_checker.update_is_overlapping_with() update_state() - func _on_rotated_radians(_from, _to): + overlap_checker.update_is_overlapping_with() + overlap_freer.update_is_overlapping_with() + placeable_area_checker.update_is_overlapping_with() + update_state() + +func _on_area_entered(_area): + placeable_area_checker.update_is_overlapping_with() + update_state() + +func _on_area_exited(_area): + placeable_area_checker.update_is_overlapping_with() + update_state() + +func _on_body_entered(_body): + overlap_checker.update_is_overlapping_with() + overlap_freer.update_is_overlapping_with() + update_state() + +func _on_body_exited(_body): + overlap_checker.update_is_overlapping_with() + overlap_freer.update_is_overlapping_with() update_state() diff --git a/interface/ghost/ghost.tscn b/interface/ghost/ghost.tscn index aa95989..50a440e 100644 --- a/interface/ghost/ghost.tscn +++ b/interface/ghost/ghost.tscn @@ -34,9 +34,9 @@ overlap_mask = 16 [node name="OverlapFreer" parent="." instance=ExtResource("6_y1rxa")] overlap_mask = 4 -[connection signal="area_entered" from="." to="PlaceableAreaChecker" method="update_is_overlapping_with"] -[connection signal="area_exited" from="." to="PlaceableAreaChecker" method="update_is_overlapping_with"] -[connection signal="body_entered" from="." to="OverlapChecker" method="update_is_overlapping_with"] -[connection signal="body_exited" from="." to="OverlapChecker" method="update_is_overlapping_with"] +[connection signal="area_entered" from="." to="." method="_on_area_entered"] +[connection signal="area_exited" from="." to="." method="_on_area_exited"] +[connection signal="body_entered" from="." to="." method="_on_body_entered"] +[connection signal="body_exited" from="." to="." method="_on_body_exited"] [connection signal="moved" from="PreciseTouchPlacer" to="." method="_on_moved"] [connection signal="rotated_radians" from="PreciseTouchPlacer" to="." method="_on_rotated_radians"] diff --git a/interface/ghost/overlap_freer.gd b/interface/ghost/overlap_freer.gd index afdeefa..9dfc4c3 100644 --- a/interface/ghost/overlap_freer.gd +++ b/interface/ghost/overlap_freer.gd @@ -11,12 +11,30 @@ class_name OverlapFreer @export var overlap_with_tilemap: bool = false +## Color to restore modulation on objects that wouldn't be deleted anymore. +@export var valid_color: Color = Color.WHITE + +## Color to modulate objects that would be deleted if [area_queue_free] was called right now. +@export var invalid_color: Color = Color.RED + + +var is_overlapping_with: Array[Node2D] = []: + get: + return is_overlapping_with + set(value): + for body in is_overlapping_with: + body.modulate = valid_color + for body in value: + body.modulate = invalid_color + is_overlapping_with = value + + ## The [Area2D] this script should act on. @onready var target: Area2D = get_parent() ## Get all bodies overlapping the [target]. -func get_all_overlapping_bodies() -> Array[Node2D]: +func update_is_overlapping_with() -> void: var bodies: Array[Node2D] = [] for body in target.get_overlapping_bodies(): if body is TileMap: @@ -25,9 +43,9 @@ func get_all_overlapping_bodies() -> Array[Node2D]: elif body is PhysicsBody2D: if body.collision_layer & overlap_mask: bodies.append(body) - return bodies + is_overlapping_with = bodies -## Emitted when a body is about to be [queue_free]d. +## Emitted when a body is about to be [queue_free]d by [area_queue_free]. signal queueing_free(body: Node2D) ## Queue free the passed nodes. @@ -38,4 +56,4 @@ func mass_queue_free(nodes: Array[Node2D]): ## Queue free all overlapping bodies. func area_queue_free() -> void: - mass_queue_free(get_all_overlapping_bodies()) + mass_queue_free(is_overlapping_with)