fork download
  1. [System.Serializable]
  2. public class MapComponent : MonoBehaviour {
  3.  
  4.  
  5.  
  6. // Private variables
  7.  
  8. // PUBLIC
  9. // Base values
  10. public GameObject tileBasicPrefab; // Basic prefab
  11. public GameObject tileRoadPrefab; // Single road prefab
  12. public GameObject mapPivot; // Parent object of tiles
  13.  
  14. public enum TileState {
  15. Exist,
  16. Deleted,
  17. IsRoad,
  18. };
  19.  
  20. // Grid
  21. public float cellSize = 5f; // Distance between tiles
  22. public int width = 10; // Width in tiles
  23. public int height = 10; // Height in tiles
  24.  
  25. public GameObject[,] tilesGrid = new GameObject[0, 0]; // Array of tiles
  26. public TileState[,] tilesState = new TileState[0, 0]; // Array of tiles state
  27.  
  28. public GridCoordinates localCoordinates; // Grid of local coordinates
  29.  
  30. public volatile bool isGenerated = false;// Is generation procedure executed
  31.  
  32.  
  33. /**
  34. * On tile deletion
  35. * @param - <INT> height in tiles
  36. * @param - <INT> width in tiles
  37. * @param - <FLOAT> distance between cells in units
  38. * @return - NONE
  39. */
  40. public void DestroyTile(Vector3 tilePos)
  41. {
  42.  
  43. }
  44.  
  45. /**
  46. * Compare tile real state with state array. Replace if needed.
  47. * @param - NONE
  48. * @return - NONE
  49. */
  50. public void Repair()
  51. {
  52. bool isWrongTile;
  53. GameObject tile;
  54. TileState tileState;
  55. for (int x = 0; x < width; x++) {
  56. for (int z = 0; z < height; z++) {
  57. tile = (tilesGrid[x, z]);
  58. tileState = tilesState[x, z];
  59. switch (tileState) {
  60. case TileState.Exist:
  61. isWrongTile = ((tile == null) || (tile != tileBasicPrefab));
  62. if (isWrongTile) {
  63. if (tile != null) DestroyImmediate(tile);
  64. CreateTile(x, z);
  65. }
  66. break;
  67.  
  68. case TileState.Deleted:
  69. isWrongTile = ((tile != null));
  70. if (isWrongTile) DestroyImmediate(tile);
  71. break;
  72.  
  73. case TileState.IsRoad:
  74. isWrongTile = ((tile == null) || (tile != tileRoadPrefab));
  75. if (isWrongTile) {
  76. if (tile != null) DestroyImmediate(tile);
  77. CreateRoad(x, z);
  78. }
  79. break;
  80.  
  81.  
  82. default: Debug.LogError("Wrong enum arg in MapComponent"); break;
  83. }
  84. }
  85. }
  86. }
  87.  
  88.  
  89. /**
  90. * Delete previous tilemap and creates new by received arguments
  91. * @param - <INT> height in tiles
  92. * @param - <INT> width in tiles
  93. * @param - <FLOAT> distance between cells in units
  94. * @return - NONE
  95. */
  96. public void Generate(int new_height, int new_width, float new_cellSize)
  97. {
  98. // Set new variables
  99. height = new_height;
  100. width = new_width;
  101. cellSize = new_cellSize;
  102.  
  103. // Clear map pivot and childrens if exist
  104. if (mapPivot != null) {
  105. isGenerated = false;
  106. DestroyImmediate(mapPivot);
  107. }
  108.  
  109. // Create new pivot
  110. mapPivot = new GameObject("MapPivot");
  111. mapPivot.transform.parent = this.transform;
  112.  
  113. // Create and generate new grid
  114. localCoordinates = new GridCoordinates(width, height, cellSize);
  115.  
  116.  
  117. tilesGrid = null;
  118. tilesGrid = new GameObject[width, height];
  119. tilesState = null;
  120. tilesState = new TileState[width, height];
  121.  
  122. // Fill pivot with childrens by local grid
  123. for (int x = 0; x < localCoordinates.GetWidth(); x++) {
  124. for (int z = 0; z < localCoordinates.GetHeight(); z++) {
  125. CreateTile(x, z);
  126. }
  127. }
  128.  
  129. // Return new pivot to old one position
  130. mapPivot.transform.position = this.gameObject.transform.position;
  131. mapPivot.transform.rotation = this.gameObject.transform.rotation;
  132.  
  133. // Mark as generated
  134. isGenerated = true;
  135.  
  136. // Write variables
  137. UnityEditor.EditorUtility.SetDirty(this);
  138. }
  139.  
  140. private GameObject CreateTile(int x, int z)
  141. {
  142. // Get tile local position
  143. Vector3 spawnPos = localCoordinates.GetCoordsOf(x, z);
  144. // Create tile by prefab
  145. GameObject tile = Instantiate(tileBasicPrefab, spawnPos, Quaternion.identity);
  146. // Initialize tile
  147. tile.AddComponent<MapTileComponent>().Init(this, x, z);
  148. // Add tile to array
  149. tilesGrid[x, z] = tile;
  150. // Attach tile to map pivot
  151. tile.transform.SetParent(mapPivot.transform);
  152. // Set local coords
  153. tile.transform.localPosition = spawnPos;
  154. // Set tile state
  155. tilesState[x, z] = TileState.Exist;
  156.  
  157. return tile;
  158. }
  159.  
  160. private GameObject CreateRoad(int x, int z)
  161. {
  162. // Get tile local position
  163. Vector3 spawnPos = localCoordinates.GetCoordsOf(x, z);
  164. // Create tile by prefab
  165. GameObject tile = Instantiate(tileRoadPrefab, spawnPos, Quaternion.identity);
  166. // Initialize tile
  167. tile.AddComponent<MapTileComponent>().Init(this, x, z);
  168. // Add tile to array
  169. tilesGrid[x, z] = tile;
  170. // Attach tile to map pivot
  171. tile.transform.SetParent(mapPivot.transform);
  172. // Set local coords
  173. tile.transform.localPosition = spawnPos;
  174. // Set tile state
  175. tilesState[x, z] = TileState.IsRoad;
  176.  
  177. return tile;
  178. }
  179.  
  180. public void SwitchType(Vector3Int tilePos)
  181. {
  182. GameObject tile = (tilesGrid[tilePos.x, tilePos.z]);
  183. TileState tileState = tilesState[tilePos.x, tilePos.z];
  184. DestroyImmediate(tile);
  185. switch (tileState) {
  186. case TileState.Exist:
  187. CreateRoad(tilePos.x, tilePos.z);
  188. tilesState[tilePos.x, tilePos.z] = TileState.IsRoad;
  189. break;
  190.  
  191. case TileState.IsRoad:
  192. CreateTile(tilePos.x, tilePos.z);
  193. tilesState[tilePos.x, tilePos.z] = TileState.Exist;
  194. break;
  195.  
  196. default: Debug.LogError("Wrong enum arg in MapComponent"); break;
  197. }
  198.  
  199. }
  200. }
  201.  
  202.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(2,29): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(10,9): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(11,9): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(12,9): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(25,9): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(28,9): error CS0246: The type or namespace name `GridCoordinates' could not be found. Are you missing an assembly reference?
prog.cs(40,26): error CS0246: The type or namespace name `Vector3' could not be found. Are you missing `System.Numerics' using directive?
prog.cs(140,10): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(160,10): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
prog.cs(180,25): error CS0246: The type or namespace name `Vector3Int' could not be found. Are you missing an assembly reference?
Compilation failed: 10 error(s), 0 warnings
stdout
Standard output is empty