Exemplo n.º 1
0
    void Update()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)
        {
            Debug.Log("Error. Check internet connection!");
        }
        try {
            if (serverMessage.Length != 0)
            {
                JObject objFromServer = JObject.Parse(serverMessage);
                int     messageId     = (int)objFromServer.GetValue("id");
                switch (messageId)
                {
                case 0: {
                    Node.sLoadCameraGO.SetActive(false);
                    //Node
                    Node.sPlayerGO = Instantiate(playerGO);
                    Node.sPlayerGO.transform.parent = Node.sGO.transform;
                    Node.sPlayerGO.name             = "Player";
                    Node.sPlayerGO_sPlayerClass     = Node.sPlayerGO.GetComponent <MyPlayer>();
                    Node.sPlayerGO_sMoveClass       = Node.sPlayerGO.GetComponent <Move>();

                    Node.sPlayerGO_sPlayerClass.playerId   = (long)objFromServer.GetValue("playerId");
                    Node.sPlayerGO_sPlayerClass.playerName = objFromServer.GetValue("name").ToString();
                    Node.sPlayerGO_sPlayerClass.x          = (float)objFromServer.GetValue("x");
                    Node.sPlayerGO_sPlayerClass.y          = (float)objFromServer.GetValue("y");
                    Node.sPlayerGO_sPlayerClass.position   = objFromServer.GetValue("position").ToString();
                    Node.sPlayerGO_sPlayerClass.connect    = true;
                    break;
                }

                case 1: {
                    int playerId = (int)objFromServer.GetValue("playerId");

                    bool isNew = !otherPlayers.ContainsKey(playerId);
                    if (isNew)
                    {
                        GameObject otherPlayerGON = Instantiate(otherPlayerGO);
                        otherPlayers.Add(playerId, otherPlayerGON);
                        otherPlayerGON.transform.parent = Node.sOtherPlayersGO.transform;
                        isNew = true;
                    }

                    OtherPlayer optionsOtherPlayer = otherPlayers[playerId].GetComponent <OtherPlayer>();
                    float       tempOtherPlayerX = optionsOtherPlayer.newX, tempOtherPlayerY = optionsOtherPlayer.newY;
                    optionsOtherPlayer.newX     = (float)objFromServer.GetValue("x");
                    optionsOtherPlayer.newY     = (float)objFromServer.GetValue("y");
                    optionsOtherPlayer.position = objFromServer.GetValue("position").ToString();

                    //0.5 из-за того что даются координаты центра объекта
                    if (isNew)
                    {
                        optionsOtherPlayer.playerId = playerId;
                        optionsOtherPlayer.setPlayerName(objFromServer.GetValue("name").ToString());
                        optionsOtherPlayer.setXY();
                        playersInCells[Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationX(optionsOtherPlayer.newX + 0.5f)), Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationY(optionsOtherPlayer.newY + 0.5f))].Add(otherPlayers[playerId]);
                    }
                    else
                    {
                        if (Mathf.FloorToInt(tempOtherPlayerX + 0.5f) != Mathf.FloorToInt(optionsOtherPlayer.newX + 0.5f) ||
                            Mathf.FloorToInt(tempOtherPlayerY + 0.5f) != Mathf.FloorToInt(optionsOtherPlayer.newY + 0.5f))
                        {
                            playersInCells[Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationX(tempOtherPlayerX + 0.5f)), Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationY(tempOtherPlayerY + 0.5f))].Remove(otherPlayers[playerId]);
                            playersInCells[Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationX(optionsOtherPlayer.newX + 0.5f)), Mathf.FloorToInt(Node.sMapGO_sMapClass.calibrationY(optionsOtherPlayer.newY + 0.5f))].Add(otherPlayers[playerId]);
                        }
                    }
                    break;
                }

                case 2: {
                    deletePlayer((int)objFromServer.GetValue("playerId"));
                    break;
                }

                case 3: {
                    //type lower average top
                    JArray blocks = (JArray)objFromServer.GetValue("blocks");
                    switch ((string)objFromServer.GetValue("type"))
                    {
                    case "lower": {
                        Node.sMapGO_sMapClass.dataToLowerMapArr(blocks);
                        break;
                    }

                    case "average": {
                        Node.sMapGO_sMapClass.dataToAverageMapArr(blocks);
                        break;
                    }
                    }
                    break;
                }

                case 4: {
                    deletePlayer((int)objFromServer.GetValue("playerId"));
                    break;
                }

                case 5: {
                    string      message            = objFromServer.GetValue("message").ToString();
                    int         playerId           = (int)objFromServer.GetValue("playerId");
                    OtherPlayer optionsOtherPlayer = otherPlayers[playerId].GetComponent <OtherPlayer>();
                    StartCoroutine(optionsOtherPlayer.setLocalMessage(message));
                    break;
                }

                case 6: {
                    Node.sCalendarGO_sCalendarClass.period = 60000 / (long)objFromServer.GetValue("period");
                    Node.sCalendarGO_sCalendarClass.year   = (int)objFromServer.GetValue("year");
                    Node.sCalendarGO_sCalendarClass.month  = (int)objFromServer.GetValue("month");
                    Node.sCalendarGO_sCalendarClass.day    = (int)objFromServer.GetValue("day");
                    Node.sCalendarGO_sCalendarClass.hour   = (int)objFromServer.GetValue("hour");
                    Node.sCalendarGO_sCalendarClass.minute = (int)objFromServer.GetValue("minute");
                    break;
                }

                case 7: {
                    JArray objects = (JArray)objFromServer.GetValue("objects");
                    Node.sMapGO_sMapClass.dataToObjectMapArr(objects);
                    break;
                }
                }
            }
            serverMessage = string.Empty;
        }
        catch {
            Node.sLoadCameraGO.SetActive(true);
            Node.sLoadCameraGO_TextGO.text = "Связь с сервером потеряна.";
            disconnect();
        }
    }