public Layer[] LoadObjectLayers(TmxMap tmxMap, ContentManager content)
        {
            var layers = new Layer[tmxMap.ObjectGroups.Count];

            for (int i = 0; i < tmxMap.ObjectGroups.Count; i++)
            {
                OrderedDictionary<string, IGameObject> _gameObjects = new OrderedDictionary<string, IGameObject>();

                foreach (var tmxObject in tmxMap.ObjectGroups[i].Objects)
                {
                    if (tmxObject.Type == "Particle Emitter")
                    {
                        IGameObject gameObject = this.LoadParticleEmitter(tmxObject, content);
                        _gameObjects.Add(tmxObject.Name, gameObject);
                    }
                    else if (tmxObject.Type == "NPC Spawner")
                    {
                        IGameObject gameObject = this.LoadNpcSpawner(tmxObject, _gameObjects, content);
                        _gameObjects.Add(tmxObject.Name, gameObject);
                    }
                }

                layers[i] = new Layer(_gameObjects, tmxMap.ObjectGroups[i].ZOrder);
            }

            return layers;
        }
Example #2
0
        public static Map Load(string filePath, ContentManager content)
        {
            Map map = null;

            var tmxMap = new TmxMap(filePath);

            Console.WriteLine("Loading map file: {0}", filePath);

            var layers = new Layer[tmxMap.Layers.Count + tmxMap.ObjectGroups.Count];

            var tilesets = new Tileset[tmxMap.Tilesets.Count];

            // Load the tilesets.
            for (int i = 0; i < tmxMap.Tilesets.Count; i++)
            {
                tilesets[i] = new Tileset(tmxMap.Tilesets[i], content);
            }

            for (int i = 0; i < tmxMap.Layers.Count; i++)
            {
                var tmxLayer = tmxMap.Layers[i];

                var graphicsDevice = (content.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService).GraphicsDevice;

                var tiles = new Tile[tmxLayer.Tiles.Count];

                for (int b = 0; b < tmxLayer.Tiles.Count; b++)
                {
                    tiles[b] = LoadTile(tmxLayer, tmxLayer.Tiles[b], tilesets, content, graphicsDevice);
                }

                layers[i] = new Layer(tiles, tmxMap.Layers[i].ZOrder);
            }

            MapObjectLoader gameObjectLoader = new MapObjectLoader();
            Layer[] objectLayers = gameObjectLoader.LoadObjectLayers(tmxMap, content);

            // Copy the objectLayers array to the layers array, starting at the first available slot, which is at the end of the tile layers.
            Array.Copy(objectLayers, 0, layers, tmxMap.Layers.Count, objectLayers.Length);

            // Sort the layers properly.
            Array.Sort(layers, (a, b) => a.ZOrder.CompareTo(b.ZOrder));

            map = new Map()
            {
                _layers = layers,
                _size = new Point(tmxMap.Width, tmxMap.Height),
                _spawnInformation = gameObjectLoader
            };

            Console.WriteLine("Finished loading map file: {0}", filePath);

            return map;
        }