Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 29.98 KB | None | 0 0
  1. #region Usings
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using Rite_of_passage.Actors;
  6. using Rite_of_passage.Actors.Objects;
  7. using Rite_of_passage.Actors.Objects.Interactive;
  8. using Rite_of_passage.Actors.Objects.Traps;
  9. using Rite_of_passage.Globals;
  10. using Rite_of_passage.Models;
  11. using Rite_of_passage.Tools;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using TiledSharp;
  16.  
  17. #endregion
  18.  
  19. namespace Rite_of_passage.Managers
  20. {
  21.     public class TiledSharpWrapper : Component
  22.     {
  23.         public Camera Camera;
  24.         public static TmxMap Map;
  25.  
  26.         #region Private
  27.  
  28.         private TmxTileSet _tileSet;
  29.         Texture2D _tileSetTexture2D;
  30.         static int _tileWidth;
  31.         static int _tileHeight;
  32.         static int _mapWidth;
  33.         static int _mapHeight;
  34.         int _tileSetLines;
  35.         int _tileSetColumns;
  36.  
  37.         #endregion
  38.  
  39.         #region Protected
  40.  
  41.         protected List<MapLayer> BackLayers; // Depth = 0
  42.         protected List<MapLayer> MidLayers; // Depth = 1
  43.         protected List<MapLayer> FrontLayers; // Depth = 2
  44.  
  45.         protected bool IsDebug => World.IsDebug;
  46.         protected MapLayer CollisionsLayer;
  47.         protected static Tile CurrentTestedTile;
  48.         protected string ColLayerStr;
  49.  
  50.         protected static Rectangle CurrentCollisionRectangle;
  51.  
  52.         protected Rectangle CamPos => new Rectangle(
  53.             Camera.WorldRectangle.X - Map.TileWidth,
  54.             Camera.WorldRectangle.Y,
  55.             Screen.Width + Map.TileWidth * 2,
  56.             Screen.Height);
  57.  
  58.         #endregion
  59.  
  60.  
  61.         public TiledSharpWrapper()
  62.         {
  63.             ColLayerStr = "collisions";
  64.         }
  65.  
  66.  
  67.         public override void LoadContent(ContentManager pContent)
  68.         {
  69.             LoadMap(pContent);
  70.             CollisionsLayer = GetLayerByName("collisions");
  71.             SortLayersByDepth(Map);
  72.  
  73.             // Init Camera
  74.             Camera = World.Camera;
  75.  
  76.             World.SetMap(Map);
  77.             World.TsWrapper = this;
  78.  
  79.             base.LoadContent();
  80.         }
  81.  
  82.         public void Draw(SpriteBatch pSpriteBatch, List<Sprite2D> pBAck,
  83.             List<Sprite2D> pMiddle, List<Sprite2D> pFront)
  84.         {
  85.             #region Commented
  86.  
  87.             //            int nbLayers = LstLayers.Count;
  88.             //            for (int nLayer = 0; nLayer < nbLayers; nLayer++)
  89.             //            {
  90.             //                var tLayer = LstLayers[nLayer];
  91.             //                if (tLayer.IsVisible)
  92.             //                {
  93.             //                    int columnStart = (CamPos.Left / _tileWidth);
  94.             //                    int columnEnd = ((CamPos.Right) / _tileWidth);
  95.             //                    int lineStart = (CamPos.Top / _tileHeight);
  96.             //                    int lineEnd = (CamPos.Bottom / _tileHeight);
  97.             //                    for (int column = columnStart; column <= columnEnd; column++)
  98.             //                    {
  99.             //                        for (int line = lineStart; line <= lineEnd; line++)
  100.             //                        {
  101.             //                            if (column < 0 || column >= _mapWidth ||
  102.             //                                line < 0 || line >= _mapHeight)
  103.             //                                continue;
  104.             //
  105.             //                            var tile = tLayer.Tiles[column + line * _mapWidth];
  106.             //                            int gid = tile.Gid;
  107.             //                            if (gid != 0 && tile.IsVisible)
  108.             //                            {
  109.             //                                int tileFrame = gid - 1;
  110.             //                                int tileSetColumn = tileFrame % _tileSetColumns;
  111.             //                                int tileSetLine =
  112.             //                                    (int)Math.Floor(
  113.             //                                        tileFrame / (double)_tileSetColumns
  114.             //                                    );
  115.             //                                float x = column * _tileWidth;
  116.             //                                float y = line * _tileHeight;
  117.             //                                Rectangle tileSetRec =
  118.             //                                    new Rectangle(
  119.             //                                        _tileWidth * tileSetColumn,
  120.             //                                        _tileHeight * tileSetLine,
  121.             //                                        _tileWidth, _tileHeight);
  122.             //                                pSpriteBatch.Draw(
  123.             //                                    _tileSetTexture2D,
  124.             //                                    new Vector2(x, y),
  125.             //                                    tileSetRec, tile.Color);
  126.             //                            }
  127.             //                        }
  128.             //                    }
  129.             //                }
  130.             //            }
  131.  
  132.             #endregion
  133.  
  134.             DrawLayer(pSpriteBatch, BackLayers); // Map Layer
  135.             DrawLayer(pSpriteBatch, MidLayers); // Map Layer
  136.  
  137.             DrawActors(pSpriteBatch, pBAck); // Actors
  138.             DrawActorsInReverseOrder(pSpriteBatch, pMiddle); // Actors
  139.             DrawActors(pSpriteBatch, pFront); // Actors
  140.  
  141.             DrawLayer(pSpriteBatch, FrontLayers); // Map Layer
  142.  
  143.             //            if (IsDebug)
  144.             //                Debug(pSpriteBatch);
  145.  
  146.             base.Draw(pSpriteBatch);
  147.         }
  148.  
  149.         private void DrawActors(SpriteBatch pSpriteBatch, List<Sprite2D> pList)
  150.         {
  151.             if (pList != null &&
  152.                 pList.Count > 0)
  153.                 for (int i = 0; i < pList.Count; i++)
  154.                 {
  155.                     var deco = pList[i];
  156.                     deco.Draw(pSpriteBatch);
  157.                 }
  158.         }
  159.  
  160.         private void DrawActorsInReverseOrder(SpriteBatch pSpriteBatch, List<Sprite2D> pList)
  161.         {
  162.             if (pList != null &&
  163.                 pList.Count > 0)
  164.                 for (int i = pList.Count; i-- > 0;)
  165.                 {
  166.                     var deco = pList[i];
  167.                     deco.Draw(pSpriteBatch);
  168.                 }
  169.         }
  170.  
  171.         public void DrawLayer(SpriteBatch pSpriteBatch, List<MapLayer> pLayer)
  172.         {
  173.             int nbLayers = pLayer.Count;
  174.             for (int nLayer = 0; nLayer < nbLayers; nLayer++)
  175.             {
  176.                 var tLayer = pLayer[nLayer];
  177.                 if (tLayer.IsVisible)
  178.                 {
  179.                     int columnStart = (CamPos.Left / _tileWidth);
  180.                     int columnEnd = ((CamPos.Right) / _tileWidth);
  181.                     int lineStart = (CamPos.Top / _tileHeight);
  182.                     int lineEnd = (CamPos.Bottom / _tileHeight);
  183.                     for (int column = columnStart; column <= columnEnd; column++)
  184.                     {
  185.                         for (int line = lineStart; line <= lineEnd; line++)
  186.                         {
  187.                             if (column < 0 || column >= _mapWidth ||
  188.                                line < 0 || line >= _mapHeight)
  189.                                 continue;
  190.  
  191.                             var tile = tLayer.Tiles[column + line * _mapWidth];
  192.                             int gid = tile.Gid;
  193.                             if (gid != 0 && tile.IsVisible)
  194.                             {
  195.                                 int tileFrame = gid - 1;
  196.                                 int tileSetColumn = tileFrame % _tileSetColumns;
  197.                                 int tileSetLine =
  198.                                     (int)Math.Floor(
  199.                                         tileFrame / (double)_tileSetColumns
  200.                                     );
  201.                                 float x = column * _tileWidth;
  202.                                 float y = line * _tileHeight;
  203.                                 Rectangle tileSetRec =
  204.                                     new Rectangle(
  205.                                         _tileWidth * tileSetColumn,
  206.                                         _tileHeight * tileSetLine,
  207.                                         _tileWidth, _tileHeight);
  208.                                 pSpriteBatch.Draw(
  209.                                     _tileSetTexture2D,
  210.                                     new Vector2(x, y),
  211.                                     tileSetRec, tile.Color);
  212.                             }
  213.                         }
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.  
  219.         protected void Debug(SpriteBatch pSpriteBatch)
  220.         {
  221.             if (CurrentTestedTile != null)
  222.                 pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
  223.                     "Tile Id: " + CurrentTestedTile.Gid.ToString(),
  224.                     new Vector2(Camera.Position.X, Camera.Position.Y), Color.White);
  225.  
  226.             if (CurrentTestedTile != null)
  227.                 pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
  228.                 "Tile type: " + CurrentTestedTile.CurrentType.ToString(),
  229.                 new Vector2(Camera.Position.X, Camera.Position.Y + 16), Color.White);
  230.  
  231.             pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
  232.                 "Nb collision Tiles: " + CollisionsLayer.Tiles.Count,
  233.                 new Vector2(Camera.Position.X, Camera.Position.Y + 32), Color.White);
  234.  
  235.             for (int i = 0; i < CollisionsLayer.Tiles.Count; i++)
  236.             {
  237.                 var tile = CollisionsLayer.Tiles[i];
  238.                 var color = Color.White;
  239.  
  240.                 switch (tile.CurrentType)
  241.                 {
  242.                     case Tile.Type.Break:
  243.                         break;
  244.                     case Tile.Type.Climb:
  245.                         break;
  246.                     case Tile.Type.Exit:
  247.                         break;
  248.                     case Tile.Type.Hurt:
  249.                         break;
  250.                     case Tile.Type.Ladder:
  251.                         break;
  252.                     case Tile.Type.JumpThrough:
  253.                         color = Color.Orange;
  254.                         break;
  255.                     case Tile.Type.None:
  256.                         break;
  257.                     case Tile.Type.Move:
  258.                         break;
  259.                     case Tile.Type.Slide:
  260.                         break;
  261.                     case Tile.Type.SlideRight:
  262.                         break;
  263.                     case Tile.Type.SlideLeft:
  264.                         break;
  265.                     case Tile.Type.Solid:
  266.                         color = Color.Red;
  267.                         break;
  268.                     default:
  269.                         color = Color.Green;
  270.                         break;
  271.                 }
  272.  
  273.                 if (tile.CurrentType != Tile.Type.None)
  274.                 {
  275.                     Primitives2D.DrawRectangle(pSpriteBatch,
  276.                         new Rectangle(tile.Column * _tileWidth, tile.Line * _tileHeight, _tileWidth, _tileHeight),
  277.                         color, 1);
  278.                 }
  279.             }
  280.         }
  281.  
  282.  
  283.         #region Delegates
  284.  
  285.         public void OnTileDestroy(MobileObject pMob)
  286.         {
  287.             DestroyTile(pMob);
  288.         }
  289.  
  290.         #endregion
  291.  
  292.  
  293.         #region Collisions
  294.  
  295.         /// <summary>
  296.         /// Checks if a sprite is colliding in a given direction.
  297.         /// </summary>
  298.         /// <param name="pSprite2D"></param>
  299.         /// <param name="pDirection"></param>
  300.         /// <returns></returns>
  301.         public bool Collide(Sprite2D pSprite2D, Direction pDirection)
  302.         {
  303.             switch (pDirection)
  304.             {
  305.                 case Direction.Above:
  306.                     var id1A = GetTilesAt(new Vector2(
  307.                         pSprite2D.BoundingBox.X + 1,
  308.                         pSprite2D.BoundingBox.Y + 1));
  309.  
  310.                     var id2A = GetTilesAt(new Vector2(
  311.                         pSprite2D.BoundingBox.X + (pSprite2D.BoundingBox.Width - 2),
  312.                         pSprite2D.BoundingBox.Y - _tileHeight / 2));
  313.  
  314.                     if (IsSolid(id1A) || IsSolid(id2A))
  315.                     {
  316.                         if (IsJumpThrough(id1A) || IsJumpThrough(id2A) &&
  317.                             pSprite2D.MovementSpeed.Y < 0)
  318.                             return false;
  319.                         return true;
  320.                     }
  321.                     return false;
  322.  
  323.                 case Direction.Below:
  324.                     var iUnder = GetTilesAt(new Vector2(
  325.                        pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width / 2,
  326.                        pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height + pSprite2D.MovementSpeed.Y));
  327.  
  328.                     var overLap = GetTilesAt(new Vector2(
  329.                         pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width / 2,
  330.                         pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height + pSprite2D.MovementSpeed.Y - 1));
  331.  
  332.                     if (IsSolid(iUnder) || IsSolid(overLap))
  333.                         return true;
  334.                     return false;
  335.  
  336.                 case Direction.Left:
  337.                     var id1L = GetTilesAt(new Vector2(
  338.                         pSprite2D.BoundingBox.Left + pSprite2D.MovementSpeed.X - 1,
  339.                         pSprite2D.BoundingBox.Y + 6));
  340.  
  341.                     var id2L = GetTilesAt(new Vector2(
  342.                         pSprite2D.BoundingBox.Left + pSprite2D.MovementSpeed.X - 1,
  343.                         pSprite2D.BoundingBox.Y + (pSprite2D.BoundingBox.Height - 1)));
  344.  
  345.                     if (IsSolid(id1L) || IsSolid(id2L))
  346.                     {
  347.                         if (IsJumpThrough(id1L) || IsJumpThrough(id2L))
  348.                             return false;
  349.                         return true;
  350.                     }
  351.                     return false;
  352.  
  353.                 case Direction.Right:
  354.                     var id1R = GetTilesAt(new Vector2(
  355.                         pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width + pSprite2D.MovementSpeed.X + 3,
  356.                         pSprite2D.BoundingBox.Y + 3));
  357.  
  358.                     var id2R = GetTilesAt(new Vector2(
  359.                         pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width + pSprite2D.MovementSpeed.X + 3,
  360.                         pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height - 2));
  361.  
  362.                     if (IsSolid(id1R) || IsSolid(id2R))
  363.                     {
  364.                         if (IsJumpThrough(id1R) || IsJumpThrough(id2R))
  365.                             return false;
  366.                         return true;
  367.                     }
  368.                     return false;
  369.  
  370.                 default:
  371.                     return false;
  372.             }
  373.         }
  374.  
  375.         /// <summary>
  376.         /// Returns a tile at (vector2) position.
  377.         /// </summary>
  378.         /// <param name="pPos"></param>
  379.         /// <returns></returns>
  380.         public Tile.Type GetTilesAt(Vector2 pPos)
  381.         {
  382.             var mobRect = new Rectangle((int)pPos.X, (int)pPos.Y, _tileWidth, _tileHeight);
  383.  
  384.             var col = Math.Floor(pPos.X / _tileWidth);
  385.             var lig = Math.Floor(pPos.Y / _tileHeight);
  386.             if (col >= 0 && col < _mapWidth &&
  387.                 lig >= 0 && lig < _mapHeight)
  388.             {
  389.                 var tile = CollisionsLayer.Tiles[(int)((lig * _mapWidth) + col)];
  390.  
  391.                 CurrentTestedTile = tile;
  392.                 CurrentCollisionRectangle = new Rectangle((int)(col * _tileWidth), (int)(lig * _tileHeight), _tileWidth, _tileHeight);
  393.  
  394.                 if (mobRect.Intersects(tile.Rectangle))
  395.                     return tile.CurrentType;
  396.             }
  397.  
  398.             return Tile.Type.None;
  399.         }
  400.  
  401.         /// <summary>
  402.         /// Can we jump through the tile?
  403.         /// </summary>
  404.         /// <param name="pType"></param>
  405.         /// <returns></returns>
  406.         protected static bool IsJumpThrough(Tile.Type pType)
  407.         {
  408.             if (pType == Tile.Type.JumpThrough)
  409.                 return true;
  410.  
  411.             return false;
  412.         }
  413.  
  414.         /// <summary>
  415.         /// Is the tile a ladder?
  416.         /// </summary>
  417.         /// <param name="pType"></param>
  418.         /// <returns></returns>
  419.         public bool IsLadder(Tile.Type pType)
  420.         {
  421.             if (pType == Tile.Type.Ladder)
  422.                 return true;
  423.  
  424.             return false;
  425.         }
  426.  
  427.         /// <summary>
  428.         /// Is the tile solid?
  429.         /// </summary>
  430.         /// <param name="pType"></param>
  431.         /// <returns></returns>
  432.         protected static bool IsSolid(Tile.Type pType)
  433.         {
  434.             if (pType == Tile.Type.Solid ||
  435.                 pType == Tile.Type.Break ||
  436.                 pType == Tile.Type.JumpThrough ||
  437.                 pType == Tile.Type.Climb)
  438.                 return true;
  439.  
  440.             return false;
  441.         }
  442.  
  443.         #endregion
  444.  
  445.  
  446.         #region Destroy Tiles
  447.  
  448.         /// <summary>
  449.         /// Destroys a tile.
  450.         /// </summary>
  451.         /// <param name="pMob"></param>
  452.         public void DestroyTile(MobileObject pMob)
  453.         {
  454.             if (pMob is DestructibleBlock pDBlock)
  455.             {
  456.                 DestroyTilesAt(new Vector2(
  457.                     pDBlock.BoundingBox.X,
  458.                     pDBlock.BoundingBox.Y));
  459.             }
  460.             else if (pMob is DroppingBlock pDpBlock)
  461.             {
  462.                 DestroyTilesAt(new Vector2(
  463.                     pDpBlock.BoundingBox.X,
  464.                     pDpBlock.BoundingBox.Y + _tileHeight));
  465.             }
  466.  
  467.         }
  468.  
  469.         /// <summary>
  470.         /// destroys a tile from the collision List,
  471.         /// doesn't affect other layers.
  472.         /// </summary>
  473.         /// <param name="pPos"></param>
  474.         protected void DestroyTilesAt(Vector2 pPos)
  475.         {
  476.             var col = Math.Floor(pPos.X / _tileWidth);
  477.             var lig = Math.Floor(pPos.Y / _tileHeight);
  478.             if (col >= 0 && col < _mapWidth &&
  479.                 lig >= 0 && lig < _mapHeight)
  480.             {
  481.                 var tile = CollisionsLayer.Tiles[(int)((lig * _mapWidth) + col)];
  482.                 tile.Break();
  483.             }
  484.         }
  485.  
  486.         #endregion
  487.  
  488.  
  489.         public enum Direction
  490.         {
  491.             Above,
  492.             Below,
  493.             Left,
  494.             Right
  495.         }
  496.  
  497.  
  498.         #region Helper Methods
  499.  
  500.         protected int CoordToGid(List<Tile> pList, Vector2 pPos) // Todo
  501.         {
  502.             var index = 0;
  503.  
  504.             var col = Math.Floor(pPos.X / _tileWidth);
  505.             var lig = Math.Floor(pPos.Y / _tileHeight);
  506.             if (col >= 0 && col < _mapWidth &&
  507.                 lig >= 0 && lig < _mapHeight)
  508.             {
  509.                 index = (int)((lig * _mapWidth) + col);
  510.             }
  511.  
  512.             return index;
  513.         }
  514.  
  515.         /// <summary>
  516.         /// Get all the map layers in a list.
  517.         /// </summary>
  518.         /// <param name="pMap"></param>
  519.         /// <returns></returns>
  520.         protected List<MapLayer> GetAllLayers(TmxMap pMap)
  521.         {
  522.             var lstLayers = new List<MapLayer>();
  523.  
  524.             for (int i = 0; i < Map.Layers.Count; i++)
  525.             {
  526.                 if (Map.Layers[i] is TmxLayer layer)
  527.                 {
  528.                     var oldLayer = layer;
  529.  
  530.                     // 1: create an empty new layer
  531.                     var newLayer = new MapLayer()
  532.                     {
  533.                         Name = oldLayer.Name,
  534.                         IsVisible = oldLayer.Visible
  535.                     };
  536.  
  537.                     var newTiles = new List<Tile>();
  538.  
  539.                     // 2 create tiles, transpose values and fill up the new layer.
  540.                     for (int j = 0; j < oldLayer.Tiles.Count; j++)
  541.                     {
  542.                         var oldTile = oldLayer.Tiles[j];
  543.  
  544.                         var newTile = new Tile(oldTile, Map);
  545.  
  546.                         // 3 get the Type from the tileSet.
  547.                         if (layer.Name.ToLower() == ColLayerStr.ToLower())
  548.                             newTile.CurrentType = newTile.ParseType(_tileSet);
  549.  
  550.                         newTiles.Add(newTile);
  551.                     }
  552.  
  553.                     newLayer.Tiles = newTiles;
  554.                     lstLayers.Add(newLayer);
  555.                 }
  556.             }
  557.  
  558.             return lstLayers;
  559.         }
  560.  
  561.         /// <summary>
  562.         /// Puts layers in separate lists depending on their depth.
  563.         /// </summary>
  564.         /// <param name="pMap"></param>
  565.         protected void SortLayersByDepth(TmxMap pMap)
  566.         {
  567.             BackLayers = new List<MapLayer>();
  568.             MidLayers = new List<MapLayer>();
  569.             FrontLayers = new List<MapLayer>();
  570.  
  571.             for (int i = 0; i < Map.Layers.Count; i++)
  572.             {
  573.                 if (Map.Layers[i] is TmxLayer layer)
  574.                 {
  575.                     var oldLayer = layer;
  576.  
  577.                     // 1: create an empty new layer
  578.                     var newLayer = new MapLayer()
  579.                     {
  580.                         Name = oldLayer.Name,
  581.                         IsVisible = oldLayer.Visible
  582.                     };
  583.  
  584.                     const string depthStr = "Depth";
  585.                     if (oldLayer.Properties.ContainsKey(depthStr))
  586.                         newLayer.Depth = Convert.ToInt16(oldLayer.Properties[depthStr]);
  587.                     else
  588.                         newLayer.Depth = 999;
  589.  
  590.                     var newTiles = new List<Tile>();
  591.  
  592.                     // 2 create tiles, transpose values and fill up the new layer.
  593.                     for (int j = 0; j < oldLayer.Tiles.Count; j++)
  594.                     {
  595.                         var oldTile = oldLayer.Tiles[j];
  596.  
  597.                         var newTile = new Tile(oldTile, Map);
  598.  
  599.                         // 3 get the Type from the tileSet.
  600.                         if (layer.Name.ToLower() == ColLayerStr.ToLower())
  601.                             newTile.CurrentType = newTile.ParseType(_tileSet);
  602.  
  603.                         newTiles.Add(newTile);
  604.                     }
  605.  
  606.                     newLayer.Tiles = newTiles;
  607.                     if (newLayer.Depth == 0)
  608.                     {
  609.                         FrontLayers.Add(newLayer);
  610.                     }
  611.                     else if (newLayer.Depth == 1)
  612.                     {
  613.                         MidLayers.Add(newLayer);
  614.                     }
  615.                     else if (newLayer.Depth == 2)
  616.                     {
  617.                         BackLayers.Add(newLayer);
  618.                     }
  619.                 }
  620.             }
  621.         }
  622.  
  623.         /// <summary>
  624.         /// Returns the layer named after the param string..
  625.         /// </summary>
  626.         /// <param name="pName"></param>
  627.         /// <returns></returns>
  628.         protected MapLayer GetLayerByName(string pName)
  629.         {
  630.             var requestedLayer = new MapLayer();
  631.             var allLayers = GetAllLayers(Map);
  632.             for (int i = 0; i < allLayers.Count; i++)
  633.             {
  634.                 var layer = allLayers[i];
  635.                 if (layer.Name.ToLower() == pName.ToLower())
  636.                     requestedLayer = layer;
  637.             }
  638.  
  639.             return requestedLayer;
  640.         }
  641.  
  642.         /// <summary>
  643.         /// Init map variables.
  644.         /// </summary>
  645.         /// <param name="pContent"></param>
  646.         private void LoadMap(ContentManager pContent)
  647.         {
  648.             Map = new TmxMap(World.LevelPath);
  649.             _tileSet = Map.Tilesets[0];
  650.             _tileSetTexture2D = pContent.Load<Texture2D>("Levels/" + _tileSet.Name);
  651.             _tileWidth = _tileSet.TileWidth;
  652.             _tileHeight = _tileSet.TileHeight;
  653.             _mapWidth = Map.Width;
  654.             _mapHeight = Map.Height;
  655.             _tileSetColumns = _tileSetTexture2D.Width / _tileWidth;
  656.             _tileSetLines = _tileSetTexture2D.Height / _tileHeight;
  657.         }
  658.  
  659.         #endregion
  660.     }
  661.  
  662.     public class MapLayer
  663.     {
  664.         public string Name;
  665.         public bool IsVisible;
  666.         public List<Tile> Tiles;
  667.         public int Depth;
  668.  
  669.         public void Fade(Tile.FadeMotion pFade)
  670.         {
  671.             for (int i = 0; i < Tiles.Count; i++)
  672.             {
  673.                 Tiles[i].Fade(pFade);
  674.             }
  675.         }
  676.     }
  677.  
  678.     public class Tile : Component
  679.     {
  680.         public int Gid;
  681.         public int Column;
  682.         public int Line;
  683.         public int X => Column * TileSize;
  684.         public int Y => Line * TileSize;
  685.         public Vector2 Position => new Vector2(X, Y);
  686.  
  687.         public Rectangle Rectangle
  688.         {
  689.             get
  690.             {
  691.                 if (CurrentType == Type.JumpThrough)
  692.                     return new Rectangle(X, Y, TileSize, TileSize / 2);
  693.                 else return new Rectangle(X, Y, TileSize, TileSize);
  694.             }
  695.         }
  696.  
  697.         protected int TileSize;
  698.         public bool OldVisible;
  699.         public bool IsVisible;
  700.  
  701.         #region Colors
  702.  
  703.         public Color Color
  704.         {
  705.             get { return BaseColor * Alpha; }
  706.         }
  707.  
  708.         protected float Alpha;
  709.         protected float AlphaGoal;
  710.         protected float AlphaMax;
  711.         protected float AlphaMin;
  712.         protected float AlphaStep;
  713.         protected Color BaseColor;
  714.         protected FadeMotion CurrentFade;
  715.  
  716.         #endregion
  717.  
  718.         public Type OldType;
  719.         public Type CurrentType;
  720.  
  721.         public Tile(TmxLayerTile pTile, TmxMap pMap)
  722.         {
  723.             OldType = Type.None;
  724.             CurrentType = Type.None;
  725.             IsVisible = true;
  726.             OldVisible = true;
  727.             Gid = pTile.Gid;
  728.             Column = pTile.X;
  729.             Line = pTile.Y;
  730.             TileSize = pMap.TileWidth;
  731.  
  732.             Alpha = 1f;
  733.             AlphaGoal = 1f;
  734.             AlphaMin = 0;
  735.             AlphaMax = 1;
  736.             AlphaStep = 0.1f;
  737.             BaseColor = Color.White;
  738.         }
  739.  
  740.  
  741.         #region Helper Methods
  742.  
  743.         public void Fade(FadeMotion pFade)
  744.         {
  745.             switch (pFade)
  746.             {
  747.                 case FadeMotion.None:
  748.                     break;
  749.                 case FadeMotion.In:
  750.                     FadeIn();
  751.                     break;
  752.                 case FadeMotion.Out:
  753.                     FadeOut();
  754.                     break;
  755.             }
  756.         }
  757.  
  758.         protected void FadeIn()
  759.         {
  760.             AlphaGoal = AlphaMax;
  761.             CurrentFade = FadeMotion.In;
  762.         }
  763.  
  764.         protected void FadeOut()
  765.         {
  766.             AlphaGoal = AlphaMin;
  767.             CurrentFade = FadeMotion.Out;
  768.         }
  769.  
  770.         public void Break()
  771.         {
  772.             if (CurrentType != Type.Break)
  773.                 return;
  774.  
  775.             if (OldType != CurrentType)
  776.             {
  777.                 OldType = CurrentType;
  778.                 CurrentType = Type.None;
  779.                 Alpha = 0;
  780.             }
  781.         }
  782.  
  783.         public void Repair()
  784.         {
  785.             var tempType = CurrentType;
  786.  
  787.             CurrentType = OldType;
  788.             OldType = tempType;
  789.             Alpha = 1;
  790.         }
  791.  
  792.         public void SetVisible(bool pVisible)
  793.         {
  794.             IsVisible = pVisible;
  795.         }
  796.  
  797.         public Type ParseType(TmxTileSet pTileSet)
  798.         {
  799.             if (Gid == 0)
  800.                 return Type.None;
  801.  
  802.             if (pTileSet.Tiles.ContainsKey(Gid - 1))
  803.             {
  804.                 var tile = pTileSet.Tiles[Gid - 1];
  805.  
  806.                 switch (tile.Type.ToLower())
  807.                 {
  808.                     case "":
  809.                         return Type.None;
  810.                     case "break":
  811.                         return Type.Break;
  812.                     case "climb":
  813.                         return Type.Climb;
  814.                     case "exit":
  815.                         return Type.Exit;
  816.                     case "hurt":
  817.                         return Type.Hurt;
  818.                     case "ladder":
  819.                         return Type.Ladder;
  820.                     case "jumpthrough":
  821.                         return Type.JumpThrough;
  822.                     case "none":
  823.                         return Type.None;
  824.                     case "move":
  825.                         return Type.Move;
  826.                     case "slide":
  827.                         return Type.Slide;
  828.                     case "slideright":
  829.                         return Type.SlideRight;
  830.                     case "slideleft":
  831.                         return Type.SlideLeft;
  832.                     case "solid":
  833.                         return Type.Solid;
  834.                     default:
  835.                         return Type.None;
  836.                 }
  837.             }
  838.             else
  839.             {
  840.  
  841.                 return Type.None;
  842.             }
  843.         }
  844.  
  845.         private void UpdateColors()
  846.         {
  847.             if (CurrentFade == FadeMotion.Out)
  848.             {
  849.                 if (Alpha > AlphaGoal)
  850.                 {
  851.                     Alpha -= AlphaStep;
  852.                     if (Alpha < AlphaMin)
  853.                         Alpha = AlphaMin;
  854.                 }
  855.             }
  856.  
  857.             if (CurrentFade == FadeMotion.In)
  858.             {
  859.                 if (Alpha < AlphaGoal)
  860.                 {
  861.                     Alpha += AlphaStep;
  862.                     if (Alpha > AlphaMax)
  863.                         Alpha = AlphaMax;
  864.                 }
  865.             }
  866.         }
  867.  
  868.         #endregion
  869.  
  870.  
  871.         public override void Update(GameTime pGameTime)
  872.         {
  873.             UpdateColors();
  874.             base.Update(pGameTime);
  875.         }
  876.  
  877.         /// <summary>
  878.         /// Enum => types of tiles.
  879.         /// </summary>
  880.         public enum Type
  881.         {
  882.             Break,
  883.             Climb,
  884.             Exit,
  885.             Hurt,
  886.             Ladder,
  887.             JumpThrough,
  888.             None,
  889.             Move,
  890.             Slide,
  891.             SlideRight,
  892.             SlideLeft,
  893.             Solid,
  894.         }
  895.  
  896.         /// <summary>
  897.         /// Type of fade effect.
  898.         /// </summary>
  899.         public enum FadeMotion
  900.         {
  901.             None,
  902.             In,
  903.             Out
  904.         }
  905.     }
  906. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement