コード例 #1
0
ファイル: NGRoom.cs プロジェクト: mengtest/netgo-unity-client
        public void LeaveRoom()
        {
            SendMessage leaveroom = new SendMessage();

            leaveroom.MsgType = MessageType.LeaveRoom;

            var buf = NGMessageCodec.Encode(leaveroom.ToByteArray());

            NGNetwork.Socket.Send(buf);

            NGInterface.CurrentRoom().ClearPlayers();
        }
コード例 #2
0
ファイル: NGRoom.cs プロジェクト: mengtest/netgo-unity-client
        public GameObject Instantiate(uint peerid, bool isLocal, UInt32[] viewids, string prefabname, Vector3 position = default, Quaternion rotation = default)
        {
            NGResourceManager manager = new NGResourceManager();
            var go = manager.InstantiatePrefab(prefabname, position, rotation);

            NGView[] views = NGViewUtils.GetViews(go);
            //NGLogger.LogDebug(views.Length.ToString());
            if (isLocal)
            {
                uint   i   = 1;
                uint[] ids = new uint[views.Length];
                foreach (var var in views)
                {
                    var.PlayerID = peerid;
                    var.ViewID   = peerid * 1000 + i;
                    ids[i - 1]   = var.ViewID;
                    i++;
                    NGViewContainer.RegisterView(var);
                }
                LocalPlayer.GO = go;
                Instantiate(prefabname, position, rotation, ids);
            }
            else
            {
                for (int i = 0; i < views.Length; i++)
                {
                    //NGLogger.LogDebug("Print View ID" + views[i].ViewID);
                    views[i].ViewID   = viewids[i];
                    views[i].PlayerID = peerid;
                    NGViewContainer.RegisterView(views[i]);
                }
                var player = NGInterface.CurrentRoom().GetNGPlayer(peerid);
                player.GO = go;
            }

            return(go);
        }
コード例 #3
0
        private void ProcessRecieveData(byte[] data)
        {
            //NGLogger.LogInfo("Receive a message");
            Stream stream = new MemoryStream(data);

            ReceiveMessage message = ReadMessage <ReceiveMessage>(stream);

            switch (message.ReceiveMsgType)
            {
            case ReceiveMessageType.ResponseSocketStatus:
                switch (message.RssMsg.SStatus)
                {
                case SocketStatus.Connected:
                    NGNetwork.Status = SocketStatus.Connected;
                    NGNetworkEvent.OnConnected();
                    break;
                }
                break;

            case ReceiveMessageType.ResponseOperation:

                ResponseOperationMessage rmsg = message.RoMsg;
                NGInterface.CurrentRoom().ResponseProcessor(rmsg);
                break;

            case ReceiveMessageType.Forward:

                ForwardMessage fmsg   = message.FMsg;
                uint           peerId = fmsg.PeerId;
                switch (fmsg.MsgType)
                {
                case MessageType.JoinRoom:
                    var      jrParams  = fmsg.JrfParams;
                    NGPlayer newplayer = new NGPlayer(false, peerId);
                    NGInterface.CurrentRoom().AddPlayer(newplayer);
                    NGEvent.OnOtherPlayerEnteredRoom(newplayer);
                    break;

                case MessageType.JoinOrCreateRoom:
                    var      jocParams = fmsg.JocrfParams;
                    NGPlayer player    = new NGPlayer(false, peerId);
                    NGInterface.CurrentRoom().AddPlayer(player);
                    NGEvent.OnOtherPlayerEnteredRoom(player);
                    break;

                case MessageType.LeaveRoom:
                    var lParams = fmsg.LrfParams;
                    NGLogger.LogDebug("LeaveRoom" + peerId);

                    var leaveplayer = NGInterface.CurrentRoom().GetNGPlayer(peerId);
                    NGEvent.OnOtherPlayerLeftRoom(leaveplayer);
                    Destroy(leaveplayer.GO);
                    NGInterface.CurrentRoom().RemovePlayer(peerId);

                    break;

                case MessageType.Rpc:
                    var     rpcparams = fmsg.RfParams;
                    NGAny[] objs      = NGProtobufConverter <NGAny> .RepeatedField2Array(rpcparams.Parameters);

                    NGRPC.ExcuteRPC(rpcparams.ViewID, rpcparams.MethodName, objs);
                    break;

                case MessageType.Instantiation:
                    var i   = fmsg.IfParams;
                    var ids = NGProtobufConverter <uint> .RepeatedField2Array(i.ViewIDs);

                    NGInterface.CurrentRoom().Instantiate(peerId, false, ids, i.PrefabName, i.Position, i.Rotation);
                    break;

                case MessageType.ViewSync:
                    var             viewsyncparams = fmsg.VsfParams;
                    var             viewParams     = viewsyncparams.VsdParams;
                    NGViewContainer container      = new NGViewContainer();
                    container.DeserializeAll(viewParams);
                    break;

                case MessageType.CustomEvent:
                    var ceParams = fmsg.CeParams;
                    NGEvent.OnCustomEvent(ceParams.EventID, NGProtobufConverter <NGAny> .RepeatedField2Array(ceParams.CustomData));
                    break;
                }
                break;
            }
        }