func _physics_process(delta: float) -> void:
    if not is_on_floor():
        velocity += get_gravity() * delta

    var candies = get_tree().get_nodes_in_group("candies")
    var players = get_tree().get_nodes_in_group("players")
    var closest_target = null
    var closest_distance = INF
    var target_is_candy = true

    var closest_threat = null  # Ближайший игрок, представляющий угрозу (у которого больше конфет)
    var closest_threat_distance = INF  # Расстояние до ближайшей угрозы

    # Вычисляем радиус приоритета на основе количества конфет
    var dynamic_priority_range = BASE_PRIORITY_RANGE + (candy_count * PRIORITY_RANGE_PER_CANDY)

    # Находим ближайшую конфету
    for candy in candies:
        var distance = global_transform.origin.distance_to(candy.global_transform.origin)
        if distance < closest_distance:
            closest_distance = distance
            closest_target = candy
            target_is_candy = true

    # Определяем угрозы (игроков с большим количеством конфет) и потенциальные цели (игроков с меньшим количеством конфет)
    for player in players:
        if player != self:
            var distance = global_transform.origin.distance_to(player.global_transform.origin)

            if player.candy_count > candy_count:
                # Игрок представляет угрозу
                if distance < closest_threat_distance:
                    closest_threat_distance = distance
                    closest_threat = player
            else:
                # Игрок является потенциальной целью для преследования
                if distance < closest_distance and player.candy_count < candy_count:
                    closest_distance = distance
                    closest_target = player
                    target_is_candy = false
                elif distance < dynamic_priority_range and candy_count > player.candy_count:
                    closest_distance = distance
                    closest_target = player
                    target_is_candy = false

    if closest_threat and closest_threat_distance < dynamic_priority_range:
        # Убегаем от угрозы
        var flee_direction = (global_transform.origin - closest_threat.global_transform.origin).normalized()
        current_direction = current_direction.lerp(flee_direction, ROTATION_SPEED * delta)
        current_direction = current_direction.normalized()

        var speed = SPEED
        velocity.x = current_direction.x * speed
        velocity.z = current_direction.z * speed

        if current_direction != Vector3.ZERO:
            look_at(global_transform.origin + Vector3(current_direction.x, 0, current_direction.z), Vector3.UP)

        if animation_player.current_animation != "move":
            animation_player.play("move", BLEND_TIME)

    elif closest_target:
        # Движемся к цели (конфете или более слабому игроку)
        target_direction = (closest_target.global_transform.origin - global_transform.origin).normalized()
        current_direction = current_direction.lerp(target_direction, ROTATION_SPEED * delta)
        current_direction = current_direction.normalized()

        var speed = SPEED
        velocity.x = current_direction.x * speed
        velocity.z = current_direction.z * speed

        if current_direction != Vector3.ZERO:
            look_at(global_transform.origin + Vector3(current_direction.x, 0, current_direction.z), Vector3.UP)

        if animation_player.current_animation != "move":
            animation_player.play("move", BLEND_TIME)
    else:
        # Замедляемся и останавливаемся, если нет цели или угрозы
        velocity.x = move_toward(velocity.x, 0, SPEED * delta)
        velocity.z = move_toward(velocity.z, 0, SPEED * delta)

        if abs(velocity.x) < 0.1 and abs(velocity.z) < 0.1:
            velocity.x = 0
            velocity.z = 0
            if animation_player.current_animation != "stop":
                animation_player.play("stop", BLEND_TIME)

    move_and_slide() персонаж забивается в угол стены и его становится легко поймать, исправь