Exemplo n.º 1
0
        /// <summary>
        ///     Deserializes an IMapManager and ITileDefinitionManager from a properly formatted NetMessage.
        /// </summary>
        /// <param name="message">The message containing a serialized map and tileDefines.</param>
        private void HandleTileMap(MsgMap message)
        {
            Debug.Assert(_netManager.IsClient, "Why is the server calling this?");

            _gridsReceived++;

            var mapIndex  = message.MapIndex;
            var gridIndex = message.GridIndex;

            var chunkSize  = message.ChunkSize;
            var chunkCount = message.ChunkDefs.Length;

            if (!TryGetMap(mapIndex, out var map))
            {
                map = CreateMap(mapIndex);
            }

            if (!map.GridExists(gridIndex))
            {
                GetMap(mapIndex).CreateGrid(gridIndex, chunkSize);
            }

            var grid = GetMap(mapIndex).GetGrid(gridIndex);

            SuppressOnTileChanged = true;
            var modified = new List <(int x, int y, Tile tile)>();

            for (var i = 0; i < chunkCount; ++i)
            {
                var chunkPos = new MapGrid.Indices(message.ChunkDefs[i].X, message.ChunkDefs[i].Y);
                var chunk    = grid.GetChunk(chunkPos);

                var counter = 0;
                for (ushort x = 0; x < chunk.ChunkSize; x++)
                {
                    for (ushort y = 0; y < chunk.ChunkSize; y++)
                    {
                        var tile = (Tile)message.ChunkDefs[i].Tiles[counter];
                        if (chunk.GetTile(x, y).Tile != tile)
                        {
                            chunk.SetTile(x, y, tile);
                            modified.Add((x + chunk.X * chunk.ChunkSize, y + chunk.Y * chunk.ChunkSize, tile));
                        }
                        counter++;
                    }
                }
            }

            SuppressOnTileChanged = false;
            if (modified.Count != 0)
            {
                GridChanged?.Invoke(this, new GridChangedEventArgs(grid, modified));
            }

            if (_gridsReceived == _gridsToReceive)
            {
                IoCManager.Resolve <IEntityManager>().MapsInitialized = true;
            }
        }
