Example #1
0
 internal TileRef(int argMap, int gridIndex, MapGrid.Indices gridTile, Tile tile)
 {
     _mapIndex  = argMap;
     _gridTile  = gridTile;
     _gridIndex = gridIndex;
     _tile      = tile;
 }
        /// <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?");

            var mapIndex = message.MapIndex;

            _defManager.RegisterServerTileMapping(message);

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

            if (!_mapManager.TryGetGrid(mapIndex, out IMapGrid grid))
            {
                grid = _mapManager.CreateGrid(mapIndex, chunkSize);
            }

            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++;
                    }
                }
            }
        }
Example #3
0
 internal TileRef(int argMap, int gridIndex, int xIndex, int yIndex, Tile tile)
 {
     _mapIndex  = argMap;
     _gridTile  = new MapGrid.Indices(xIndex, yIndex);
     _gridIndex = gridIndex;
     _tile      = tile;
 }
Example #4
0
 internal TileRef(MapId argMap, GridId gridIndex, MapGrid.Indices gridTile, Tile tile)
 {
     MapIndex  = argMap;
     _gridTile = gridTile;
     GridIndex = gridIndex;
     _tile     = tile;
 }
Example #5
0
 internal TileRef(MapId argMap, GridId gridIndex, int xIndex, int yIndex, Tile tile)
 {
     MapIndex  = argMap;
     _gridTile = new MapGrid.Indices(xIndex, yIndex);
     GridIndex = gridIndex;
     _tile     = tile;
 }
Example #6
0
 internal TileRef(MapManager manager, int gridIndex, MapGrid.Indices gridTile, Tile tile)
 {
     _manager   = manager;
     _gridTile  = gridTile;
     _gridIndex = gridIndex;
     _tile      = tile;
 }
Example #7
0
 internal TileRef(MapManager manager, int gridIndex, int xIndex, int yIndex, Tile tile)
 {
     _manager   = manager;
     _gridTile  = new MapGrid.Indices(xIndex, yIndex);
     _gridIndex = gridIndex;
     _tile      = tile;
 }
Example #8
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;
            }
        }
Example #9
0
        /// <inheritdoc />
        public MapGrid.Indices GridTileToChunkTile(MapGrid.Indices gridTile)
        {
            var size = ChunkSize;
            var x    = Mod(gridTile.X, size);
            var y    = Mod(gridTile.Y, size);

            return(new MapGrid.Indices(x, y));
        }
Example #10
0
        /// <summary>
        ///     Constructs an instance of a MapGrid chunk.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="grid"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="chunkSize"></param>
        public Chunk(MapManager manager, MapGrid grid, int x, int y, ushort chunkSize)
        {
            _mapManager  = manager;
            ChunkSize    = chunkSize;
            _grid        = grid;
            _gridIndices = new MapGrid.Indices(x, y);

            _tiles = new Tile[ChunkSize, ChunkSize];
        }
Example #11
0
        public TileRef GetTile(MapGrid.Indices indices)
        {
            // array out of bounds
            if (indices.X >= ChunkSize || indices.X < 0 || indices.Y >= ChunkSize || indices.Y < 0)
            {
                throw new ArgumentOutOfRangeException("Tile indices out of bounds.");
            }

            return(new TileRef(_grid.MapID, _grid.Index, indices.X, indices.Y, _tiles[indices.X, indices.Y]));
        }
        /// <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;
            }
        }
Example #13
0
        /// <inheritdoc />
        public bool IndicesToTile(MapGrid.Indices indices, out TileRef tile)
        {
            MapGrid.Indices chunkindices = new MapGrid.Indices(indices.X / ChunkSize, indices.Y / ChunkSize);
            if (!_chunks.ContainsKey(chunkindices))
            {
                tile = new TileRef(); //Nothing should ever use or access this, bool check should occur first
                return(false);
            }
            Chunk chunk = _chunks[chunkindices];

            tile = chunk.GetTile(new MapGrid.Indices(indices.X % ChunkSize, indices.Y % ChunkSize));
            return(true);
        }
Example #14
0
 /// <summary>
 ///     Translates chunk tile indices to grid tile indices.
 /// </summary>
 /// <param name="chunkTile">The indices relative to the chunk origin.</param>
 /// <returns>The indices relative to the grid origin.</returns>
 private MapGrid.Indices ChunkTileToGridTile(MapGrid.Indices chunkTile)
 {
     return(chunkTile + _gridIndices * ChunkSize);
 }