private bool PlayKeyFrame() { if (_lockStepKeyFrameDataList.Count == 0) { // 数据还未接收完毕,继续等待 return(false); } LockStepKeyFrameData lockStepKeyFrameData = _lockStepKeyFrameDataList.Values[0]; if (lockStepKeyFrameData.KeyFrame == _clientFrame) { foreach (LockStepClientMsgItem LockStepClientMsgItem in lockStepKeyFrameData.ReceiveMsgList) { if (LockStepClientMsgItem.msgId == MsgID.SteerPositionRsp) { SteerPositionRsp resp = (SteerPositionRsp)LockStepClientMsgItem.msg; GameEntity entity = _context.CreateEntity(); entity.AddPlayerId(resp.PlayerId); entity.AddSteerPosition(new Vector2(resp.X, resp.Y)); Log4U.LogDebug("LockStepClientMgr:FixedUpdate _clientFrame=", _clientFrame, " resp=", resp.ToString()); } } _lockStepKeyFrameDataList.RemoveAt(0); _nearstServerKeyFrame = _clientFrame + Config.SYN_RATE_SERVER; _gameController.Execute(); _clientFrame++; } return(true); }
private IEnumerator LoadConfig() { //读取config.txt //StreamReader reader = new StreamReader(Application.streamingAssetsPath + "/config.txt"); //string config = reader.ReadLine(); //reader.Close(); // 重新导入 ////AssetDatabase.ImportAsset("Assets/Resources/config.txt"); //TextAsset textAsset = Resources.Load<TextAsset>("config"); //string config = textAsset.text; WWW www = new WWW(Application.streamingAssetsPath + "/config.txt"); yield return(www); byte[] bytes = www.bytes; // BOM是“Byte Order Mark”标记文件的编码 EF BB BF UTF-8保存的文本有,ANSI无 byte[] configBytes = new byte[bytes.Length - 3]; Array.Copy(bytes, 3, configBytes, 0, configBytes.Length); ConfigJson configJson = JsonUtility.FromJson <ConfigJson>(System.Text.Encoding.Default.GetString(configBytes)); Config.IsServer = configJson.IsServer; Config.ServerAddress = configJson.ServerAddress; Config.PlayerId = configJson.PlayerId; Log4U.Init(); Log4U.LogDebug("IsServer=", configJson.IsServer); Log4U.LogDebug("ServerAddress=", configJson.ServerAddress); Log4U.LogDebug("PlayerId=", configJson.PlayerId); InitGame(); }
private void RecHandler(byte[] buf, ASynServerKcp serverKcp) { int playerId = BitConverter.ToInt32(buf, 0); serverKcp.PlayerId = playerId; MsgID msgId = (MsgID)BitConverter.ToInt32(buf, 4); MemoryStream stream = new MemoryStream(buf, 8, buf.Length - 8); IMessage msg = null; switch (msgId) { case MsgID.LoginReq: msg = LoginReq.Parser.ParseFrom(stream); break; case MsgID.LoginOutReq: msg = LoginOutReq.Parser.ParseFrom(stream); break; case MsgID.SteerPositionReq: //msg = SteerPositionReq.Descriptor.Parser.ParseFrom(stream); msg = SteerPositionReq.Parser.ParseFrom(stream); break; default: Log4U.LogDebug("SimulateServerUDP:RecHandler Not handler msgId=", msgId); break; } stream.Dispose(); Log4U.LogDebug("SimulateServerUDP:RecHandler<<<<<< playerId=", playerId, " msgId=", msgId, " msg=", msg); MessageDispatcher.GetInstance().DispatchMessageAsyn(msgId, msg, playerId); }
public void SendMsgToAll(MsgID msgId, IMessage msg) { Log4U.LogDebug("SimulateServerUDP:SendMsgToAll>>>>>> msgId=", msgId, " msg=", msg); byte[] msgIdBytes = BitConverter.GetBytes((int)msgId); byte[] msgBytes = msg.ToByteArray(); int bufferSize = msgIdBytes.Length + msgBytes.Length; byte[] sendBuf = new byte[bufferSize]; int offset = 0; Array.Copy(msgIdBytes, 0, sendBuf, offset, msgIdBytes.Length); offset += msgIdBytes.Length; Array.Copy(msgBytes, 0, sendBuf, offset, msgBytes.Length); _socket.SendToAll(sendBuf); }
private void FixedUpdate() { Log4U.LogDebug("LockStepClientMgr:FixedUpdate _clientFrame=", _clientFrame); if (_clientFrame + Config.QUICK_PLAY_GAP < _serverKeyFrame) { // 追平服务器 int targetFrame = Math.Min(_serverKeyFrame, _clientFrame + Config.QUICK_PLAY_SPEED_MAX); while (_clientFrame < targetFrame) { if (_clientFrame == _nearstServerKeyFrame) { // 关键帧 if (!PlayKeyFrame()) { break; } } else { // 非关键帧 _gameController.Execute(); _clientFrame++; } } } else { // 正常走 if (_clientFrame == _nearstServerKeyFrame) { // 关键帧 PlayKeyFrame(); } else if (_clientFrame < _nearstServerKeyFrame) { // 非关键帧 _gameController.Execute(); _clientFrame++; } } }
static private void MakeAtlas() { DirectoryInfo rootDirInfo = new DirectoryInfo(Application.dataPath + "/Raw/Image"); foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) { string prefabDirPath = dirInfo.FullName; prefabDirPath = prefabDirPath.Insert(prefabDirPath.IndexOf("Raw"), @"Resources\"); Log4U.LogInfo(prefabDirPath); if (!Directory.Exists(prefabDirPath)) { Directory.CreateDirectory(prefabDirPath); } foreach (FileInfo pngFile in dirInfo.GetFiles()) { if (pngFile.FullName.EndsWith("png") || pngFile.FullName.EndsWith("jpg")) { string allPath = pngFile.FullName; string assetPath = allPath.Substring(allPath.IndexOf("Assets")); string prefabPath = assetPath.Insert(assetPath.IndexOf("Raw"), @"Resources\"); prefabPath = prefabPath.Replace("png", "prefab"); prefabPath = prefabPath.Replace("jpg", "prefab"); prefabPath = prefabPath.Replace(@"\", @"/"); string allPrefabPath = Application.dataPath + "/" + prefabPath.Substring(prefabPath.LastIndexOf("Resources")); if (!File.Exists(allPrefabPath)) { Sprite sprite = Resources.LoadAssetAtPath <Sprite>(assetPath); GameObject go = new GameObject(sprite.name); go.AddComponent <SpriteRenderer>().sprite = sprite; PrefabUtility.CreatePrefab(prefabPath, go); GameObject.DestroyImmediate(go); } } } } }
public void Execute() { foreach (GameEntity entity in _moveGroup.GetEntities()) { if (!entity.move.isMove) { continue; } FixVec2 dirVec2 = entity.directionVec2.value; dirVec2 *= Config.Speed; FixVec2 oldPos; if (entity.hasPosition) { oldPos = entity.position.value; } else { oldPos = new FixVec2(); } entity.ReplacePosition(oldPos + dirVec2); Log4U.LogDebug("MoveSystem:Execute newPositionFixVec2=", oldPos + dirVec2); } }
public static IExtensible UnPack(ENetworkMessage networkMessage, int startIndex, int length, byte[] buffer) { IExtensible packet = null; try { using (MemoryStream streamForProto = new MemoryStream(buffer, startIndex, length)) { switch (networkMessage) { case ENetworkMessage.KEEP_ALIVE_SYNC: packet = Serializer.Deserialize <KeepAliveSync>(streamForProto); break; case ENetworkMessage.LOGIN_REQ: packet = Serializer.Deserialize <LoginReq>(streamForProto); break; case ENetworkMessage.LOGIN_RSP: packet = Serializer.Deserialize <LoginRsp>(streamForProto); break; case ENetworkMessage.GET_USERINFO_REQ: packet = Serializer.Deserialize <GetUserinfoReq>(streamForProto); break; case ENetworkMessage.GET_USERINFO_RSP: packet = Serializer.Deserialize <GetUserinfoRsp>(streamForProto); break; case ENetworkMessage.LOGOUT_REQ: packet = Serializer.Deserialize <LogoutReq>(streamForProto); break; case ENetworkMessage.LOGOUT_RSP: packet = Serializer.Deserialize <LogoutRsp>(streamForProto); break; case ENetworkMessage.OFFLINE_SYNC: packet = Serializer.Deserialize <OfflineSync>(streamForProto); break; case ENetworkMessage.GET_QUESTION_INFO_REQ: packet = Serializer.Deserialize <GetQuestionInfoReq>(streamForProto); break; case ENetworkMessage.GET_QUESTION_INFO_RSP: packet = Serializer.Deserialize <GetQuestionInfoRsp>(streamForProto); break; default: Log4U.LogInfo("No Such Packet, packet type is " + networkMessage); break; } } } catch (System.Exception ex) { Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source); } return(packet); }
private void InitGame() { Log4U.LogDebug("PreMain Start"); this.gameObject.AddComponent <Main>(); }
public void FixedUpdate() { Log4U.LogDebug("Main:FixedUpdate _fixUpdateFrame=", _fixUpdateFrame); _fixUpdateFrame++; }
public void Update() { Log4U.LogDebug("Main:Update _updateFrame=", _updateFrame); _updateFrame++; }