Reworld------《炸弹归你了》

--服务器管理
--主要对加入游戏的玩家统一进行管理
depositDie = {} --存放死亡玩家的表
depositbeing = {} --存放现在场景里还存活的玩家
playServer={}
MinPlayerNum=2

local actionbool = true
local GOAction = true

RoundBegin=false		--游戏战局开始
--用于结束游戏方法中的变量
local firstBeginGame=true

--设置时间种子
math.randomseed(tostring(os.time()):reverse():sub(1, 7)) 

--全局变量,判断是否处于游戏中
--当一局结束后要将其设置成false
GamePlaying=WorkSpace.GamePlaying 
--游戏开始倒计时协程引用
local Timefunc=nil

--房间已经开始过运行开关,防止后期多次通过玩家加入调用开始事件
local SceneOpen=false
WorkSpace.AudioSoces.圣诞背景音:Play()
WorkSpace.AudioSoces.圣诞背景音.IsLoop=true

--存储场景中的全部玩家(列表中的)
--local _PlayersTable={}
--炸弹检测协程==炸弹存在退出
BombDetectExist_Coroutine=coroutine.create(function()
		while(1)do
			local players=Players:GetAllPlayer()
			for k,v in ipairs(players) do
				if v.Avatar~=nil then
					if v.Avatar.炸弹~=nil then
						coroutine.yield()
					end
					
				else
					MessageEvent.FireAllClient("Log","v.Avatar==nil================================================BombDetectExist_Coroutine")
				end
				
			end
			
		end
		
	end)
--炸弹检测协程==炸弹生成退出
BombDetectCreate_Coroutine=coroutine.create(function()
		while(1)do
			local boomCloneObj=WorkSpace.Temp.炸弹			
			if boomCloneObj~=nil then
--				return true	
				coroutine.yield()
				
			else
				MessageEvent.FireAllClient("Log","boomCloneObj==nil================================================BombDetectCreate_Coroutine")
			end	
		end
		
		end)
--游戏开局倒计时
local function GameTimeMng(time)
	if GamePlaying.Value==false then
		collectgarbage("collect")
		--print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Count1:"..tostring(collectgarbage("count")))
		--打开游玩开关
		GamePlaying.Value=true
