2025-01-04 23:00:19 +08:00

172 lines
6.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local EventAction_WaitingPlayer = {
WaitTime = 40;
WaitTeamPlayerNum = 2;
WaitFinishEvent = 0;
WaitFailureEvent = 0;
AtLeastWaitingTime = 10;
GameWillBeginWaitTime = 5;
}
-- 触发器激活时将执行Action的Execute
function EventAction_WaitingPlayer:Execute(...)
if not UGCGameSystem.IsServer() then return end
UGCGameSystem.GameState:SetGameStateType(CustomEnum.EGameState.Waiting)
self.DoOnceGameWillBegin = true;
self.EnterStateTime = UGCSystemLibrary.GetGameTime()
self.ModifiableWaitTime = self.WaitTime
self.LastUpdateRemainTime = self.WaitTime
self.JoinedPlayerNum = 0
-- 申请玩家加入
UGCGameSystem.GameState:UpdatePlayerJoin()
self.bEnableActionTick = true
return true
end
function EventAction_WaitingPlayer:Update(DeltaSeconds)
local CurrentRealTime = UGCSystemLibrary.GetGameTime();
local RemainTime = self.ModifiableWaitTime - (CurrentRealTime - self.EnterStateTime);
RemainTime = RemainTime >= 0 and RemainTime or 0
local CurrentUpdateRemainTime = math.ceil(RemainTime)
self.JoinedPlayerNum = #UGCGameSystem.GetAllPlayerState(false)
if self.LastUpdateRemainTime - CurrentUpdateRemainTime >= 1 then
self.LastUpdateRemainTime = CurrentUpdateRemainTime
end
local NowWaitTime = CurrentRealTime - self.EnterStateTime
-- 玩家等待时间即将结束,且满足最低开局要求,发送即将开局信息
if (self.DoOnceGameWillBegin and self.JoinedPlayerNum >= 2 and self.LastUpdateRemainTime < self.GameWillBeginWaitTime) then
self.DoOnceGameWillBegin = false
UGCGameSystem.GameState:GameWillBeginNotify()
self.EnterStateTime = UGCSystemLibrary.GetGameTime()
self.ModifiableWaitTime = self.LastUpdateRemainTime
end
-- 玩家满足直接开局要求,发送即将开局信息
if self.JoinedPlayerNum >= self.WaitTeamPlayerNum
and NowWaitTime >= self.AtLeastWaitingTime then
self.LastUpdateRemainTime = math.min(self.GameWillBeginWaitTime, self.LastUpdateRemainTime)
if self.DoOnceGameWillBegin then
self.DoOnceGameWillBegin = false
UGCGameSystem.GameState:GameWillBeginNotify()
self.EnterStateTime = UGCSystemLibrary.GetGameTime()
self.ModifiableWaitTime = self.LastUpdateRemainTime
end
-- 等待时间结束游戏开局
elseif CurrentUpdateRemainTime <= 0 then
if self.JoinedPlayerNum >= 2 then
UGCLogSystem.Log("[EventAction_WaitingPlayer_Update] BeginLoadMap")
-- self:AsyncLoadMap()
self:WaitPlayerJoinFinishPreload()
self.LastUpdateRemainTime = 0
UGCLogSystem.Log("[EventAction_WaitingPlayer_Update] WaitFinish")
else
self:WaitPlayJoinFinish(false)
end
self.bEnableActionTick = false
end
--更新GameState.WaitPlayerJoinTime
UGCGameSystem.GameState.WaitPlayerJoinTime = self.LastUpdateRemainTime
end
function EventAction_WaitingPlayer:WaitPlayerJoinFinishPreload()
self:RandomSelectVoteMap()
end
function EventAction_WaitingPlayer:RandomSelectVoteMap()
local PlayerSelectMapList = UGCGameSystem.GameState:GetPlayerSelectMapList()
local VoteMapTypes, VoteMapTypes_Set = {}, {}
for PlayerKey, MapType in pairs(PlayerSelectMapList) do
VoteMapTypes[#VoteMapTypes + 1] = MapType
if not table.hasValue(VoteMapTypes_Set, MapType) then
VoteMapTypes_Set[#VoteMapTypes_Set + 1] = MapType
end
end
if #VoteMapTypes_Set == 0 then
self:AsyncLoadMapFromType(MapConfig.MapType.Random)
elseif #VoteMapTypes_Set == 1 then
self:AsyncLoadMapFromType(VoteMapTypes_Set[1])
else
local RandomVoteMapIndex = math.random(1, #VoteMapTypes)
local RandomVoteMapType = VoteMapTypes[RandomVoteMapIndex]
UGCSendRPCSystem.RPCEvent(nil, EventEnum.RandomSelectVoteMap, VoteMapTypes, RandomVoteMapIndex)
UGCEventSystem.SetTimer(UGCGameSystem.GameState, function() self:AsyncLoadMapFromType(RandomVoteMapType) end, GlobalConfigs.GameSetting.RollMapTime)
end
end
function EventAction_WaitingPlayer:AsyncLoadMapFromType(MapType)
UGCGameSystem.GameState:SetMapSelectionResult(MapType)
local LoadMapType = UGCGameSystem.GameState:GetMapSelectionFinalResult()
--if MapType == MapConfig.MapType.Random then
-- local EnableMaps = UGCGameSystem.GameState:GetEnableMapExcludeRandom()
-- LoadMapType = table.Rand(EnableMaps)
--end
UGCLogSystem.Log("[EventAction_WaitingPlayer_AsyncLoadMapFromType] MapType:%s, LoadMapType:%s", tostring(MapType), tostring(LoadMapType))
local MapName = MapConfig.GetMultiModeMapInfo()[LoadMapType].MapName
local SpecialModeType = MapConfig.GetMultiModeMapInfo()[LoadMapType].SpecialModeType
if SpecialModeType and MapConfig.SpecialModeFunc[SpecialModeType] then
MapConfig.SpecialModeFunc[SpecialModeType](MapConfig.GetMultiModeMapInfo()[LoadMapType].SpecialModeParam)
end
UGCSendRPCSystem.RPCEvent(nil, EventEnum.LoadMap, MapType)
LevelStreamUtil.UnLoadStreamLevels(MapConfig.DefaultMaps, {Object = self, Func = self.UnLoadMapFinish}, false);
LevelStreamUtil.LoadStreamLevels({MapName}, {Object = self, Func = self.LoadMapFinish}, false);
end
function EventAction_WaitingPlayer:AsyncLoadMap()
local SelectMapType = UGCGameSystem.GameState:GetMapSelectionResult(true)
--- 实际地图若选择的类型为随机则返回随机出的地图否则与SelectMapType一致
local MapSelectionFinalType = UGCGameSystem.GameState:GetMapSelectionFinalResult()
local MapName = MapConfig.GetMultiModeMapInfo()[MapSelectionFinalType].MapName
local SpecialModeType = MapConfig.GetMultiModeMapInfo()[MapSelectionFinalType].SpecialModeType
if SpecialModeType and MapConfig.SpecialModeFunc[SpecialModeType] then
MapConfig.SpecialModeFunc[SpecialModeType](MapConfig.GetMultiModeMapInfo()[MapSelectionFinalType].SpecialModeParam)
end
UGCSendRPCSystem.RPCEvent(nil, EventEnum.LoadMap, SelectMapType)
LevelStreamUtil.UnLoadStreamLevels(MapConfig.DefaultMaps, {Object = self, Func = self.UnLoadMapFinish}, false);
LevelStreamUtil.LoadStreamLevels({MapName}, {Object = self, Func = self.LoadMapFinish}, false);
end
function EventAction_WaitingPlayer:LoadMapFinish()
-- 更新获取新场景的PlayerStart
-- UGCGameSystem.GameState:GetPlayerStart()
self:WaitPlayJoinFinish(true)
end
function EventAction_WaitingPlayer:UnLoadMapFinish(bWaitSucceed)
UGCLogSystem.Log("[EventAction_WaitingPlayer_UnLoadMapFinish] bWaitSucceed:%s", tostring(bWaitSucceed))
end
function EventAction_WaitingPlayer:WaitPlayJoinFinish(bWaitSucceed)
if bWaitSucceed then
UGCEventSystem.SendEvent(self.WaitFinishEvent)
else
UGCGameSystem.GameState:SetGameStateType(CustomEnum.EGameState.InsufficientNumberOfPeople)
UGCEventSystem.SendEvent(self.WaitFailureEvent)
end
end
return EventAction_WaitingPlayer