EnhancedBattleSetup.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.AI;
  4. /// <summary>
  5. /// Enhanced BattleSetup that works with CombatDataTransfer for richer combat initialization
  6. /// Extends the existing BattleSetup functionality with team data and terrain information
  7. /// </summary>
  8. public class EnhancedBattleSetup : MonoBehaviour
  9. {
  10. [Header("Enhanced Combat Setup")]
  11. [Tooltip("Enable enhanced setup using CombatDataTransfer")]
  12. public bool useEnhancedSetup = true;
  13. [Header("UI Elements")]
  14. [Tooltip("UI Text to display battle context (terrain, weather, etc.)")]
  15. public UnityEngine.UI.Text battleContextText;
  16. [Header("Terrain Setup")]
  17. [Tooltip("Terrain generator for setting up battle environment")]
  18. public BFMTerrainGenerator terrainGenerator;
  19. [Header("Debug")]
  20. public bool showDebugLogs = false;
  21. // Reference to original BattleSetup if needed
  22. private BattleSetup originalBattleSetup;
  23. void Start()
  24. {
  25. // Get reference to original BattleSetup component
  26. originalBattleSetup = GetComponent<BattleSetup>();
  27. if (useEnhancedSetup && CombatDataTransfer.HasValidSession())
  28. {
  29. SetupEnhancedCombat();
  30. }
  31. else
  32. {
  33. if (showDebugLogs)
  34. {
  35. Debug.Log("🔧 Enhanced setup disabled or no combat session - using standard BattleSetup");
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Set up combat using the enhanced CombatDataTransfer system
  41. /// </summary>
  42. private void SetupEnhancedCombat()
  43. {
  44. var session = CombatDataTransfer.GetCurrentSession();
  45. if (session == null)
  46. {
  47. Debug.LogError("❌ No combat session data available!");
  48. return;
  49. }
  50. if (showDebugLogs)
  51. {
  52. Debug.Log("🎯 Setting up enhanced combat...");
  53. }
  54. // Set up terrain
  55. SetupBattleTerrain(session);
  56. // Update UI with battle context
  57. UpdateBattleContextUI(session);
  58. // The original BattleSetup will handle character placement using BattleSetupData
  59. // which was already populated by CombatDataTransfer.PopulateLegacyBattleSetupData()
  60. if (showDebugLogs)
  61. {
  62. Debug.Log("✅ Enhanced combat setup complete");
  63. }
  64. }
  65. /// <summary>
  66. /// Set up the battle terrain based on combat session data
  67. /// </summary>
  68. private void SetupBattleTerrain(CombatDataTransfer.CombatSessionData session)
  69. {
  70. if (terrainGenerator == null)
  71. {
  72. terrainGenerator = FindFirstObjectByType<BFMTerrainGenerator>();
  73. }
  74. if (terrainGenerator != null)
  75. {
  76. BFMTerrainType bfmTerrain = ConvertToBFMTerrainType(session.battleTerrain);
  77. if (showDebugLogs)
  78. {
  79. Debug.Log($"🌍 Setting battle terrain: {bfmTerrain} (from {session.battleTerrain})");
  80. }
  81. terrainGenerator.SetTerrainType(bfmTerrain);
  82. // If there's a regenerate method, call it
  83. // terrainGenerator.RegenerateTerrain(); // Uncomment if this method exists
  84. }
  85. else
  86. {
  87. if (showDebugLogs)
  88. {
  89. Debug.LogWarning("⚠️ No BFMTerrainGenerator found - terrain setup skipped");
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// Update UI elements with battle context information
  95. /// </summary>
  96. private void UpdateBattleContextUI(CombatDataTransfer.CombatSessionData session)
  97. {
  98. if (battleContextText != null)
  99. {
  100. string terrainDesc = CombatDataTransfer.GetTerrainDescription(session.battleTerrain, session.battleFeature);
  101. string timeDesc = GetTimeDescription(session.timeOfDay);
  102. string weatherDesc = session.weather.ToString().ToLower();
  103. string contextDescription = $"Battle {terrainDesc}\n{timeDesc.ToUpper()} - {weatherDesc} weather";
  104. battleContextText.text = contextDescription;
  105. if (showDebugLogs)
  106. {
  107. Debug.Log($"🎨 Updated battle context UI: {contextDescription}");
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Convert TerrainType to BFMTerrainType
  113. /// </summary>
  114. private BFMTerrainType ConvertToBFMTerrainType(TerrainType terrainType)
  115. {
  116. return terrainType switch
  117. {
  118. TerrainType.Plains => BFMTerrainType.Plain,
  119. TerrainType.Forest => BFMTerrainType.Forest,
  120. TerrainType.Mountain => BFMTerrainType.Mountain,
  121. TerrainType.ForestRiver => BFMTerrainType.Forest,
  122. _ => BFMTerrainType.Plain
  123. };
  124. }
  125. /// <summary>
  126. /// Convert time of day to readable description
  127. /// </summary>
  128. private string GetTimeDescription(float timeOfDay)
  129. {
  130. return timeOfDay switch
  131. {
  132. >= 6f and < 12f => "morning",
  133. >= 12f and < 18f => "afternoon",
  134. >= 18f and < 22f => "evening",
  135. _ => "night"
  136. };
  137. }
  138. /// <summary>
  139. /// Get enhanced character stats for a team member
  140. /// </summary>
  141. public static void ApplyEnhancedStats(GameObject characterObject, CombatDataTransfer.TeamCharacterCombatData combatData)
  142. {
  143. if (characterObject == null || combatData == null) return;
  144. var character = characterObject.GetComponent<Character>();
  145. if (character != null)
  146. {
  147. // Apply enhanced stats from combat data
  148. character.CharacterName = combatData.characterName;
  149. // Apply combat stats if the Character component supports them
  150. // character.MaxHealth = combatData.maxHealth; // Uncomment if property exists
  151. // character.CurrentHealth = combatData.currentHealth;
  152. // character.ArmorClass = combatData.armorClass;
  153. Debug.Log($"📊 Applied enhanced stats to {combatData.characterName}: HP={combatData.currentHealth}/{combatData.maxHealth}, AC={combatData.armorClass}");
  154. }
  155. }
  156. /// <summary>
  157. /// Get enhanced enemy stats for an enemy character
  158. /// </summary>
  159. public static void ApplyEnhancedEnemyStats(GameObject enemyObject, CombatDataTransfer.EnemyCombatData enemyData)
  160. {
  161. if (enemyObject == null || enemyData == null) return;
  162. var character = enemyObject.GetComponent<Character>();
  163. if (character != null)
  164. {
  165. character.CharacterName = enemyData.enemyName;
  166. // Apply enhanced enemy stats
  167. // character.MaxHealth = enemyData.maxHealth; // Uncomment if property exists
  168. // character.CurrentHealth = enemyData.currentHealth;
  169. // character.ArmorClass = enemyData.armorClass;
  170. Debug.Log($"👹 Applied enhanced enemy stats to {enemyData.enemyName}: HP={enemyData.currentHealth}/{enemyData.maxHealth}, AC={enemyData.armorClass}, Threat={enemyData.threatLevel}");
  171. }
  172. }
  173. /// <summary>
  174. /// Method to be called when battle ends to clean up
  175. /// </summary>
  176. public void EndBattleSession(bool playerVictory)
  177. {
  178. if (showDebugLogs)
  179. {
  180. Debug.Log($"🏆 Battle ended - Player victory: {playerVictory}");
  181. }
  182. // Find and use CombatSceneManager to handle the end
  183. var combatSceneManager = FindFirstObjectByType<MonoBehaviour>();
  184. if (combatSceneManager != null && combatSceneManager.GetType().Name == "CombatSceneManager")
  185. {
  186. var method = combatSceneManager.GetType().GetMethod("EndCombatSession");
  187. if (method != null)
  188. {
  189. method.Invoke(combatSceneManager, new object[] { playerVictory });
  190. }
  191. }
  192. else
  193. {
  194. // Fallback: just clear the session
  195. CombatDataTransfer.ClearSession();
  196. Debug.Log("🧹 Combat session cleared (no CombatSceneManager found)");
  197. }
  198. }
  199. }