Пример #1
0
        public static void WorldUpdate(List <WorldUpdate> worldUpdates, List <PreviousWorldUpdatePacks> previousWorldUpdatePacks)
        {
            using (Packet packet = new Packet((int)ServerPackets.worldUpdates))
            {
                Packet compressedPacked = new Packet();
                compressedPacked.Write(worldUpdates.Count);

                for (int i = 0; i < worldUpdates.Count; i++)
                {
                    compressedPacked.Write(worldUpdates[i].worldGridItems.Length);
                    foreach (WorldGridItem worldGridItem in worldUpdates[i].worldGridItems)
                    {
                        compressedPacked.Write(worldGridItem.tileType);

                        compressedPacked.Write(worldGridItem.updatedCellGridWorldPositionList.Count);

                        foreach (Vector2Int v in worldGridItem.updatedCellGridWorldPositionList)
                        {
                            compressedPacked.Write(v);
                        }
                    }

                    compressedPacked.Write(worldUpdates[i].projectileDatas.Count);
                    foreach (KeyValuePair <int, ProjectileData> keyValuePair in worldUpdates[i].projectileDatas)
                    {
                        compressedPacked.Write(keyValuePair.Key);

                        compressedPacked.Write(keyValuePair.Value.projectileOwnerId);

                        compressedPacked.Write(keyValuePair.Value.projectileType);

                        compressedPacked.Write(keyValuePair.Value.projectilePosition);

                        compressedPacked.Write(keyValuePair.Value.faceDirection);
                    }

                    compressedPacked.Write(worldUpdates[i].enemyDatas.Count);
                    foreach (KeyValuePair <int, EnemyData> keyValuePair in worldUpdates[i].enemyDatas)
                    {
                        compressedPacked.Write(keyValuePair.Key);

                        compressedPacked.Write(keyValuePair.Value.leaderNetworkId);
                        compressedPacked.Write(keyValuePair.Value.leadercharacterType);

                        compressedPacked.Write(keyValuePair.Value.enemyType);

                        compressedPacked.Write(keyValuePair.Value.animationIndexNumber);

                        compressedPacked.Write(keyValuePair.Value.faceDirection);

                        compressedPacked.Write(keyValuePair.Value.enemyState);

                        compressedPacked.Write(keyValuePair.Value.enemyPosition);
                    }

                    compressedPacked.Write(worldUpdates[i].animatingTileDatas.Count);
                    foreach (KeyValuePair <int, AnimatingStaticTile> keyValuePair in worldUpdates[i].animatingTileDatas)
                    {
                        compressedPacked.Write(keyValuePair.Key);

                        compressedPacked.Write(keyValuePair.Value.tileType);

                        compressedPacked.Write(keyValuePair.Value.animationSpriteIndex);

                        compressedPacked.Write(keyValuePair.Value.pos);
                    }

                    compressedPacked.Write(worldUpdates[i].portalEntranceDic.Count);
                    foreach (KeyValuePair <Vector3Int, PortalInfo> item in worldUpdates[i].portalEntranceDic)
                    {
                        compressedPacked.Write(item.Key);

                        compressedPacked.Write(item.Value.portalOutlet);

                        compressedPacked.Write(item.Value.portalOwner);
                    }

                    compressedPacked.Write(worldUpdates[i].gameData.gameState);
                    compressedPacked.Write(worldUpdates[i].gameData.matchStartTime);

                    compressedPacked.Write(worldUpdates[i].sequenceNumber);
                    //Debug.LogWarning("<color=green>Sending inputs packet to server </color>playerMovingCommandSequenceNumber : " + inputCommands[i].sequenceNumber + " w " + inputCommands[i].commands[0] + " a " + inputCommands[i].commands[1] + " s " + inputCommands[i].commands[2] + " d " + inputCommands[i].commands[3] + "<color=green> adding previous : </color>");
                }

                compressedPacked.Write(previousWorldUpdatePacks.Count);
                for (int i = 0; i < previousWorldUpdatePacks.Count; i++)
                {
                    compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates.Length);
                    for (int j = 0; j < previousWorldUpdatePacks[i].previousWorldUpdates.Length; j++)
                    {
                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].worldGridItems.Length);

                        foreach (WorldGridItem previousWorldGridItem in previousWorldUpdatePacks[i].previousWorldUpdates[j].worldGridItems)
                        {
                            compressedPacked.Write(previousWorldGridItem.tileType);

                            compressedPacked.Write(previousWorldGridItem.updatedCellGridWorldPositionList.Count);

                            foreach (Vector2Int v in previousWorldGridItem.updatedCellGridWorldPositionList)
                            {
                                compressedPacked.Write(v);
                            }
                        }

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].projectileDatas.Count);
                        foreach (KeyValuePair <int, ProjectileData> keyValuePair in previousWorldUpdatePacks[i].previousWorldUpdates[j].projectileDatas)
                        {
                            compressedPacked.Write(keyValuePair.Key);

                            compressedPacked.Write(keyValuePair.Value.projectileOwnerId);

                            compressedPacked.Write(keyValuePair.Value.projectileType);

                            compressedPacked.Write(keyValuePair.Value.projectilePosition);

                            compressedPacked.Write(keyValuePair.Value.faceDirection);
                        }

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].enemyDatas.Count);
                        foreach (KeyValuePair <int, EnemyData> keyValuePair in previousWorldUpdatePacks[i].previousWorldUpdates[j].enemyDatas)
                        {
                            compressedPacked.Write(keyValuePair.Key);

                            compressedPacked.Write(keyValuePair.Value.leaderNetworkId);
                            compressedPacked.Write(keyValuePair.Value.leadercharacterType);

                            compressedPacked.Write(keyValuePair.Value.enemyType);

                            compressedPacked.Write(keyValuePair.Value.animationIndexNumber);

                            compressedPacked.Write(keyValuePair.Value.faceDirection);

                            compressedPacked.Write(keyValuePair.Value.enemyState);

                            compressedPacked.Write(keyValuePair.Value.enemyPosition);
                        }

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].animatingTileDatas.Count);
                        foreach (KeyValuePair <int, AnimatingStaticTile> keyValuePair in previousWorldUpdatePacks[i].previousWorldUpdates[j].animatingTileDatas)
                        {
                            compressedPacked.Write(keyValuePair.Key);

                            compressedPacked.Write(keyValuePair.Value.tileType);

                            compressedPacked.Write(keyValuePair.Value.animationSpriteIndex);

                            compressedPacked.Write(keyValuePair.Value.pos);
                        }

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].portalEntranceDic.Count);
                        foreach (KeyValuePair <Vector3Int, PortalInfo> item in previousWorldUpdatePacks[i].previousWorldUpdates[j].portalEntranceDic)
                        {
                            compressedPacked.Write(item.Key);

                            compressedPacked.Write(item.Value.portalOutlet);

                            compressedPacked.Write(item.Value.portalOwner);
                        }

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].gameData.gameState);
                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].gameData.matchStartTime);

                        compressedPacked.Write(previousWorldUpdatePacks[i].previousWorldUpdates[j].sequenceNumber);
                    }
                }
                //Debug.LogError("Before " + compressedPacked.Length());
                byte[] compressedBytes = ByteSizeManupulator.Compress(compressedPacked.ToArray());
                //Debug.LogError("After " + compressedBytes.Length);
                packet.SetBytes(compressedBytes);
                SendUDPDataToAll(packet);
            }
        }
        public static void WorldStateUpdated(Packet packet)
        {
            //Debug.LogError("Before " + packet.Length());
            byte[] unreadByteArr      = packet.ReadBytes(packet.UnreadLength());
            byte[] decompressedBytes  = ByteSizeManupulator.Decompress(unreadByteArr);
            Packet decompressedPacket = new Packet(decompressedBytes);
            //Debug.LogError("After " + decompressedPacket.Length());


            int dataCount = decompressedPacket.ReadInt();

            for (int j = 0; j < dataCount; j++)
            {
                WorldGridItem[] worldItems = new WorldGridItem[decompressedPacket.ReadInt()];
                for (int i = 0; i < worldItems.Length; i++)
                {
                    int tileType = decompressedPacket.ReadInt();

                    int cellPositionCount = decompressedPacket.ReadInt();

                    List <Vector3Int> cellPositionList = new List <Vector3Int>();
                    for (int k = 0; k < cellPositionCount; k++)
                    {
                        Vector2Int cell = decompressedPacket.ReadVector2Int();
                        cellPositionList.Add(new Vector3Int(cell.x, cell.y, 0));
                    }

                    worldItems[i] = new WorldGridItem(tileType, cellPositionList);
                }
                Dictionary <int, ProjectileData> keyValuePairs = new Dictionary <int, ProjectileData>();

                int projectileDatasCount = decompressedPacket.ReadInt();
                for (int i = 0; i < projectileDatasCount; i++)
                {
                    int     projectileId       = decompressedPacket.ReadInt();
                    int     projectileOwnerId  = decompressedPacket.ReadInt();
                    int     projectileTileType = decompressedPacket.ReadInt();
                    Vector3 projectilePosition = decompressedPacket.ReadVector3();
                    int     faceDirection      = decompressedPacket.ReadInt();

                    keyValuePairs.Add(projectileId, new ProjectileData(projectileId, projectileOwnerId, projectileTileType, projectilePosition, faceDirection));
                }

                Dictionary <int, EnemyData> enemyValuePairs = new Dictionary <int, EnemyData>();
                int enemyDatasCount = decompressedPacket.ReadInt();
                for (int i = 0; i < enemyDatasCount; i++)
                {
                    int     enemyId              = decompressedPacket.ReadInt();
                    int     leaderNetworkId      = decompressedPacket.ReadInt();
                    int     leadercharacterType  = decompressedPacket.ReadInt();
                    int     enemyType            = decompressedPacket.ReadInt();
                    int     animationIndexNumber = decompressedPacket.ReadInt();
                    int     faceDirection        = decompressedPacket.ReadInt();
                    int     enemyState           = decompressedPacket.ReadInt();
                    Vector3 enemyPosition        = decompressedPacket.ReadVector3();

                    enemyValuePairs.Add(enemyId, new EnemyData(enemyId, leaderNetworkId, leadercharacterType, enemyType, animationIndexNumber, faceDirection, enemyState, enemyPosition));
                }

                Dictionary <int, AnimatingStaticTile> keyValuePairsAnimation = new Dictionary <int, AnimatingStaticTile>();
                int animatingTilesCount = decompressedPacket.ReadInt();
                for (int i = 0; i < animatingTilesCount; i++)
                {
                    int uid = decompressedPacket.ReadInt();

                    int tileType = decompressedPacket.ReadInt();

                    int animationSpIndex = decompressedPacket.ReadInt();

                    Vector3Int pos = decompressedPacket.ReadVector3Int();

                    keyValuePairsAnimation.Add(uid, new AnimatingStaticTile(uid, tileType, animationSpIndex, pos));
                }

                Dictionary <Vector3Int, PortalInfo> keyValuePairsPortals = new Dictionary <Vector3Int, PortalInfo>();
                int portalinfoCount = decompressedPacket.ReadInt();
                for (int i = 0; i < portalinfoCount; i++)
                {
                    Vector3Int portalInLet = decompressedPacket.ReadVector3Int();

                    Vector3Int portalOutlet = decompressedPacket.ReadVector3Int();

                    int portalOwner = decompressedPacket.ReadInt();

                    keyValuePairsPortals.Add(portalInLet, new PortalInfo(portalOwner, portalOutlet));
                }
                int gameState                 = decompressedPacket.ReadInt();
                int gameMatchStartTime        = decompressedPacket.ReadInt();
                int worldUpdateSequenceNumber = decompressedPacket.ReadInt();
                //Debug.LogWarning("<color=green>receiving inputs decompressedPacket to server </color>playerMovingCommandSequenceNumber : " + worldUpdateSequenceNumber + " w " + inputs[0] + " a " + inputs[1] + " s " + inputs[2] + " d " + inputs[3]);
                ClientSideGameManager.instance.AccumulateWorldUpdatesToBePlayedOnClientFromServer(new WorldUpdate(worldUpdateSequenceNumber, worldItems, new GameData(gameState, gameMatchStartTime), keyValuePairs, enemyValuePairs, keyValuePairsAnimation, keyValuePairsPortals));
            }

            int previousWorldUpdatePacks = decompressedPacket.ReadInt();

            for (int i = 0; i < previousWorldUpdatePacks; i++)
            {
                int previousWorldUpdateWorldItemsInPacks = decompressedPacket.ReadInt();
                for (int j = 0; j < previousWorldUpdateWorldItemsInPacks; j++)
                {
                    WorldGridItem[] previousDataWorldItems = new WorldGridItem[decompressedPacket.ReadInt()];
                    for (int k = 0; k < previousDataWorldItems.Length; k++)
                    {
                        int tileType          = decompressedPacket.ReadInt();
                        int cellPositionCount = decompressedPacket.ReadInt();

                        List <Vector3Int> cellPositionList = new List <Vector3Int>();
                        for (int l = 0; l < cellPositionCount; l++)
                        {
                            Vector2Int cell = decompressedPacket.ReadVector2Int();
                            cellPositionList.Add(new Vector3Int(cell.x, cell.y, 0));
                        }
                        previousDataWorldItems[k] = new WorldGridItem(tileType, cellPositionList);
                    }

                    Dictionary <int, ProjectileData> previouskeyValuePairs = new Dictionary <int, ProjectileData>();
                    int previousProjectileDatasCount = decompressedPacket.ReadInt();
                    for (int k = 0; k < previousProjectileDatasCount; k++)
                    {
                        int     projectileId       = decompressedPacket.ReadInt();
                        int     projectileOwnerId  = decompressedPacket.ReadInt();
                        int     projectileTileType = decompressedPacket.ReadInt();
                        Vector3 projectilePosition = decompressedPacket.ReadVector3();
                        int     faceDirection      = decompressedPacket.ReadInt();

                        previouskeyValuePairs.Add(projectileId, new ProjectileData(projectileId, projectileOwnerId, projectileTileType, projectilePosition, faceDirection));
                    }

                    Dictionary <int, EnemyData> previousEnemyValuePairs = new Dictionary <int, EnemyData>();
                    int previousEnemyDatasCount = decompressedPacket.ReadInt();
                    for (int k = 0; k < previousEnemyDatasCount; k++)
                    {
                        int     enemyId              = decompressedPacket.ReadInt();
                        int     leaderNetworkId      = decompressedPacket.ReadInt();
                        int     leadercharacterType  = decompressedPacket.ReadInt();
                        int     enemyType            = decompressedPacket.ReadInt();
                        int     animationIndexNumber = decompressedPacket.ReadInt();
                        int     faceDirection        = decompressedPacket.ReadInt();
                        int     enemyState           = decompressedPacket.ReadInt();
                        Vector3 enemyPosition        = decompressedPacket.ReadVector3();

                        previousEnemyValuePairs.Add(enemyId, new EnemyData(enemyId, leaderNetworkId, leadercharacterType, enemyType, animationIndexNumber, faceDirection, enemyState, enemyPosition));
                    }

                    Dictionary <int, AnimatingStaticTile> keyValuePairsAnimation = new Dictionary <int, AnimatingStaticTile>();
                    int animatingTilesCount = decompressedPacket.ReadInt();
                    for (int k = 0; k < animatingTilesCount; k++)
                    {
                        int uid = decompressedPacket.ReadInt();

                        int tileType = decompressedPacket.ReadInt();

                        int animationSpIndex = decompressedPacket.ReadInt();

                        Vector3Int pos = decompressedPacket.ReadVector3Int();

                        keyValuePairsAnimation.Add(uid, new AnimatingStaticTile(uid, tileType, animationSpIndex, pos));
                    }

                    Dictionary <Vector3Int, PortalInfo> keyValuePairsPortals = new Dictionary <Vector3Int, PortalInfo>();
                    int portalinfoCount = decompressedPacket.ReadInt();
                    for (int k = 0; k < portalinfoCount; k++)
                    {
                        Vector3Int portalInLet = decompressedPacket.ReadVector3Int();

                        Vector3Int portalOutlet = decompressedPacket.ReadVector3Int();

                        int portalOwner = decompressedPacket.ReadInt();

                        keyValuePairsPortals.Add(portalInLet, new PortalInfo(portalOwner, portalOutlet));
                    }
                    int gameState          = decompressedPacket.ReadInt();
                    int gameMatchStartTime = decompressedPacket.ReadInt();
                    int previousSeqNo      = decompressedPacket.ReadInt();
                    ClientSideGameManager.instance.AccumulateWorldUpdatesToBePlayedOnClientFromServer(new WorldUpdate(previousSeqNo, previousDataWorldItems, new GameData(gameState, gameMatchStartTime), previouskeyValuePairs, previousEnemyValuePairs, keyValuePairsAnimation, keyValuePairsPortals));
                }
            }
        }