Esempio n. 1
0
        /// <inheritdoc />
        public IMap CreateMap(MapId mapID, bool overwrite = false)
        {
            if (!overwrite && _maps.ContainsKey(mapID))
            {
                Logger.WarningS("map", "Attempted to overwrite existing map.");
                return(null);
            }

            var newMap = new Map(this, mapID);

            _maps.Add(mapID, newMap);
            MapCreated?.Invoke(this, new MapEventArgs(newMap));

            if (_netManager.IsClient)
            {
                return(newMap);
            }

            var msg = _netManager.CreateNetMessage <MsgMap>();

            msg.MessageType = MapMessage.CreateMap;
            msg.MapIndex    = newMap.Index;

            _netManager.ServerSendToAll(msg);

            return(newMap);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public IMap CreateMap(MapId?mapID = null, GridId?defaultGridID = null)
        {
            if (defaultGridID != null && GridExists(defaultGridID.Value))
            {
                throw new InvalidOperationException($"Grid '{defaultGridID}' already exists.");
            }
            MapId actualID;

            if (mapID != null)
            {
                actualID = mapID.Value;
            }
            else
            {
                actualID = new MapId(HighestMapID.Value + 1);
            }

            if (MapExists(actualID))
            {
                throw new InvalidOperationException($"A map with ID {actualID} already exists");
            }

            if (HighestMapID.Value < actualID.Value)
            {
                HighestMapID = actualID;
            }

            var newMap = new Map(this, actualID);

            _maps.Add(actualID, newMap);
            MapCreated?.Invoke(this, new MapEventArgs(newMap));
            newMap.DefaultGrid = CreateGrid(newMap.Index, defaultGridID);

            if (_netManager.IsClient)
            {
                return(newMap);
            }

            var msg = _netManager.CreateNetMessage <MsgMap>();

            msg.MessageType = MapMessage.CreateMap;
            msg.MapIndex    = newMap.Index;

            _netManager.ServerSendToAll(msg);

            return(newMap);
        }
        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;
        }