/// <summary> /// 发送可以开始 /// </summary> public void SendWaitStart() { //byte[] bytes = RoomPlayerInfoMessage.GetBytes(new RoomPlayerInfoMessage("", true)); byte[] bytes = PlayerInfoMessage.GetBytes(playerInfo); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, NetworkMessage.GetBytes(new NetworkMessage(10, NetworkTools.GetLocalIP(), bytes))); }
/// <summary> /// 向玩家发送单个玩家的IP,枪械情况 /// </summary> /// <param name="ip"></param> /// <param name="info"></param> public void SendPlayrMessageInfo(string ip, PlayerInfoMessage info) { byte[] bytes = PlayerInfoMessage.GetBytes(info); NetworkMessage sendM = new NetworkMessage(17, NetworkTools.GetLocalIP(), bytes); SendMsg(ip, NetworkConstent.UDPClientPort, NetworkMessage.GetBytes(sendM)); }
/// <summary> /// 向所有客户端发送房间关闭信息 /// </summary> void SendAllClose() { server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); EndPoint point = new IPEndPoint(IPAddress.Broadcast, NetworkConstent.UDPClientPort); NetworkMessage m = new NetworkMessage(3, NetworkTools.GetLocalIP(), Encoding.UTF8.GetBytes(NetworkTools.GetLocalIP())); server.SendTo(NetworkMessage.GetBytes(m), point); }
/// <summary> /// 发送退出房间 /// </summary> public void SendQuitRoom() { if (client != null) { SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, NetworkMessage.GetBytes(new NetworkMessage(9, NetworkTools.GetLocalIP(), new byte[0]))); } }
/// <summary> /// 发送Boss受到伤害 /// </summary> /// <param name="hurt"></param> /// <param name="id"></param> /// <param name="listNum"></param> public void SendBossHurtMsg(float hurt, int id) { byte[] by = EnemyHurtMessge.GetBytes( new EnemyHurtMessge(hurt, id, 0)); NetworkMessage message = new NetworkMessage(72, NetworkTools.GetLocalIP(), by); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); }
/// <summary> /// Boss的状态信息 /// </summary> /// <param name="state"></param> /// <param name="id"></param> /// <param name="listNum"></param> public void SendBossStateMsg(BossState state, BossAtkState s, int id) { byte[] by = BossStateMessage.GetBytes( new BossStateMessage(state, s)); NetworkMessage message = new NetworkMessage(71, NetworkTools.GetLocalIP(), by); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); }
/// <summary> /// 向玩家发送房间内所有玩家信息 /// </summary> /// <param name="ip"></param> public void SendPlayerInfosToClient(string ip) { foreach (var item in playerData.GetPlayersInfo().Keys) { byte[] bytes = RoomPlayerInfoMessage.GetBytes(playerData.GetPlayersInfo()[item]); NetworkMessage sendM = new NetworkMessage(15, NetworkTools.GetLocalIP(), bytes); SendMsg(ip, NetworkConstent.UDPClientPort, NetworkMessage.GetBytes(sendM)); } }
/// <summary> /// 同步小怪的目标 /// </summary> /// <param name="hurt"></param> /// <param name="id"></param> /// <param name="listNum"></param> public void SendEnemyTargetMsg(string ip, int id, int listNum) { byte[] by = EnemyTargetMessage.GetBytes( new EnemyTargetMessage(ip, id, listNum)); NetworkMessage message = new NetworkMessage(63, NetworkTools.GetLocalIP(), by); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); }
/// <summary> /// 发送Boss的位置信息 /// </summary> /// <param name="t"></param> /// <param name="id"></param> /// <param name="listNum"></param> public void SendBossTransformMsg(Transform t, int id) { byte[] by = EnemyInfoMessage.GetBytes( new EnemyInfoMessage(t.position, t.rotation.eulerAngles, id, 0)); NetworkMessage message = new NetworkMessage(70, NetworkTools.GetLocalIP(), by); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); }
/// <summary> /// 启动客户端接收房间信息 /// </summary> public virtual void StartServer(int port) { server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.Bind(new IPEndPoint(IPAddress.Parse(NetworkTools.GetLocalIP()), port));//绑定端口号和IP //NetworkTools.PrintMessage("客户端已启动:" + NetworkTools.GetLocalIP() + " -- " + NetworkConstent.UDPClientPort); reciveTh = new Thread(ReciveMsg); reciveTh.Start(); }
/// <summary> /// 启动服务器 /// </summary> public virtual void StartServer() { server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.Bind(new IPEndPoint(IPAddress.Parse(NetworkTools.GetLocalIP()), NetworkConstent.UDPServerPort));//绑定端口号和IP server.IOControl((int)NetworkConstent.SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null); NetworkTools.PrintMessage("服务器已启动:" + NetworkTools.GetLocalIP() + " -- " + NetworkConstent.UDPServerPort); reciveTh = new Thread(ReciveMsg); reciveTh.Start(); }
/// <summary> /// 向房间内的玩家发送开始游戏请求 /// </summary> public void SendOtherPlayerStartGame() { RoomSingle.AddPlayer(playerInfo); byte[] infos = PlayerInfoMessage.GetBytes(RoomSingle.GetInfos()[NetworkTools.GetLocalIP()]); byte[] bytes = NetworkMessage. GetBytes(new NetworkMessage(13, NetworkTools.GetLocalIP(), infos)); foreach (var ip in playerData.GetPlayersInfo().Keys) { SendMsg(ip, NetworkConstent.UDPClientPort, bytes); } }
public override void StartServer(int port) { base.StartServer(port); NetworkMessage m = new NetworkMessage(2, NetworkTools.GetLocalIP(), Encoding.UTF8.GetBytes(NetworkTools.GetLocalIP())); NetworkManager._Instance.AddCallBack(1, GetRoomMsgCallBack); NetworkManager._Instance.AddCallBack(3, GetCloseRoomMsgCallBack); rooms = new RoomData(); message = NetworkMessage.GetBytes(m); }
public void Awake() { base.BaseAwake(); if (NetworkTools.GetLocalIP().Equals(RoomSingle.roomIP)) { Destroy(this); return; } NetworkManager._Instance.AddCallBack(60, GetNetworkMsgCallBack); NetworkManager._Instance.AddCallBack(61, GetNetworkMsgCallBack); NetworkManager._Instance.AddCallBack(62, GetNetworkMsgCallBack); }
public void SendPlayerShootToServer() { try { NetworkMessage message = new NetworkMessage(18, NetworkTools.GetLocalIP(), new byte[0]); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); } catch (Exception e) { Debug.LogError(e.ToString()); } }
protected override void GetNetworkMsgCallBack(params object[] obj_arr) { NetworkMessage message = (NetworkMessage)obj_arr[0]; if (message.type == 62 && !NetworkTools.GetLocalIP().Equals(message.ip)) { EnemyHurtMessge hurt = EnemyHurtMessge.GetMessage(message.message); if (hurt.enemyType == enemy.enemyData.ID && hurt.num == enemy.enemyData.Listnum) { AddMessage((NetworkMessage)obj_arr[0]); } } }
/// <summary> /// 同步状态的方法 /// </summary> /// <param name="state"></param> public void SendPlayerHurtToServer(float hurt) { try { NetworkMessage message = new NetworkMessage(6, NetworkTools.GetLocalIP(), PlayerHurtMessage.GetBytes(new PlayerHurtMessage(hurt))); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); } catch (Exception e) { Debug.LogError(e.ToString()); } }
/// <summary> /// 想要加入的房间 /// </summary> /// <param name="ip"></param> public void SendAddRoom(string ip) { try { IPAddress.Parse(ip); Debug.Log("请求加入: " + ip); } catch (System.Exception) { Debug.LogError("请输入有效IP"); return; } byte[] info = RoomPlayerInfoMessage.GetBytes(new RoomPlayerInfoMessage("", false, NetworkTools.GetLocalIP())); SendMsg(ip, NetworkConstent.UDPServerPort, NetworkMessage.GetBytes(new NetworkMessage(7, NetworkTools.GetLocalIP(), info))); }
/// <summary> /// 退出房间的回调 /// </summary> /// <param name="obj_arr"></param> void QuitRoomMsgCallBack(params object[] obj_arr) { NetworkMessage m = (NetworkMessage)obj_arr[0]; playerData.RemovePlayerInfo(m.ip); NetworkManager._Instance.AddMessage(102, playerData); RoomSingle.RemovePlayer(m.ip);//装备情况删除 //向所有玩家发送退出信息 foreach (var item in playerData.GetPlayersInfo().Keys) { if (!item.Equals(NetworkTools.GetLocalIP())) { SendRemovePlayrInfo(item, new RoomPlayerInfoMessage("", true, m.ip)); } } }
/// <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()); } }
/// <summary> /// 同步位置方法 /// </summary> /// <param name="trans"></param> public void SendTranfromToServer(Transform trans, float gunAngle) { try { NetworkMessage message = new NetworkMessage(4, NetworkTools.GetLocalIP(), TransformMessage.GetBytes(new TransformMessage( trans.position, trans.rotation.eulerAngles, gunAngle))); byte[] bytes = NetworkMessage.GetBytes(message); //Debug.Log("发送位置数据"); //测试 SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); } catch (Exception e) { Debug.LogError(e.ToString()); } }
void CreateRoom(string roomName) { room = new RoomMessage(roomName, NetworkTools.GetLocalIP()); NetworkManager._Instance.AddCallBack(2, SendRoomMsg); NetworkManager._Instance.AddCallBack(7, AddRoomMsgCallBack); NetworkManager._Instance.AddCallBack(9, QuitRoomMsgCallBack); NetworkManager._Instance.AddCallBack(10, UpdateRoomPlayerMsgCallBack); NetworkManager._Instance.AddCallBack(14, ReciveGetPlayerInfos); StartServer(); playerData = new RoomPlayerData(); playerData.AddPlayerInfo(NetworkTools.GetLocalIP(), new RoomPlayerInfoMessage("", true, NetworkTools.GetLocalIP())); playerInfo = new PlayerInfoMessage(1, 101, 102, "房主", NetworkTools.GetLocalIP()); //RoomSingle.AddPlayer(new PlayerInfoMessage(2,104,105,"房主",NetworkTools.GetLocalIP())); NetworkManager._Instance.AddMessage(102, playerData); EventCenterManager._Instance.AddListener(EventType.GetPlayerInfo, GetPlayerInfoCallBack); EventCenterManager._Instance.AddListener(EventType.SetPlayerInfo, SetPlayerInfoCallBack); }
protected override void NetworkCallback(NetworkMessage message) { if (message.type == 60) { EnemyInfoMessage info = EnemyInfoMessage.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (!enemyDic.ContainsKey(key)) { GameObject prefab = Resources.Load <GameObject>("Prefab/Enemy/Enemy_AI_" + info.enemyType); GameObject obj = Instantiate <GameObject>(prefab, new Vector3(info.x, info.y, info.z), Quaternion.Euler(info.angleX, info.angleY, info.angleZ), transform); NetEnemyBase net = obj.GetComponent <NetEnemyBase>(); net.SetIDandListNum(info.enemyType, info.num); net.SetTarget(info); enemyDic.Add(key, net); } } else if (message.type == 61) { //Debug.Log("同步小怪状态"); EnemyStateMessage info = EnemyStateMessage.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (enemyDic.ContainsKey(key)) { enemyDic[key].SwitchEnemyState((EnemyState)info.state); if (info.state == (int)(EnemyState.Death)) { enemyDic.Remove(key); } } } else if (message.type == 62 && !NetworkTools.GetLocalIP().Equals(message.ip)) { EnemyHurtMessge info = EnemyHurtMessge.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (enemyDic.ContainsKey(key)) { enemyDic[key].ReduceHP(info.hurt); } } }
/// <summary> /// 向客户端发送可以加入信息 /// </summary> /// <param name="obj_arr"></param> void AddRoomMsgCallBack(params object[] obj_arr) { //房间最多4个人 if (playerData.GetPlayersInfo().Count < 4) { NetworkMessage m = (NetworkMessage)obj_arr[0]; RoomPlayerInfoMessage info = RoomPlayerInfoMessage.GetMessage(m.message); playerData.AddPlayerInfo(m.ip, info); NetworkManager._Instance.AddMessage(102, playerData); Debug.Log("房间加入玩家" + m.ip + " --- " + playerData.GetPlayersInfo().Count); byte[] bytes = NetworkMessage.GetBytes(new NetworkMessage(8, NetworkTools.GetLocalIP(), new byte[0])); SendMsg(m.ip, NetworkConstent.UDPClientPort, bytes); //向所有玩家发送加入信息 foreach (var item in playerData.GetPlayersInfo().Keys) { if (!item.Equals(NetworkTools.GetLocalIP())) { SendPlayerInfosToClient(item); } } } }
protected override void GetNetworkMsgCallBack(params object[] obj_arr) { NetworkMessage message = (NetworkMessage)obj_arr[0]; if (message.type == 50 && !message.ip.Equals(NetworkTools.GetLocalIP()))//位置同步 { BulletMessage bullet = BulletMessage.GetMessage(message.message); if ((bullet.bulletType == (int)BulletType.boosMissile || bullet.bulletType == (int)BulletType.bossBullet) && NetworkTools.GetLocalIP().Equals(RoomSingle.roomIP)) { return; } if (bullets.ContainsKey(message.ip + bullet.num) && bullet.bulletType != (int)BulletType.bossBullet)//有这颗子弹 { if (bullet.bulletType == (int)BulletType.bossBullet) { return; } bullets[message.ip + bullet.num].SetTarget(bullet); } else//没有这个子弹, 根据子弹的类型生成子弹 { AddMessage(message); } } else if (message.type == 51 && !message.ip.Equals(NetworkTools.GetLocalIP()))//位置同步 { BulletMessage bullet = BulletMessage.GetMessage(message.message); if (bullet.bulletType == (int)BulletType.bossBullet) { return; } if (bullets.ContainsKey(message.ip + bullet.num))//有这颗子弹 { //爆炸 AddMessage(message); } } }
protected override void NetworkCallback(NetworkMessage message) { if (message.type == 50 && !message.ip.Equals(NetworkTools.GetLocalIP()))//位置同步 { BulletMessage bullet = BulletMessage.GetMessage(message.message); if ((bullet.bulletType == (int)BulletType.boosMissile || bullet.bulletType == (int)BulletType.bossBullet) && NetworkTools.GetLocalIP().Equals(RoomSingle.roomIP)) { return; } if (!bullets.ContainsKey(message.ip + bullet.num))//有这颗子弹 { GameObject prefab = Resources.Load <GameObject>("Prefab/Bullet/Net_" + ((BulletType)bullet.bulletType).ToString()); GameObject obj = Instantiate <GameObject>(prefab, new Vector3(bullet.x, bullet.y, bullet.z), Quaternion.Euler(bullet.angleX, bullet.angleY, bullet.angleZ)); obj.GetComponent <NetworkBullet>().SetTarget(bullet); if (bullet.bulletType != (int)BulletType.bossBullet) { bullets.Add(message.ip + bullet.num, obj.GetComponent <NetworkBullet>()); } } } else if (message.type == 51 && !message.ip.Equals(NetworkTools.GetLocalIP()))//包扎同步 { BulletMessage bullet = BulletMessage.GetMessage(message.message); if (bullet.bulletType == (int)BulletType.bossBullet) { return; } if (bullets.ContainsKey(message.ip + bullet.num))//有这颗子弹 { bullets[message.ip + bullet.num].Explosion(); bullets.Remove(message.ip + bullet.num); } } }
public void SendGetRoomPlayerInfos() { SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, NetworkMessage.GetBytes(new NetworkMessage(14, NetworkTools.GetLocalIP(), null))); }
private void OnEnable() { NetworkManager._Instance.AddCallBack(102, GetNetworkMsgCallBack); NetworkManager._Instance.CreateRoom("房间111"); RoomSingle.roomIP = NetworkTools.GetLocalIP(); }
/// <summary> /// 子弹爆炸 /// </summary> /// <param name="p"></param> /// <param name="r"></param> /// <param name="type"></param> /// <param name="num"></param> public void SendExplosion(Vector3 p, Vector3 r, int type, int num) { byte[] b = BulletMessage.GetBytes(new BulletMessage(p, r, type, num)); byte[] m = NetworkMessage.GetBytes(new NetworkMessage(51, NetworkTools.GetLocalIP(), b)); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, m); }
public WaitStartRoomSendClient() { playerInfo = new PlayerInfoMessage(1, 101, 102, "玩家", NetworkTools.GetLocalIP()); }