Пример #1
0
    private void CaseInputDirection(int recConnectionID, object[] unPackDatas)
    {
        int playerNo = EMPTY_NETCODE;

        if (TryGetNetCodeByConnectionID(recConnectionID, out playerNo))
        {
            sendBufferSize = 0;
            sendBuffer     = NetworkCommunicateHelper.ToPack(out sendBufferSize, Tool.Insert(unPackDatas, 1, playerNo));
            SendToEveryPlayer(hostID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
        }
        //SynchronizeAllPlayerTransform();
    }
Пример #2
0
 private void CaseSynchronizePlayerInfo(object[] unPackDatas)
 {
     for (int i = 1; i < unPackDatas.Length; i++)
     {
         int netCode = (int)unPackDatas[i];
         if (GetPlayerGOByNetCode(netCode) == null)
         {
             GameControler.ins.SpawnPlayer(netCode, netCode.ToString());
         }
     }
     sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "SynchronizePlayerInfoDone");
     NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
 }
Пример #3
0
    private void SendInputDirection()
    {
        Vector3 inputDirection = (Input.GetAxis("V") * CameraCtrl.rigTrans.forward + Input.GetAxis("H") * CameraCtrl.cameraTrans.right);

        if (lastInputDirection != inputDirection)
        {
            sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "InputDirection", inputDirection);
            NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
            lastInputDirection = inputDirection;
        }
        //sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "InputDirection", inputDirection);
        //NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
    }
Пример #4
0
    // 通知所有玩家哪两位玩家进行身份调换
    public void SwitchFaction(AttributesManager originalGhostAttrManager, AttributesManager originalPeopleAttrManager)
    {
        int originalGhostPlayerNo  = EMPTY_NETCODE;
        int originalPeoplePlayerNo = EMPTY_NETCODE;

        if (Client.ins.TryGetNetCodeByPlayerGO(originalGhostAttrManager.gameObject, out originalGhostPlayerNo) &&
            Client.ins.TryGetNetCodeByPlayerGO(originalPeopleAttrManager.gameObject, out originalPeoplePlayerNo))
        {
            // 通知交换身份
            sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "SwitchFaction", originalGhostPlayerNo, originalPeoplePlayerNo);
            SendToEveryPlayer(hostID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
        }
    }
Пример #5
0
    private void SynchronizeAllPlayerTransform()
    {
        //Debug.Log("playerGOs.Length = " + playerGOs.Length);
        List <object> synTransformDataList = new List <object>();

        synTransformDataList.Add("SynchronizePlayerTransform");
        for (int i = 0; i < currentConnCount; i++)
        {
            int netCode = netCodeList[i];
            synTransformDataList.Add(netCode);
            GameObject playerGO = Client.ins.GetPlayerGOByNetCode(netCode);
            synTransformDataList.Add(playerGO.transform.position);
            synTransformDataList.Add(playerGO.transform.rotation);
        }
        sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, synTransformDataList.ToArray());
        SendToEveryPlayer(hostID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
    }
Пример #6
0
    private void SendAxis(string axisName)
    {
        float axisScale = Input.GetAxis(axisName);
        //Debug.Log("axisName = " + axisName + ";axisScale = " + axisScale);
        float axisLastScale = 0;
        bool  tryGetSuccess = axisLastScalePair.TryGetValue(axisName, out axisLastScale);

        if (!tryGetSuccess)
        {
            sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "GetAxis", axisName, axisScale);
            NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
            axisLastScalePair.Add(axisName, axisScale);
        }
        else if (axisLastScale != axisScale)
        {
            sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "GetAxis", axisName, axisScale);
            NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
            axisLastScalePair[axisName] = axisScale;
        }
    }
