Exemple #1
0
        /// <summary>
        /// Loads map from specified path, creates tiles and objects and put them into new Atlas object.
        /// </summary>
        /// <param name="map">Deserialized .tmx file</param>
        /// <param name="tilesetTable">Table of numbers of game object and theirs classes</param>
        /// <param name="initializer"></param>
        /// <returns>Atlas with initial state of ToyWorld</returns>
        public static IAtlas LoadMap(Map map, TilesetTable tilesetTable, Action <GameActor> initializer)
        {
            IAtlas atlas = new Atlas.Layers.Atlas();

            map.Tilesets = map.Tilesets.OrderBy(x => x.Firstgid).ToList();

            foreach (LayerType layerType in Enum.GetValues(typeof(LayerType)).Cast <LayerType>())
            {
                string layerName = Enum.GetName(typeof(LayerType), layerType);

                Debug.Assert(layerName != null);

                int  type       = (int)layerType;
                bool simpleType = type > 0 && (type & (type - 1)) == 0;
                if (!simpleType)
                {
                    continue;
                }

                if (layerName.Contains("Object"))
                {
                    ObjectGroup objectLayer = map.ObjectGroups.FirstOrDefault(x => x.Name == layerName);
                    if (objectLayer == null)    // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    IObjectLayer filledObjectLayer = FillObjectLayer(
                        atlas,
                        objectLayer,
                        layerType,
                        initializer,
                        map.Tilesets,
                        map.Tilewidth,
                        map.Tileheight,
                        map.Height
                        );
                    atlas.ObjectLayers.Add(
                        filledObjectLayer);
                }
                else
                {
                    Layer tileLayer = map.Layers.FirstOrDefault(x => x.Name == layerName);
                    if (tileLayer == null)  // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    Random     rnd             = new Random(); // Shared across all layers
                    ITileLayer filledTileLayer = FillTileLayer(
                        tileLayer,
                        layerType,
                        atlas.StaticTilesContainer,
                        tilesetTable,
                        initializer,
                        rnd);
                    atlas.TileLayers.Add(
                        filledTileLayer);
                }
            }

            FillNamedAreas(atlas, map);

            SetTileRelations(atlas, map);

            return(atlas);
        }
Exemple #2
0
        /// <summary>
        /// Loads map from specified path, creates tiles and objects and put them into new Atlas object.
        /// </summary>
        /// <param name="map">Deserialized .tmx file</param>
        /// <param name="tilesetTable">Table of numbers of game object and theirs classes</param>
        /// <param name="initializer"></param>
        /// <returns>Atlas with initial state of ToyWorld</returns>
        public static IAtlas LoadMap(Map map, TilesetTable tilesetTable, Action<GameActor> initializer)
        {
            IAtlas atlas = new Atlas.Layers.Atlas();

            map.Tilesets = map.Tilesets.OrderBy(x => x.Firstgid).ToList();

            foreach (LayerType layerType in Enum.GetValues(typeof(LayerType)).Cast<LayerType>())
            {
                string layerName = Enum.GetName(typeof(LayerType), layerType);

                Debug.Assert(layerName != null);

                int type = (int)layerType;
                bool simpleType = type > 0 && (type & (type - 1)) == 0;
                if (!simpleType) continue;

                if (layerName.Contains("Object"))
                {
                    ObjectGroup objectLayer = map.ObjectGroups.FirstOrDefault(x => x.Name == layerName);
                    if (objectLayer == null)    // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    IObjectLayer filledObjectLayer = FillObjectLayer(
                        atlas,
                        objectLayer,
                        layerType,
                        initializer,
                        map.Tilesets,
                        map.Tilewidth,
                        map.Tileheight,
                        map.Height
                        );
                    atlas.ObjectLayers.Add(
                        filledObjectLayer);
                }
                else
                {
                    Layer tileLayer = map.Layers.FirstOrDefault(x => x.Name == layerName);
                    if (tileLayer == null)  // TMX does not contain such layer
                    {
                        Log.Instance.Warn("Layer " + layerName + " not found in given .tmx file!");
                        continue;
                    }
                    Random rnd = new Random(); // Shared across all layers
                    ITileLayer filledTileLayer = FillTileLayer(
                        tileLayer,
                        layerType,
                        atlas.StaticTilesContainer,
                        tilesetTable,
                        initializer,
                        rnd);
                    atlas.TileLayers.Add(
                        filledTileLayer);
                }
            }

            FillNamedAreas(atlas, map);

            SetTileRelations(atlas, map);

            return atlas;
        }