extends AnimatedSprite2D

var mover: PlayerBase;
@export var shadow: Sprite2D;
@export var past_modulate: Color;
var curr_modulate: Color;

func _ready() -> void:
	play("idle");
	curr_modulate = modulate;
	mover = get_parent() as PlayerBase;
	if Globals.current_time != Utils.TIME.PRESENT:
		modulate = past_modulate;
	
	SignalBus.changed_time.connect(on_timeshift);

func on_timeshift() -> void:
	if Globals.current_time == Utils.TIME.PRESENT:
		modulate = curr_modulate;
	else:
		modulate = past_modulate;


func _process(_delta: float) -> void:
	if not mover:
		return
	var direction: Vector2 = mover.cached_direction;
	direction = direction.normalized();
	if (direction.length() < 0.01):
		play("idle");
	elif abs(direction.x) > abs(direction.y):
		flip_h = direction.x < 0;
		shadow.flip_h = direction.x < 0;
		play("walk_h");
	elif (direction.y > 0.0):
		play("walk_b");
	else:
		play("walk_t");
