TileMap.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Microsoft.Xna.Framework.Storage;
  12. using System.IO;
  13. using System.Xml.Serialization;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. namespace Tile_Engine
  16. {
  17. public static class TileMap
  18. {
  19. #region Declarations
  20. public const int TileWidth = 48;
  21. public const int TileHeight = 48;
  22. public const int MapWidth = 160;
  23. public const int MapHeight = 12;
  24. public const int MapLayers = 3;
  25. private const int skyTile = 2;
  26. static private MapSquare[,] mapCells =
  27. new MapSquare[MapWidth, MapHeight];
  28. public static bool EditorMode = false;
  29. public static SpriteFont spriteFont;
  30. static private Texture2D tileSheet;
  31. #endregion
  32. #region Initialization
  33. static public void Initialize(Texture2D tileTexture)
  34. {
  35. tileSheet = tileTexture;
  36. for (int x = 0; x < MapWidth; x++)
  37. {
  38. for (int y = 0; y < MapHeight; y++)
  39. {
  40. for (int z = 0; z < MapLayers; z++)
  41. {
  42. mapCells[x, y] = new MapSquare(skyTile, 0, 0, "", true);
  43. }
  44. }
  45. }
  46. }
  47. #endregion
  48. #region Tile and Tile Sheet Handling
  49. public static int TilesPerRow
  50. {
  51. get { return tileSheet.Width / TileWidth; }
  52. }
  53. public static Rectangle TileSourceRectangle(int tileIndex)
  54. {
  55. return new Rectangle(
  56. (tileIndex % TilesPerRow) * TileWidth,
  57. (tileIndex / TilesPerRow) * TileHeight,
  58. TileWidth,
  59. TileHeight);
  60. }
  61. #endregion
  62. #region Information about Map Cells
  63. static public int GetCellByPixelX(int pixelX)
  64. {
  65. return pixelX / TileWidth;
  66. }
  67. static public int GetCellByPixelY(int pixelY)
  68. {
  69. return pixelY / TileHeight;
  70. }
  71. static public Vector2 GetCellByPixel(Vector2 pixelLocation)
  72. {
  73. return new Vector2(
  74. GetCellByPixelX((int)pixelLocation.X),
  75. GetCellByPixelY((int)pixelLocation.Y));
  76. }
  77. static public Vector2 GetCellCenter(int cellX, int cellY)
  78. {
  79. return new Vector2(
  80. (cellX * TileWidth) + (TileWidth / 2),
  81. (cellY * TileHeight) + (TileHeight / 2));
  82. }
  83. static public Vector2 GetCellCenter(Vector2 cell)
  84. {
  85. return GetCellCenter(
  86. (int)cell.X,
  87. (int)cell.Y);
  88. }
  89. static public Rectangle CellWorldRectangle(int cellX, int cellY)
  90. {
  91. return new Rectangle(
  92. cellX * TileWidth,
  93. cellY * TileHeight,
  94. TileWidth,
  95. TileHeight);
  96. }
  97. static public Rectangle CellWorldRectangle(Vector2 cell)
  98. {
  99. return CellWorldRectangle(
  100. (int)cell.X,
  101. (int)cell.Y);
  102. }
  103. static public Rectangle CellScreenRectangle(int cellX, int cellY)
  104. {
  105. return Camera.WorldToScreen(CellWorldRectangle(cellX, cellY));
  106. }
  107. static public Rectangle CellSreenRectangle(Vector2 cell)
  108. {
  109. return CellScreenRectangle((int)cell.X, (int)cell.Y);
  110. }
  111. static public bool CellIsPassable(int cellX, int cellY)
  112. {
  113. MapSquare square = GetMapSquareAtCell(cellX, cellY);
  114. if (square == null)
  115. return false;
  116. else
  117. return square.Passable;
  118. }
  119. static public bool CellIsPassable(Vector2 cell)
  120. {
  121. return CellIsPassable((int)cell.X, (int)cell.Y);
  122. }
  123. static public bool CellIsPassableByPixel(Vector2 pixelLocation)
  124. {
  125. return CellIsPassable(
  126. GetCellByPixelX((int)pixelLocation.X),
  127. GetCellByPixelY((int)pixelLocation.Y));
  128. }
  129. static public string CellCodeValue(int cellX, int cellY)
  130. {
  131. MapSquare square = GetMapSquareAtCell(cellX, cellY);
  132. if (square == null)
  133. return "";
  134. else
  135. return square.CodeValue;
  136. }
  137. static public string CellCodeValue(Vector2 cell)
  138. {
  139. return CellCodeValue((int)cell.X, (int)cell.Y);
  140. }
  141. #endregion
  142. #region Information about MapSquare objects
  143. static public MapSquare GetMapSquareAtCell(int tileX, int tileY)
  144. {
  145. if ((tileX >= 0) && (tileX < MapWidth) &&
  146. (tileY >= 0) && (tileY < MapHeight))
  147. {
  148. return mapCells[tileX, tileY];
  149. }
  150. else
  151. {
  152. return null;
  153. }
  154. }
  155. static public void SetMapSquareAtCell(
  156. int tileX,
  157. int tileY,
  158. MapSquare tile)
  159. {
  160. if ((tileX >= 0) && (tileX < MapWidth) &&
  161. (tileY >= 0) && (tileY < MapHeight))
  162. {
  163. mapCells[tileX, tileY] = tile;
  164. }
  165. }
  166. static public void SetTileAtCell(
  167. int tileX,
  168. int tileY,
  169. int layer,
  170. int tileIndex)
  171. {
  172. if ((tileX >= 0) && (tileX < MapWidth) &&
  173. (tileY >= 0) && (tileY < MapHeight))
  174. {
  175. mapCells[tileX, tileY].LayerTiles[layer] = tileIndex;
  176. }
  177. }
  178. static public MapSquare GetMapSquareAtPixel(int pixelX, int pixelY)
  179. {
  180. return GetMapSquareAtCell(
  181. GetCellByPixelX(pixelX),
  182. GetCellByPixelY(pixelY));
  183. }
  184. static public MapSquare GetMapSquareAtPixel(Vector2 pixelLocation)
  185. {
  186. return GetMapSquareAtPixel(
  187. (int)pixelLocation.X,
  188. (int)pixelLocation.Y);
  189. }
  190. #endregion
  191. #region Loading and Saving Maps
  192. public static void SaveMap(FileStream fileStream)
  193. {
  194. BinaryFormatter formatter = new BinaryFormatter();
  195. formatter.Serialize(fileStream, mapCells);
  196. fileStream.Close();
  197. }
  198. public static void LoadMap(FileStream fileStream)
  199. {
  200. try
  201. {
  202. BinaryFormatter formatter = new BinaryFormatter();
  203. mapCells = (MapSquare[,])formatter.Deserialize(fileStream);
  204. fileStream.Close();
  205. }
  206. catch
  207. {
  208. ClearMap();
  209. }
  210. }
  211. public static void ClearMap()
  212. {
  213. for (int x = 0; x < MapWidth; x++)
  214. for (int y = 0; y < MapHeight; y++)
  215. for (int z = 0; z < MapLayers; z++)
  216. {
  217. mapCells[x, y] = new MapSquare(2, 0, 0, "", true);
  218. }
  219. }
  220. #endregion
  221. #region Drawing
  222. static public void Draw(SpriteBatch spriteBatch)
  223. {
  224. int startX = GetCellByPixelX((int)Camera.Position.X);
  225. int endX = GetCellByPixelX((int)Camera.Position.X +
  226. Camera.ViewPortWidth);
  227. int startY = GetCellByPixelY((int)Camera.Position.Y);
  228. int endY = GetCellByPixelY((int)Camera.Position.Y +
  229. Camera.ViewPortHeight);
  230. for (int x = startX; x <= endX; x++)
  231. for (int y = startY; y <= endY; y++)
  232. {
  233. for (int z = 0; z < MapLayers; z++)
  234. {
  235. if ((x >= 0) && (y >= 0) &&
  236. (x < MapWidth) && (y < MapHeight))
  237. {
  238. spriteBatch.Draw(
  239. tileSheet,
  240. CellScreenRectangle(x, y),
  241. TileSourceRectangle(mapCells[x, y].LayerTiles[z]),
  242. Color.White,
  243. 0.0f,
  244. Vector2.Zero,
  245. SpriteEffects.None,
  246. 1f - ((float)z * 0.1f));
  247. }
  248. }
  249. if (EditorMode)
  250. {
  251. DrawEditModeItems(spriteBatch, x, y);
  252. }
  253. }
  254. }
  255. public static void DrawEditModeItems(
  256. SpriteBatch spriteBatch,
  257. int x,
  258. int y)
  259. {
  260. if ((x < 0) || (x >= MapWidth) ||
  261. (y < 0) || (y >= MapHeight))
  262. return;
  263. if (!CellIsPassable(x, y))
  264. {
  265. spriteBatch.Draw(
  266. tileSheet,
  267. CellScreenRectangle(x, y),
  268. TileSourceRectangle(1),
  269. new Color(255, 0, 0, 80),
  270. 0.0f,
  271. Vector2.Zero,
  272. SpriteEffects.None,
  273. 0.0f);
  274. }
  275. if (mapCells[x, y].CodeValue != "")
  276. {
  277. Rectangle screenRect = CellScreenRectangle(x, y);
  278. spriteBatch.DrawString(
  279. spriteFont,
  280. mapCells[x, y].CodeValue,
  281. new Vector2(screenRect.X, screenRect.Y),
  282. Color.White,
  283. 0.0f,
  284. Vector2.Zero,
  285. 1.0f,
  286. SpriteEffects.None,
  287. 0.0f);
  288. }
  289. }
  290. #endregion
  291. }
  292. }