TravelSystemSetup.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Setup script to configure the new travel system in the map scene.
  5. /// This script automatically sets up the travel system components and ensures proper integration.
  6. /// </summary>
  7. public class TravelSystemSetup : MonoBehaviour
  8. {
  9. [Header("Setup Options")]
  10. public bool autoSetupOnStart = true;
  11. public bool showDebugLogs = true;
  12. [Header("Travel System Settings")]
  13. [Range(0.1f, 5f)]
  14. public float plainsMovementCost = 1f;
  15. [Range(0.1f, 5f)]
  16. public float forestMovementCost = 2f;
  17. [Range(0.1f, 5f)]
  18. public float mountainMovementCost = 4f;
  19. [Range(0.1f, 2f)]
  20. public float roadMovementCost = 0.5f;
  21. [Range(0.1f, 2f)]
  22. public float townMovementCost = 0.1f;
  23. [Header("Visual Settings")]
  24. public Material pathLineMaterial;
  25. public Color standardPathColor = Color.blue;
  26. public Color expensivePathColor = Color.red;
  27. [Range(0.1f, 1f)]
  28. public float pathLineWidth = 0.2f;
  29. void Start()
  30. {
  31. if (autoSetupOnStart)
  32. {
  33. SetupTravelSystem();
  34. }
  35. }
  36. [ContextMenu("Setup Travel System")]
  37. public void SetupTravelSystem()
  38. {
  39. if (showDebugLogs)
  40. {
  41. Debug.Log("🔧 Setting up Travel System...");
  42. }
  43. // 1. Create TeamTravelSystem if it doesn't exist
  44. SetupTeamTravelSystem();
  45. // 2. Setup TravelUIController if UI exists
  46. SetupTravelUIController();
  47. // 2.5. Setup TravelUI component
  48. SetupTravelUIComponent();
  49. // 3. Verify SimpleTeamPlacement integration
  50. VerifyTeamPlacementIntegration();
  51. if (showDebugLogs)
  52. {
  53. Debug.Log("✅ Travel System setup complete!");
  54. }
  55. }
  56. private void SetupTeamTravelSystem()
  57. {
  58. var existingTravelSystem = FindFirstObjectByType<TeamTravelSystem>();
  59. if (existingTravelSystem == null)
  60. {
  61. // Create new travel system
  62. GameObject travelSystemObj = new GameObject("TeamTravelSystem");
  63. var travelSystem = travelSystemObj.AddComponent<TeamTravelSystem>();
  64. // Apply settings
  65. travelSystem.plainsMovementCost = plainsMovementCost;
  66. travelSystem.forestMovementCost = forestMovementCost;
  67. travelSystem.mountainMovementCost = mountainMovementCost;
  68. travelSystem.roadMovementCost = roadMovementCost;
  69. travelSystem.townMovementCost = townMovementCost;
  70. travelSystem.pathLineMaterial = pathLineMaterial;
  71. travelSystem.standardPathColor = standardPathColor;
  72. travelSystem.expensivePathColor = expensivePathColor;
  73. travelSystem.pathLineWidth = pathLineWidth;
  74. travelSystem.showDebugLogs = showDebugLogs;
  75. if (showDebugLogs)
  76. {
  77. Debug.Log("📦 Created TeamTravelSystem component");
  78. }
  79. }
  80. else
  81. {
  82. if (showDebugLogs)
  83. {
  84. Debug.Log("✅ TeamTravelSystem already exists");
  85. }
  86. }
  87. }
  88. private void SetupTravelUIController()
  89. {
  90. // Look for UI GameObject
  91. GameObject uiGameObject = GameObject.Find("UI");
  92. if (uiGameObject == null)
  93. {
  94. if (showDebugLogs)
  95. {
  96. Debug.Log("⚠️ No UI GameObject found - TravelUIController not setup");
  97. }
  98. return;
  99. }
  100. var existingUIController = uiGameObject.GetComponent<TravelUIController>();
  101. if (existingUIController == null)
  102. {
  103. // Add TravelUIController to UI GameObject
  104. var uiController = uiGameObject.AddComponent<TravelUIController>();
  105. uiController.showDebugLogs = showDebugLogs;
  106. if (showDebugLogs)
  107. {
  108. Debug.Log("📦 Added TravelUIController to UI GameObject");
  109. }
  110. }
  111. else
  112. {
  113. if (showDebugLogs)
  114. {
  115. Debug.Log("✅ TravelUIController already exists");
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Sets up the TravelUI component to handle the travel interface
  121. /// </summary>
  122. private void SetupTravelUIComponent()
  123. {
  124. // Check if TravelUI exists
  125. var travelUI = FindFirstObjectByType<TravelUI>();
  126. if (travelUI == null)
  127. {
  128. // Create TravelUI GameObject
  129. GameObject travelUIGameObject = new GameObject("TravelUI");
  130. var uiDocument = travelUIGameObject.AddComponent<UIDocument>();
  131. var travelUIComponent = travelUIGameObject.AddComponent<TravelUI>();
  132. travelUIComponent.uiDocument = uiDocument;
  133. // Try to load and assign the TravelUI.uxml file
  134. var travelUIAsset = Resources.Load<VisualTreeAsset>("UI/Map/TravelUI");
  135. if (travelUIAsset != null)
  136. {
  137. uiDocument.visualTreeAsset = travelUIAsset;
  138. travelUIComponent.travelUIAsset = travelUIAsset;
  139. if (showDebugLogs)
  140. {
  141. Debug.Log("✅ Assigned TravelUI.uxml to new TravelUI component");
  142. }
  143. }
  144. else
  145. {
  146. Debug.LogWarning("⚠️ Could not find TravelUI.uxml in Resources/UI/Map/");
  147. }
  148. if (showDebugLogs)
  149. {
  150. Debug.Log("📦 Created TravelUI GameObject with component");
  151. }
  152. }
  153. else
  154. {
  155. // Ensure existing TravelUI has proper UXML assigned
  156. var uiDocument = travelUI.uiDocument;
  157. if (uiDocument != null && (uiDocument.visualTreeAsset == null || travelUI.travelUIAsset == null))
  158. {
  159. var travelUIAsset = Resources.Load<VisualTreeAsset>("UI/Map/TravelUI");
  160. if (travelUIAsset != null)
  161. {
  162. uiDocument.visualTreeAsset = travelUIAsset;
  163. travelUI.travelUIAsset = travelUIAsset;
  164. if (showDebugLogs)
  165. {
  166. Debug.Log("✅ Assigned TravelUI.uxml to existing TravelUI component");
  167. }
  168. }
  169. }
  170. if (showDebugLogs)
  171. {
  172. Debug.Log("✅ TravelUI component already exists and is properly configured");
  173. }
  174. }
  175. }
  176. private void VerifyTeamPlacementIntegration()
  177. {
  178. var teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  179. if (teamPlacement == null)
  180. {
  181. if (showDebugLogs)
  182. {
  183. Debug.LogWarning("⚠️ SimpleTeamPlacement not found - travel system may not work properly");
  184. }
  185. return;
  186. }
  187. if (showDebugLogs)
  188. {
  189. Debug.Log("✅ SimpleTeamPlacement found and ready for integration");
  190. }
  191. }
  192. [ContextMenu("Show Travel System Status")]
  193. public void ShowTravelSystemStatus()
  194. {
  195. Debug.Log("=== TRAVEL SYSTEM STATUS ===");
  196. var travelSystem = FindFirstObjectByType<TeamTravelSystem>();
  197. Debug.Log($"TeamTravelSystem: {(travelSystem != null ? "✅ Found" : "❌ Missing")}");
  198. var uiController = FindFirstObjectByType<TravelUIController>();
  199. Debug.Log($"TravelUIController: {(uiController != null ? "✅ Found" : "❌ Missing")}");
  200. var teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  201. Debug.Log($"SimpleTeamPlacement: {(teamPlacement != null ? "✅ Found" : "❌ Missing")}");
  202. var mapMaker = FindFirstObjectByType<MapMaker2>();
  203. Debug.Log($"MapMaker2: {(mapMaker != null ? "✅ Found" : "❌ Missing")}");
  204. Debug.Log("===========================");
  205. }
  206. [ContextMenu("Test Travel Planning")]
  207. public void TestTravelPlanning()
  208. {
  209. var travelSystem = FindFirstObjectByType<TeamTravelSystem>();
  210. var teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  211. if (travelSystem == null || teamPlacement == null)
  212. {
  213. Debug.LogError("❌ Cannot test travel planning - missing components");
  214. return;
  215. }
  216. if (!teamPlacement.IsTeamPlaced())
  217. {
  218. Debug.LogError("❌ Cannot test travel planning - team not placed");
  219. return;
  220. }
  221. // Plan travel to a random nearby position
  222. Vector2Int currentPos = teamPlacement.GetTeamPosition();
  223. Vector2Int testDestination = new Vector2Int(currentPos.x + 10, currentPos.y + 10);
  224. travelSystem.PlanTravelTo(testDestination);
  225. Debug.Log($"🧪 Test travel planning from {currentPos} to {testDestination}");
  226. }
  227. }