示例#1
0
        /// <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="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)));
 }
示例#3
0
 /// <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());
     }
 }
示例#4
0
 /// <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());
     }
 }
示例#5
0
        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);
        }
示例#6
0
 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);
         }
     }
 }
示例#7
0
 /// <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);
             }
         }
     }
 }
示例#8
0
 /// <summary>
 /// 接收发送给本机ip对应端口号的数据报
 /// </summary>
 protected virtual void ReciveMsg()
 {
     try
     {
         NetworkTools.PrintMessage("客户端开始接收消息");
         byte[] buffer = new byte[1024];
         while (true)
         {
             EndPoint point        = new IPEndPoint(IPAddress.Any, 0);      //用来保存发送方的ip和端口号
             int      length       = server.ReceiveFrom(buffer, ref point); //接收数据报
             byte[]   messageBytes = buffer.Skip(0).Take(length).ToArray(); //截取数组,从第0位开始,截取length长度的
             //NetworkTools.PrintMessage("接收到消息" + messageBytes.Length);
             NetworkMessage me = NetworkMessage.GetMessage(messageBytes);
             //Debug.Log("收到客户端信息; " + me.type);
             NetworkManager._Instance.AddMessage(me);
         }
     }
     catch (Exception e)
     {
         //Debug.LogError(e.ToString());
     }
 }
示例#9
0
        /// <summary>
        /// 关闭客户端
        /// </summary>
        public virtual void CloseServer()
        {
            try
            {
                if (reciveTh != null)
                {
                    reciveTh.Abort();
                    reciveTh = null;
                }

                if (server != null)
                {
                    server.Close();
                    server = null;
                    NetworkTools.PrintMessage("关闭客户端!");
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
示例#10
0
        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);
                }
            }
        }
示例#11
0
 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);
         }
     }
 }
示例#12
0
 private void OnEnable()
 {
     NetworkManager._Instance.AddCallBack(102, GetNetworkMsgCallBack);
     NetworkManager._Instance.CreateRoom("房间111");
     RoomSingle.roomIP = NetworkTools.GetLocalIP();
 }
示例#13
0
        /// <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);
        }
 public void SendGetRoomPlayerInfos()
 {
     SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort,
             NetworkMessage.GetBytes(new NetworkMessage(14, NetworkTools.GetLocalIP(), null)));
 }
示例#15
0
 /// <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());
 }
示例#17
0
        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);
        }