mirror of
https://github.com/Steffo99/swear-jar.git
synced 2024-11-21 15:24:18 +00:00
Color red entities that would be deleted on placement
This commit is contained in:
parent
d78f353431
commit
903c69e61b
3 changed files with 50 additions and 11 deletions
|
@ -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()
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue