--任务副本2
--进入副本→播放MOV→刷出一波小怪→刷出一个boss→击杀boss后播放MOV→播放完毕后结束

x040021_g_scriptId = 040021

--副本参数
x040021_g_CSParamIdxCurStage = 0              --大阶段
x040021_g_CSParamIdxCurSubStage = 1           --小阶段
x040021_g_CSParam1 = 2
x040021_g_CSParam2 = 3   -- 7为当前阶段

x040021_g_CommitMissionTick = 4                       -- 完成任务的tick
x040021_g_Alltick = 6                               -- 总tick
x040021_g_BloodTick = 8  -- 血脉提示留存时间

x040021_g_CSParamAnimationID = 2008           --mov动画1ID
x040021_g_CSParamAnimationID2 = 2004            --mov动画2ID

x040021_g_Mov1 = 20 -- mov1 最大等待时间
x040021_g_Mov2 = 20 -- mov2 最大等待时间

x040021_g_ClearBloodShowQuestion = 6 -- 清除血脉cd 多少秒以后弹出血脉使用提示

x040021_g_MonstorList = {}
x040021_g_MonstorList[1] = { 1002110, 1002110, 1002110, 1002110, 1002110, 1002110, 1002110, 1002110, 1002110, 1002110 }  --小怪
x040021_g_MonstorList[2] = { 1002111 }

x040021_g_TargetList = {}                      --目标
x040021_g_TargetList[1] = { 1002110 }  --第一阶段目标1
x040021_g_TargetList[2] = { 1002111 }   --第2阶段目标1
x040021_g_TargetList[3] = {}

x040021_g_FirstMissionId = 2012    -- 第一个任务
x040021_g_SecMissionId = 2013 -- 第2个任务

x040021_g_MonstorPosList = {}
x040021_g_MonstorPosList[1] = { { 37.3, 57.76, -1.57 }, { 39.03, 64.19, -1.57 }, { 33.68, 62.09, -1.57 }, { 37.27, 62.01, -1.57 }, { 40.92, 62.1, -1.57 }, { 35.27, 59.76, -1.57 }, { 39.67, 59.69, -1.57 }, { 33.31, 57.36, -1.57 }, { 41.71, 57.25, -1.57 }, { 35.53, 64.26, -1.57 } } --小怪坐标
x040021_g_MonstorPosList[2] = { { 37.75, 96.33, -1.57 } }

function x040021_OnCopySceneTimeOut(scene)
    ClearPlayer(scene)
end
function x040021_OnCopySceneOpen(scene)
    AddCSMission(scene, x040021_g_FirstMissionId)
    AddCSMission(scene, x040021_g_SecMissionId)
end
function x040021_ClearAllMonster(scene)
    DeleHostileMonster(scene)
end
function x040021_StageControl(scene, nstage)
    SetCopySceneParam(scene, 2, 0)
    SetCopySceneParam(scene, 3, 0)
    SetCopySceneParam(scene, 4, 0)
    SetCopySceneParam(scene, 7, nstage)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, j)
        SynCsStageData(scene, nUserObjId)
    end
end
function x040021_OnObjEnter(scene, objId)
    SetCopySceneParam(scene, x040021_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, 0)
    SetCopySceneParam(scene, x040021_g_CSParam1, 0)
    SetCopySceneParam(scene, x040021_g_CSParam2, 0)
    SetCopySceneParam(scene, x040021_g_CommitMissionTick, 0)
    x040021_ClearAllMonster(scene)
    SetCopySceneParam(scene, 7, 0)
    SynCsStageData(scene, objId, true)
end

function x040021_OnDieCountDown1(scene, rolebaseId, stage)
    local TargetId = x040021_g_TargetList[stage + 1][1]
    if (TargetId == rolebaseId) then
        local OldValue = GetCopySceneParam(scene, x040021_g_CSParam1)
        SetCopySceneParam(scene, x040021_g_CSParam1, OldValue + 1)
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            SynCsStageData(scene, nUserObjId)
        end
    end
end

--死亡回调
function x040021_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x040021_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x040021_OnDieCountDown1(scene, rolebaseId, oldStage)
    if MonCount == 0 then
        if nCurStage == 1 then
            x040021_SetStats(scene, 1, 1)
            x040021_CreateMonster(scene, 2, 1)
            x040021_OnBloodSkill(scene)
        end
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, 0)
    end
