x020002_g_scriptId=020001

x020002_g_MonsterPaoPaoList={}		--文字泡内容strDictionaryＩＤ
x020002_g_MonsterPaoPaoList[5]={5001,5002,5003,5004,5005}				
x020002_g_MonsterPaoPaoList[7]={5006,5007,5008,5009,5010}
x020002_g_MonsterPaoPaoList[11]={5011,5012,5013}
x020002_g_MonsterPaoPaoList[13]={5014}
x020002_g_MonsterPaoPaoList[15]={5015,5016,5017}

x020002_g_MonsterIdList={}					--rolebaseID所有空置的也是必须要有的。
x020002_g_MonsterIdList[0]={5021,5022,5023,5024,5025}		--吃瓜群众吃瓜群众

x020002_g_CSParamIdxCurStage=0				--阵营状态
x020002_g_CSParamIdxCurSubStage=1

x020002_g_CSParamIdxMonsterIndex=2
x020002_g_CSParamIdxStoryIndex=3
x020002_g_CSParamIdxAnimationIndex=4

x020002_g_NpcAnimation =815					--NPC欢呼动作

x020002_g_BuffPos={}						--buff出生位置坐标XZ角度
x020002_g_BuffPos[1]={361.36,192}
x020002_g_BuffPos[2]={373,200}
x020002_g_BuffPos[3]={365.2,208.2}			

x020002_g_BuffIdLst={100,101,102}			--BuffID，grouppointID

--副本Tick方法，用于流程控制
function x020002_OnCopySceneTick(scene,tickCount)					
	local curStage = GetCopySceneParam(scene,x020002_g_CSParamIdxCurStage) +1
	local curSubStage = GetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage) +1

	SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,curSubStage)

	if curStage == 1 then
		x020002_Stage_1(scene,curStage,curSubStage)
	elseif curStage == 2 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 3 then
		x020002_Stage_2(scene,curStage,curSubStage)
	elseif curStage == 4 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 5 then
		x020002_Stage_3(scene,curStage,curSubStage)
	elseif curStage == 6 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 7 then
		x020002_Stage_4(scene,curStage,curSubStage)
	elseif curStage == 8 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 9 then
		x020002_Stage_5(scene,curStage,curSubStage)
	elseif curStage == 10 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 11 then
		x020002_Stage_6(scene,curStage,curSubStage)
	elseif curStage == 12 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 13 then
		x020002_Stage_7(scene,curStage,curSubStage)
	elseif curStage == 14 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 15 then
		x020002_Stage_8(scene,curStage,curSubStage)
	elseif curStage == 16 then
		x020002_CommonStage(scene ,curStage, curSubStage)
	elseif curStage == 17 then
		x020002_Stage_End(scene ,curStage, curSubStage)
	else
		SetCopySceneSuccess(scene)
	end									
end

--共同阶段：创建怪物，剧情对话
function x020002_CommonStage(scene ,stage, substage)                    
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		x020002_CreateMonster(scene,stage,1)
	elseif substage == 2 then
		x020002_StartClienStory(scene)
	elseif substage == 4 then
		local nMonsterIndex = GetCopySceneParam(scene,x020002_g_CSParamIdxMonsterIndex) - 1
		SetNpcCampByStory(scene, nMonsterIndex)
	end
end

--奖励结算
function x02002_Succeed(scene)
	AccoutByStory(scene)
end

--第一阶段：创建吃瓜群众与播放开始剧情动画
function x020002_Stage_1(scene ,stage,substage)                         
	if substage == 1 then
		x020002_CreateMonster(scene,0,1)
		x020002_StartStoryAnimation(scene)
	elseif substage == 18 then
		x020002_StartClienStory(scene)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第二阶段：NPC呼唤动作，剧情对话，
function x020002_Stage_2(scene ,stage,substage)                         
	if(substage == 1) then
		x020002_HuanHuAnimation(scene)
	elseif substage == 2 then
		x020002_StartClienStory(scene)
	elseif substage == 4 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第三阶段：NPC呼喊，对话泡泡，情景对话
function x020002_Stage_3(scene ,stage,substage)                         --NPC欢呼连续文字跑并刷出（楚尘
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_MonsterPaoPaoList[stage][1])
	elseif substage == 2 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_MonsterPaoPaoList[stage][2])
	elseif substage == 3 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_MonsterPaoPaoList[stage][3])
	elseif substage == 4 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][4],x020002_g_MonsterPaoPaoList[stage][4])
	elseif substage == 5 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][5],x020002_g_MonsterPaoPaoList[stage][5])						
	elseif substage == 6 then

		x020002_StartClienStory(scene)
	elseif substage == 8 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第四阶段：NPC泡泡，情景对话
function x020002_Stage_4(scene ,stage,substage)							--NPC欢呼连续文字泡并刷出（帝星学院弟子
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_MonsterPaoPaoList[stage][1])
	elseif substage == 2 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_MonsterPaoPaoList[stage][2])
	elseif substage == 3 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_MonsterPaoPaoList[stage][3])
	elseif substage == 4 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][4],x020002_g_MonsterPaoPaoList[stage][4])
	elseif substage == 5 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][5],x020002_g_MonsterPaoPaoList[stage][5])						
	elseif substage == 6 then

		x020002_StartClienStory(scene)
	elseif substage == 8 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第五阶段：NPC呼喊，情景对话
function x020002_Stage_5(scene ,stage,substage)							--NPC欢呼并刷出（欧辰
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
	elseif substage == 2 then

		x020002_StartClienStory(scene)
	elseif substage == 4 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第六阶段：NPC泡泡，情景对话
