Ejemplo n.º 1
0
        public void SendIntoGame(string connectionID, string playerName)
        {
            Vector3 spawnPos = ServerSideGameManager.instance.spawnPositions[(count++) % ServerSideGameManager.instance.spawnPositions.Count];

            serverMasterController = ServerSideGameManager.instance.InstantiatePlayer((int)EnumData.Heroes.Medusa);
            serverMasterController.Initialise(id, connectionID, playerName, spawnPos);


            //This will send all other players information to our new player
            foreach (ServerSideClient client in Server.clients.Values)
            {
                if (client.serverMasterController != null)
                {
                    if (client.id != id)
                    {
                        ServerSend.SpawnPlayer(id, client.serverMasterController);
                    }
                }
            }


            //This will send new player information to all other players and themselves
            foreach (ServerSideClient client in Server.clients.Values)
            {
                if (client.serverMasterController != null)
                {
                    ServerSend.SpawnPlayer(client.id, serverMasterController);
                }
            }

            List <WorldGridItem> worldGridItemList = new List <WorldGridItem>();

            for (int i = 0; i < ServerSideGameManager.instance.toNetworkTileType.Count; i++)
            {
                List <Vector3Int> positionsOfTile = GridManager.instance.GetAllPositionForTileMap(ServerSideGameManager.instance.toNetworkTileType[i]);
                WorldGridItem     worldGridItem   = new WorldGridItem((int)ServerSideGameManager.instance.toNetworkTileType[i], positionsOfTile);
                worldGridItemList.Add(worldGridItem);
            }

            WorldUpdate worldUpdate = new WorldUpdate(ServerSideGameManager.instance.serverWorldSequenceNumber, worldGridItemList.ToArray(), new GameData((int)ServerSideGameManager.instance.currentGameState, ServerSideGameManager.instance.timeToStartMatch), ServerSideGameManager.projectilesDic, ServerSideGameManager.enemiesDic, ServerSideGameManager.animatingStaticTileDic, GridManager.instance.portalTracker.portalEntranceDic);

            ServerSend.SpawnGridWorld(id, worldUpdate);
        }
Ejemplo n.º 2
0
        private void FixedUpdate()
        {
            serverWorldSequenceNumber++;

            DealItemSpawn();
            DealCereberausHeadRotation();
            DealWorldDestruction();
            DealMatchStartTime();
            //////Debug.Log("<color=blue>inputsequence </color>"+ playerMovingCommandSequenceNumber + "<color=blue>inputs </color> "+ inputs[0]+" "+inputs[1]+" "+inputs[2]+" "+inputs[3]);

            List <WorldGridItem> worldGridItemList = new List <WorldGridItem>();

            for (int i = 0; i < toNetworkTileType.Count; i++)
            {
                List <Vector3Int> positionsOfTile = GridManager.instance.GetAllPositionForTileMap(toNetworkTileType[i]);
                //Debug.LogError("TileType: " + toNetworkTileType[i] + " positionsOfTile " + positionsOfTile.Count);
                WorldGridItem worldGridItem = new WorldGridItem((int)toNetworkTileType[i], positionsOfTile);
                worldGridItemList.Add(worldGridItem);
            }


            GameData gameData = new GameData((int)currentGameState, timeToStartMatch);

            worldUpdatesToBeSentFromServerToClient.Add(new WorldUpdate(serverWorldSequenceNumber, worldGridItemList.ToArray(), gameData, new Dictionary <int, ProjectileData>(projectilesDic), new Dictionary <int, EnemyData>(enemiesDic), new Dictionary <int, AnimatingStaticTile>(animatingStaticTileDic), GridManager.instance.portalTracker.portalEntranceDic));

            //Local client sending data
            if (worldUpdatesToBeSentFromServerToClient.Count >= snapShotsInOnePacket)
            {
                if (previousHistoryForWorldUpdatesToBeSentToServerCollection.Count > packetHistorySize)
                {
                    previousHistoryForWorldUpdatesToBeSentToServerCollection.RemoveAt(0);
                }

                ServerSend.WorldUpdate(worldUpdatesToBeSentFromServerToClient, previousHistoryForWorldUpdatesToBeSentToServerCollection);

                previousHistoryForWorldUpdatesToBeSentToServerCollection.Add(new PreviousWorldUpdatePacks(worldUpdatesToBeSentFromServerToClient.ToArray()));

                worldUpdatesToBeSentFromServerToClient.Clear();

                //Debug.Log("<color=red>--------------------------------------------------------------------</color>");
            }
        }
        public static void SpawnGridWorld(Packet packet)
        {
            WorldGridItem[] worldItems = new WorldGridItem[packet.ReadInt()];
            for (int i = 0; i < worldItems.Length; i++)
            {
                int tileType = packet.ReadInt();

                int cellPositionCount = packet.ReadInt();

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

            Dictionary <int, ProjectileData>      keyValuePairs          = new Dictionary <int, ProjectileData>();
            Dictionary <int, EnemyData>           keyValueEnemyPairs     = new Dictionary <int, EnemyData>();
            Dictionary <int, AnimatingStaticTile> keyValuePairsAnimation = new Dictionary <int, AnimatingStaticTile>();
            Dictionary <Vector3Int, PortalInfo>   keyValuePairsPortal    = new Dictionary <Vector3Int, PortalInfo>();

            int projectileDatasCount = packet.ReadInt();

            for (int i = 0; i < projectileDatasCount; i++)
            {
                int uid = packet.ReadInt();
                int projectileOwnerId = packet.ReadInt();

                int projectileTileType = packet.ReadInt();

                Vector3 projectilePosition = packet.ReadVector3();

                int faceDirection = packet.ReadInt();

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

            int enemyDatasCount = packet.ReadInt();

            for (int i = 0; i < enemyDatasCount; i++)
            {
                int uid = packet.ReadInt();

                int leaderNetworkId = packet.ReadInt();

                int leadercharacterType = packet.ReadInt();

                int enemyType = packet.ReadInt();

                int animationIndexNumber = packet.ReadInt();

                int faceDirection = packet.ReadInt();

                int enemyState = packet.ReadInt();

                Vector3 enemyPosition = packet.ReadVector3();

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

            int animationStaticTileCount = packet.ReadInt();

            for (int i = 0; i < animationStaticTileCount; i++)
            {
                int uid = packet.ReadInt();

                int tileType = packet.ReadInt();

                int animationSpIndex = packet.ReadInt();

                Vector3Int pos = packet.ReadVector3Int();

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


            int portalElementCount = packet.ReadInt();

            for (int i = 0; i < portalElementCount; i++)
            {
                Vector3Int portalInLet = packet.ReadVector3Int();

                Vector3Int portalOutLet = packet.ReadVector3Int();

                int portalOwner = packet.ReadInt();

                keyValuePairsPortal.Add(portalInLet, new PortalInfo(portalOwner, portalOutLet));
            }

            int gameState          = packet.ReadInt();
            int gameMatchStartTime = packet.ReadInt();

            int worldUpdateSequenceNumber = packet.ReadInt();

            WorldUpdate worldUpdate = new WorldUpdate(worldUpdateSequenceNumber, worldItems, new GameData(gameState, gameMatchStartTime), keyValuePairs, keyValueEnemyPairs, keyValuePairsAnimation, keyValuePairsPortal);

            ClientSideGameManager.instance.SpawnWorldGridElements(worldUpdate);
        }
        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));
                }
            }
        }