Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using MonoGame.Extended.Graphics;
  2. using MonoGame.Extended.Tiled;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace MonoGame.Extended
  10. {
  11.     public class TiledMapCombiner
  12.     {
  13.         private TiledMap _mapOne;
  14.         private TiledMap _mapTwo;
  15.  
  16.         public TiledMapCombiner(TiledMap mapOne, TiledMap mapTwo)
  17.         {
  18.             _mapOne = mapOne;
  19.             _mapTwo = mapTwo;
  20.         }
  21.  
  22.         public TiledMap Combine()
  23.         {
  24.             var reference = _mapOne;
  25.             var currentMap = ReadMetaData(reference);
  26.             ReadTilesets(reference, currentMap);
  27.             ReadLayers(reference, currentMap);
  28.             return currentMap;
  29.         }
  30.  
  31.         private TiledMap ReadMetaData(TiledMap reference)
  32.         {
  33.             var name = reference.Name;
  34.             var width = _mapOne.Width + _mapTwo.Width;
  35.             var height = _mapOne.Height + _mapTwo.Height;
  36.             var tileWidth = reference.TileWidth;
  37.             var tileHeight = reference.TileHeight;
  38.             var backgroundColor = reference.BackgroundColor;
  39.             var renderOrder = reference.RenderOrder;
  40.             var orientation = reference.Orientation;
  41.  
  42.             var map = new TiledMap(name, width, height, tileWidth, tileHeight, renderOrder, orientation)
  43.             {
  44.                 BackgroundColor = backgroundColor
  45.             };
  46.  
  47.             return map;
  48.         }
  49.  
  50.         private void ReadTilesets(TiledMap reference, TiledMap map)
  51.         {
  52.             for (var i = 0; i < reference.Tilesets.Count; i++)
  53.             {
  54.                 map.AddTileset(reference.Tilesets[i]);
  55.             }
  56.         }
  57.  
  58.         private void ReadLayers(TiledMap reference, TiledMap map)
  59.         {
  60.             for (var i = 0; i < _mapOne.Layers.Count; i++)
  61.             {
  62.                 var lone = (TiledMapTileLayer)_mapOne.Layers[i];
  63.                 var ltwo = (TiledMapTileLayer)_mapTwo.Layers[i];
  64.  
  65.                 var tilesOne = lone.Tiles;
  66.                 var tilesTwo = ltwo.Tiles;
  67.                 var tiles = new List<TiledMapTile>(tilesOne);
  68.                 foreach (var tile in tilesTwo)
  69.                 {
  70.                     var x = (ushort)(tile.X + _mapOne.Width);
  71.                     var y = tile.Y;
  72.                     var newTile = new TiledMapTile(tile.GlobalTileIdentifierWithFlags, x, y);
  73.                     tiles.Add(newTile);
  74.                 }
  75.  
  76.                 var width = lone.Width + ltwo.Width;
  77.                 var height = lone.Height + ltwo.Height;
  78.                 var layer = new TiledMapTileLayer(map, lone.Name, lone.IsVisible, lone.Opacity, 0, 0, width, height, tiles);
  79.  
  80.                 ReadModels(lone, ltwo, layer, map);
  81.  
  82.                 map.AddLayer(layer);
  83.             }
  84.         }
  85.  
  86.         private void ReadModels(TiledMapLayer layerOne, TiledMapLayer layerTwo, TiledMapLayer layer, TiledMap map)
  87.         {
  88.             /*
  89.              * Stuck here
  90.              *
  91.             var modelCount = layerOne.Models.Count() + layerTwo.Models.Count();
  92.             var animatedModelCount = layerOne.AnimatedModels.Count() + layerTwo.AnimatedModels.Count();
  93.  
  94.             var models = layer.Models = new TiledMapLayerModel[modelCount];
  95.             var animatedModels = layer.AnimatedModels = new TiledMapLayerAnimatedModel[animatedModelCount];
  96.  
  97.             var animatedModelIndex = 0;
  98.  
  99.             for (var modelIndex = 0; modelIndex < modelCount; modelIndex++)
  100.             {
  101.                 var isAnimated = input.ReadBoolean();
  102.                 var model = isAnimated ? new TiledMapLayerAnimatedModel(input, map) : new TiledMapLayerModel(input);
  103.  
  104.                 models[modelIndex] = model;
  105.                 if (isAnimated)
  106.                     animatedModels[animatedModelIndex++] = (TiledMapLayerAnimatedModel)model;
  107.             }
  108.             */
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement