--任务副本2
--副本流程为：进入副本→播mov→刷出一波小怪→播boss出场mov→刷出boss→击杀后结束

x040019_g_scriptId = 040019

--副本参数
x040019_g_CSParamIdxCurStage = 0              --大阶段
x040019_g_CSParamIdxCurSubStage = 1           --小阶段
x040019_g_CSParam1 = 2
x040019_g_CSParam2 = 3   -- 7为当前阶段

x040019_g_CSParamAnimationID = 3001           --mov动画1ID
x040019_g_CSParamAnimationID2 = 2007           --mov动画2ID

x040019_g_CSParamAnimationID2Time = 13 -- 最大mov时间
x040019_g_CSParamAnimationIDTime = 20 -- 最大mov时间


x040019_g_MonstorList = {}
x040019_g_MonstorList[1] = { 1003117, 1003117, 1003117, 1003117, 1003117, 1003117, 1003117, 1003117, 1003117, 1003117 }  --小怪
x040019_g_MonstorList[2] = { 1003113 }

x040019_g_TargetList = {}                      --目标
x040019_g_TargetList[1] = { 1003117 }  --第一阶段目标1
x040019_g_TargetList[2] = { 1003113 }   --第2阶段目标1
x040019_g_TargetList[3] = {}

x040019_g_FirstMissionId = 2024    -- 第一个任务
x040019_g_SecMissionId = 2025 -- 第2个任务

x040019_g_MonstorPosList = {}
x040019_g_MonstorPosList[1] = { { 22.94, 59.82, -1.57 }, { 19.28, 65.08, -1.57 }, { 16.8, 63.22, -1.57 }, { 17.01, 60.11, -1.57 }, { 30.75, 63.02, -1.57 }, { 27.99, 59.5, -1.57 }, { 27.48, 64.04, -1.57 }, { 20.79, 61.74, -1.57 }, { 22.86, 64.28, -1.57 }, { 25.15, 62.04, -1.57 } } --小怪坐标
x040019_g_MonstorPosList[2] = { { 22.76, 68.55, -1.57 } }

function x040019_OnCopySceneTimeOut(scene)
    ClearPlayer(scene)
end
function x040019_OnCopySceneOpen(scene)
    AddCSMission(scene, x040019_g_FirstMissionId)
    AddCSMission(scene, x040019_g_SecMissionId)
end
function x040019_ClearAllMonster(scene)
    DeleHostileMonster(scene)
end
function x040019_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 x040019_OnObjEnter(scene, objId)
    SetCopySceneParam(scene, x040019_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040019_g_CSParamIdxCurSubStage, 0)
    SetCopySceneParam(scene, x040019_g_CSParam1, 0)
    SetCopySceneParam(scene, x040019_g_CSParam2, 0)
    x040019_ClearAllMonster(scene)
    SetCopySceneParam(scene, 7, 0)
    SynCsStageData(scene, objId, true)
end


--死亡回调
function x040019_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x040019_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    local TargetId = x040019_g_TargetList[oldStage + 1][1]
    if TargetId == rolebaseId then
        SetCopySceneParam(scene, x040019_g_CSParam1, GetCopySceneParam(scene, x040019_g_CSParam1) + 1)
        SynCsStageData(scene, killerId)
    end
    if MonCount == 0 then
        if (rolebaseId == x040019_g_MonstorList[2][1]) then
            x040019_StageControl(scene, 2)
            StoryAnimation(scene, x040019_g_CSParamAnimationID)
        end
        SetCopySceneParam(scene, x040019_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x040019_g_CSParamIdxCurSubStage, 0)
    end
end

--动画/对话回调
function x040019_OnStoryEventEnd(scene, nStoryId, nAnimationId)
    if nAnimationId == x040019_g_CSParamAnimationID2 then
        local curStage = GetCopySceneParam(scene, x040019_g_CSParamIdxCurStage)
        SetCopySceneParam(scene, x040019_g_CSParamIdxCurStage, curStage + 1)
        SetCopySceneParam(scene, x040019_g_CSParamIdxCurSubStage, 0)
    elseif nAnimationId == x040019_g_CSParamAnimationID then
        SetCopySceneSuccess(scene)
    end
end

--副本Tick
function x040019_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x040019_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x040019_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x040019_g_CSParamIdxCurSubStage, curSubStage + 1)
    if curStage == 0 then
        x040019_SetStats(scene, 0, curSubStage)
        x040019_CreateMonster(scene, 1, curSubStage)
    elseif curStage == 1 then
        x040019_Stage_2(scene, curStage, curSubStage)
    elseif curStage == 2 then
        x040019_SetStats(scene, 1, curSubStage)
        x040019_CreateMonster(scene, curStage, curSubStage)
    end

    if curStage == 1 and curSubStage == x040019_g_CSParamAnimationID2Time and GetHostileMonsterCount(scene) == 0 then
        x040019_SetStats(scene, 1, 1)
        x040019_CreateMonster(scene, 2, 1)
        SetCopySceneParam(scene, x040019_g_CSParamIdxCurStage, curStage + 2)
    elseif curStage == 3 and curSubStage == x040019_g_CSParamAnimationIDTime then
        SetCopySceneSuccess(scene)
    end
end

function x040019_SetStats(scene, stage, substage)
    if substage == 1 then
        x040019_StageControl(scene, stage)
        OnCSMissionStage(scene, stage)
    end
end

function x040019_Stage_2(scene, stage, substage)
    if substage == 1 then
        StoryAnimation(scene, x040019_g_CSParamAnimationID2)
    end
end

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