SimpleMapMaker2.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using UnityEngine;
  2. /// <summary>
  3. /// Simplified MapMaker2 - Basic map generation with team placement on towns/villages
  4. /// No exploration or expansion systems - just a clean, working map
  5. /// </summary>
  6. public class SimpleMapMaker2 : MonoBehaviour
  7. {
  8. [Header("Basic Map Settings")]
  9. public int mapSize = 100;
  10. public int seed = 12345;
  11. [Header("Team Placement")]
  12. public GameObject teamMarkerPrefab;
  13. [Header("Visualization")]
  14. public MapVisualizer mapVisualizer;
  15. // Core components
  16. private MapData mapData;
  17. private Transform teamMarker;
  18. void Start()
  19. {
  20. GenerateSimpleMap();
  21. }
  22. /// <summary>
  23. /// Generate a simple map and place team marker on a town/village
  24. /// </summary>
  25. public void GenerateSimpleMap()
  26. {
  27. Debug.Log("🗺️ SimpleMapMaker2: Generating basic map...");
  28. // Generate basic map data
  29. CreateBasicMapData();
  30. // Visualize the map
  31. if (mapVisualizer != null)
  32. {
  33. mapVisualizer.VisualizeMap(mapData);
  34. Debug.Log("✅ Map visualization complete");
  35. }
  36. else
  37. {
  38. Debug.LogWarning("⚠️ MapVisualizer not assigned - creating simple cubes for visualization");
  39. CreateSimpleCubeVisualization();
  40. }
  41. // Place team marker on a town/village
  42. PlaceTeamMarkerOnTown();
  43. Debug.Log($"🎯 SimpleMapMaker2: Map generation complete - Size: {mapSize}x{mapSize}");
  44. }
  45. /// <summary>
  46. /// Create basic map data with terrain and settlements
  47. /// </summary>
  48. private void CreateBasicMapData()
  49. {
  50. Random.InitState(seed);
  51. mapData = new MapData(mapSize, mapSize);
  52. // Generate basic terrain
  53. for (int x = 0; x < mapSize; x++)
  54. {
  55. for (int y = 0; y < mapSize; y++)
  56. {
  57. // Simple terrain generation
  58. float noise = Mathf.PerlinNoise(x * 0.1f, y * 0.1f);
  59. MapTile tile = mapData.GetTile(x, y);
  60. if (tile != null)
  61. {
  62. if (noise < 0.3f)
  63. {
  64. tile.terrainType = TerrainType.Plains;
  65. }
  66. else if (noise < 0.6f)
  67. {
  68. tile.terrainType = TerrainType.Forest;
  69. }
  70. else if (noise < 0.8f)
  71. {
  72. tile.terrainType = TerrainType.Plains; // Use plains for hills
  73. tile.height = 1f; // Use height for elevation
  74. }
  75. else
  76. {
  77. tile.terrainType = TerrainType.Mountain;
  78. }
  79. }
  80. }
  81. }
  82. // Add some roads
  83. AddBasicRoads();
  84. // Add settlements (towns and villages)
  85. AddSettlements();
  86. Debug.Log($"📊 Basic map data created: {mapSize}x{mapSize}");
  87. }
  88. /// <summary>
  89. /// Add basic road network
  90. /// </summary>
  91. private void AddBasicRoads()
  92. {
  93. // Horizontal road across the middle
  94. int midY = mapSize / 2;
  95. for (int x = 0; x < mapSize; x++)
  96. {
  97. MapTile tile = mapData.GetTile(x, midY);
  98. if (tile != null && tile.terrainType != TerrainType.Mountain)
  99. {
  100. tile.featureType = FeatureType.Road;
  101. }
  102. }
  103. // Vertical road across the middle
  104. int midX = mapSize / 2;
  105. for (int y = 0; y < mapSize; y++)
  106. {
  107. MapTile tile = mapData.GetTile(midX, y);
  108. if (tile != null && tile.terrainType != TerrainType.Mountain)
  109. {
  110. tile.featureType = FeatureType.Road;
  111. }
  112. }
  113. Debug.Log("🛤️ Basic roads added");
  114. }
  115. /// <summary>
  116. /// Add towns and villages to the map
  117. /// </summary>
  118. private void AddSettlements()
  119. {
  120. // Add a few towns and villages
  121. Vector2Int[] settlementPositions = {
  122. new Vector2Int(25, 25),
  123. new Vector2Int(75, 25),
  124. new Vector2Int(25, 75),
  125. new Vector2Int(75, 75),
  126. new Vector2Int(50, 50), // Center town
  127. new Vector2Int(30, 60),
  128. new Vector2Int(70, 40)
  129. };
  130. for (int i = 0; i < settlementPositions.Length; i++)
  131. {
  132. Vector2Int pos = settlementPositions[i];
  133. // Make sure position is valid
  134. if (pos.x >= 0 && pos.x < mapSize && pos.y >= 0 && pos.y < mapSize)
  135. {
  136. MapTile tile = mapData.GetTile(pos.x, pos.y);
  137. if (tile != null)
  138. {
  139. // First three are towns, rest are villages
  140. FeatureType settlementType = i < 3 ? FeatureType.Town : FeatureType.Village;
  141. tile.featureType = settlementType;
  142. // Give settlements names
  143. if (settlementType == FeatureType.Town)
  144. {
  145. tile.name = $"Town_{i + 1}";
  146. }
  147. else
  148. {
  149. tile.name = $"Village_{i + 1}";
  150. }
  151. Debug.Log($"🏘️ Placed {settlementType} at ({pos.x}, {pos.y})");
  152. }
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Place team marker on a town or village
  158. /// </summary>
  159. private void PlaceTeamMarkerOnTown()
  160. {
  161. // Find a suitable town or village
  162. Vector2Int? townPosition = FindTownOrVillage();
  163. if (townPosition.HasValue)
  164. {
  165. PlaceTeamMarkerAt(townPosition.Value);
  166. }
  167. else
  168. {
  169. // Fallback to center if no town found
  170. Debug.LogWarning("⚠️ No town/village found, placing team marker at center");
  171. PlaceTeamMarkerAt(new Vector2Int(mapSize / 2, mapSize / 2));
  172. }
  173. }
  174. /// <summary>
  175. /// Find the first town or village on the map
  176. /// </summary>
  177. private Vector2Int? FindTownOrVillage()
  178. {
  179. for (int x = 0; x < mapSize; x++)
  180. {
  181. for (int y = 0; y < mapSize; y++)
  182. {
  183. MapTile tile = mapData.GetTile(x, y);
  184. if (tile != null && (tile.featureType == FeatureType.Town || tile.featureType == FeatureType.Village))
  185. {
  186. return new Vector2Int(x, y);
  187. }
  188. }
  189. }
  190. return null;
  191. }
  192. /// <summary>
  193. /// Place team marker at specific map coordinates
  194. /// </summary>
  195. private void PlaceTeamMarkerAt(Vector2Int mapPos)
  196. {
  197. // Find existing team marker or create one
  198. GameObject teamMarkerObj = GameObject.Find("TeamMarker");
  199. if (teamMarkerObj == null)
  200. {
  201. // Create a simple team marker if none exists
  202. if (teamMarkerPrefab != null)
  203. {
  204. teamMarkerObj = Instantiate(teamMarkerPrefab);
  205. teamMarkerObj.name = "TeamMarkerSimpleMapMaker2";
  206. Debug.Log("🎯 Created new TeamMarker from prefab");
  207. }
  208. else
  209. {
  210. // Create a simple sphere as team marker
  211. teamMarkerObj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  212. teamMarkerObj.name = "TeamMarkerSimplMapMaker2";
  213. teamMarkerObj.transform.localScale = new Vector3(2f, 2f, 2f);
  214. // Make it green and easily visible
  215. Renderer renderer = teamMarkerObj.GetComponent<Renderer>();
  216. if (renderer != null)
  217. {
  218. renderer.material.color = Color.green;
  219. }
  220. Debug.Log("🎯 Created simple sphere TeamMarker");
  221. }
  222. }
  223. if (teamMarkerObj != null)
  224. {
  225. // Convert map coordinates to world position
  226. Vector3 worldPos = new Vector3(mapPos.x, 1f, mapPos.y); // Y=1 to be above ground
  227. teamMarkerObj.transform.position = worldPos;
  228. teamMarker = teamMarkerObj.transform;
  229. Debug.Log($"📍 Team marker placed at map position ({mapPos.x}, {mapPos.y}) = world position {worldPos}");
  230. // Log what type of settlement we're on
  231. MapTile tile = mapData.GetTile(mapPos.x, mapPos.y);
  232. if (tile != null)
  233. {
  234. Debug.Log($"🏘️ Team marker placed on: Terrain={tile.terrainType}, Feature={tile.featureType}");
  235. if (!string.IsNullOrEmpty(tile.name))
  236. {
  237. Debug.Log($"🏘️ Settlement name: {tile.name}");
  238. }
  239. }
  240. }
  241. else
  242. {
  243. Debug.LogError("❌ Could not create TeamMarker!");
  244. }
  245. }
  246. /// <summary>
  247. /// Context menu helper to regenerate map
  248. /// </summary>
  249. [ContextMenu("Generate New Map")]
  250. public void RegenerateMap()
  251. {
  252. seed = Random.Range(1, 99999);
  253. GenerateSimpleMap();
  254. }
  255. /// <summary>
  256. /// Context menu helper to disable conflicting systems
  257. /// </summary>
  258. [ContextMenu("Setup Simple System (Disable Conflicts)")]
  259. public void SetupSimpleSystem()
  260. {
  261. Debug.Log("🔧 Setting up simple system by disabling conflicting components...");
  262. // Disable SimpleTeamPlacement if present
  263. SimpleTeamPlacement teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  264. if (teamPlacement != null)
  265. {
  266. teamPlacement.enabled = false;
  267. Debug.Log("🚫 Disabled SimpleTeamPlacement component");
  268. }
  269. // Disable old TravelUI if present
  270. TravelUI oldTravelUI = FindFirstObjectByType<TravelUI>();
  271. if (oldTravelUI != null)
  272. {
  273. oldTravelUI.enabled = false;
  274. Debug.Log("🚫 Disabled old TravelUI component");
  275. }
  276. // Disable TeamTravelSystem if present
  277. TeamTravelSystem travelSystem = FindFirstObjectByType<TeamTravelSystem>();
  278. if (travelSystem != null)
  279. {
  280. travelSystem.enabled = false;
  281. Debug.Log("🚫 Disabled TeamTravelSystem component");
  282. }
  283. // Disable ExplorationManager if present (search by component name)
  284. MonoBehaviour[] allComponents = FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None);
  285. foreach (var component in allComponents)
  286. {
  287. if (component.GetType().Name == "ExplorationManager")
  288. {
  289. component.enabled = false;
  290. Debug.Log("🚫 Disabled ExplorationManager component");
  291. break;
  292. }
  293. }
  294. Debug.Log("✅ Simple system setup complete - conflicting systems disabled");
  295. // Regenerate the map to ensure clean state
  296. GenerateSimpleMap();
  297. }
  298. /// <summary>
  299. /// Context menu helper to place team marker on different town
  300. /// </summary>
  301. [ContextMenu("Move Team to Random Town")]
  302. public void MoveTeamToRandomTown()
  303. {
  304. // Find all towns and villages
  305. var settlements = new System.Collections.Generic.List<Vector2Int>();
  306. for (int x = 0; x < mapSize; x++)
  307. {
  308. for (int y = 0; y < mapSize; y++)
  309. {
  310. MapTile tile = mapData.GetTile(x, y);
  311. if (tile != null && (tile.featureType == FeatureType.Town || tile.featureType == FeatureType.Village))
  312. {
  313. settlements.Add(new Vector2Int(x, y));
  314. }
  315. }
  316. }
  317. if (settlements.Count > 0)
  318. {
  319. int randomIndex = Random.Range(0, settlements.Count);
  320. PlaceTeamMarkerAt(settlements[randomIndex]);
  321. }
  322. else
  323. {
  324. Debug.LogWarning("⚠️ No settlements found on map");
  325. }
  326. }
  327. /// <summary>
  328. /// Create simple cube visualization when MapVisualizer is not available
  329. /// </summary>
  330. private void CreateSimpleCubeVisualization()
  331. {
  332. // Create a parent object for all tiles
  333. GameObject mapContainer = GameObject.Find("MapContainer");
  334. if (mapContainer == null)
  335. {
  336. mapContainer = new GameObject("MapContainer");
  337. }
  338. // Clear existing tiles
  339. for (int i = mapContainer.transform.childCount - 1; i >= 0; i--)
  340. {
  341. DestroyImmediate(mapContainer.transform.GetChild(i).gameObject);
  342. }
  343. // Create simple cubes for visualization
  344. for (int x = 0; x < mapSize; x++)
  345. {
  346. for (int y = 0; y < mapSize; y++)
  347. {
  348. MapTile tile = mapData.GetTile(x, y);
  349. if (tile != null)
  350. {
  351. CreateTileVisualization(x, y, tile, mapContainer.transform);
  352. }
  353. }
  354. }
  355. Debug.Log($"✅ Created simple cube visualization with {mapSize * mapSize} tiles");
  356. }
  357. /// <summary>
  358. /// Create a simple cube to represent a tile
  359. /// </summary>
  360. private void CreateTileVisualization(int x, int y, MapTile tile, Transform parent)
  361. {
  362. // Create a cube for the tile
  363. GameObject tileObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  364. tileObj.name = $"Tile_{x}_{y}";
  365. tileObj.transform.parent = parent;
  366. tileObj.transform.position = new Vector3(x, 0, y);
  367. tileObj.transform.localScale = new Vector3(0.9f, 0.1f, 0.9f); // Flat tiles
  368. // Get the renderer to change colors
  369. Renderer renderer = tileObj.GetComponent<Renderer>();
  370. if (renderer != null)
  371. {
  372. // Color based on terrain type
  373. Color terrainColor = GetTerrainColor(tile.terrainType);
  374. renderer.material.color = terrainColor;
  375. }
  376. // Add feature visualization on top if needed
  377. if (tile.featureType != FeatureType.None)
  378. {
  379. CreateFeatureVisualization(x, y, tile, tileObj.transform);
  380. }
  381. }
  382. /// <summary>
  383. /// Create feature visualization (towns, villages, roads)
  384. /// </summary>
  385. private void CreateFeatureVisualization(int x, int y, MapTile tile, Transform tileParent)
  386. {
  387. GameObject featureObj = null;
  388. switch (tile.featureType)
  389. {
  390. case FeatureType.Town:
  391. featureObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  392. featureObj.name = $"Town_{x}_{y}";
  393. featureObj.transform.localScale = new Vector3(0.6f, 0.5f, 0.6f);
  394. featureObj.GetComponent<Renderer>().material.color = Color.blue;
  395. break;
  396. case FeatureType.Village:
  397. featureObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  398. featureObj.name = $"Village_{x}_{y}";
  399. featureObj.transform.localScale = new Vector3(0.4f, 0.3f, 0.4f);
  400. featureObj.GetComponent<Renderer>().material.color = Color.cyan;
  401. break;
  402. case FeatureType.Road:
  403. featureObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  404. featureObj.name = $"Road_{x}_{y}";
  405. featureObj.transform.localScale = new Vector3(0.8f, 0.05f, 0.8f);
  406. featureObj.GetComponent<Renderer>().material.color = Color.yellow;
  407. break;
  408. }
  409. if (featureObj != null)
  410. {
  411. featureObj.transform.parent = tileParent;
  412. featureObj.transform.localPosition = new Vector3(0, 0.6f, 0); // On top of tile
  413. }
  414. }
  415. /// <summary>
  416. /// Get color for terrain type
  417. /// </summary>
  418. private Color GetTerrainColor(TerrainType terrainType)
  419. {
  420. return terrainType switch
  421. {
  422. TerrainType.Plains => Color.green,
  423. TerrainType.Forest => new Color(0f, 0.5f, 0f), // Dark green
  424. TerrainType.Mountain => Color.gray,
  425. TerrainType.Ocean => Color.blue,
  426. TerrainType.Lake => new Color(0f, 0.5f, 1f), // Light blue
  427. TerrainType.River => Color.cyan,
  428. _ => Color.white
  429. };
  430. }
  431. /// <summary>
  432. /// Get the current map data
  433. /// </summary>
  434. public MapData GetMapData()
  435. {
  436. return mapData;
  437. }
  438. /// <summary>
  439. /// Get the team marker transform
  440. /// </summary>
  441. public Transform GetTeamMarker()
  442. {
  443. return teamMarker;
  444. }
  445. }