/// <summary> /// 执行每一帧 /// </summary> /// <param name="frameId"></param> /// <param name="frame"></param> private void ExecuteFrame(int frameId, FSPFrame frame) { if (frame != null && !frame.IsEmpty()) { for (int i = 0; i < frame.msgs.Count; i++) { var msg = frame.msgs[i]; switch (msg.cmd) { case FSPBasicCmd.GAME_BEGIN: Handle_GameBegin(msg.args[0]); break; case FSPBasicCmd.ROUND_BEGIN: Handle_RoundBegin(msg.args[0]); break; case FSPBasicCmd.CONTROL_START: Handle_ControlStart(msg.args[0]); break; case FSPBasicCmd.ROUND_END: Handle_RoundEnd(msg.args[0]); break; case FSPBasicCmd.GAME_END: Handle_GameEnd(msg.args[0]); break; case FSPBasicCmd.GAME_EXIT: Handle_GameExit(msg.playerId); break; } } } if (m_FrameListener != null) { m_FrameListener(frameId, frame); } }
//--------------------------------------------------------- /// <summary> /// 驱动游戏状态 /// </summary> public void EnterFrame() { for (int i = 0; i < m_ListPlayersExitOnNextFrame.Count; i++) { FSPPlayer player = m_ListPlayersExitOnNextFrame[i]; FSPServer.Instance.DelSession(player.Sid); player.Dispose(); } m_ListPlayersExitOnNextFrame.Clear(); //在这里处理状态 HandleGameState(); //经过上面状态处理之后,有可能状态还会发生变化 if (m_State == FSPGameState.None) { return; } if (m_LockedFrame.frameId != 0 || !m_LockedFrame.IsEmpty()) { //将当前帧扔给Player for (int i = 0; i < m_ListPlayer.Count; i++) { FSPPlayer player = m_ListPlayer[i]; player.SendToClient(m_LockedFrame); if (player.WaitForExit) { m_ListPlayersExitOnNextFrame.Add(player); m_ListPlayer.RemoveAt(i); --i; } } } //0帧每个循环需要额外清除掉再重新统计 if (m_LockedFrame.frameId == 0) { m_LockedFrame = new FSPFrame(); //防止GC if (UseDelayGC) { m_ListObjectsForDelayGC.Add(m_LockedFrame); } } //在这个阶段,帧号才会不停往上加 if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart) { m_CurFrameId++; m_LockedFrame = new FSPFrame(); m_LockedFrame.frameId = m_CurFrameId; //防止GC if (UseDelayGC) { m_ListObjectsForDelayGC.Add(m_LockedFrame); } } }
//======================================================================================= /// <summary> /// 驱动游戏状态 /// </summary> public void EnterFrame() { for (int i = 0; i < m_ListPlayersExitOnNextFrame.Count; i++) { var player = m_ListPlayersExitOnNextFrame[i]; player.Release(); } m_ListPlayersExitOnNextFrame.Clear(); //处理游戏状态切换 HandleGameState(); //经过上面状态处理之后,有可能状态还会发生变化 if (m_State == FSPGameState.None) { return; } if (m_LockedFrame.frameId != 0 || !m_LockedFrame.IsEmpty()) { //将当前帧扔级Player for (int i = 0; i < m_ListPlayer.Count; i++) { FSPPlayer player = m_ListPlayer[i]; player.SendToClient(m_LockedFrame); if (player.WaitForExit) { m_ListPlayersExitOnNextFrame.Add(player); m_ListPlayer.RemoveAt(i); --i; } } } //0帧每个循环需要额外清除掉再重新统计 if (m_LockedFrame.frameId == 0) { m_LockedFrame = new FSPFrame(); } //在这个阶段,帧号才会不停往上加 if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart) { m_CurFrameId++; m_LockedFrame = new FSPFrame(); m_LockedFrame.frameId = m_CurFrameId; } }
public void EnterFrame() { // 延迟移除,因为还要给些时间发包 for (int i = 0; i < m_ListPlayersExitOnNextFrame.Count; i++) { FSPPlayer player = m_ListPlayersExitOnNextFrame[i]; FSPServer.Instance.DelSession(player.Sid); player.Dispose(); } m_ListPlayersExitOnNextFrame.Clear(); // 处理状态变化 HandleGameState(); //经过上面状态处理之后,有可能状态还会发生变化 if (m_State == FSPGameState.None) { return; } if (m_LockedFrame.frameId != 0 || //已经在跑逻辑了 !m_LockedFrame.IsEmpty() //可能还在准备阶段 ) { for (int i = m_ListPlayer.Count - 1; i >= 0; i--) { FSPPlayer player = m_ListPlayer[i]; player.SendToClient(m_LockedFrame); if (player.WaitForExit) { m_ListPlayer.RemoveAt(i); m_ListPlayersExitOnNextFrame.Add(player); } } } //0帧每个循环需要额外清除掉再重新统计 if (m_LockedFrame.frameId == 0) { // 为了保留m_LockedFrame.vkeys,没有用Clear()来清除 m_LockedFrame = new FSPFrame(); m_FrameRecords.Add(m_LockedFrame); } //在这个阶段,帧号才会不停往上加 if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart) { m_CurFrameId++; m_LockedFrame = new FSPFrame(); m_LockedFrame.frameId = m_CurFrameId; m_FrameRecords.Add(m_LockedFrame); } }
//--------------------------------------------------------- /// <summary> /// enter frame /// </summary> public void EnterFrame() { //delete exited players for (int i = 0; i < mPlayersExitOnNextFrameList.Count; i++) { FSPPlayer player = mPlayersExitOnNextFrameList[i]; FSPServer.Instance.DelSession(player.Sid); player.Dispose(); } mPlayersExitOnNextFrameList.Clear(); //hand game state HandleGameState(); //check if game state changes if (mState == FSPGameState.None) { return; } if (mLockedFrame.frameId != 0 || !mLockedFrame.IsEmpty()) { //send frame to players for (int i = 0; i < mPlayerList.Count; i++) { FSPPlayer player = mPlayerList[i]; player.SendToClient(mLockedFrame); if (player.WaitForExit) { mPlayersExitOnNextFrameList.Add(player); mPlayerList.RemoveAt(i); --i; } } } //clear frame 0 in each iteration if (mLockedFrame.frameId == 0) { mLockedFrame = new FSPFrame(); //delay GC if (UseDelayGC) { mListObjectsForDelayGC.Add(mLockedFrame); } } //add up frame id if (mState == FSPGameState.RoundBegin || mState == FSPGameState.ControlStart) { mCurFrameId++; mLockedFrame = new FSPFrame(); mLockedFrame.frameId = mCurFrameId; //防止GC if (UseDelayGC) { mListObjectsForDelayGC.Add(mLockedFrame); } } }