public void OnMessage(MessageBuffer msg) { int cproto = msg.ReadInt(); Debug.Log(cproto); switch (cproto) { case cProto.CONNECT: // SceneManager.instance.InitViewMap(); int roleId = msg.ReadInt(); // SceneManager.instance.viewMap.CreateMe(roleId); SceneManager.instance.viewMap.LogicMap.curRoleId = roleId; Debug.Log("玩家,id = " + roleId); break; case cProto.READY: break; case cProto.SYNC_POS: int cRoleId = msg.ReadInt(); string pos = msg.ReadString(); SceneManager.instance.viewMap.SyncPos(cRoleId, pos); Debug.Log(string.Format("玩家 {0} ,pos = {1}", cRoleId ,pos)); break; case cProto.SYNC_KEY: int servFrameCount = msg.ReadInt(); if (servFrameCount >= SceneManager.instance.viewMap.LogicMap.curFrameCount) { string keyStr = msg.ReadString(); string[] keyData = keyStr.Split(';'); for (int i = 0; i < keyData.Length; ++i ) { if(keyData[i] == "") { continue; } KeyData data = new KeyData(keyData[i]); SceneManager.instance.viewMap.LogicMap.DoCmd(data); } SceneManager.instance.viewMap.LogicMap.curFrameCount += 1; } break; case cProto.START: string players = msg.ReadString(); SceneManager.instance.viewMap.CreateAllPlayer(players); TimerHeap.AddTimer(0, 50, SycMePos); TimerHeap.AddTimer(0, 100, SycKey); break; } }
/// <summary> /// 发送消息 /// </summary> /// <param name="msgBuf"></param> private void Send(MessageBuffer msgBuf) { if (client.Connected) { client.Send(msgBuf); } }
/// <summary> /// 发送消息 /// </summary> /// <param name="cproto"></param> /// <param name="msg"></param> private void Send(int cproto, string msg) { MessageBuffer msgBuf = new MessageBuffer(); msgBuf.WriteInt(cproto); msgBuf.WriteString(msg); if (client.Connected) { client.Send(msgBuf); } }
/// <summary> /// 同步自己的位置 /// </summary> public void SycMePos() { Vector3 pos = SceneManager.instance.viewMap.CurViewObj.Pos; Vector3 angle = SceneManager.instance.viewMap.CurViewObj.EulerAngles; MessageBuffer msgBuf = new MessageBuffer(); msgBuf.WriteInt(cProto.SYNC_POS); msgBuf.WriteInt(SceneManager.instance.viewMap.CurViewObj.Id); string cPos = string.Format("{0}#{1}#{2}#{3}#{4}#{5}", pos.x, pos.y, pos.z, angle.x, angle.y, angle.z); msgBuf.WriteString(cPos); Send(msgBuf); }
/// <summary> /// 发送关键帧 /// </summary> public void SycKey() { string keyStr = string.Join(";", keyPack.ToArray()); MessageBuffer msgBuf = new MessageBuffer(); msgBuf.WriteInt(cProto.SYNC_KEY); msgBuf.WriteInt(SceneManager.instance.viewMap.LogicMap.curFrameCount); msgBuf.WriteString(keyStr); Send(msgBuf); ClearKeyPack(); }
/// <summary> /// 客户端准备 /// </summary> public void Ready() { MessageBuffer msgBuf = new MessageBuffer(); msgBuf.WriteInt(cProto.READY); msgBuf.WriteInt(SceneManager.instance.viewMap.LogicMap.curRoleId); Send(msgBuf); }
public void Send(MessageBuffer msg) { outMessages.Add(msg); }