GameMap.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. /// <summary>
  4. /// A map/grid of tiles. The GameMapsAsset contains a list of these maps.
  5. /// The map's 0x0 point is considered to be at the bottom-left and WxH at top-right
  6. /// </summary>
  7. [System.Serializable]
  8. public class GameMap
  9. {
  10. /// <summary> a unique identifier for the map </summary>
  11. //public int id;
  12. /// <summary> a unique name by which map can be identified </summary>
  13. public string ident;
  14. /// <summary> width of the map (determines how big grid is) </summary>
  15. public int width;
  16. /// <summary> height of map (determines how big grid is) </summary>
  17. public int height;
  18. /// <summary> The grid/map. -1 is an empty tile, else a value related to GameMapTile.id will be present
  19. /// This is also known as layer-0 or the default layer when there are movethan one layer in the map.
  20. /// </summary>
  21. public int[] grid = new int[0];
  22. /// <summary> The GameMap can have additional layers. In that case the grid[] field above will be the default, or layer-0. </summary>
  23. public GameMapLayer[] layers = new GameMapLayer[0];
  24. // *** extra properties related to this map
  25. // add any addition serializable types here and they will appear
  26. // for editing in map properties section of the map editor
  27. //public Color backgroundColor = Color.black;
  28. // ----------------------------------------------------------------------------------------------------------------
  29. /// <summary> Return the grid index of a tile at position (x,y). No error checking, x or y value is out of bounds, to improve performance. </summary>
  30. public int PositionToIdx(int x, int y)
  31. {
  32. return (y * width + x);
  33. }
  34. /// <summary> Get an x and y position, given specific index into grid[]. No error checking, idx out of bounds, to improve performance. </summary>
  35. public void IdxToPosition(int idx, out int x, out int y)
  36. {
  37. y = idx / width;
  38. x = idx - (y * width);
  39. }
  40. /// <summary> This returns layers.Length + 1 since grid[] is layer-0. </summary>
  41. public int LayerCount { get { return layers.Length + 1; } }
  42. /// <summary> Returns data from a layer. layerIdx=0 is the same as reading GameMap.grid[] while any higher value (1+) will be data from GameMap.layers[layerIdx-1].grid </summary>
  43. public int[] GetlayerData(int layerIdx)
  44. {
  45. return (layerIdx == 0 ? grid : layers[layerIdx - 1].grid);
  46. }
  47. /// <summary> Return an array of GameMapIdxIdPair with the ID and Index of tiles neighbouring the one at x,y.
  48. /// Array is always length 4 else 8 if includeDiagonal=true.
  49. /// idx or id = -1 represents no tile or areas outside of map grid.
  50. /// Result starts at tile above (north) and go around clockwise. </summary>
  51. public GameMapIdxIdPair[] GetNeighbours(int x, int y, bool includeDiagonal = false, int layerIdx = 0)
  52. {
  53. int[] g = (layerIdx == 0 ? grid : layers[layerIdx - 1].grid);
  54. if (includeDiagonal)
  55. {
  56. return new GameMapIdxIdPair[]
  57. {
  58. (y < height - 1 ? new GameMapIdxIdPair(g[((y + 1) * width + (x + 0))], ((y + 1) * width + (x + 0))) : new GameMapIdxIdPair(-1, -1)),
  59. (x < width - 1 && y < height - 1 ? new GameMapIdxIdPair(g[((y + 1) * width + (x + 1))], ((y + 1) * width + (x + 1))) : new GameMapIdxIdPair(-1, -1)),
  60. (x < width - 1 ? new GameMapIdxIdPair(g[((y + 0) * width + (x + 1))], ((y + 0) * width + (x + 1))) : new GameMapIdxIdPair(-1, -1)),
  61. (x < width - 1 && y > 0 ? new GameMapIdxIdPair(g[((y - 1) * width + (x + 1))], ((y - 1) * width + (x + 1))) : new GameMapIdxIdPair(-1, -1)),
  62. (y > 0 ? new GameMapIdxIdPair(g[((y - 1) * width + (x + 0))], ((y - 1) * width + (x + 0))) : new GameMapIdxIdPair(-1, -1)),
  63. (x > 0 && y > 0 ? new GameMapIdxIdPair(g[((y - 1) * width + (x - 1))], ((y - 1) * width + (x - 1))) : new GameMapIdxIdPair(-1, -1)),
  64. (x > 0 ? new GameMapIdxIdPair(g[((y + 0) * width + (x - 1))], ((y + 0) * width + (x - 1))) : new GameMapIdxIdPair(-1, -1)),
  65. (x > 0 && y < height - 1 ? new GameMapIdxIdPair(g[((y + 1) * width + (x - 1))], ((y + 1) * width + (x - 1))) : new GameMapIdxIdPair(-1, -1))
  66. };
  67. }
  68. else
  69. {
  70. return new GameMapIdxIdPair[]
  71. {
  72. (y < height - 1 ? new GameMapIdxIdPair(g[((y + 1) * width + (x + 0))], ((y + 1) * width + (x + 0))) : new GameMapIdxIdPair(-1, -1)),
  73. (x < width - 1 ? new GameMapIdxIdPair(g[((y + 0) * width + (x + 1))], ((y + 0) * width + (x + 1))) : new GameMapIdxIdPair(-1, -1)),
  74. (y > 0 ? new GameMapIdxIdPair(g[((y - 1) * width + (x + 0))], ((y - 1) * width + (x + 0))) : new GameMapIdxIdPair(-1, -1)),
  75. (x > 0 ? new GameMapIdxIdPair(g[((y + 0) * width + (x - 1))], ((y + 0) * width + (x - 1))) : new GameMapIdxIdPair(-1, -1))
  76. };
  77. }
  78. }
  79. // ----------------------------------------------------------------------------------------------------------------
  80. /// <summary> This will destroy the exiting map. Use Resize if you want to keep existing tile data. Mainly used by editor. </summary>
  81. public void SetSize(int w, int h)
  82. {
  83. if (w < 1) w = 1;
  84. if (h < 1) h = 1;
  85. width = w;
  86. height = h;
  87. grid = new int[width * height];
  88. foreach(GameMapLayer l in layers) l.grid = new int[width * height];
  89. ClearMap();
  90. }
  91. /// <summary> Resizes map, keeping existing tile data. Use SetSize() to create a clean resized map. Mainly used by editor. </summary>
  92. public void Resize(int w, int h)
  93. {
  94. if (w < 1) w = 1;
  95. if (h < 1) h = 1;
  96. int[] old = grid;
  97. grid = new int[w * h];
  98. ClearLayer(0);
  99. for (int y = 0; y < h; y++)
  100. {
  101. if (y >= height) break;
  102. for (int x = 0; x < w; x++)
  103. {
  104. if (x >= width) break;
  105. grid[(y * w + x)] = old[(y * width + x)];
  106. }
  107. }
  108. for (int i =0; i < layers.Length; i++)
  109. {
  110. old = layers[i].grid;
  111. layers[i].grid = new int[w * h];
  112. ClearLayer(i+1);
  113. for (int y = 0; y < h; y++)
  114. {
  115. if (y >= height) break;
  116. for (int x = 0; x < w; x++)
  117. {
  118. if (x >= width) break;
  119. layers[i].grid[(y * w + x)] = old[(y * width + x)];
  120. }
  121. }
  122. }
  123. width = w;
  124. height = h;
  125. }
  126. /// <summary> Fill grid with empty tiles (-1). Mainly used by editor. </summary>
  127. public void ClearMap()
  128. {
  129. for (int i = 0; i < grid.Length; i++)
  130. {
  131. grid[i] = -1;
  132. }
  133. foreach (GameMapLayer l in layers)
  134. {
  135. for (int i = 0; i < l.grid.Length; i++)
  136. {
  137. l.grid[i] = -1;
  138. }
  139. }
  140. }
  141. /// <summary> This set the size of the layer. This will destroy any exsiting tile placements in the layer.
  142. /// idx=0 refers to the grid[] while anything higher will be layers[idx-1].grid </summary>
  143. public void InitLayer(int idx)
  144. {
  145. if (idx == 0)
  146. {
  147. grid = new int[width * height];
  148. ClearLayer(0);
  149. }
  150. else
  151. {
  152. layers[idx-1].grid = new int[width * height];
  153. ClearLayer(idx);
  154. }
  155. }
  156. /// <summary> idx=0 refers to the grid[] while anything higher will be layers[idx-1].grid </summary>
  157. public void ClearLayer(int idx)
  158. {
  159. if (idx == 0)
  160. {
  161. for (int i = 0; i < grid.Length; i++)
  162. {
  163. grid[i] = -1;
  164. }
  165. }
  166. else
  167. {
  168. idx--;
  169. for (int i = 0; i < layers[idx].grid.Length; i++)
  170. {
  171. layers[idx].grid[i] = -1;
  172. }
  173. }
  174. }
  175. public override string ToString()
  176. {
  177. return ident;
  178. }
  179. // ----------------------------------------------------------------------------------------------------------------
  180. }