Exemplo n.º 2
0
 public void Initialize()
 {
     InitializeRows(_settings.GridLength, _settings.GridWidth);
     CreateRowCellLinks();
     GenerateBombs(_settings.BombsCount);
     Initialized = true;
     GridChanged?.Invoke(this, EventArgs.Empty);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Changes to level to a given map
        /// </summary>
        /// <param name="map">New streamed tile map</param>
        /// <param name="disposeCurrentMap">Disposes the current level when true</param>
        public void Changelevel(StreamedTileMap map, bool disposeCurrentMap)
        {
            if (!disposeCurrentMap && currentLevel != null)
            {
                maps.Add(currentLevel);
            }
            currentLevel = map;

            GridChanged?.Invoke(this, new GridEventArgs(currentLevel.GridRow, currentLevel.GridColumn, -1, -1, false));
        }
        /// <summary>
        ///     Deserializes an IMapManager and ITileDefinitionManager from a properly formatted NetMessage.
        /// </summary>
        /// <param name="message">The message containing a serialized map and tileDefines.</param>
        private void HandleTileMap(MsgMap message)
        {
            Debug.Assert(_netManager.IsClient, "Why is the server calling this?");

            _gridsReceived++;

            var mapIndex  = message.MapIndex;
            var gridIndex = message.GridIndex;

            _defManager.RegisterServerTileMapping(message);

            var chunkSize  = message.ChunkSize;
            var chunkCount = message.ChunkDefs.Length;

            if (!TryGetMap(mapIndex, out var map))
            {
                map = CreateMap(mapIndex);
            }

            if (!map.GridExists(gridIndex))
            {
                GetMap(mapIndex).CreateGrid(gridIndex, chunkSize);
            }

            var grid = GetMap(mapIndex).GetGrid(gridIndex);

            SuppressOnTileChanged = true;

            for (var i = 0; i < chunkCount; ++i)
            {
                var chunkPos = new MapGrid.Indices(message.ChunkDefs[i].X, message.ChunkDefs[i].Y);
                var chunk    = grid.GetChunk(chunkPos);

                var counter = 0;
                for (ushort x = 0; x < chunk.ChunkSize; x++)
                {
                    for (ushort y = 0; y < chunk.ChunkSize; y++)
                    {
                        chunk.SetTile(x, y, (Tile)message.ChunkDefs[i].Tiles[counter]);
                        counter++;
                    }
                }
            }

            SuppressOnTileChanged = false;
            GridChanged?.Invoke(this, new GridChangedEventArgs(grid));

            if (_gridsReceived == _gridsToReceive)
            {
                IoCManager.Resolve <IEntityManager>().MapsInitialized = true;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Changes to level to map that will be generated based on the current position
        /// </summary>
        /// <param name="tileColumn">Tile column index</param>
        /// <param name="tileRow">Tile row index</param>
        /// <param name="disposeCurrentMap">Disposes the current level when true</param>
        public void Changelevel(int tileColumn, int tileRow, bool disposeCurrentMap)
        {
            if (!disposeCurrentMap && currentLevel != null)
            {
                maps.Add(CurrentLevel);
            }

            UnSub();
            currentLevel = generator.GenerateMap(tileColumn, tileRow);
            Sub();

            GridChanged?.Invoke(this, new GridEventArgs(currentLevel.GridRow, currentLevel.GridColumn, -1, -1, false));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Changes to level to map that will be generated based on the current position
        /// </summary>
        /// <param name="settings">Setting to generate the new map</param>
        /// <param name="tileColumn">Tile column index</param>
        /// <param name="tileRow">Tile row index</param>
        /// <param name="disposeCurrentMap">Disposes the current level when true</param>
        public void Changelevel(GeneratorSettings settings, int tileColumn, int tileRow, bool disposeCurrentMap)
        {
            this.settings = settings;
            if (!disposeCurrentMap && currentLevel != null)
            {
                maps.Add(CurrentLevel);
            }
            generator = new TileMapGenerator();
            UnSub();
            currentLevel = generator.GenerateMap(settings, spreads, tileColumn, tileRow);
            Sub();

            GridChanged?.Invoke(this, new GridEventArgs(currentLevel.GridRow, currentLevel.GridColumn, -1, -1, false));
        }
Exemplo n.º 7
0
 /// <summary>
 /// Tries to update the map when the maps it's fully loaded
 /// </summary>
 /// <param name="currentGridRow">Grid row index</param>
 /// <param name="currentGridColumn">Grid column index</param>
 /// <returns>Returns true on success</returns>
 private bool TryMapUpdate(int currentGridRow, int currentGridColumn)
 {
     if (newMapAvalible)
     {
         if ((currentGridRow == newMap.maps[4].GridRow && currentGridColumn == newMap.maps[4].GridColumn) || newMapRequested)
         {
             List <TileMapPart> oldMap = maps;
             maps            = newMap.maps;
             newMap.maps     = oldMap;
             CurrentMapIndex = 4;
             newMapAvalible  = false;
             newMapRequested = false;
             GridChanged?.Invoke(this, new GridEventArgs(GridRow, GridColumn, newMap.maps[4].GridRow, newMap.maps[4].GridColumn, !newMapRequested));
             return(true);
         }
         newMapAvalible = false;
     }
     return(false);
 }
Exemplo n.º 8
0
        public void ApplyGameStatePre(GameStateMapData data)
        {
            DebugTools.Assert(_netManager.IsClient, "Only the client should call this.");

            // First we need to figure out all the NEW MAPS.
            // And make their default grids too.
            foreach (var(mapId, gridId) in data.CreatedMaps)
            {
                if (_maps.ContainsKey(mapId))
                {
                    continue;
                }
                var gridCreation = data.CreatedGrids[gridId];
                DebugTools.Assert(gridCreation.IsTheDefault);

                var newMap = new Map(this, mapId);
                _maps.Add(mapId, newMap);
                MapCreated?.Invoke(this, new MapEventArgs(newMap));
                newMap.DefaultGrid = CreateGrid(newMap.Index, gridId, gridCreation.ChunkSize, gridCreation.SnapSize);
            }

            // Then make all the other grids.
            foreach (var(gridId, creationDatum) in data.CreatedGrids)
            {
                if (creationDatum.IsTheDefault || _grids.ContainsKey(gridId))
                {
                    continue;
                }

                CreateGrid(data.GridData[gridId].Coordinates.MapId, gridId, creationDatum.ChunkSize,
                           creationDatum.SnapSize);
            }

            SuppressOnTileChanged = true;
            // Ok good all the grids and maps exist now.
            foreach (var(gridId, gridDatum) in data.GridData)
            {
                var grid = _grids[gridId];
                if (grid.MapID != gridDatum.Coordinates.MapId)
                {
                    throw new NotImplementedException("Moving grids between maps is not yet implemented");
                }

                grid.WorldPosition = gridDatum.Coordinates.Position;

                var modified = new List <(MapIndices position, Tile tile)>();
                foreach (var chunkData in gridDatum.ChunkData)
                {
                    var chunk = grid.GetChunk(chunkData.Index);
                    DebugTools.Assert(chunkData.TileData.Length == grid.ChunkSize * grid.ChunkSize);

                    var counter = 0;
                    for (ushort x = 0; x < grid.ChunkSize; x++)
                    {
                        for (ushort y = 0; y < grid.ChunkSize; y++)
                        {
                            var tile = chunkData.TileData[counter++];
                            if (chunk.GetTile(x, y).Tile != tile)
                            {
                                chunk.SetTile(x, y, tile);
                                modified.Add((new MapIndices(chunk.X * grid.ChunkSize + x, chunk.Y * grid.ChunkSize + y), tile));
                            }
                        }
                    }
                }

                if (modified.Count != 0)
                {
                    GridChanged?.Invoke(this, new GridChangedEventArgs(grid, modified));
                }
            }

            SuppressOnTileChanged = false;
        }
Exemplo n.º 9
0
 private void OnGridChanged()
 {
     GridChanged?.Invoke(this, new EventArgs());
 }
Exemplo n.º 10
0
 void _RaiseGridChanged()
 {
     GridChanged?.Invoke(this);
 }