1
Fork 0
mirror of https://github.com/Steffo99/swear-jar.git synced 2024-11-24 08:34:19 +00:00
swear-jar/interface/ghost/overlap_checker.gd

43 lines
1.3 KiB
GDScript3
Raw Permalink Normal View History

2023-10-08 01:56:29 +00:00
extends Node
class_name OverlapChecker
## 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 = true
## The [Area2D] this script should act on.
@onready var target: Area2D = get_parent()
## What the [target] object is currently overlapping with.
var is_overlapping_with: Node2D = null
## Get the first body overlapping the [target].
func get_first_overlapping_body() -> Node2D:
for body in target.get_overlapping_bodies():
if body is TileMap:
if overlap_with_tilemap:
return body
elif body is PhysicsBody2D:
if body.collision_layer & overlap_mask:
return body
return null
## Update the [is_overlapping_with] variable.
2023-10-12 16:41:16 +00:00
func update_is_overlapping_with() -> void:
2023-10-08 01:56:29 +00:00
var current_overlap = get_first_overlapping_body()
if current_overlap != is_overlapping_with:
2023-10-13 17:21:07 +00:00
print("[OverlapChecker] Now overlapping with: ", current_overlap)
2023-10-08 01:56:29 +00:00
overlap_changing.emit(current_overlap)
is_overlapping_with = current_overlap
## Emitted when the value of [is_overlapping_with] changes because of [_update_is_overlapping_with].
signal overlap_changing(to: Node2D)