--任务副本2
--进入副本→播放MOV→刷NOSS→击杀BOSS→副本完成→传出副本

x040017_g_scriptId = 040017

--副本参数
x040017_g_CSParamIdxCurStage = 0              --大阶段
x040017_g_CSParamIdxCurSubStage = 1           --小阶段
x040017_g_CSParam1 = 2
x040017_g_CSParam2 = 3   -- 7为当前阶段
x040017_g_CSParamAnimationID = 2003           --mov动画ID

x040017_g_MonstorList = {}
x040017_g_MonstorList[1] = { 1002108 }

x040017_g_TargetList = {}                      --目标
x040017_g_TargetList[1] = { 1002108 }  --第1阶段目标1
x040017_g_TargetList[2] = {}

x040017_g_FirstMissionId1 = 2011    -- 第一个任务

x040017_g_MonstorPosList = {}
x040017_g_MonstorPosList[1] = { { 31.31, 30.84, -1.57 } }

function x040017_OnCopySceneTimeOut(scene)
    ClearPlayer(scene)
end

function x040017_OnCopySceneOpen(scene)
    AddCSMission(scene, x040017_g_FirstMissionId1)
end

function x040017_ClearAllMonster(scene)
    DeleHostileMonster(scene)
end
function x040017_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 x040017_OnObjEnter(scene, objId)
    SetCopySceneParam(scene, x040017_g_CSParamIdxCurStage, 0)
    SetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage, 0)
    SetCopySceneParam(scene, x040017_g_CSParam1, 0)
    SetCopySceneParam(scene, x040017_g_CSParam2, 0)
    x040017_ClearAllMonster(scene)
    SetCopySceneParam(scene, 7, 0)
    SynCsStageData(scene, objId, true)
end

function x040017_OnDieCountDown1(scene, rolebaseId, stage)
    local TargetId = x040017_g_TargetList[stage + 1][1]
    if (TargetId == rolebaseId) then
        local OldValue = GetCopySceneParam(scene, x040017_g_CSParam1)
        SetCopySceneParam(scene, x040017_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 x040017_OnObjDie(scene, objId, rolebaseId, killerId)
    local nCurStage = GetCopySceneParam(scene, x040017_g_CSParamIdxCurStage)
    local MonCount = GetHostileMonsterCount(scene)
    local oldStage = GetCopySceneParam(scene, 7)
    x040017_OnDieCountDown1(scene, rolebaseId, oldStage)
    if MonCount == 0 then
        if (rolebaseId == x040017_g_MonstorList[1][1]) then
            SetCopySceneSuccess(scene)
            x040017_StageControl(scene, oldStage + 1)
        end
        SetCopySceneParam(scene, x040017_g_CSParamIdxCurStage, nCurStage + 1)
        SetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage, 0)
    end
end

--动画/对话回调
function x040017_OnStoryEventEnd(scene, nStoryId, nAnimationId)
    if nAnimationId == x040017_g_CSParamAnimationID then
        SetCopySceneParam(scene, x040017_g_CSParamIdxCurStage, 1)
        SetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage, 0)
        TellPlayerAutoCombat(scene, true)
        x040017_Stage_0(scene, 1, 1)
        x040017_CreateMonster(scene, 1)
    end
end

--副本Tick
function x040017_OnCopySceneTick(scene, tickCount)
    local curStage = GetCopySceneParam(scene, x040017_g_CSParamIdxCurStage)
    local curSubStage = GetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage)
    SetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage, curSubStage + 1)
    if curStage == 0 then
        x040017_Stage_1(scene, curSubStage + 1)
        if curSubStage >= 20 then
            SetCopySceneParam(scene, x040017_g_CSParamIdxCurStage, 1)
            SetCopySceneParam(scene, x040017_g_CSParamIdxCurSubStage, 0)
            x040017_Stage_0(scene, 1, 1)
            x040017_CreateMonster(scene, 1)
        end
    end
end

function x040017_Stage_0(scene, stage, substage)
    if substage == 1 then
        OnCSMissionStage(scene, stage - 1)
        x040017_StageControl(scene, 0)
    end
end

function x040017_CreateMonster(scene, stage)
    if stage == 1 then
        local count = #x040017_g_MonstorList[stage]
        for i = 1, count do
            CreateMonster(scene, x040017_g_MonstorList[stage][i], x040017_g_MonstorPosList[stage][i][3], x040017_g_MonstorPosList[stage][i][1], x040017_g_MonstorPosList[stage][i][2])
        end
    end
end

--第1阶段 播放MOV
function x040017_Stage_1(scene, stage)
    if stage == 1 then
        TellPlayerAutoCombat(scene, false)
        StoryAnimation(scene, x040017_g_CSParamAnimationID)
    end
end