MapData.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.Linq;
  4. public class MapData
  5. {
  6. public int Width { get; private set; }
  7. public int Height { get; private set; }
  8. private MapTile[,] tiles;
  9. private List<Settlement> settlements;
  10. public MapData(int width, int height)
  11. {
  12. Width = width;
  13. Height = height;
  14. tiles = new MapTile[width, height];
  15. settlements = new List<Settlement>();
  16. InitializeTiles();
  17. }
  18. private void InitializeTiles()
  19. {
  20. for (int x = 0; x < Width; x++)
  21. {
  22. for (int y = 0; y < Height; y++)
  23. {
  24. tiles[x, y] = new MapTile(x, y);
  25. }
  26. }
  27. }
  28. public MapTile GetTile(int x, int y)
  29. {
  30. if (IsValidPosition(x, y))
  31. return tiles[x, y];
  32. return null;
  33. }
  34. public void SetTile(int x, int y, MapTile tile)
  35. {
  36. if (IsValidPosition(x, y))
  37. tiles[x, y] = tile;
  38. }
  39. public bool IsValidPosition(int x, int y)
  40. {
  41. return x >= 0 && x < Width && y >= 0 && y < Height;
  42. }
  43. public void ExpandMap(int newWidth, int newHeight)
  44. {
  45. MapTile[,] newTiles = new MapTile[newWidth, newHeight];
  46. // Copy existing tiles
  47. for (int x = 0; x < Width; x++)
  48. {
  49. for (int y = 0; y < Height; y++)
  50. {
  51. newTiles[x, y] = tiles[x, y];
  52. }
  53. }
  54. // Initialize new tiles
  55. for (int x = 0; x < newWidth; x++)
  56. {
  57. for (int y = 0; y < newHeight; y++)
  58. {
  59. if (newTiles[x, y] == null)
  60. {
  61. newTiles[x, y] = new MapTile(x, y);
  62. }
  63. }
  64. }
  65. tiles = newTiles;
  66. Width = newWidth;
  67. Height = newHeight;
  68. }
  69. public void ExpandMapWithOffset(int newWidth, int newHeight, int offsetX, int offsetY)
  70. {
  71. MapTile[,] newTiles = new MapTile[newWidth, newHeight];
  72. // Copy existing tiles with offset
  73. for (int x = 0; x < Width; x++)
  74. {
  75. for (int y = 0; y < Height; y++)
  76. {
  77. int newX = x + offsetX;
  78. int newY = y + offsetY;
  79. if (newX >= 0 && newX < newWidth && newY >= 0 && newY < newHeight)
  80. {
  81. newTiles[newX, newY] = tiles[x, y];
  82. // Update tile coordinates
  83. newTiles[newX, newY].x = newX;
  84. newTiles[newX, newY].y = newY;
  85. }
  86. }
  87. }
  88. // Initialize new tiles
  89. for (int x = 0; x < newWidth; x++)
  90. {
  91. for (int y = 0; y < newHeight; y++)
  92. {
  93. if (newTiles[x, y] == null)
  94. {
  95. newTiles[x, y] = new MapTile(x, y);
  96. }
  97. }
  98. }
  99. // Update settlements positions
  100. foreach (var settlement in settlements)
  101. {
  102. settlement.position.x += offsetX;
  103. settlement.position.y += offsetY;
  104. }
  105. tiles = newTiles;
  106. Width = newWidth;
  107. Height = newHeight;
  108. }
  109. public void AddSettlement(Settlement settlement)
  110. {
  111. settlements.Add(settlement);
  112. }
  113. public List<Settlement> GetTowns()
  114. {
  115. return settlements.Where(s => s.Type == SettlementType.Town).ToList();
  116. }
  117. public List<Settlement> GetVillages()
  118. {
  119. return settlements.Where(s => s.Type == SettlementType.Village).ToList();
  120. }
  121. public List<Vector2Int> GetNeighbors(int x, int y, bool includeDiagonals = false)
  122. {
  123. List<Vector2Int> neighbors = new List<Vector2Int>();
  124. for (int dx = -1; dx <= 1; dx++)
  125. {
  126. for (int dy = -1; dy <= 1; dy++)
  127. {
  128. if (dx == 0 && dy == 0) continue;
  129. if (!includeDiagonals && dx != 0 && dy != 0) continue;
  130. int nx = x + dx;
  131. int ny = y + dy;
  132. if (IsValidPosition(nx, ny))
  133. {
  134. neighbors.Add(new Vector2Int(nx, ny));
  135. }
  136. }
  137. }
  138. return neighbors;
  139. }
  140. }
  141. [System.Serializable]
  142. public class Settlement
  143. {
  144. public string name;
  145. public SettlementType Type;
  146. public Vector2Int position;
  147. public List<Vector2Int> tiles;
  148. public Settlement(string name, SettlementType type, Vector2Int position)
  149. {
  150. this.name = name;
  151. this.Type = type;
  152. this.position = position;
  153. this.tiles = new List<Vector2Int>();
  154. }
  155. }
  156. public enum SettlementType
  157. {
  158. Town,
  159. Village
  160. }