public void OnRespawnPlayer(MessageRespawnPlayer message) { foreach (ClientKeys key in Clients.Keys) { if (key.NetworkID == message.NetworkID) { if (!IsBossAlive) { message.UnitID = 4; message.Position = GameObject.FindGameObjectWithTag(Tags.BossSpawnPoint).transform.position; IsBossAlive = true; } Clients [key].State.IsAlive = true; UnitStates states = UnitsManager.Instance.GetUnitStatsByID(message.UnitID); Clients [key].State.Stats = states; Clients [key].State.Stats.PerHP = states.PerHP; Clients [key].State.Stats.PerLife = states.PerLife; message.States = states; //开始广播 string s = JsonUtility.ToJson(message); BroadcastingMessage(s); return; } } //没有对应的玩家 Debug.LogError("ID为" + message.NetworkID + "的玩家不存在!该玩家重生请求被拒绝,请检查脚本有没有写错!"); }
public void RespawnRequest(int ID) { MessageRespawnPlayer respawn = new MessageRespawnPlayer(); respawn.States = UnitsManager.Instance.GetUnitStatsByID(ID); respawn.NetworkID = MyID; respawn.UnitID = ID; respawn.Position = UnityEngine.Random.onUnitSphere + GameObject.FindGameObjectWithTag(Tags.AlliesSpawnPoint).transform.position; respawn.Rotation = Vector3.zero; SendMessage(respawn); }
public void OnRespawnPlayer(MessageRespawnPlayer message) { //TODOnetworkID来锁定目标,UnitID来确定复活的单位 //判断ID是否为自己 if (MyID == message.NetworkID && clients [message.NetworkID].NetworkStates.IsAlive == false) { GameObject go = GameObject.Instantiate(UnitsManager.Instance.GetUnitObjectByID(message.UnitID, true), message.Position, Quaternion.Euler(message.Rotation)); clients [message.NetworkID].NetworkStates.IsAlive = true; clients [message.NetworkID].unit = go.GetComponent <Unit> (); clients [message.NetworkID].unit.States = message.States; clients [message.NetworkID].unit.States.PerHP = message.States.MaxHP; clients [message.NetworkID].unit.States.PerLife = message.States.MaxLife; } else if (clients [message.NetworkID].NetworkStates.IsAlive == false) { Debug.Log(clients [message.NetworkID]); GameObject go = GameObject.Instantiate(UnitsManager.Instance.GetUnitObjectByID(message.UnitID, false), message.Position, Quaternion.Euler(message.Rotation)); clients [message.NetworkID].NetworkStates.IsAlive = true; clients [message.NetworkID].unit = go.GetComponent <Unit> (); clients [message.NetworkID].unit.States = message.States; clients [message.NetworkID].unit.States.PerHP = message.States.MaxHP; clients [message.NetworkID].unit.States.PerLife = message.States.MaxLife; } }
public void FiringMessage(string s) { string[] message = s.Split('$'); foreach (string text in message) { try { if (text.Length < 4) { return; } MessageBase TempBase = JsonUtility.FromJson <MessageBase> (text); MessageBase MsgBase = (MessageBase)JsonUtility.FromJson(text, Type.GetType(TempBase.ProtoName)); //批发处理 if (typeof(MessageMove).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageMove move = (MessageMove)MsgBase; OnMoveMessage(move); } else if (typeof(MessageAttack).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageAttack attack = (MessageAttack)MsgBase; OnAttackMessage(attack); } else if (typeof(MessageBuildingAttack).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageBuildingAttack attack = (MessageBuildingAttack)MsgBase; OnBuildingAttackMessage(attack); } else if (typeof(MessageBuildingFocusing).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageBuildingFocusing focus = (MessageBuildingFocusing)MsgBase; OnBuildingFocusMessage(focus); } else if (typeof(MessageInteractiveStart).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageInteractiveStart interactive = (MessageInteractiveStart)MsgBase; OnInteractiveStartMessage(interactive); } else if (typeof(MessageInteractiveExit).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageInteractiveExit interactive = (MessageInteractiveExit)MsgBase; OnInteractiveExitMessage(interactive); } else if (typeof(MessageRespawnConstruction).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageRespawnConstruction respawn = (MessageRespawnConstruction)MsgBase; OnConstructionRespawn(respawn); } else if (typeof(MessageChangingWeapon).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageChangingWeapon Stats = (MessageChangingWeapon)MsgBase; OnChangeingWeaponMessage(Stats); } else if (typeof(MessageDamage).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageDamage Stats = (MessageDamage)MsgBase; OnDamageMessage(Stats); } else if (typeof(MessagePlayerDead).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessagePlayerDead dead = (MessagePlayerDead)MsgBase; OnPlayerDeadMessage(dead); } else if (typeof(MessageClientsStats).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageClientsStats Stats = (MessageClientsStats)MsgBase; OnNewClientMessage(Stats); } else if (typeof(MessageRespawnPlayer).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageRespawnPlayer respawn = (MessageRespawnPlayer)MsgBase; OnRespawnPlayer((MessageRespawnPlayer)MsgBase); } else if (typeof(MessageLeaveGame).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageLeaveGame leave = (MessageLeaveGame)MsgBase; OnPlayerLeaveGame(leave); } else if (typeof(MessageChat).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageChat chat = (MessageChat)MsgBase; OnPlayerChatting(chat); } else { Debug.Log("未知数据类型,该数据为:" + MsgBase.ToString() + ",记得处理!"); } }catch (ArgumentException ex) { }catch (Exception ex) { Debug.LogError("协议包无法解析!出错报告为:" + ex.ToString()); Debug.LogError("数据包为:" + text); } } }
public void DecodeMessage(Socket sender, byte[] Message, int count) { string message = System.Text.Encoding.Default.GetString(Message); // string[] s = message.Split ('"'); // string type = s [3]; // //判断数据类型 // //TODO 你永远也更新不完 // if (type == "MessageMove") { // try { // MessageMove move = (MessageMove)JsonUtility.FromJson (message, Type.GetType (s [3])); // OnMoveMessage (move); // } catch { // Debug.Log (message); // } // } else if (type == "MessageAttack") { // MessageAttack Attack = (MessageAttack)JsonUtility.FromJson (message, Type.GetType (s [3])); // OnAttackMessage (Attack); // } else if (type == "MessageChangingWeapon") { // MessageChangingWeapon change = (MessageChangingWeapon)JsonUtility.FromJson (message, Type.GetType (s [3])); // OnChangingWeaponMessage (change); // } // else if (type == "MessageDamage") // { // MessageDamage Damage = (MessageDamage)JsonUtility.FromJson(message, Type.GetType(s[3])); // OnDamageMessage(Damage); // } // else if (type == "MessagePlayerDead") // { // MessagePlayerDead dead = (MessagePlayerDead)JsonUtility.FromJson(message, Type.GetType(s[3])); // OnPlayerDeadMessage(dead); // } // else if (type == "MessageClientsStats") // { // MessageClientsStats Stats = (MessageClientsStats)JsonUtility.FromJson(message, Type.GetType(s[3])); // OnNewClientMessage(Stats); // } // else if (type == "MessageRespawnPlayer") // { // MessageRespawnPlayer respawn = (MessageRespawnPlayer)JsonUtility.FromJson(message, Type.GetType(s[3])); // OnRespawnPlayer(respawn); // } string[] s = message.Split('$'); foreach (string text in s) { try { MessageBase TempBase = JsonUtility.FromJson <MessageBase> (text); MessageBase MsgBase = (MessageBase)JsonUtility.FromJson(text, Type.GetType(TempBase.ProtoName)); if (typeof(MessageMove).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageMove move = (MessageMove)MsgBase; OnMoveMessage(move); } else if (typeof(MessageAttack).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageAttack attack = (MessageAttack)MsgBase; OnAttackMessage(attack); } else if (typeof(MessageBuildingAttack).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageBuildingAttack attack = (MessageBuildingAttack)MsgBase; OnBuildingAttackMessage(attack); } else if (typeof(MessageBuildingFocusing).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageBuildingFocusing focus = (MessageBuildingFocusing)MsgBase; OnBuildingFocusingMessage(focus); } else if (typeof(MessageInteractiveStart).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageInteractiveStart interactive = (MessageInteractiveStart)MsgBase; OnInteractiveStartMessage(interactive); } else if (typeof(MessageInteractiveExit).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageInteractiveExit interactive = (MessageInteractiveExit)MsgBase; OnInteractiveExitMessage(interactive); } else if (typeof(MessageChangingWeapon).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageChangingWeapon Stats = (MessageChangingWeapon)MsgBase; OnChangingWeaponMessage(Stats); } else if (typeof(MessageDamage).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageDamage Stats = (MessageDamage)MsgBase; OnDamageMessage(Stats); } else if (typeof(MessagePlayerDead).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessagePlayerDead dead = (MessagePlayerDead)MsgBase; OnPlayerDeadMessage(dead); } else if (typeof(MessageClientsStats).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageClientsStats Stats = (MessageClientsStats)MsgBase; OnNewClientMessage(sender, Stats); } else if (typeof(MessageRespawnPlayer).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageRespawnPlayer respawn = (MessageRespawnPlayer)MsgBase; OnRespawnPlayer(respawn); } else if (typeof(MessageRespawnConstruction).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageRespawnConstruction respawn = (MessageRespawnConstruction)MsgBase; OnConstructionRespawn(respawn); } else if (typeof(MessageChat).IsAssignableFrom(Type.GetType(MsgBase.ProtoName))) { MessageChat chat = (MessageChat)MsgBase; OnChattingMessage(chat); } else { Debug.Log("未知数据类型,该数据为:" + text + ",服务器已做丢包处理.或者说,你忘记处理了?"); } }catch (ArgumentException ex) { }catch (Exception ex) { Debug.LogError("协议包无法解析!出错报告为:" + ex.ToString()); Debug.LogError("数据包为:" + text); } } }