extends Node2D
class_name TimeShift

# TIMESHIFT NOW CONSIDERED SIBLING OF PLAYER_BASE (in player_base script)
@export var timeshift_target: CharacterBody2D 

var map_loader : MapLoader

var timeshift_check_scene = preload("res://scenes/nodes/player/timeshift_check.tscn")
var timeshift_check

func _ready() -> void:
	if not timeshift_target:
		push_error("No timeshift target assigned!\nThis may cause errors when timeshifting into obstructed areas!")
		assert(false)
	timeshift_check = timeshift_check_scene.instantiate() as TimeshiftCheck
	timeshift_target.add_child.call_deferred(timeshift_check)

# simplified regular timeshift and added target time period
# for remnant and boss who don't create animations, sound effects, or affect the "global" time period
func trigger(time : Utils.TIME = Globals.current_time):
	time_shift(time)

func time_shift(time : Utils.TIME = Globals.current_time):
	timeshift_target.global_position.y += map_loader.get_timeshift_offset(time)
	#timeshift_target.global_position += disel.calc_offset() * (-1 if time == Utils.TIME.PRESENT else 1)

	if not timeshift_check.is_position_valid:
		timeshift_target.global_position = timeshift_check.last_valid_global_position
	reset_set(time)

#region validity check
func _set_timecheck_position(current_time : Utils.TIME = Globals.current_time):
	timeshift_check.global_position.y -= map_loader.get_timeshift_offset(current_time)
	#timeshift_check.global_position += disel.calc_offset() * (1 if current_time == Utils.TIME.PRESENT else -1)

func _reset_timecheck_position():
	timeshift_check.position = Vector2(0, 0)

func reset_set(current_time : Utils.TIME = Globals.current_time):
	_reset_timecheck_position()
	_set_timecheck_position(current_time)
#endregion
