CombatIntegrationSetup.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. /// <summary>
  4. /// Automatic setup script for the Combat Scene Integration system
  5. /// Run this in the Unity Editor to quickly set up the system in your scenes
  6. /// </summary>
  7. public class CombatIntegrationSetup : MonoBehaviour
  8. {
  9. [Header("Setup Configuration")]
  10. [Tooltip("The name of your battle scene")]
  11. public string battleSceneName = "BattleScene";
  12. [Tooltip("Enable debug logging for all components")]
  13. public bool enableDebugLogs = true;
  14. [Header("Setup Actions")]
  15. [Tooltip("Set up the map scene with CombatSceneManager")]
  16. public bool setupMapScene = true;
  17. [Tooltip("Set up the battle scene with EnhancedBattleSetup")]
  18. public bool setupBattleScene = true;
  19. [Header("UI Setup")]
  20. [Tooltip("Create a battle context UI text element")]
  21. public bool createBattleContextUI = true;
  22. [Tooltip("Font size for the battle context text")]
  23. public int contextTextFontSize = 18;
  24. /// <summary>
  25. /// Set up the entire combat integration system
  26. /// </summary>
  27. [ContextMenu("Setup Combat Integration System")]
  28. public void SetupSystem()
  29. {
  30. Debug.Log("🔧 Setting up Combat Integration System...");
  31. string currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  32. if (setupMapScene && IsMapScene(currentScene))
  33. {
  34. SetupMapSceneComponents();
  35. }
  36. if (setupBattleScene && IsBattleScene(currentScene))
  37. {
  38. SetupBattleSceneComponents();
  39. }
  40. Debug.Log("✅ Combat Integration System setup complete!");
  41. Debug.Log("📖 See COMBAT_SCENE_INTEGRATION_GUIDE.md for usage instructions");
  42. }
  43. /// <summary>
  44. /// Set up components needed in the map scene
  45. /// </summary>
  46. [ContextMenu("Setup Map Scene")]
  47. public void SetupMapSceneComponents()
  48. {
  49. Debug.Log("🗺️ Setting up map scene components...");
  50. // 1. Find or create CombatSceneManager
  51. var existingManager = FindFirstObjectByType<CombatSceneManager>();
  52. if (existingManager == null)
  53. {
  54. GameObject managerObj = new GameObject("CombatSceneManager");
  55. var manager = managerObj.AddComponent<CombatSceneManager>();
  56. // Configure the manager
  57. SetManagerProperties(manager);
  58. Debug.Log("✅ Created CombatSceneManager");
  59. }
  60. else
  61. {
  62. SetManagerProperties(existingManager);
  63. Debug.Log("✅ Found and configured existing CombatSceneManager");
  64. }
  65. // 2. Verify CombatEventIntegration exists
  66. var combatIntegration = FindFirstObjectByType<CombatEventIntegration>();
  67. if (combatIntegration == null)
  68. {
  69. Debug.LogWarning("⚠️ No CombatEventIntegration found in scene - please ensure it's set up for combat events to work");
  70. }
  71. else
  72. {
  73. Debug.Log("✅ Found CombatEventIntegration");
  74. }
  75. // 3. Check for terrain generator
  76. var terrainGen = FindFirstObjectByType<BFMTerrainGenerator>();
  77. if (terrainGen == null)
  78. {
  79. Debug.LogWarning("⚠️ No BFMTerrainGenerator found - terrain effects will be limited");
  80. }
  81. else
  82. {
  83. Debug.Log("✅ Found BFMTerrainGenerator");
  84. }
  85. }
  86. /// <summary>
  87. /// Set up components needed in the battle scene
  88. /// </summary>
  89. [ContextMenu("Setup Battle Scene")]
  90. public void SetupBattleSceneComponents()
  91. {
  92. Debug.Log("⚔️ Setting up battle scene components...");
  93. // 1. Find existing BattleSetup
  94. var battleSetup = FindFirstObjectByType<BattleSetup>();
  95. if (battleSetup == null)
  96. {
  97. Debug.LogError("❌ No BattleSetup component found in scene! Please ensure you're in the correct battle scene.");
  98. return;
  99. }
  100. // 2. Add EnhancedBattleSetup if not present
  101. var enhancedSetup = battleSetup.GetComponent<EnhancedBattleSetup>();
  102. if (enhancedSetup == null)
  103. {
  104. enhancedSetup = battleSetup.gameObject.AddComponent<EnhancedBattleSetup>();
  105. Debug.Log("✅ Added EnhancedBattleSetup component");
  106. }
  107. else
  108. {
  109. Debug.Log("✅ Found existing EnhancedBattleSetup component");
  110. }
  111. // Configure enhanced setup
  112. SetEnhancedSetupProperties(enhancedSetup);
  113. // 3. Set up terrain generator
  114. var terrainGen = FindFirstObjectByType<BFMTerrainGenerator>();
  115. if (terrainGen == null)
  116. {
  117. Debug.LogWarning("⚠️ No BFMTerrainGenerator found in battle scene - terrain setup will be skipped");
  118. }
  119. else
  120. {
  121. enhancedSetup.terrainGenerator = terrainGen;
  122. Debug.Log("✅ Connected BFMTerrainGenerator to EnhancedBattleSetup");
  123. }
  124. // 4. Set up battle context UI if requested
  125. if (createBattleContextUI)
  126. {
  127. SetupBattleContextUI(enhancedSetup);
  128. }
  129. }
  130. /// <summary>
  131. /// Configure CombatSceneManager properties
  132. /// </summary>
  133. private void SetManagerProperties(CombatSceneManager manager)
  134. {
  135. if (manager == null) return;
  136. // Use reflection to set properties since we might have compilation issues
  137. var type = manager.GetType();
  138. // Set battle scene name
  139. var battleSceneField = type.GetField("battleSceneName");
  140. if (battleSceneField != null)
  141. {
  142. battleSceneField.SetValue(manager, battleSceneName);
  143. }
  144. // Set debug logs
  145. var debugField = type.GetField("showDebugLogs");
  146. if (debugField != null)
  147. {
  148. debugField.SetValue(manager, enableDebugLogs);
  149. }
  150. Debug.Log($"📝 Configured CombatSceneManager: scene='{battleSceneName}', debug={enableDebugLogs}");
  151. }
  152. /// <summary>
  153. /// Configure EnhancedBattleSetup properties
  154. /// </summary>
  155. private void SetEnhancedSetupProperties(EnhancedBattleSetup enhancedSetup)
  156. {
  157. if (enhancedSetup == null) return;
  158. var type = enhancedSetup.GetType();
  159. // Enable enhanced setup
  160. var useEnhancedField = type.GetField("useEnhancedSetup");
  161. if (useEnhancedField != null)
  162. {
  163. useEnhancedField.SetValue(enhancedSetup, true);
  164. }
  165. // Set debug logs
  166. var debugField = type.GetField("showDebugLogs");
  167. if (debugField != null)
  168. {
  169. debugField.SetValue(enhancedSetup, enableDebugLogs);
  170. }
  171. Debug.Log($"📝 Configured EnhancedBattleSetup: enhanced=true, debug={enableDebugLogs}");
  172. }
  173. /// <summary>
  174. /// Set up battle context UI
  175. /// </summary>
  176. private void SetupBattleContextUI(EnhancedBattleSetup enhancedSetup)
  177. {
  178. // Look for existing Canvas
  179. Canvas canvas = FindFirstObjectByType<Canvas>();
  180. if (canvas == null)
  181. {
  182. Debug.LogWarning("⚠️ No Canvas found - cannot create battle context UI");
  183. return;
  184. }
  185. // Check if battle context text already exists
  186. Transform existingText = canvas.transform.Find("BattleContextText");
  187. if (existingText != null)
  188. {
  189. var existingTextComp = existingText.GetComponent<Text>();
  190. if (existingTextComp != null)
  191. {
  192. AssignBattleContextText(enhancedSetup, existingTextComp);
  193. Debug.Log("✅ Found and assigned existing BattleContextText");
  194. return;
  195. }
  196. }
  197. // Create new battle context UI
  198. GameObject textObj = new GameObject("BattleContextText");
  199. textObj.transform.SetParent(canvas.transform, false);
  200. // Add Text component
  201. Text textComponent = textObj.AddComponent<Text>();
  202. textComponent.text = "Battle Context Will Appear Here";
  203. textComponent.fontSize = contextTextFontSize;
  204. textComponent.color = Color.white;
  205. textComponent.alignment = TextAnchor.UpperCenter;
  206. // Try to assign a font
  207. textComponent.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
  208. // Position at top of screen
  209. RectTransform rectTransform = textComponent.GetComponent<RectTransform>();
  210. rectTransform.anchorMin = new Vector2(0, 1);
  211. rectTransform.anchorMax = new Vector2(1, 1);
  212. rectTransform.pivot = new Vector2(0.5f, 1);
  213. rectTransform.offsetMin = new Vector2(10, -100);
  214. rectTransform.offsetMax = new Vector2(-10, -10);
  215. // Assign to enhanced setup
  216. AssignBattleContextText(enhancedSetup, textComponent);
  217. Debug.Log("✅ Created BattleContextText UI element");
  218. }
  219. /// <summary>
  220. /// Assign battle context text to enhanced setup using reflection
  221. /// </summary>
  222. private void AssignBattleContextText(EnhancedBattleSetup enhancedSetup, Text textComponent)
  223. {
  224. var type = enhancedSetup.GetType();
  225. var textField = type.GetField("battleContextText");
  226. if (textField != null)
  227. {
  228. textField.SetValue(enhancedSetup, textComponent);
  229. Debug.Log("📝 Assigned battle context text to EnhancedBattleSetup");
  230. }
  231. }
  232. /// <summary>
  233. /// Check if current scene is a map scene
  234. /// </summary>
  235. private bool IsMapScene(string sceneName)
  236. {
  237. return sceneName.ToLower().Contains("map") ||
  238. sceneName.ToLower().Contains("travel") ||
  239. sceneName.ToLower().Contains("world");
  240. }
  241. /// <summary>
  242. /// Check if current scene is a battle scene
  243. /// </summary>
  244. private bool IsBattleScene(string sceneName)
  245. {
  246. return sceneName.ToLower().Contains("battle") ||
  247. sceneName.ToLower().Contains("combat") ||
  248. sceneName.ToLower().Contains("fight");
  249. }
  250. /// <summary>
  251. /// Validate the current setup
  252. /// </summary>
  253. [ContextMenu("Validate Setup")]
  254. public void ValidateSetup()
  255. {
  256. Debug.Log("🔍 Validating Combat Integration Setup...");
  257. string currentScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
  258. bool isValid = true;
  259. if (IsMapScene(currentScene))
  260. {
  261. isValid &= ValidateMapScene();
  262. }
  263. else if (IsBattleScene(currentScene))
  264. {
  265. isValid &= ValidateBattleScene();
  266. }
  267. else
  268. {
  269. Debug.LogWarning($"⚠️ Unknown scene type: {currentScene}");
  270. }
  271. if (isValid)
  272. {
  273. Debug.Log("✅ Setup validation passed!");
  274. }
  275. else
  276. {
  277. Debug.LogWarning("⚠️ Setup validation found issues - see messages above");
  278. }
  279. }
  280. /// <summary>
  281. /// Validate map scene setup
  282. /// </summary>
  283. private bool ValidateMapScene()
  284. {
  285. bool valid = true;
  286. // Check CombatSceneManager
  287. var manager = FindFirstObjectByType<CombatSceneManager>();
  288. if (manager == null)
  289. {
  290. Debug.LogError("❌ CombatSceneManager not found in map scene");
  291. valid = false;
  292. }
  293. else
  294. {
  295. Debug.Log("✅ CombatSceneManager found");
  296. }
  297. // Check CombatEventIntegration
  298. var integration = FindFirstObjectByType<CombatEventIntegration>();
  299. if (integration == null)
  300. {
  301. Debug.LogWarning("⚠️ CombatEventIntegration not found - combat events may not work");
  302. }
  303. else
  304. {
  305. Debug.Log("✅ CombatEventIntegration found");
  306. }
  307. return valid;
  308. }
  309. /// <summary>
  310. /// Validate battle scene setup
  311. /// </summary>
  312. private bool ValidateBattleScene()
  313. {
  314. bool valid = true;
  315. // Check BattleSetup
  316. var battleSetup = FindFirstObjectByType<BattleSetup>();
  317. if (battleSetup == null)
  318. {
  319. Debug.LogError("❌ BattleSetup not found in battle scene");
  320. valid = false;
  321. }
  322. else
  323. {
  324. Debug.Log("✅ BattleSetup found");
  325. // Check EnhancedBattleSetup
  326. var enhancedSetup = battleSetup.GetComponent<EnhancedBattleSetup>();
  327. if (enhancedSetup == null)
  328. {
  329. Debug.LogWarning("⚠️ EnhancedBattleSetup not found - enhanced features will not work");
  330. }
  331. else
  332. {
  333. Debug.Log("✅ EnhancedBattleSetup found");
  334. }
  335. }
  336. return valid;
  337. }
  338. /// <summary>
  339. /// Clean up this setup script (remove it after setup is complete)
  340. /// </summary>
  341. [ContextMenu("Remove Setup Script")]
  342. public void RemoveSetupScript()
  343. {
  344. Debug.Log("🧹 Removing CombatIntegrationSetup script...");
  345. DestroyImmediate(this);
  346. }
  347. }