2
0

TileMap.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace Robot_Rampage
  8. {
  9. static class TileMap
  10. {
  11. #region Declarations
  12. public const int TileWidth = 32;
  13. public const int TileHeight = 32;
  14. public const int MapWidth = 50;
  15. public const int MapHeight = 50;
  16. public const int FloorTileStart = 0;
  17. public const int FloorTileEnd = 3;
  18. public const int WallTileStart = 4;
  19. public const int WallTileEnd = 7;
  20. static private Texture2D texture;
  21. static private List<Rectangle> tiles = new List<Rectangle>();
  22. static private int[,] mapSquares = new int[MapWidth, MapHeight];
  23. static private Random rand = new Random();
  24. #endregion
  25. #region Initialization
  26. static public void Initialize(Texture2D tileTexture)
  27. {
  28. texture = tileTexture;
  29. tiles.Clear();
  30. tiles.Add(new Rectangle(0, 0, TileWidth, TileHeight));
  31. tiles.Add(new Rectangle(32, 0, TileWidth, TileHeight));
  32. tiles.Add(new Rectangle(64, 0, TileWidth, TileHeight));
  33. tiles.Add(new Rectangle(96, 0, TileWidth, TileHeight));
  34. tiles.Add(new Rectangle(0, 32, TileWidth, TileHeight));
  35. tiles.Add(new Rectangle(32, 32, TileWidth, TileHeight));
  36. tiles.Add(new Rectangle(64, 32, TileWidth, TileHeight));
  37. tiles.Add(new Rectangle(96, 32, TileWidth, TileHeight));
  38. for (int x = 0; x < MapWidth; x++)
  39. for (int y = 0; y < MapHeight; y++)
  40. {
  41. mapSquares[x, y] = FloorTileStart;
  42. }
  43. GenerateRandomMap();
  44. }
  45. #endregion
  46. #region Information about Map Squares
  47. static public int GetSquareByPixelX(int pixelX)
  48. {
  49. return pixelX / TileWidth;
  50. }
  51. static public int GetSquareByPixelY(int pixelY)
  52. {
  53. return pixelY / TileHeight;
  54. }
  55. static public Vector2 GetSquareAtPixel(Vector2 pixelLocation)
  56. {
  57. return new Vector2(
  58. GetSquareByPixelX((int)pixelLocation.X),
  59. GetSquareByPixelY((int)pixelLocation.Y));
  60. }
  61. static public Vector2 GetSquareCenter(int squareX, int squareY)
  62. {
  63. return new Vector2(
  64. (squareX * TileWidth) + (TileWidth / 2),
  65. (squareY * TileHeight) + (TileHeight / 2));
  66. }
  67. static public Vector2 GetSquareCenter(Vector2 square)
  68. {
  69. return GetSquareCenter(
  70. (int)square.X,
  71. (int)square.Y);
  72. }
  73. static public Rectangle SquareWorldRectangle(int x, int y)
  74. {
  75. return new Rectangle(
  76. x * TileWidth,
  77. y * TileHeight,
  78. TileWidth,
  79. TileHeight);
  80. }
  81. static public Rectangle SquareWorldRectangle(Vector2 square)
  82. {
  83. return SquareWorldRectangle(
  84. (int)square.X,
  85. (int)square.Y);
  86. }
  87. static public Rectangle SquareScreenRectangle(int x, int y)
  88. {
  89. return Camera.Transform(SquareWorldRectangle(x, y));
  90. }
  91. static public Rectangle SquareSreenRectangle(Vector2 square)
  92. {
  93. return SquareScreenRectangle((int)square.X, (int)square.Y);
  94. }
  95. #endregion
  96. #region Information about Map Tiles
  97. static public int GetTileAtSquare(int tileX, int tileY)
  98. {
  99. if ((tileX >= 0) && (tileX < MapWidth) &&
  100. (tileY >= 0) && (tileY < MapHeight))
  101. {
  102. return mapSquares[tileX, tileY];
  103. }
  104. else
  105. {
  106. return -1;
  107. }
  108. }
  109. static public void SetTileAtSquare(int tileX, int tileY, int tile)
  110. {
  111. if ((tileX >= 0) && (tileX < MapWidth) &&
  112. (tileY >= 0) && (tileY < MapHeight))
  113. {
  114. mapSquares[tileX, tileY] = tile;
  115. }
  116. }
  117. static public int GetTileAtPixel(int pixelX, int pixelY)
  118. {
  119. return GetTileAtSquare(
  120. GetSquareByPixelX(pixelX),
  121. GetSquareByPixelY(pixelY));
  122. }
  123. static public int GetTileAtPixel(Vector2 pixelLocation)
  124. {
  125. return GetTileAtPixel(
  126. (int)pixelLocation.X,
  127. (int)pixelLocation.Y);
  128. }
  129. static public bool IsWallTile(int tileX, int tileY)
  130. {
  131. int tileIndex = GetTileAtSquare(tileX, tileY);
  132. if (tileIndex == -1)
  133. {
  134. return false;
  135. }
  136. return tileIndex >= WallTileStart;
  137. }
  138. static public bool IsWallTile(Vector2 square)
  139. {
  140. return IsWallTile((int)square.X, (int)square.Y);
  141. }
  142. static public bool IsWallTileByPixel(Vector2 pixelLocation)
  143. {
  144. return IsWallTile(
  145. GetSquareByPixelX((int)pixelLocation.X),
  146. GetSquareByPixelY((int)pixelLocation.Y));
  147. }
  148. #endregion
  149. #region Map Generation
  150. static public void GenerateRandomMap()
  151. {
  152. int wallChancePerSqare = 10;
  153. int floorTile = rand.Next(FloorTileStart, FloorTileEnd + 1);
  154. int wallTile = rand.Next(WallTileStart, WallTileEnd + 1);
  155. for (int x = 0; x < MapWidth; x++)
  156. for (int y = 0; y < MapHeight; y++)
  157. {
  158. mapSquares[x, y] = floorTile;
  159. if ((x == 0) || (y == 0) ||
  160. (x == MapWidth - 1) || (y == MapHeight - 1))
  161. {
  162. mapSquares[x, y] = wallTile;
  163. continue;
  164. }
  165. if ((x == 1) || (y == 1) ||
  166. (x == MapWidth - 2) || (y == MapHeight - 2))
  167. {
  168. continue;
  169. }
  170. if (rand.Next(0, 100) <= wallChancePerSqare)
  171. mapSquares[x, y] = wallTile;
  172. }
  173. }
  174. #endregion
  175. #region Drawing
  176. static public void Draw(SpriteBatch spriteBatch)
  177. {
  178. int startX = GetSquareByPixelX((int)Camera.Position.X);
  179. int endX = GetSquareByPixelX((int)Camera.Position.X +
  180. Camera.ViewPortWidth);
  181. int startY = GetSquareByPixelY((int)Camera.Position.Y);
  182. int endY = GetSquareByPixelY((int)Camera.Position.Y +
  183. Camera.ViewPortHeight);
  184. for (int x = startX; x <= endX; x++)
  185. for (int y = startY; y <= endY; y++)
  186. {
  187. if ((x >= 0) && (y >= 0) &&
  188. (x < MapWidth) && (y < MapHeight))
  189. {
  190. spriteBatch.Draw(
  191. texture,
  192. SquareScreenRectangle(x, y),
  193. tiles[GetTileAtSquare(x, y)],
  194. Color.White);
  195. }
  196. }
  197. }
  198. #endregion
  199. }
  200. }