require "Insurgent/InsurgentTraitDefinitions"

BreacherTrait = CreateInsurgentTrait()
BreacherTrait.name = "Breacher"
BreacherTrait.insurgentExclusive = true

function BreacherTrait.Create()
    return TraitFactory.addTrait("Breacher", getText("UI_trait_Breacher"), 8, getText("UI_trait_BreacherDesc"), false, false);
end

function BreacherTrait.OnPlayerMove(player)
    if not player:isLocalPlayer() or not player:HasTrait("Breacher") then
        return
    end
    if not player:isSprinting() and not player:IsRunning() then
        return
    end

    local knockdownChance = 25;
    if player:HasTrait("Stout") or player:HasTrait("Strong") then
        knockdownChance = knockdownChance + 25;
    end
    if player:HasTrait("Runner") then
        knockdownChance = knockdownChance + 10;
    end
    if player:isSprinting() then
        knockdownChance = knockdownChance + 25;
    end

    local zeds = player:getSpottedList();
    if zeds:size() == 0 then
        return
    end

    for i = 0, zeds:size() - 1 do
        if zeds:get(i):isZombie() then
            local zombie = zeds:get(i);
            local distance = zeds:get(i):DistTo(player);

            if (player:isSprinting() and distance <= 1.1) or (player:IsRunning() and distance <= 0.75) then
                if ZombRand(100) < knockdownChance then
                    zeds:get(i):knockDown(true)
                end
            end
        end
    end
end

function BreacherTrait.RegisterEvents()
    Events.OnPlayerMove.Add(BreacherTrait.OnPlayerMove);
end
