class_name BossTimeshiftCheck
extends Node2D
@onready var force_field_collision_check: Area2D = $ForceFieldCollisionCheck
@onready var movement_collision_check: Area2D = $MovementCollisionCheck

var is_position_valid := true
var last_valid_global_position := Vector2(0, 0)

var colliding_bodies_force : Array[PlayerBase] = []
var colliding_bodies_movement : Array[Node2D] = []

func _process(_delta: float) -> void:
	if is_position_valid:
		last_valid_global_position = global_position

func _on_force_field_collision_check_body_entered(body: Node2D) -> void:
	if body is Area2D:
		return
	if body is PlayerBase:
		if not colliding_bodies_force.has(body):
			colliding_bodies_force.append(body)
		is_position_valid = false

func _on_force_field_collision_check_body_exited(body: Node2D) -> void:
	if body is Area2D:
		return
	if force_field_collision_check.get_overlapping_bodies().any(func (b) : return b is PlayerBase):
		return
	if body is PlayerBase:
		if colliding_bodies_force.has(body):
			colliding_bodies_force.remove_at(colliding_bodies_force.find(body))
		if colliding_bodies_force.is_empty() and colliding_bodies_movement.is_empty():
			is_position_valid = true

func _on_movement_collision_check_body_entered(body: Node2D) -> void:
	if body is Area2D:
		return
	if not colliding_bodies_movement.has(body):
		colliding_bodies_movement.append(body)
	is_position_valid = false

func _on_movement_collision_check_body_exited(body: Node2D) -> void:
	if body is Area2D:
		return
	if movement_collision_check.has_overlapping_bodies():
		return
	if colliding_bodies_movement.has(body):
		colliding_bodies_movement.remove_at(colliding_bodies_movement.find(body))
	if colliding_bodies_force.is_empty() and colliding_bodies_movement.is_empty():
		is_position_valid = true