function x020002_Stage_6(scene ,stage,substage)							--NPC欢呼连续文字泡并刷出（皇家学院弟子
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_MonsterPaoPaoList[stage][1])
	elseif substage == 2 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_MonsterPaoPaoList[stage][2])
	elseif substage == 3 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_MonsterPaoPaoList[stage][3])					
	elseif substage == 4 then

		x020002_StartClienStory(scene)
	elseif substage == 6 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第七阶段：NPC呼喊，泡泡，情景对话
function x020002_Stage_7(scene ,stage,substage)							--NPC欢呼连续文字泡并刷出（司空明月
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_MonsterPaoPaoList[stage][1])
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_MonsterPaoPaoList[stage][1])	
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_MonsterPaoPaoList[stage][1])					
	elseif substage == 2 then

		x020002_StartClienStory(scene)
	elseif substage == 4 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--第八阶段：NPC呼喊，泡泡，情景对话
function x020002_Stage_8(scene ,stage,substage)							--NPC欢呼连续文字泡并刷出（洛千秋
	if substage == 1 then
		x020002_HuanHuAnimation(scene)
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_MonsterPaoPaoList[stage][1])
	elseif substage == 2 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_MonsterPaoPaoList[stage][2])
	elseif substage == 3 then
		NpcPaoPaoByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_MonsterPaoPaoList[stage][3])					
	elseif substage == 4 then

		x020002_StartClienStory(scene)
	elseif substage == 6 then
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

--NPC死亡回调，用于阶段监测以及转换阶段
function x020002_OnObjDie( scene,objId,rolebaseId,killerId )					

	local MonCount = GetHostileMonsterCount(scene)
	if(MonCount == 0) then
		local nCurStage = GetCopySceneParam(scene,x020002_g_CSParamIdxCurStage)

		if nCurStage % 15 == 0 then
			local nUserCount = GetSceneUserCount(scene)
			for i=0,nUserCount-1 do	
				local nUserObjId = GetUserObjIdByIdx(scene,i)	
				x020002_CheckMissionMD(scene, nUserObjId, 61)
				local BossTime = GetMissionMD(scene, nUserObjId, 1, 61)

				if BossTime % 100 == 0 then                                      --只有第一次获得奖励
					CreatDropBox(scene,nUserObjId, 100168,objId)                       --BOSS奖励包
					SetMissionMD(scene, nUserObjId, 1, 61, BossTime+1)
				end
			end
		end
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurSubStage,0)
	end
end

function x020002_Stage_End(scene ,stage,substage)
	if substage == 1 then
		x020002_StartStoryAnimation(scene)
	elseif substage == 11 then
		x02002_Succeed(scene)
		SetCopySceneParam(scene,x020002_g_CSParamIdxCurStage,stage+1)

		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do	
			local nUserObjId = GetUserObjIdByIdx(scene,i)	
			AddActivtyNum(scene, nUserObjId, 9)
		end
	end
end

--创建怪物
function x020002_CreateMonster( scene,idx,camp)							--刷怪（
	local nMonsterIndex = GetCopySceneParam(scene,x020002_g_CSParamIdxMonsterIndex)
	CreateMonsterByStory(scene, nMonsterIndex)
	SetCopySceneParam(scene,x020002_g_CSParamIdxMonsterIndex,nMonsterIndex+1)
end

function x020002_StartClienStory(scene)
	local nStoryIndex = GetCopySceneParam(scene,x020002_g_CSParamIdxStoryIndex)
	StartClientStoryByStory(scene, nStoryIndex);
	SetCopySceneParam(scene,x020002_g_CSParamIdxStoryIndex, nStoryIndex+1)
end

function x020002_StartStoryAnimation(scene)
	local nAnimationIndex = GetCopySceneParam(scene,x020002_g_CSParamIdxAnimationIndex)
	StartStoryAnimationByStory(scene, nAnimationIndex)
	SetCopySceneParam(scene,x020002_g_CSParamIdxAnimationIndex, nAnimationIndex+1)
end

--NPC呼喊动作
function x020002_HuanHuAnimation( scene )								--NPC动作
	NpcAnimationByRBId(scene,x020002_g_MonsterIdList[0][1],x020002_g_NpcAnimation)
	NpcAnimationByRBId(scene,x020002_g_MonsterIdList[0][2],x020002_g_NpcAnimation)
	NpcAnimationByRBId(scene,x020002_g_MonsterIdList[0][3],x020002_g_NpcAnimation)
	NpcAnimationByRBId(scene,x020002_g_MonsterIdList[0][4],x020002_g_NpcAnimation)
	NpcAnimationByRBId(scene,x020002_g_MonsterIdList[0][5],x020002_g_NpcAnimation)
end

--重置玩家位置和朝向
function x020002_SetPos (scene)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do		
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		SetPos(scene, nUserObjId, 362.67, 199.45 , 78)
	end
end

function x020002_CheckMissionMD(scene ,objId, type)
	local nLastData = GetMissionMD(scene, objId, 1, type)
	local nNewData = CheckRefreshTime(scene, nLastData, objId, 0)
	if math.floor(nLastData/100) ~= nNewData then
		SetMissionMD(scene, objId, 1, type, nNewData*100 + nLastData%100)
	end
end

--副本任务
function x020002_OnCopySceneOpen( scene )
	AddCSMissionByStiry(scene)
end                    

