--任务副本1
--进入副本→播放MOV→刷第一波小怪→击杀第一波小怪→刷BOSS→击杀BOSS→副本完成→传出副本

x040010_g_scriptId = 040010

--副本参数
x040010_g_CSParamIdxCurStage = 0			  --大阶段	
x040010_g_CSParamIdxCurSubStage = 1           --小阶段
x040010_g_CSParam1 = 2
x040010_g_CSParam2 = 3
x040010_g_CSParamAnimationID = 5033           --mov动画ID

x040010_g_MonstorList={}
x040010_g_MonstorList[1]={1002112,1002112,1002112,1002112,1002112,1002112,1002112,1002112,1002112,1002112}  --小怪
x040010_g_MonstorList[2]={1002113}                                  --BOSS


x040010_g_TargetList = {}                      --目标
x040010_g_TargetList[1]={1002112}  --第一阶段目标1
x040010_g_TargetList[2]={1002113}   --第2阶段目标1   
x040010_g_TargetList[3]={}

x040010_g_FirstMissionId = 1807	-- 第一个任务
x040010_g_SecMissionId = 1808 -- 第2个任务

x040010_g_Tickout = 7   -- 防止掉线的最小切换副本阶段时间戳，需要减2

x040010_g_MonstorPosList={}
x040010_g_MonstorPosList[1]={{63.14,65.45,-1.26},{64.91,65.45,-1.26},{63.99,66.37,-1.26},{63.02,67.22,-1.26},{65.02,67.26,-1.26},{69.6,67.26,-2.12},{67.6,67.22,-2.12},{68.57,66.37,-2.12},{69.49,65.45,-2.12},{67.72,65.45,-2.12}} --小怪坐标
x040010_g_MonstorPosList[2]={{66.9,72.07,-1.57}}

function x040010_OnCopySceneTimeOut(scene)
	ClearPlayer(scene)
end
function x040010_ClearAllMonster(scene)
	--print(count)
    --DeleMonsterByRoleId(scene,  x040010_g_TargetList[1][1])
    --DeleMonsterByRoleId(scene,  x040010_g_TargetList[2][1])
    DeleHostileMonster(scene)
end
function x040010_OnCopySceneOpen(scene)
	AddCSMission(scene,x040010_g_FirstMissionId)
	AddCSMission(scene,x040010_g_SecMissionId)
end

--死亡回调
function  x040010_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 x040010_OnObjEnter(scene, objId)
	SetCopySceneParam(scene, x040010_g_CSParamIdxCurStage, 0)
	SetCopySceneParam(scene, x040010_g_CSParamIdxCurSubStage, 0)
	SetCopySceneParam(scene, x040010_g_CSParam1, 0)
	SetCopySceneParam(scene, x040010_g_CSParam2, 0)
	x040010_ClearAllMonster(scene)
	SetCopySceneParam(scene, 7, 0)
	SynCsStageData(scene, objId, true)
--	AutoCombat(scene,objId,true)
end

function x040010_OnDieCountDown1(scene, rolebaseId,  stage)
	local TargetId = x040010_g_TargetList[stage+1][1]
	if(TargetId == rolebaseId) then
		local OldValue = GetCopySceneParam(scene, x040010_g_CSParam1)
		SetCopySceneParam(scene, x040010_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 x040010_OnObjDie( scene,objId,rolebaseId,killerId )
	local nCurStage = GetCopySceneParam(scene,x040010_g_CSParamIdxCurStage)
	local MonCount = GetHostileMonsterCount(scene)
	local oldStage = GetCopySceneParam(scene, 7)
	x040010_OnDieCountDown1(scene, rolebaseId, oldStage)
	if MonCount == 0 then
		if(rolebaseId == x040010_g_MonstorList[2][1]) then
			SetCopySceneSuccess(scene)
			x040010_StageControl(scene, 2)
		elseif rolebaseId ~= x040010_g_MonstorList[2][1] then
			x040010_Stage_2(scene, 2, 1)
		end
		SetCopySceneParam(scene,x040010_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x040010_g_CSParamIdxCurSubStage,0)
	end
end

--动画/对话回调
function x040010_OnStoryEventEnd(scene,nStoryId,nAnimationId)
	if nAnimationId == x040010_g_CSParamAnimationID then
		local nCurStage = GetCopySceneParam(scene,x040010_g_CSParamIdxCurStage)
		local MonCount = GetHostileMonsterCount(scene)
		if nCurStage == 0 then
			SetCopySceneParam(scene,x040010_g_CSParamIdxCurStage,1)
			SetCopySceneParam(scene,x040010_g_CSParamIdxCurSubStage,0)
			x040010_Stage_1(scene, 1, 1)
		end
	end
end

--副本Tick
function x040010_OnCopySceneTick(scene,tickCount)
	local curStage = GetCopySceneParam(scene,x040010_g_CSParamIdxCurStage)
	local curSubStage = GetCopySceneParam(scene,x040010_g_CSParamIdxCurSubStage)
	SetCopySceneParam(scene, x040010_g_CSParamIdxCurSubStage, curSubStage+1)
	if curStage == 0 then  
		x040010_Stage_0(scene, curStage, curSubStage)
		if curSubStage >= 20 then
			SetCopySceneParam(scene,x040010_g_CSParamIdxCurStage,1)
			SetCopySceneParam(scene,x040010_g_CSParamIdxCurSubStage,0)
			x040010_Stage_1(scene, 1, 1)
		end
	end
end

--第0阶段 播放MOV 刷第一波小怪
function x040010_Stage_0(scene ,stage,substage)
	local nCurStage = GetCopySceneParam(scene,x040010_g_CSParamIdxCurStage)
	if substage == 1 then
		StoryAnimation(scene, x040010_g_CSParamAnimationID)
	end
end

--第1阶段 刷小怪
function x040010_Stage_1(scene ,stage,substage)
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		TellPlayerAutoCombat(scene)
		x040010_StageControl(scene, 0)
		local count = #x040010_g_MonstorList[1]
		for i = 1, count do
			CreateMonster(scene, x040010_g_MonstorList[1][i], x040010_g_MonstorPosList[1][i][3], x040010_g_MonstorPosList[1][i][1], x040010_g_MonstorPosList[1][i][2])
		end
	end
end

--第2阶段 刷BOSS
function x040010_Stage_2(scene ,stage,substage)
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		TellPlayerAutoCombat(scene)
		x040010_StageControl(scene, 1)
		CreateMonster(scene, x040010_g_MonstorList[2][1], x040010_g_MonstorPosList[2][1][3], x040010_g_MonstorPosList[2][1][1], x040010_g_MonstorPosList[2][1][2])
	end
end

