public static byte[] GetBytes(PlayerStateMessage p) { byte[] newBytes = new byte[8]; BitConverter.GetBytes((int)p.state).CopyTo(newBytes, 0); BitConverter.GetBytes((int)p.atkState).CopyTo(newBytes, 4); return(newBytes); }
/// <summary> /// 同步状态的方法 /// </summary> /// <param name="state"></param> public void SendPlayerStateToServer(PlayerState playerState, AtkState atkState) { try { NetworkMessage message = new NetworkMessage(5, NetworkTools.GetLocalIP(), PlayerStateMessage.GetBytes(new PlayerStateMessage(playerState, atkState))); byte[] bytes = NetworkMessage.GetBytes(message); //Debug.Log("发送位置数据"); //测试 SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); } catch (Exception e) { Debug.LogError(e.ToString()); } }
protected override void NetworkCallback(NetworkMessage message) { if (message.type == 5 && message.ip == IP) { PlayerStateMessage m = PlayerStateMessage.GetMessage(message.message); player.SetState(m.state, m.atkState); } else if (message.type == 6 && message.ip == IP) { PlayerHurtMessage m = PlayerHurtMessage.GetMessage(message.message); player.ReduceHP(m.hurt); } else if (message.type == 18 && message.ip == IP) { player.Shoot(); } }