Пример #7
0
 private void SendKey(KeyCode key)
 {
     if (Input.GetKeyUp(key))
     {
         Debug.Log("Enum.GetName(key.GetType(), key) = " + Enum.GetName(key.GetType(), key));
         sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "GetKeyUp", Enum.GetName(key.GetType(), key));
         NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
     }
     else if (Input.GetKeyDown(key))
     {
         Debug.Log("Enum.GetName(key.GetType(), key) = " + Enum.GetName(key.GetType(), key));
         sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "GetKeyDown", Enum.GetName(key.GetType(), key));
         NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
     }
     else if (Input.GetKey(key))
     {
         Debug.Log("Enum.GetName(key.GetType(), key) = " + Enum.GetName(key.GetType(), key));
         sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "GetKey", Enum.GetName(key.GetType(), key));
         NetworkTransport.Send(hostID, connectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);
     }
 }
Пример #8
0
    // Update is called once per frame
    void Update()
    {
        if (hostID <= EMPTY_HOST_ID)
        {
            if (Input.GetKeyDown(KeyCode.F1))
            {
                isServer = true;
            }
            if (Input.GetKeyDown(KeyCode.F2))
            {
                isServer = false;
            }
            if (Input.GetKeyDown(KeyCode.C))
            {
                if (isServer)
                {
                    NetworkTransport.Init();
                    ConnectionConfig connConfig = new ConnectionConfig();
                    reliableChannelID   = connConfig.AddChannel(QosType.Reliable);
                    unreliableChannelID = connConfig.AddChannel(QosType.Unreliable);
                    HostTopology hostTopology = new HostTopology(connConfig, maxPlayerCount);
                    hostID    = NetworkTransport.AddHost(hostTopology, serverPort);
                    recBuffer = new byte[recBufferSize];
                }
                else
                {
                    enabled = false;
                }
            }
        }
        else
        {
            NetworkEventType eventType = NetworkTransport.ReceiveFromHost(hostID, out recConnectionID, out channelID, recBuffer, recBufferSize, out receivedSize, out error);
            switch (eventType)
            {
            case NetworkEventType.ConnectEvent:
                int netCode = EMPTY_NETCODE;
                if (TryPortionNetCode(recConnectionID, out netCode))
                {
                    currentConnCount++;
                    // 通知新玩家设置自己的编号
                    sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "SetPlayerNo", netCode);
                    NetworkTransport.Send(hostID, recConnectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error);

                    // 通知所有玩家设置各信息
                    List <object> synPlayerInfoDataList = new List <object>();
                    synPlayerInfoDataList.Add("SynchronizePlayerInfo");
                    for (int i = 0; i < currentConnCount; i++)
                    {
                        synPlayerInfoDataList.Add(netCodeList[i]);
                    }
                    sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, synPlayerInfoDataList.ToArray());
                    SendToEveryPlayer(hostID, unreliableChannelID, sendBuffer, sendBufferSize, out error);

                    //// 通知所有玩家同步位置
                    //SynchronizeAllPlayerTransform();
                    Debug.Log("有新客户端连接,当前连接数 " + currentConnCount + "/" + maxPlayerCount);
                }
                break;

            case NetworkEventType.DataEvent:
                Debug.Log("接收到客户端的新数据");
                ExecuteInstruction(recConnectionID, NetworkCommunicateHelper.UnPack(recBuffer));
                break;

            case NetworkEventType.DisconnectEvent:
                int recyclePlayerNo = EMPTY_NETCODE;
                TryRecycleNetCode(recConnectionID, out recyclePlayerNo);
                Debug.Log("有客户端断开连接,当前连接数 " + currentConnCount + "/" + maxPlayerCount);
                break;

            case NetworkEventType.Nothing:
                Debug.Log("无数据");
                break;

            default:
                break;
            }

            //if (Time.time - lastSynchronizeTime > synchronizeFrequency)
            //{
            //    // 通知所有玩家同步位置
            //    SynchronizeAllPlayerTransform();
            //    lastSynchronizeTime = Time.time;
            //}
        }
    }