Пример #1
0
    private void receivePacket(Packet packet)
    {
        PacketType pt = (PacketType)packet.ReadByte();

        int id, len;

        switch (pt)
        {
        case PacketType.LOGIN:
            waitingForLoginResponse = false;
            id = packet.ReadInt();
            if (id >= 0)
            {
                Debug.Log("CLIENT: authenticated by server, joining lobby");
                myName = packet.ReadString();
                menuUI.setStatusText("Login successful!", Color.yellow, false);

                // load into next scene
                SceneManager.LoadScene(1);
                waitToCheckMessages = true;       // pause checking packets until in new scene
            }
            else if (id == -1)
            {
                Debug.Log("CLIENT: invalid login");
                menuUI.setStatusText("Invalid login info!", Color.red, true);
            }
            else if (id == -2)
            {
                Debug.Log("CLIENT: already loggged in");
                menuUI.setStatusText("Already logged in!", Color.red, true);
            }
            break;

        case PacketType.STATE_UPDATE:
            int numAlivePlayers = packet.ReadInt();
            for (int i = 0, ai = 0; ai < numAlivePlayers; ++ai)
            {
                id = packet.ReadInt();
                Vector3 pos = packet.ReadVector3();
                // if player id mismatch then delete players until they match
                while (i < playersInGame.Count && playersInGame[i].playerID != id)
                {
                    Destroy(playersInGame[i].gameObject);
                    playersInGame.RemoveAt(i);
                }
                if (i == playersInGame.Count)       // init new gameObject if new player
                {
                    GameObject pgo       = (GameObject)Instantiate(playerPrefab, pos, Quaternion.identity);
                    PlayerSync newPlayer = pgo.GetComponent <PlayerSync>();
                    newPlayer.init(id, playersInRoom[i].color, i == getMyPlayerIndex() ? this : null);
                    playersInGame.Add(newPlayer);
                }
                else if (i != getMyPlayerIndex())       // else update existing player if not you
                {
                    playersInGame[i].updatePosition(pos);
                }
                ++i;
            }
            // remove players from the end of list
            while (playersInGame.Count > 0 && playersInGame.Count > numAlivePlayers)
            {
                Destroy(playersInGame[playersInGame.Count - 1].gameObject);
                playersInGame.RemoveAt(playersInGame.Count - 1);
            }
            break;

        case PacketType.SPAWN_BOMB:
            level.placeBomb(packet.ReadVector3(), false, packet.ReadInt());
            break;

        case PacketType.SPAWN_POWERUP:
            level.placePowerUp(packet.ReadVector3(), packet.ReadInt());
            break;

        case PacketType.PLAYER_JOINED_ROOM:        // a player joined your room
            int     pjid    = packet.ReadInt();
            string  pjname  = packet.ReadString();
            Color32 pjcolor = packet.ReadColor();
            playersInRoom.Add(new PlayerInfo(pjid, pjname, pjcolor, false));
            lobbyUI.updateRoomNames();
            lobbyUI.logConnectionMessage(pjname, pjcolor, true, false);
            break;

        case PacketType.PLAYER_LEFT_ROOM:        // a player left your room
            int plid = packet.ReadInt();
            for (int i = 0; i < playersInRoom.Count; ++i)
            {
                PlayerInfo pi = playersInRoom[i];
                if (pi.id == plid)
                {
                    playersInRoom.RemoveAt(i);
                    indexInRoom = -1;
                    lobbyUI.updateRoomNames();
                    lobbyUI.logConnectionMessage(pi.name, pi.color, false, false);
                    break;
                }
            }
            break;

        case PacketType.CHAT_MESSAGE:
            lobbyUI.logChatMessage(packet.ReadString(), packet.ReadColor(), packet.ReadString());
            break;

        case PacketType.CHANGE_ROOM:
            // only time client will receive a packet of this type is if
            // they tried to change a room but it failed somehow
            lobbyUI.onChangeRoomFailure(packet.ReadBool());
            waitingForRoomChangeResponse = false;
            break;

        case PacketType.YOU_JOINED_ROOM:      // you joined a room
            //lobbyUI.setLobbyActive(true);
            waitingForRoomChangeResponse = false;
            roomName = packet.ReadString();
            len      = packet.ReadInt();
            playersInRoom.Clear();
            for (int i = 0; i < len; ++i)
            {
                playersInRoom.Add(new PlayerInfo(
                                      packet.ReadInt(), packet.ReadString(), packet.ReadColor(), packet.ReadBool()));
            }
            indexInRoom = -1;
            if (justConnected)
            {
                justConnected = false;
                PlayerInfo pi = getMyPlayer();
                lobbyUI.logConnectionMessage(pi.name, pi.color, true, true);
            }

            lobbyUI.changedRoom(roomName);
            lobbyUI.updateRoomNames();
            break;

        case PacketType.PLAYER_JOINED_SERVER:
            lobbyUI.logConnectionMessage(packet.ReadString(), packet.ReadColor(), true, true);
            break;

        case PacketType.PLAYER_LEFT_SERVER:
            lobbyUI.logConnectionMessage(packet.ReadString(), packet.ReadColor(), false, true);
            break;

        case PacketType.ROOM_LIST_UPDATE:
            List <string> roomListUpdate = new List <string>();
            len = packet.ReadInt();
            for (int i = 0; i < len; ++i)
            {
                roomListUpdate.Add(packet.ReadString());
                roomListUpdate.Add(packet.ReadInt().ToString());
            }
            lobbyUI.updateRoomList(roomListUpdate);
            break;

        case PacketType.SET_READY:
            string playerName = packet.ReadString();
            for (int i = 0; i < playersInRoom.Count; ++i)
            {
                if (playersInRoom[i].name == playerName)
                {
                    playersInRoom[i].ready = packet.ReadBool();
                    break;
                }
            }
            lobbyUI.updateRoomNames();
            break;

        case PacketType.GAME_COUNTDOWN:
            int t = packet.ReadInt();
            if (t == 0)
            {
                lobbyUI.logMessage("Game Start!", Color.yellow);

                GameObject levelGO = GameObject.Find("Level");
                if (!levelGO)
                {
                    Debug.LogError("CLIENT: can't find level!!!");
                    return;
                }

                level = levelGO.GetComponent <Level>();
                LevelData ld = new LevelData();
                level.ld = ld;

                // read level data
                len = packet.ReadInt();
                for (int i = 0; i < len; ++i)
                {
                    ld.setTile(i, packet.ReadByte());
                }
                level.buildMesh();

                lobbyUI.setLobbyActive(false);
                lobbyUI.fadeInFromBlack();
            }
            else
            {
                lobbyUI.logMessage(t + "...", Color.yellow);
            }
            break;

        case PacketType.GAME_END:
            string message = packet.ReadString();
            // destroy remaining player objects
            for (int i = 0; i < playersInGame.Count; ++i)
            {
                Destroy(playersInGame[i].gameObject);
            }
            // find and destroy all leftover powerups
            GameObject[] powerups = GameObject.FindGameObjectsWithTag("PowerUp");
            for (int i = 0; i < powerups.Length; ++i)
            {
                Destroy(powerups[i]);
            }
            playersInGame.Clear();
            lobbyUI.fadeOutWithText(message);

            break;

        default:
            break;
        }
    }