2023-10-08 01:56:29 +00:00
|
|
|
extends Node
|
|
|
|
class_name OverlapFreer
|
|
|
|
|
|
|
|
|
|
|
|
## Layers to consider when checking overlap with [PhysicBody2D].
|
|
|
|
@export_flags_2d_physics var overlap_mask: int
|
|
|
|
|
|
|
|
## Whether [TileMap]s should be considered in collisions or not.
|
|
|
|
##
|
|
|
|
## Their [collision_layer]s are always ignored.
|
|
|
|
@export var overlap_with_tilemap: bool = false
|
|
|
|
|
|
|
|
|
2023-10-13 21:51:55 +00:00
|
|
|
## 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
|
|
|
|
|
|
|
|
|
2023-10-08 01:56:29 +00:00
|
|
|
## The [Area2D] this script should act on.
|
|
|
|
@onready var target: Area2D = get_parent()
|
|
|
|
|
|
|
|
|
|
|
|
## Get all bodies overlapping the [target].
|
2023-10-13 21:51:55 +00:00
|
|
|
func update_is_overlapping_with() -> void:
|
2023-10-08 01:56:29 +00:00
|
|
|
var bodies: Array[Node2D] = []
|
|
|
|
for body in target.get_overlapping_bodies():
|
|
|
|
if body is TileMap:
|
|
|
|
if overlap_with_tilemap:
|
|
|
|
bodies.append(body)
|
|
|
|
elif body is PhysicsBody2D:
|
|
|
|
if body.collision_layer & overlap_mask:
|
|
|
|
bodies.append(body)
|
2023-10-13 21:51:55 +00:00
|
|
|
is_overlapping_with = bodies
|
2023-10-08 01:56:29 +00:00
|
|
|
|
2023-10-13 21:51:55 +00:00
|
|
|
## Emitted when a body is about to be [queue_free]d by [area_queue_free].
|
2023-10-13 00:04:43 +00:00
|
|
|
signal queueing_free(body: Node2D)
|
|
|
|
|
|
|
|
## Queue free the passed nodes.
|
|
|
|
func mass_queue_free(nodes: Array[Node2D]):
|
|
|
|
for node in nodes:
|
|
|
|
queueing_free.emit(node)
|
|
|
|
node.queue_free()
|
2023-10-08 01:56:29 +00:00
|
|
|
|
|
|
|
## Queue free all overlapping bodies.
|
|
|
|
func area_queue_free() -> void:
|
2023-10-13 21:51:55 +00:00
|
|
|
mass_queue_free(is_overlapping_with)
|