Ejemplo n.º 1
0
        private static void LoadGridNode(IMapManager mapMan, IMap map, ref GridId?newId, YamlMappingNode gridNode)
        {
            var info  = (YamlMappingNode)gridNode["settings"];
            var chunk = (YamlSequenceNode)gridNode["chunks"];

            YamlGridSerializer.DeserializeGrid(mapMan, map, ref newId, info, chunk);
        }
Ejemplo n.º 2
0
        public IMapGrid CreateGrid(MapId currentMapID, GridId?gridID = null, ushort chunkSize = 16, float snapSize = 1)
        {
            var map = _maps[currentMapID];

            GridId actualID;

            if (gridID != null)
            {
                actualID = gridID.Value;
            }
            else
            {
                actualID = new GridId(HighestGridID.Value + 1);
            }

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

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

            var grid = new MapGrid(this, actualID, chunkSize, snapSize, currentMapID);

            _grids.Add(actualID, grid);
            map.AddGrid(grid);
            OnGridCreated?.Invoke(actualID);
            return(grid);
        }
Ejemplo n.º 3
0
        public static void DeserializeGrid(IMapManagerInternal mapMan, MapId mapId, ref GridId?gridId, YamlMappingNode info,
                                           YamlSequenceNode chunks, IReadOnlyDictionary <ushort, string> tileDefMapping,
                                           ITileDefinitionManager tileDefinitionManager)
        {
            ushort csz  = 0;
            ushort tsz  = 0;
            float  sgsz = 0.0f;

            foreach (var kvInfo in info)
            {
                var key = kvInfo.Key.ToString();
                var val = kvInfo.Value.ToString();
                if (key == "chunksize")
                {
                    csz = ushort.Parse(val);
                }
                else if (key == "tilesize")
                {
                    tsz = ushort.Parse(val);
                }
                else if (key == "snapsize")
                {
                    sgsz = float.Parse(val);
                }
            }

            var grid = mapMan.CreateGridNoEntity(mapId, gridId);

            gridId = grid.Index;

            foreach (var chunkNode in chunks.Cast <YamlMappingNode>())
            {
                DeserializeChunk(mapMan, grid, chunkNode, tileDefMapping, tileDefinitionManager);
            }
        }
        public static void DeserializeGrid(IMapManager mapMan, IMap map, ref GridId?gridId, YamlMappingNode info, YamlSequenceNode chunks)
        {
            ushort csz  = 0;
            ushort tsz  = 0;
            float  sgsz = 0.0f;

            foreach (var kvInfo in info)
            {
                var key = kvInfo.Key.ToString();
                var val = kvInfo.Value.ToString();
                if (key == "csz")
                {
                    csz = ushort.Parse(val);
                }
                else if (key == "tsz")
                {
                    tsz = ushort.Parse(val);
                }
                else if (key == "sgsz")
                {
                    sgsz = float.Parse(val);
                }
            }

            var grid = map.CreateGrid(gridId, csz, sgsz);

            gridId = grid.Index;

            foreach (YamlMappingNode chunkNode in chunks.Cast <YamlMappingNode>())
            {
                DeserializeChunk(mapMan, grid, chunkNode);
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
            void ReadGridSection()
            {
                var grids  = RootNode.GetNode <YamlSequenceNode>("grids");
                var mapMan = IoCManager.Resolve <IMapManager>();

                foreach (var grid in grids)
                {
                    var newId = new GridId?();
                    YamlGridSerializer.DeserializeGrid(
                        mapMan, TargetMap, ref newId,
                        (YamlMappingNode)grid["settings"],
                        (YamlSequenceNode)grid["chunks"]
                        );

                    Grids.Add(mapMan.GetGrid(newId.Value));
                }
            }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public IMapGrid LoadBlueprint(IMap map, string path, GridId?newId = null)
        {
            var rootPath = _resMan.ConfigDirectory;
            var comb     = Path.Combine(rootPath, "./", path);
            var fullPath = Path.GetFullPath(comb);

            TextReader reader;

            // try user
            if (!File.Exists(fullPath))
            {
                Logger.InfoS("map", $"No user blueprint path: {fullPath}");

                // fallback to content
                if (_resMan.TryContentFileRead(ResourcePath.Root / path, out var contentReader))
                {
                    reader = new StreamReader(contentReader);
                }
                else
                {
                    Logger.ErrorS("map", $"No blueprint found: {path}");
                    return(null);
                }
            }
            else
            {
                reader = new StreamReader(fullPath);
            }

            using (reader)
            {
                Logger.InfoS("map", $"Loading Grid: {path}");

                var stream = new YamlStream();
                stream.Load(reader);

                foreach (var document in stream.Documents)
                {
                    var root = (YamlSequenceNode)document.RootNode;
                    return(LoadBpNode(map, newId, root));
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
            void ReadGridSection()
            {
                var grids = RootNode.GetNode <YamlSequenceNode>("grids");

                foreach (var grid in grids)
                {
                    var newId = new GridId?();
                    YamlGridSerializer.DeserializeGrid(
                        _mapManager, TargetMap, ref newId,
                        (YamlMappingNode)grid["settings"],
                        (YamlSequenceNode)grid["chunks"],
                        _tileMap,
                        _tileDefinitionManager
                        );

                    Grids.Add(_mapManager.GetGrid(newId.Value));
                }
            }
Ejemplo n.º 9
0
        /// <inheritdoc />
        public IMapGrid LoadBlueprint(IMap map, string path, GridId?newId = null)
        {
            TextReader reader;
            var        resPath = new ResourcePath(path).ToRootedPath();

            // try user
            if (!_resMan.UserData.Exists(resPath))
            {
                Logger.InfoS("map", $"No user blueprint path: {resPath}");

                // fallback to content
                if (_resMan.TryContentFileRead(resPath, out var contentReader))
                {
                    reader = new StreamReader(contentReader);
                }
                else
                {
                    Logger.ErrorS("map", $"No blueprint found: {resPath}");
                    return(null);
                }
            }
            else
            {
                var file = _resMan.UserData.Open(resPath, FileMode.Open);
                reader = new StreamReader(file);
            }

            using (reader)
            {
                Logger.InfoS("map", $"Loading Grid: {resPath}");

                var stream = new YamlStream();
                stream.Load(reader);

                foreach (var document in stream.Documents)
                {
                    var root = (YamlSequenceNode)document.RootNode;
                    return(LoadBpNode(map, newId, root));
                }
            }

            return(null);
        }
Ejemplo n.º 10
0
        private IMapGrid LoadBpNode(IMap map, GridId?newId, YamlSequenceNode root)
        {
            foreach (var yamlNode in root.Children)
            {
                var mapNode = (YamlMappingNode)yamlNode;

                if (mapNode.Children.TryGetValue("grid", out var gridNode))
                {
                    var gridMap = (YamlMappingNode)gridNode;
                    // This ref shit is so that the entities are loaded with the grid created by this load.
                    LoadGridNode(_mapManager, map, ref newId, gridMap);
                }
                else if (mapNode.Children.TryGetValue("entities", out var entNode))
                {
                    LoadEntNode(map, newId, (YamlSequenceNode)entNode);
                }
            }

            return(IoCManager.Resolve <IMapManager>().GetGrid(newId.Value));
        }
Ejemplo n.º 11
0
        public static void DeserializeGrid(IMapManager mapMan, IMap map, ref GridId?gridId, YamlMappingNode info, YamlSequenceNode chunks, IReadOnlyDictionary <ushort, string> tileDefMapping, ITileDefinitionManager tileDefinitionManager)
        {
            ushort csz      = 0;
            ushort tsz      = 0;
            float  sgsz     = 0.0f;
            var    worldPos = Vector2.Zero;

            foreach (var kvInfo in info)
            {
                var key = kvInfo.Key.ToString();
                var val = kvInfo.Value.ToString();
                if (key == "chunksize")
                {
                    csz = ushort.Parse(val);
                }
                else if (key == "tilesize")
                {
                    tsz = ushort.Parse(val);
                }
                else if (key == "snapsize")
                {
                    sgsz = float.Parse(val);
                }
                else if (key == "worldpos")
                {
                    worldPos = kvInfo.Value.AsVector2();
                }
            }

            var grid = map.CreateGrid(gridId, csz, sgsz);

            gridId = grid.Index;

            foreach (YamlMappingNode chunkNode in chunks.Cast <YamlMappingNode>())
            {
                DeserializeChunk(mapMan, grid, chunkNode, tileDefMapping, tileDefinitionManager);
            }
        }
Ejemplo n.º 12
0
        private void LoadEntNode(IMap map, GridId?gridId, YamlSequenceNode entNode)
        {
            foreach (var yamlNode in entNode.Children)
            {
                var yamlEnt = (YamlMappingNode)yamlNode;

                var protoName = yamlEnt["id"].ToString();

                try
                {
                    var entity = _entityMan.SpawnEntity(protoName);

                    _protoMan.LoadData(entity, yamlEnt);

                    // overwrite local position in the BP to the new map/grid ID
                    var transform = entity.GetComponent <IServerTransformComponent>();
                    transform.LocalPosition = new GridLocalCoordinates(transform.LocalPosition.Position, gridId.Value);
                }
                catch (Exception e)
                {
                    Logger.ErrorS("map", $"Error creating entity \"{protoName}\": {e.Message}");
                }
            }
        }
Ejemplo n.º 13
0
 /// <inheritdoc />
 public IMapGrid CreateGrid(GridId?gridId = null, ushort chunkSize = 16, float snapSize = 1)
 {
     return(_mapManager.CreateGrid(Index, gridId));
 }