end

--动画/对话回调
function x040021_OnStoryEventEnd(scene, nStoryId, nAnimationId)
    local curStage = GetCopySceneParam(scene, x040021_g_CSParamIdxCurStage)
    if nAnimationId == x040021_g_CSParamAnimationID then
        if curStage == 0 and GetHostileMonsterCount(scene) == 0 then
            x040021_SetStats(scene, 0, 1)
            x040021_CreateMonster(scene, 1, 1)

            SetCopySceneParam(scene, x040021_g_CSParamIdxCurStage, 1)
            SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, 0)
        end
    elseif nAnimationId == x040021_g_CSParamAnimationID2 then
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurStage, curStage + 1)
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, 0)
    end
end

--副本Tick
function x040021_OnCopySceneTick(scene, tickCount)
    SetCopySceneParam(scene, x040021_g_Alltick, tickCount)

    local curStage = GetCopySceneParam(scene, x040021_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, curSubStage + 1)

    if curStage == 0 and curSubStage == x040021_g_Mov1 and GetHostileMonsterCount(scene) == 0 then
        -- 强制刷怪
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurStage, 1)
        SetCopySceneParam(scene, x040021_g_CSParamIdxCurSubStage, 0)
        x040021_CreateMonster(scene, 1, 1)
    end

    local LastBlood = GetCopySceneParam(scene, x040021_g_CommitMissionTick)
    if LastBlood > 0 then
        SetCopySceneParam(scene, x040021_g_CommitMissionTick, LastBlood - 1)
        if LastBlood - 1 == 0 then
            local nUserCount = GetSceneUserCount(scene)
            for j = 0, nUserCount - 1 do
                local nObjId = GetUserObjIdByIdx(scene, j)
                if not IsBloodAwaken(scene, nObjId) then
                    ShowQuestion(scene, nObjId, 316)
                end
            end
        end
    end

    --if LastBlood ~= 0 and tickCount == LastBlood then
    --    local nUserCount = GetSceneUserCount(scene)
    --    for i = 0, nUserCount - 1 do
    --        local nUserObjId = GetUserObjIdByIdx(scene, i)
    --        ActiveBloodAwaken(scene, nUserObjId)
    --    end
    --    SetCopySceneParam(scene, x040021_g_CommitMissionTick, 0)
    --end

    if curStage == 3 and curSubStage == x040021_g_Mov2 then
        SetCopySceneSuccess(scene)
        x040021_StageControl(scene, 2)
    end

    if curStage == 0 then
        x040021_Stage_0(scene, curSubStage)
    elseif curStage == 3 then
        x040021_Stage_2(scene, curSubStage)
    elseif curStage == 4 then
        SetCopySceneSuccess(scene)
        x040021_StageControl(scene, 2)
    end
end

function x040021_SetStats(scene, stage, curStage)
    if curStage == 1 then
        x040021_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
    end
end

--第0阶段 播放MOV 刷第一波小怪
function x040021_Stage_0(scene, substage)
    if substage == 1 then
        StoryAnimation(scene, x040021_g_CSParamAnimationID)
    end
end

function x040021_Stage_2(scene, substage)
    if substage == 1 then
        StoryAnimation(scene, x040021_g_CSParamAnimationID2)
    end
end

function x040021_CreateMonster(scene, stage, substage)
    if substage == 1 then
        TellPlayerAutoCombat(scene)
        local count = #x040021_g_MonstorList[stage]
        for i = 1, count do
            CreateMonster(scene, x040021_g_MonstorList[stage][i], x040021_g_MonstorPosList[stage][i][3], x040021_g_MonstorPosList[stage][i][1], x040021_g_MonstorPosList[stage][i][2])
        end
    end
end

function x040021_OnBloodSkill(scene)
    local nUserCount = GetSceneUserCount(scene)
    for j = 0, nUserCount - 1 do
        local nObjId = GetUserObjIdByIdx(scene, j)
        if not IsBloodAwaken(scene, nObjId) then
            --local tick = GetCopySceneParam(scene, x040021_g_Alltick)
            RemoveBloodSkillCDTime(scene, nObjId)
        end
    end
    SetCopySceneParam(scene, x040021_g_CommitMissionTick, x040021_g_ClearBloodShowQuestion)
    TellPlayerAutoCombat(scene, true)
end