--		Timefunc=coroutine.start(function()

			MessageEvent.FireAllClient("GameTime",time,false)
			
			--将所有玩家传送到游戏场地
			--随机选择人员生成炸弹
			if #depositbeing>1 then
					local player=Players:GetPlayerByUserId(depositbeing[math.random(1,#depositbeing)])
					if player~=nil and player.Avatar~=nil and player.玩家脚本~=nil then
						--生成炸弹人物客户端的炸弹标识打开
						player.玩家脚本.ClientState.炸弹标识.Value=true
						BoomMng.Create(player.Avatar)
						MessageEvent.FireAllClient("BoomOpenHint")
						MessageEvent.FireAllClient("Log","BoomMng.Create(player.Avatar)=============================================GameTimeMng(3)")
					else
						MessageEvent.FireAllClient("Log","Player is nil================================================GameTimeMng(3)")
						
					end
			else
				MessageEvent.FireAllClient("Log","#depositbeing<=1==========================finishGame()======================GameTimeMng(3)")
				finishGame()
			end
--		end)
	else
		MessageEvent.FireAllClient("Log","GamePlaying.Value==true================================================GameTimeMng(3)")
	end
end
Players.PlayerAdded:Connect(function(Uid)
	local ren= Players:GetPlayerByUserId(Uid)
	ren.AvatarAdded:Connect(function()
			ren.Avatar.onAvatarDead:Connect(function()
			print("OnAvatarDead")
		end)
		MessageEvent.FireClient(ren.Avatar.PlayerId,"OpenSeleteMapCountDown",true)
		ren.Avatar.动作.站立.LocalClipId = "stand"
		ren.Avatar.动作.站立.Action = Enum.AnimationType.stand
		ren.Avatar.动作.站立.Loop = true
		table.insert(playServer,ren)
		--注册玩家碰撞事件

		ren.Avatar.TriggerEnter:Connect(function(other)
			
			if other:IsClass("Avatar") and GamePlaying.Value then
				PlayerAction()
				--按照逻辑来说v.Avatar.Name应该是自身的名字,但是v.Avatar.Name是碰撞的物体名字
				--other.Name是自身的名字
				--这里考虑一个问题,因为每个角色都注册了事件,所以碰撞的时候角色双方的碰撞事件都会触发
				--相互触发下可能是造成这一现象的主要原因
				--判断身上是否有炸弹,如果有的话将炸弹给予给另外的人
				MessageEvent.FireAllClient("Log","start================================================ren.Avatar.TriggerEnter")
				--如果自身没有炸弹,
				if other.炸弹==nil then
					MessageEvent.FireAllClient("Log","other.炸弹==nil===============================================1=ren.Avatar.TriggerEnter")
					return 
				end
				--如果碰撞的玩家有炸弹
				if ren.Avatar.炸弹~=nil then
					MessageEvent.FireAllClient("Log","ren.Avatar.炸弹~=nil================================================ren.Avatar.TriggerEnter")
					return 
				end
				--如果自身的炸弹冷却中
				if other.炸弹.冷却.Value==false  then 
					MessageEvent.FireAllClient("Log","other.炸弹.冷却.Value==false================================================ren.Avatar.TriggerEnter")
					return 
				end
				if ren.玩家脚本.ClientState.无敌.Value == true then
					MessageEvent.FireAllClient("Log","ren.玩家脚本.ClientState.无敌.Value == true================================================ren.Avatar.TriggerEnter")
					return
				end
				--以上不进行任何操作
				
				--打印炸弹传递给的人
--				MessageEvent.FireClient(other.PlayerId,"Test",ren.Avatar.Name)
--				coroutine.start(function()
					--将炸弹冷却关闭
					if other.炸弹~= nil then
						MessageEvent.FireAllClient("Log","other.炸弹~= nil================================================ren.Avatar.TriggerEnter")
						other.炸弹.冷却.Value=false
						--炸弹传递给另外的一个人
						other.炸弹.Parent=WorkSpace[ren.Avatar.Name]
						WorkSpace[ren.Avatar.Name].MoveSpeed=8
						other.MoveSpeed=4
						--自己客户端的炸弹标识关闭,拥有炸弹人客户端的炸弹标识打开
						Players:GetPlayerByUserId(other.PlayerId).玩家脚本.ClientState.炸弹标识.Value=false
						Players:GetPlayerByUserId(ren.Avatar.PlayerId).玩家脚本.ClientState.炸弹标识.Value=true
						wait(1)
						--炸弹传递过了,要在被传递的人身上找
						if WorkSpace[ren.Avatar.Name].炸弹~=nil then
							WorkSpace[ren.Avatar.Name].炸弹.冷却.Value=true
							MessageEvent.FireAllClient("Log","WorkSpace[ren.Avatar.Name].炸弹~=nil================================================ren.Avatar.TriggerEnter")
						else
							MessageEvent.FireAllClient("Log","WorkSpace[ren.Avatar.Name].炸弹==nil================================================ren.Avatar.TriggerEnter")
						end
						PlayerAction()
					else
						MessageEvent.FireAllClient("Log","other.炸弹== nil===========================================2=====ren.Avatar.TriggerEnter")
					end
					
--				end)
			end
		end)
	end)
	ren.AvatarRemoving:Connect(function()
			print("AvatarRemoving")
		end)
	
end)


--function testPlayer()
--	coroutine.start(function()
--	local s=Players:GetAllPlayers()
--	local n=""
--	for k,v in pairs(s) do
--		print(tostring(v.Name))
--		n=n..tostring(v.Name)
--	end
--	MessageEvent.FireClient(s[1].PlayerId,"Test",n)

--	coroutine.wait(2)
--		testPlayer()
--	end)
--end
  
----当有玩家离开游戏后,更新一下列表信息
--MessageEvent.ServerEventCallBack("UpdatePlayers"):Connect(function()
--	_PlayersTable={}
--	_PlayersTable=Players:GetAllPlayers()
--end)


--function generateBomd()
--	local players=Players:GetAllPlayers()
--	local player=players[math.random(1,#players)]
--	if player ~= nil then
--		--生成炸弹人物客户端的炸弹标识打开
--		player.玩家脚本.ClientState.炸弹标识.Value=true
--		BoomMng.Create(player.Avatar)
--		MessageEvent.FireAllClient("RestBoomTime")
--		print("发送重置消息")
--	else
--		generateBomd()
--	end
--end

----------------------------------------------------------------------------------------
--第一次开始游戏
function SetFirstGame()
	local players=Players:GetAllPlayers()
	local avatartb={}
	for k,v in ipairs(players)do
		if v.Avatar~=nil then
			table.insert(avatartb,v)
		end
		
	end
--	print("SetFirstGame")
	if #avatartb>=MinPlayerNum then		
		MessageEvent.FireAllClient("OutPutVoteCount",0)
		GameRun.Update:DisConnect(SetFirstGame)
		BeginChooseMap()
		RoundBegin=true
	end
	if #depositbeing<=1 then
		MessageEvent.FireAllClient("OpenPlayerLackHint")
	else
		MessageEvent.FireAllClient("ClosePlayerLackHint")
	end
end
GameRun.Update:Connect(SetFirstGame)

--重开游戏
function RestartGame()
	print("RestartGame")
	local players=Players:GetAllPlayers()
	local avatartb={}
	for k,v in ipairs(players)do
		if v.Avatar~=nil then
			table.insert(avatartb,v)
		end
		
	end
	if #avatartb>=MinPlayerNum and #depositbeing>1 then
		local StartGAME = WorkSpace.AudioSoces.炸弹生成
		StartGAME:Play()
		--开启游戏倒计时
		if RoundBegin then
			GamePlaying.Value=false
			GameTimeMng(3)
			MessageEvent.FireAllClient("Log","RoundBegin==true================================================RestartGame()")
		else
			MessageEvent.FireAllClient("Log","RoundBegin==false================================================RestartGame()")
		end
		print("restart game true")
		return true
	else
		MessageEvent.FireAllClient("Log","#avatartb>=MinPlayerNum and #depositbeing>1 false================================================RestartGame()")
	end
	print("restart game false")
	return false
end

function finishGame() --结束
	local str=#depositbeing.."================================================1finishGame()"
	MessageEvent.FireAllClient("Log",str)
	
	for key,value in ipairs(depositbeing)do
		print(value)
	end
	table_sort(depositbeing)
	table_sort(depositDie)
		SetRank()
	str=#depositbeing.."================================================2finishGame()"
	MessageEvent.FireAllClient("Log",str)
	if #depositbeing <= 1 then
		GameRun.Update:DisConnect(BombtimeCount_update)
		if #depositbeing == 1 then
			GamePlaying.Value=false
			RoundBegin=false
			for k,v in ipairs(depositbeing) do
				local player=Players:GetPlayerByUserId(depositbeing[1])
				MessageEvent.FireClient(player.PlayerId,"closeanaccount")
				if player.Avatar~=nil then
					local bomb=player.Avatar.炸弹
					if bomb~=nil then
						bomb:Destroy()
					end
				end
				MessageEvent.FireAllClient("BoomCloseHint") --场景里剩单个玩家的时候 炸弹删除 炸弹倒计时关闭
				MessageEvent.FireAllClient("GameTimeOver")
				local key="VictoryNum"
				local value=1
				if PlayerStoreData:GetValue(v,key) then
					value=PlayerStoreData:GetValue(v,key)
					value=value+1
				 
				else
					value=1
				end
				PlayerStoreData:SetValue(v,key,value)
				if player.Avatar~=nil then
				player.Avatar.Position=WorkSpace.出生点.Position
					if player.Avatar.force~=nil then
						player.Avatar.force:Destroy()
					end
						
				end
			end
		elseif #depositbeing == 0 then
			GamePlaying.Value=false
			RoundBegin=false
			MessageEvent.FireAllClient("BoomCloseHint") --场景里剩单个玩家的时候 炸弹删除 炸弹倒计时关闭
			MessageEvent.FireAllClient("GameTimeOver")
		end
		for allkey,allvalue in ipairs(Players:GetAllPlayer())do
			for lkey,lvalue in ipairs(depositDie)do
				if lvalue==allvalue then
					table.remove(depositDie,lkey)
				end
			end
			
			for dkey,dvalue in ipairs(depositbeing)do
				if dvalue==allvalue then
					isexist=true
				end
			end
			if not isexist then
				table.insert(depositbeing,allvalue.Uid)
			end
			--allvalue.Avatar.Position=WorkSpace.出生点.Position
			if allvalue.玩家脚本~=nil then
				allvalue.玩家脚本.ClientState.选关索引.Value=0
			end
		end
		   
		   
		wait(6)
--		for allkey,allvalue in ipairs(Players:GetAllPlayer())do
--			if allvalue.Avatar~=nil then
--				allvalue.Avatar.Position=WorkSpace.出生点.Position
--				print("allvalue.Avatar.Position")
--			end
--		end
--		print("prepering delete map")
		DeleteMap()
		print("DeleteMap")
		MessageEvent.FireAllClient("OutPutVoteCount",0)   
		wait(0.1)
		print("waiting for begin ------------------------------------------------------")
		--   goRefresh=true
		MessageEvent.FireAllClient("Log","BeginChooseMap()================================================finishGame()")
		BeginChooseMap()
		
	else
		
		local result=RestartGame()
		if not result then
			finishGame()
			MessageEvent.FireAllClient("Log","RestartGame() failed================================================finishGame()")
		else
			MessageEvent.FireAllClient("Log","RestartGame() success================================================finishGame()")
		end
		
	end
end
MessageEvent.ServerEventCallBack("finishGame"):Connect(finishGame)

MessageEvent.ServerEventCallBack("CreatePlayerNameBtn"):Connect(function(uid)
	local playerCount = #depositbeing
	local name={}
	local idlist={}
	for k,v in ipairs(depositbeing) do
		local player=Players:GetPlayerByUserId(v)
		table.insert(name,player.NickName)
		table.insert(idlist,v)
	end
	MessageEvent.FireClient(uid,"ReturnPLAYERcount",playerCount,name,idlist)
end)


function PlayerAction()
	local players = Players:GetAllPlayers()
		for k,v in ipairs(players) do
		if v.Avatar ~= nil and  v.Avatar.炸弹 ~= nil and v.Avatar.动作~=nil then
			v.Avatar.动作..LocalClipId = "chidaopao"
			v.Avatar.动作.站立.LocalClipId = "danshouchijian"
		elseif v.Avatar ~= nil and  v.Avatar.炸弹 == nil and v.Avatar.动作~=nil then
			v.Avatar.动作..LocalClipId = "run"
		end
	end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值