Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Usings
- using System;
- using System.Collections.Generic;
- using Rite_of_passage.Actors;
- using Rite_of_passage.Actors.Objects;
- using Rite_of_passage.Actors.Objects.Interactive;
- using Rite_of_passage.Actors.Objects.Traps;
- using Rite_of_passage.Globals;
- using Rite_of_passage.Models;
- using Rite_of_passage.Tools;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using TiledSharp;
- #endregion
- namespace Rite_of_passage.Managers
- {
- public class TiledSharpWrapper : Component
- {
- public Camera Camera;
- public static TmxMap Map;
- #region Private
- private TmxTileSet _tileSet;
- Texture2D _tileSetTexture2D;
- static int _tileWidth;
- static int _tileHeight;
- static int _mapWidth;
- static int _mapHeight;
- int _tileSetLines;
- int _tileSetColumns;
- #endregion
- #region Protected
- protected List<MapLayer> BackLayers; // Depth = 0
- protected List<MapLayer> MidLayers; // Depth = 1
- protected List<MapLayer> FrontLayers; // Depth = 2
- protected bool IsDebug => World.IsDebug;
- protected MapLayer CollisionsLayer;
- protected static Tile CurrentTestedTile;
- protected string ColLayerStr;
- protected static Rectangle CurrentCollisionRectangle;
- protected Rectangle CamPos => new Rectangle(
- Camera.WorldRectangle.X - Map.TileWidth,
- Camera.WorldRectangle.Y,
- Screen.Width + Map.TileWidth * 2,
- Screen.Height);
- #endregion
- public TiledSharpWrapper()
- {
- ColLayerStr = "collisions";
- }
- public override void LoadContent(ContentManager pContent)
- {
- LoadMap(pContent);
- CollisionsLayer = GetLayerByName("collisions");
- SortLayersByDepth(Map);
- // Init Camera
- Camera = World.Camera;
- World.SetMap(Map);
- World.TsWrapper = this;
- base.LoadContent();
- }
- public void Draw(SpriteBatch pSpriteBatch, List<Sprite2D> pBAck,
- List<Sprite2D> pMiddle, List<Sprite2D> pFront)
- {
- #region Commented
- // int nbLayers = LstLayers.Count;
- // for (int nLayer = 0; nLayer < nbLayers; nLayer++)
- // {
- // var tLayer = LstLayers[nLayer];
- // if (tLayer.IsVisible)
- // {
- // int columnStart = (CamPos.Left / _tileWidth);
- // int columnEnd = ((CamPos.Right) / _tileWidth);
- // int lineStart = (CamPos.Top / _tileHeight);
- // int lineEnd = (CamPos.Bottom / _tileHeight);
- // for (int column = columnStart; column <= columnEnd; column++)
- // {
- // for (int line = lineStart; line <= lineEnd; line++)
- // {
- // if (column < 0 || column >= _mapWidth ||
- // line < 0 || line >= _mapHeight)
- // continue;
- //
- // var tile = tLayer.Tiles[column + line * _mapWidth];
- // int gid = tile.Gid;
- // if (gid != 0 && tile.IsVisible)
- // {
- // int tileFrame = gid - 1;
- // int tileSetColumn = tileFrame % _tileSetColumns;
- // int tileSetLine =
- // (int)Math.Floor(
- // tileFrame / (double)_tileSetColumns
- // );
- // float x = column * _tileWidth;
- // float y = line * _tileHeight;
- // Rectangle tileSetRec =
- // new Rectangle(
- // _tileWidth * tileSetColumn,
- // _tileHeight * tileSetLine,
- // _tileWidth, _tileHeight);
- // pSpriteBatch.Draw(
- // _tileSetTexture2D,
- // new Vector2(x, y),
- // tileSetRec, tile.Color);
- // }
- // }
- // }
- // }
- // }
- #endregion
- DrawLayer(pSpriteBatch, BackLayers); // Map Layer
- DrawLayer(pSpriteBatch, MidLayers); // Map Layer
- DrawActors(pSpriteBatch, pBAck); // Actors
- DrawActorsInReverseOrder(pSpriteBatch, pMiddle); // Actors
- DrawActors(pSpriteBatch, pFront); // Actors
- DrawLayer(pSpriteBatch, FrontLayers); // Map Layer
- // if (IsDebug)
- // Debug(pSpriteBatch);
- base.Draw(pSpriteBatch);
- }
- private void DrawActors(SpriteBatch pSpriteBatch, List<Sprite2D> pList)
- {
- if (pList != null &&
- pList.Count > 0)
- for (int i = 0; i < pList.Count; i++)
- {
- var deco = pList[i];
- deco.Draw(pSpriteBatch);
- }
- }
- private void DrawActorsInReverseOrder(SpriteBatch pSpriteBatch, List<Sprite2D> pList)
- {
- if (pList != null &&
- pList.Count > 0)
- for (int i = pList.Count; i-- > 0;)
- {
- var deco = pList[i];
- deco.Draw(pSpriteBatch);
- }
- }
- public void DrawLayer(SpriteBatch pSpriteBatch, List<MapLayer> pLayer)
- {
- int nbLayers = pLayer.Count;
- for (int nLayer = 0; nLayer < nbLayers; nLayer++)
- {
- var tLayer = pLayer[nLayer];
- if (tLayer.IsVisible)
- {
- int columnStart = (CamPos.Left / _tileWidth);
- int columnEnd = ((CamPos.Right) / _tileWidth);
- int lineStart = (CamPos.Top / _tileHeight);
- int lineEnd = (CamPos.Bottom / _tileHeight);
- for (int column = columnStart; column <= columnEnd; column++)
- {
- for (int line = lineStart; line <= lineEnd; line++)
- {
- if (column < 0 || column >= _mapWidth ||
- line < 0 || line >= _mapHeight)
- continue;
- var tile = tLayer.Tiles[column + line * _mapWidth];
- int gid = tile.Gid;
- if (gid != 0 && tile.IsVisible)
- {
- int tileFrame = gid - 1;
- int tileSetColumn = tileFrame % _tileSetColumns;
- int tileSetLine =
- (int)Math.Floor(
- tileFrame / (double)_tileSetColumns
- );
- float x = column * _tileWidth;
- float y = line * _tileHeight;
- Rectangle tileSetRec =
- new Rectangle(
- _tileWidth * tileSetColumn,
- _tileHeight * tileSetLine,
- _tileWidth, _tileHeight);
- pSpriteBatch.Draw(
- _tileSetTexture2D,
- new Vector2(x, y),
- tileSetRec, tile.Color);
- }
- }
- }
- }
- }
- }
- protected void Debug(SpriteBatch pSpriteBatch)
- {
- if (CurrentTestedTile != null)
- pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
- "Tile Id: " + CurrentTestedTile.Gid.ToString(),
- new Vector2(Camera.Position.X, Camera.Position.Y), Color.White);
- if (CurrentTestedTile != null)
- pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
- "Tile type: " + CurrentTestedTile.CurrentType.ToString(),
- new Vector2(Camera.Position.X, Camera.Position.Y + 16), Color.White);
- pSpriteBatch.DrawString(AssetManager.GetFont(AssetManager.Fonts.GameFont),
- "Nb collision Tiles: " + CollisionsLayer.Tiles.Count,
- new Vector2(Camera.Position.X, Camera.Position.Y + 32), Color.White);
- for (int i = 0; i < CollisionsLayer.Tiles.Count; i++)
- {
- var tile = CollisionsLayer.Tiles[i];
- var color = Color.White;
- switch (tile.CurrentType)
- {
- case Tile.Type.Break:
- break;
- case Tile.Type.Climb:
- break;
- case Tile.Type.Exit:
- break;
- case Tile.Type.Hurt:
- break;
- case Tile.Type.Ladder:
- break;
- case Tile.Type.JumpThrough:
- color = Color.Orange;
- break;
- case Tile.Type.None:
- break;
- case Tile.Type.Move:
- break;
- case Tile.Type.Slide:
- break;
- case Tile.Type.SlideRight:
- break;
- case Tile.Type.SlideLeft:
- break;
- case Tile.Type.Solid:
- color = Color.Red;
- break;
- default:
- color = Color.Green;
- break;
- }
- if (tile.CurrentType != Tile.Type.None)
- {
- Primitives2D.DrawRectangle(pSpriteBatch,
- new Rectangle(tile.Column * _tileWidth, tile.Line * _tileHeight, _tileWidth, _tileHeight),
- color, 1);
- }
- }
- }
- #region Delegates
- public void OnTileDestroy(MobileObject pMob)
- {
- DestroyTile(pMob);
- }
- #endregion
- #region Collisions
- /// <summary>
- /// Checks if a sprite is colliding in a given direction.
- /// </summary>
- /// <param name="pSprite2D"></param>
- /// <param name="pDirection"></param>
- /// <returns></returns>
- public bool Collide(Sprite2D pSprite2D, Direction pDirection)
- {
- switch (pDirection)
- {
- case Direction.Above:
- var id1A = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + 1,
- pSprite2D.BoundingBox.Y + 1));
- var id2A = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + (pSprite2D.BoundingBox.Width - 2),
- pSprite2D.BoundingBox.Y - _tileHeight / 2));
- if (IsSolid(id1A) || IsSolid(id2A))
- {
- if (IsJumpThrough(id1A) || IsJumpThrough(id2A) &&
- pSprite2D.MovementSpeed.Y < 0)
- return false;
- return true;
- }
- return false;
- case Direction.Below:
- var iUnder = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width / 2,
- pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height + pSprite2D.MovementSpeed.Y));
- var overLap = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width / 2,
- pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height + pSprite2D.MovementSpeed.Y - 1));
- if (IsSolid(iUnder) || IsSolid(overLap))
- return true;
- return false;
- case Direction.Left:
- var id1L = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.Left + pSprite2D.MovementSpeed.X - 1,
- pSprite2D.BoundingBox.Y + 6));
- var id2L = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.Left + pSprite2D.MovementSpeed.X - 1,
- pSprite2D.BoundingBox.Y + (pSprite2D.BoundingBox.Height - 1)));
- if (IsSolid(id1L) || IsSolid(id2L))
- {
- if (IsJumpThrough(id1L) || IsJumpThrough(id2L))
- return false;
- return true;
- }
- return false;
- case Direction.Right:
- var id1R = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width + pSprite2D.MovementSpeed.X + 3,
- pSprite2D.BoundingBox.Y + 3));
- var id2R = GetTilesAt(new Vector2(
- pSprite2D.BoundingBox.X + pSprite2D.BoundingBox.Width + pSprite2D.MovementSpeed.X + 3,
- pSprite2D.BoundingBox.Y + pSprite2D.BoundingBox.Height - 2));
- if (IsSolid(id1R) || IsSolid(id2R))
- {
- if (IsJumpThrough(id1R) || IsJumpThrough(id2R))
- return false;
- return true;
- }
- return false;
- default:
- return false;
- }
- }
- /// <summary>
- /// Returns a tile at (vector2) position.
- /// </summary>
- /// <param name="pPos"></param>
- /// <returns></returns>
- public Tile.Type GetTilesAt(Vector2 pPos)
- {
- var mobRect = new Rectangle((int)pPos.X, (int)pPos.Y, _tileWidth, _tileHeight);
- var col = Math.Floor(pPos.X / _tileWidth);
- var lig = Math.Floor(pPos.Y / _tileHeight);
- if (col >= 0 && col < _mapWidth &&
- lig >= 0 && lig < _mapHeight)
- {
- var tile = CollisionsLayer.Tiles[(int)((lig * _mapWidth) + col)];
- CurrentTestedTile = tile;
- CurrentCollisionRectangle = new Rectangle((int)(col * _tileWidth), (int)(lig * _tileHeight), _tileWidth, _tileHeight);
- if (mobRect.Intersects(tile.Rectangle))
- return tile.CurrentType;
- }
- return Tile.Type.None;
- }
- /// <summary>
- /// Can we jump through the tile?
- /// </summary>
- /// <param name="pType"></param>
- /// <returns></returns>
- protected static bool IsJumpThrough(Tile.Type pType)
- {
- if (pType == Tile.Type.JumpThrough)
- return true;
- return false;
- }
- /// <summary>
- /// Is the tile a ladder?
- /// </summary>
- /// <param name="pType"></param>
- /// <returns></returns>
- public bool IsLadder(Tile.Type pType)
- {
- if (pType == Tile.Type.Ladder)
- return true;
- return false;
- }
- /// <summary>
- /// Is the tile solid?
- /// </summary>
- /// <param name="pType"></param>
- /// <returns></returns>
- protected static bool IsSolid(Tile.Type pType)
- {
- if (pType == Tile.Type.Solid ||
- pType == Tile.Type.Break ||
- pType == Tile.Type.JumpThrough ||
- pType == Tile.Type.Climb)
- return true;
- return false;
- }
- #endregion
- #region Destroy Tiles
- /// <summary>
- /// Destroys a tile.
- /// </summary>
- /// <param name="pMob"></param>
- public void DestroyTile(MobileObject pMob)
- {
- if (pMob is DestructibleBlock pDBlock)
- {
- DestroyTilesAt(new Vector2(
- pDBlock.BoundingBox.X,
- pDBlock.BoundingBox.Y));
- }
- else if (pMob is DroppingBlock pDpBlock)
- {
- DestroyTilesAt(new Vector2(
- pDpBlock.BoundingBox.X,
- pDpBlock.BoundingBox.Y + _tileHeight));
- }
- }
- /// <summary>
- /// destroys a tile from the collision List,
- /// doesn't affect other layers.
- /// </summary>
- /// <param name="pPos"></param>
- protected void DestroyTilesAt(Vector2 pPos)
- {
- var col = Math.Floor(pPos.X / _tileWidth);
- var lig = Math.Floor(pPos.Y / _tileHeight);
- if (col >= 0 && col < _mapWidth &&
- lig >= 0 && lig < _mapHeight)
- {
- var tile = CollisionsLayer.Tiles[(int)((lig * _mapWidth) + col)];
- tile.Break();
- }
- }
- #endregion
- public enum Direction
- {
- Above,
- Below,
- Left,
- Right
- }
- #region Helper Methods
- protected int CoordToGid(List<Tile> pList, Vector2 pPos) // Todo
- {
- var index = 0;
- var col = Math.Floor(pPos.X / _tileWidth);
- var lig = Math.Floor(pPos.Y / _tileHeight);
- if (col >= 0 && col < _mapWidth &&
- lig >= 0 && lig < _mapHeight)
- {
- index = (int)((lig * _mapWidth) + col);
- }
- return index;
- }
- /// <summary>
- /// Get all the map layers in a list.
- /// </summary>
- /// <param name="pMap"></param>
- /// <returns></returns>
- protected List<MapLayer> GetAllLayers(TmxMap pMap)
- {
- var lstLayers = new List<MapLayer>();
- for (int i = 0; i < Map.Layers.Count; i++)
- {
- if (Map.Layers[i] is TmxLayer layer)
- {
- var oldLayer = layer;
- // 1: create an empty new layer
- var newLayer = new MapLayer()
- {
- Name = oldLayer.Name,
- IsVisible = oldLayer.Visible
- };
- var newTiles = new List<Tile>();
- // 2 create tiles, transpose values and fill up the new layer.
- for (int j = 0; j < oldLayer.Tiles.Count; j++)
- {
- var oldTile = oldLayer.Tiles[j];
- var newTile = new Tile(oldTile, Map);
- // 3 get the Type from the tileSet.
- if (layer.Name.ToLower() == ColLayerStr.ToLower())
- newTile.CurrentType = newTile.ParseType(_tileSet);
- newTiles.Add(newTile);
- }
- newLayer.Tiles = newTiles;
- lstLayers.Add(newLayer);
- }
- }
- return lstLayers;
- }
- /// <summary>
- /// Puts layers in separate lists depending on their depth.
- /// </summary>
- /// <param name="pMap"></param>
- protected void SortLayersByDepth(TmxMap pMap)
- {
- BackLayers = new List<MapLayer>();
- MidLayers = new List<MapLayer>();
- FrontLayers = new List<MapLayer>();
- for (int i = 0; i < Map.Layers.Count; i++)
- {
- if (Map.Layers[i] is TmxLayer layer)
- {
- var oldLayer = layer;
- // 1: create an empty new layer
- var newLayer = new MapLayer()
- {
- Name = oldLayer.Name,
- IsVisible = oldLayer.Visible
- };
- const string depthStr = "Depth";
- if (oldLayer.Properties.ContainsKey(depthStr))
- newLayer.Depth = Convert.ToInt16(oldLayer.Properties[depthStr]);
- else
- newLayer.Depth = 999;
- var newTiles = new List<Tile>();
- // 2 create tiles, transpose values and fill up the new layer.
- for (int j = 0; j < oldLayer.Tiles.Count; j++)
- {
- var oldTile = oldLayer.Tiles[j];
- var newTile = new Tile(oldTile, Map);
- // 3 get the Type from the tileSet.
- if (layer.Name.ToLower() == ColLayerStr.ToLower())
- newTile.CurrentType = newTile.ParseType(_tileSet);
- newTiles.Add(newTile);
- }
- newLayer.Tiles = newTiles;
- if (newLayer.Depth == 0)
- {
- FrontLayers.Add(newLayer);
- }
- else if (newLayer.Depth == 1)
- {
- MidLayers.Add(newLayer);
- }
- else if (newLayer.Depth == 2)
- {
- BackLayers.Add(newLayer);
- }
- }
- }
- }
- /// <summary>
- /// Returns the layer named after the param string..
- /// </summary>
- /// <param name="pName"></param>
- /// <returns></returns>
- protected MapLayer GetLayerByName(string pName)
- {
- var requestedLayer = new MapLayer();
- var allLayers = GetAllLayers(Map);
- for (int i = 0; i < allLayers.Count; i++)
- {
- var layer = allLayers[i];
- if (layer.Name.ToLower() == pName.ToLower())
- requestedLayer = layer;
- }
- return requestedLayer;
- }
- /// <summary>
- /// Init map variables.
- /// </summary>
- /// <param name="pContent"></param>
- private void LoadMap(ContentManager pContent)
- {
- Map = new TmxMap(World.LevelPath);
- _tileSet = Map.Tilesets[0];
- _tileSetTexture2D = pContent.Load<Texture2D>("Levels/" + _tileSet.Name);
- _tileWidth = _tileSet.TileWidth;
- _tileHeight = _tileSet.TileHeight;
- _mapWidth = Map.Width;
- _mapHeight = Map.Height;
- _tileSetColumns = _tileSetTexture2D.Width / _tileWidth;
- _tileSetLines = _tileSetTexture2D.Height / _tileHeight;
- }
- #endregion
- }
- public class MapLayer
- {
- public string Name;
- public bool IsVisible;
- public List<Tile> Tiles;
- public int Depth;
- public void Fade(Tile.FadeMotion pFade)
- {
- for (int i = 0; i < Tiles.Count; i++)
- {
- Tiles[i].Fade(pFade);
- }
- }
- }
- public class Tile : Component
- {
- public int Gid;
- public int Column;
- public int Line;
- public int X => Column * TileSize;
- public int Y => Line * TileSize;
- public Vector2 Position => new Vector2(X, Y);
- public Rectangle Rectangle
- {
- get
- {
- if (CurrentType == Type.JumpThrough)
- return new Rectangle(X, Y, TileSize, TileSize / 2);
- else return new Rectangle(X, Y, TileSize, TileSize);
- }
- }
- protected int TileSize;
- public bool OldVisible;
- public bool IsVisible;
- #region Colors
- public Color Color
- {
- get { return BaseColor * Alpha; }
- }
- protected float Alpha;
- protected float AlphaGoal;
- protected float AlphaMax;
- protected float AlphaMin;
- protected float AlphaStep;
- protected Color BaseColor;
- protected FadeMotion CurrentFade;
- #endregion
- public Type OldType;
- public Type CurrentType;
- public Tile(TmxLayerTile pTile, TmxMap pMap)
- {
- OldType = Type.None;
- CurrentType = Type.None;
- IsVisible = true;
- OldVisible = true;
- Gid = pTile.Gid;
- Column = pTile.X;
- Line = pTile.Y;
- TileSize = pMap.TileWidth;
- Alpha = 1f;
- AlphaGoal = 1f;
- AlphaMin = 0;
- AlphaMax = 1;
- AlphaStep = 0.1f;
- BaseColor = Color.White;
- }
- #region Helper Methods
- public void Fade(FadeMotion pFade)
- {
- switch (pFade)
- {
- case FadeMotion.None:
- break;
- case FadeMotion.In:
- FadeIn();
- break;
- case FadeMotion.Out:
- FadeOut();
- break;
- }
- }
- protected void FadeIn()
- {
- AlphaGoal = AlphaMax;
- CurrentFade = FadeMotion.In;
- }
- protected void FadeOut()
- {
- AlphaGoal = AlphaMin;
- CurrentFade = FadeMotion.Out;
- }
- public void Break()
- {
- if (CurrentType != Type.Break)
- return;
- if (OldType != CurrentType)
- {
- OldType = CurrentType;
- CurrentType = Type.None;
- Alpha = 0;
- }
- }
- public void Repair()
- {
- var tempType = CurrentType;
- CurrentType = OldType;
- OldType = tempType;
- Alpha = 1;
- }
- public void SetVisible(bool pVisible)
- {
- IsVisible = pVisible;
- }
- public Type ParseType(TmxTileSet pTileSet)
- {
- if (Gid == 0)
- return Type.None;
- if (pTileSet.Tiles.ContainsKey(Gid - 1))
- {
- var tile = pTileSet.Tiles[Gid - 1];
- switch (tile.Type.ToLower())
- {
- case "":
- return Type.None;
- case "break":
- return Type.Break;
- case "climb":
- return Type.Climb;
- case "exit":
- return Type.Exit;
- case "hurt":
- return Type.Hurt;
- case "ladder":
- return Type.Ladder;
- case "jumpthrough":
- return Type.JumpThrough;
- case "none":
- return Type.None;
- case "move":
- return Type.Move;
- case "slide":
- return Type.Slide;
- case "slideright":
- return Type.SlideRight;
- case "slideleft":
- return Type.SlideLeft;
- case "solid":
- return Type.Solid;
- default:
- return Type.None;
- }
- }
- else
- {
- return Type.None;
- }
- }
- private void UpdateColors()
- {
- if (CurrentFade == FadeMotion.Out)
- {
- if (Alpha > AlphaGoal)
- {
- Alpha -= AlphaStep;
- if (Alpha < AlphaMin)
- Alpha = AlphaMin;
- }
- }
- if (CurrentFade == FadeMotion.In)
- {
- if (Alpha < AlphaGoal)
- {
- Alpha += AlphaStep;
- if (Alpha > AlphaMax)
- Alpha = AlphaMax;
- }
- }
- }
- #endregion
- public override void Update(GameTime pGameTime)
- {
- UpdateColors();
- base.Update(pGameTime);
- }
- /// <summary>
- /// Enum => types of tiles.
- /// </summary>
- public enum Type
- {
- Break,
- Climb,
- Exit,
- Hurt,
- Ladder,
- JumpThrough,
- None,
- Move,
- Slide,
- SlideRight,
- SlideLeft,
- Solid,
- }
- /// <summary>
- /// Type of fade effect.
- /// </summary>
- public enum FadeMotion
- {
- None,
- In,
- Out
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement