//the best pizza time animation ever (iguess)
//i learned new things about lua script

local target_time = 535 
local gravity_top = FRACUNIT * 1,5 
local gravity_bottom = FRACUNIT * 1,5
local horizontal_velocity_top = -FRACUNIT
local horizontal_velocity_bottom = FRACUNIT
local initial_jump_velocity = -FRACUNIT * 3,75 

local function startPlayerTimer(player)
    player.timer_value = 0
    player.timer_active = true
    player.flash_active = false
    player.hud_fall_active = false
    player.hud_fall_velocity_top = 0
    player.hud_fall_velocity_bottom = 0
    player.hud_position_y_top = 80 * FRACUNIT
    player.hud_position_y_bottom = 80 * FRACUNIT
    player.hud_position_x_top = 160 * FRACUNIT
    player.hud_position_x_bottom = 160 * FRACUNIT
    player.hud_visible = true
    player.pre_drop_hud_active = true
    player.jump_active = false 
end

addHook("MapLoad", function()
    for player in players.iterate do
        startPlayerTimer(player)
    end
end)

addHook("ThinkFrame", function()
    for player in players.iterate do
        if player.pizzanoreal
            and ((PTSR and PTSR.pizzatime and not PTSR.timeover)
                or (PTV3 and PTV3.pizzatime))
            and not (player.ptv3 and player.ptv3.extreme)
            and not player.timer_active
            and player.timer_value == 0 then
            startPlayerTimer(player)
        end
        if player.timer_active and ((PTSR and PTSR.pizzatime and not PTSR.timeover) or (PTV3 and PTV3.pizzatime)) then
            player.timer_value = player.timer_value + 1

            if player.timer_value == target_time then
                P_FlashPal(player, 1, 14)
                player.pre_drop_hud_active = false
                player.hud_fall_active = true
                player.jump_active = true 
                player.hud_fall_velocity_top = initial_jump_velocity 
                player.hud_fall_velocity_bottom = initial_jump_velocity 
            end
        end

        if player.hud_fall_active then
            if player.jump_active then
                player.hud_position_y_top = player.hud_position_y_top + player.hud_fall_velocity_top
                player.hud_position_y_bottom = player.hud_position_y_bottom + player.hud_fall_velocity_bottom
                player.jump_active = false
            else
                player.hud_fall_velocity_top = player.hud_fall_velocity_top + gravity_top
                player.hud_position_y_top = player.hud_position_y_top + player.hud_fall_velocity_top
                player.hud_position_x_top = player.hud_position_x_top + horizontal_velocity_top

                player.hud_fall_velocity_bottom = player.hud_fall_velocity_bottom + gravity_bottom
                player.hud_position_y_bottom = player.hud_position_y_bottom + player.hud_fall_velocity_bottom
                player.hud_position_x_bottom = player.hud_position_x_bottom + horizontal_velocity_bottom

                if player.hud_position_y_top > 700 * FRACUNIT and player.hud_position_y_bottom > 700 * FRACUNIT then
                    player.hud_visible = false
                    player.hud_fall_active = false 
                end
            end
        end
    end
end)

addHook("HUD", function(v, player)
    if player.hud_visible and ((PTSR and PTSR.pizzatime and not PTSR.timeover) or (PTV3 and PTV3.pizzatime) or player.hud_fall_active) then
        if player.pre_drop_hud_active then
            local sugarrushanim = ((leveltime / 2) % 2) + 1
            local sugarrushh = v.cachePatch('SRM_A' .. sugarrushanim)
            v.drawScaled(160 * FRACUNIT, 80 * FRACUNIT, FRACUNIT / 2, sugarrushh, V_PERPLAYER)
        elseif player.hud_fall_active then
            local sugaranim = ((leveltime / 2) % 1) + 1
            local sugar = v.cachePatch('SUGAR' .. sugaranim)
            local rushanim = ((leveltime / 2) % 1) + 1
            local rushh = v.cachePatch('RUSH' .. rushanim)
            local playercolor = v.getColormap(player.skin, player.mo.color)

            v.drawScaled(player.hud_position_x_top, player.hud_position_y_top, FRACUNIT / 2, sugar, V_PERPLAYER, playercolor)
            v.drawScaled(player.hud_position_x_bottom, player.hud_position_y_bottom, FRACUNIT / 2, rushh, V_PERPLAYER, playercolor)
        end
    end
end)

