CombatEventIntegration.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using UnityEngine;
  2. /// <summary>
  3. /// Integration manager that connects TravelEventSystem with CombatEventPopup
  4. /// Handles the choice between battle and escape when combat events occur
  5. /// </summary>
  6. public class CombatEventIntegration : MonoBehaviour
  7. {
  8. [Header("Combat Popup Integration")]
  9. [Tooltip("Use either the original CombatEventPopup or the new CombatEventPopupUXML")]
  10. public CombatEventPopup combatEventPopup; // Original popup (legacy)
  11. public CombatEventPopupUXML combatEventPopupUXML; // New UXML-based popup
  12. public GameObject combatPopupPrefab; // Optional: if popup component not already in scene
  13. // Component references
  14. private TravelEventSystem travelEventSystem;
  15. private TravelEventUI eventUI;
  16. private TeamTravelSystem teamTravelSystem;
  17. // Current combat state
  18. private bool isHandlingCombat = false;
  19. private CombatTravelEvent currentCombatEvent; // Store reference to the current combat event
  20. void Start()
  21. {
  22. Debug.Log("🔧 Combat Event Integration starting...");
  23. // Find required components - try same GameObject first, then search scene
  24. travelEventSystem = GetComponent<TravelEventSystem>();
  25. if (travelEventSystem == null)
  26. travelEventSystem = FindFirstObjectByType<TravelEventSystem>();
  27. eventUI = FindFirstObjectByType<TravelEventUI>();
  28. teamTravelSystem = FindFirstObjectByType<TeamTravelSystem>();
  29. if (travelEventSystem == null)
  30. {
  31. Debug.LogError("❌ Combat Event Integration requires TravelEventSystem in the scene!");
  32. Debug.LogError(" Please ensure you have a GameObject with TravelEventSystem component.");
  33. enabled = false;
  34. return;
  35. }
  36. else if (GetComponent<TravelEventSystem>() == null)
  37. {
  38. Debug.LogWarning("⚠️ CombatEventIntegration is not on the same GameObject as TravelEventSystem.");
  39. Debug.LogWarning(" Consider moving it to the TravelEventSystem GameObject for better organization.");
  40. }
  41. // Try to find existing popup components first
  42. if (combatEventPopup == null)
  43. combatEventPopup = FindFirstObjectByType<CombatEventPopup>();
  44. if (combatEventPopupUXML == null)
  45. combatEventPopupUXML = FindFirstObjectByType<CombatEventPopupUXML>();
  46. // If no popup component found and we have a prefab, instantiate it
  47. if (combatEventPopup == null && combatEventPopupUXML == null && combatPopupPrefab != null)
  48. {
  49. Debug.Log("📦 Instantiating combat popup from prefab...");
  50. GameObject popupInstance = Instantiate(combatPopupPrefab);
  51. combatEventPopup = popupInstance.GetComponent<CombatEventPopup>();
  52. combatEventPopupUXML = popupInstance.GetComponent<CombatEventPopupUXML>();
  53. if (combatEventPopup == null && combatEventPopupUXML == null)
  54. {
  55. Debug.LogError("❌ Combat popup prefab does not contain CombatEventPopup or CombatEventPopupUXML component!");
  56. enabled = false;
  57. return;
  58. }
  59. }
  60. if (combatEventPopup == null && combatEventPopupUXML == null)
  61. {
  62. Debug.LogError("❌ Combat Event Integration requires a CombatEventPopup or CombatEventPopupUXML component!");
  63. Debug.LogError(" Either add one of these components to a GameObject in the scene,");
  64. Debug.LogError(" or assign a prefab with one of these components to combatPopupPrefab field.");
  65. enabled = false;
  66. return;
  67. }
  68. // Setup combat popup callbacks
  69. if (combatEventPopup != null)
  70. combatEventPopup.OnCombatDecision += HandleCombatDecision;
  71. if (combatEventPopupUXML != null)
  72. combatEventPopupUXML.OnCombatDecision += HandleCombatDecision;
  73. Debug.Log("✅ Combat Event Integration initialized successfully");
  74. }
  75. void OnDestroy()
  76. {
  77. if (combatEventPopup != null)
  78. combatEventPopup.OnCombatDecision -= HandleCombatDecision;
  79. if (combatEventPopupUXML != null)
  80. combatEventPopupUXML.OnCombatDecision -= HandleCombatDecision;
  81. }
  82. /// <summary>
  83. /// Call this method when a combat event occurs to show the popup
  84. /// This should be called from TravelEventTypes.cs in the ExecuteEvent method
  85. /// </summary>
  86. public void ShowCombatChoicePopup(BattleEventData battleData, TravelEventContext context, string eventDescription, CombatTravelEvent combatEvent = null)
  87. {
  88. if (isHandlingCombat || battleData == null)
  89. return;
  90. Debug.Log($"🎭 Showing combat choice popup: {eventDescription}");
  91. isHandlingCombat = true;
  92. currentCombatEvent = combatEvent; // Store reference to the original combat event
  93. // Store battle data for later use
  94. currentBattleData = battleData;
  95. currentContext = context;
  96. // Show combat popup - try UXML version first, then fallback to original
  97. if (combatEventPopupUXML != null)
  98. {
  99. Debug.Log("Using UXML-based combat popup");
  100. combatEventPopupUXML.OnCombatDecision = HandleCombatDecision;
  101. combatEventPopupUXML.ShowCombatEncounter(battleData, context, eventDescription);
  102. }
  103. else if (combatEventPopup != null)
  104. {
  105. Debug.Log("Using original combat popup");
  106. combatEventPopup.OnCombatDecision = HandleCombatDecision;
  107. combatEventPopup.ShowCombatEncounter(battleData, context, eventDescription);
  108. }
  109. else
  110. {
  111. Debug.LogError("No combat popup component assigned! Please assign either CombatEventPopup or CombatEventPopupUXML.");
  112. isHandlingCombat = false;
  113. }
  114. }
  115. // Store current battle data
  116. private BattleEventData currentBattleData;
  117. private TravelEventContext currentContext;
  118. /// <summary>
  119. /// Handle the player's combat decision from the popup
  120. /// </summary>
  121. private void HandleCombatDecision(bool chooseToAttack)
  122. {
  123. if (!isHandlingCombat || currentBattleData == null)
  124. return;
  125. isHandlingCombat = false;
  126. if (chooseToAttack)
  127. {
  128. HandleAttackChoice();
  129. }
  130. else
  131. {
  132. HandleRunAwayChoice();
  133. }
  134. // Clear stored data
  135. currentBattleData = null;
  136. currentContext = null;
  137. }
  138. /// <summary>
  139. /// Handle when player chooses to attack
  140. /// </summary>
  141. private void HandleAttackChoice()
  142. {
  143. Debug.Log("⚔️ Player chose to fight! Setting up battle...");
  144. // Create and return the battle result that will start the battle
  145. var battleResult = EventResult.SimpleBattle(
  146. $"Your party prepares for battle against {currentBattleData.enemyCount} {currentBattleData.enemyType}!",
  147. currentBattleData.enemyCount,
  148. currentBattleData.enemyType
  149. );
  150. // Copy over the enemy character data
  151. battleResult.battleData.enemyCharacterData = currentBattleData.enemyCharacterData;
  152. // Apply the battle result through the travel event system
  153. ApplyBattleResult(battleResult);
  154. }
  155. /// <summary>
  156. /// Handle when player chooses to run away
  157. /// </summary>
  158. private void HandleRunAwayChoice()
  159. {
  160. Debug.Log("🏃 Player chose to run away!");
  161. EventResult escapeResult = null;
  162. // Use the combat event's escape handling if available
  163. if (currentCombatEvent != null)
  164. {
  165. Debug.Log($"🎯 Using escape configuration from {currentCombatEvent.eventName}");
  166. escapeResult = currentCombatEvent.HandleEscapeAttempt(currentContext);
  167. }
  168. else
  169. {
  170. Debug.LogWarning("⚠️ No combat event reference - using hardcoded fallback escape handling");
  171. // Hardcoded fallback values when no combat event is available
  172. const float fallbackSuccessChance = 0.75f;
  173. bool escapeSuccessful = Random.value <= fallbackSuccessChance;
  174. if (escapeSuccessful)
  175. {
  176. escapeResult = CreateFallbackSuccessfulEscape();
  177. }
  178. else
  179. {
  180. escapeResult = CreateFallbackFailedEscape();
  181. }
  182. }
  183. // Apply the escape result
  184. if (escapeResult != null)
  185. {
  186. ApplyBattleResult(escapeResult);
  187. }
  188. else
  189. {
  190. Debug.LogWarning("⚠️ Escape not allowed by combat event - forcing battle");
  191. HandleAttackChoice();
  192. }
  193. }
  194. /// <summary>
  195. /// Create fallback successful escape result when no combat event is available
  196. /// </summary>
  197. private EventResult CreateFallbackSuccessfulEscape()
  198. {
  199. string message = "Your party successfully escapes from the enemies!";
  200. Debug.Log($"✅ Fallback escape successful: {message}");
  201. return new EventResult(message)
  202. {
  203. shouldStopTravel = false,
  204. startBattle = false,
  205. eventOccurred = true,
  206. healthChange = 0
  207. };
  208. }
  209. /// <summary>
  210. /// Create fallback failed escape result when no combat event is available
  211. /// </summary>
  212. private EventResult CreateFallbackFailedEscape()
  213. {
  214. const int fallbackHealthPenalty = 5; // Hardcoded fallback value
  215. string message = $"The enemies catch up to your party! You take {fallbackHealthPenalty} damage while trying to escape.";
  216. Debug.Log($"❌ Fallback escape failed: {message}");
  217. return new EventResult(message)
  218. {
  219. shouldStopTravel = true,
  220. startBattle = false,
  221. eventOccurred = true,
  222. healthChange = -fallbackHealthPenalty
  223. };
  224. } /// <summary>
  225. /// Apply battle result through the travel event system
  226. /// </summary>
  227. private void ApplyBattleResult(EventResult result)
  228. {
  229. // Apply resource changes
  230. if (result.healthChange != 0)
  231. {
  232. Debug.Log($"❤️ Health changed by {result.healthChange}");
  233. // TODO: Actually apply health changes to party
  234. }
  235. if (result.goldChange != 0)
  236. {
  237. Debug.Log($"💰 Gold changed by {result.goldChange}");
  238. // TODO: Actually apply gold changes to party
  239. }
  240. // Handle travel stopping
  241. if (result.shouldStopTravel && teamTravelSystem != null)
  242. {
  243. teamTravelSystem.CancelJourney();
  244. Debug.Log("🛑 Travel stopped due to event");
  245. }
  246. // Handle battle start
  247. if (result.startBattle)
  248. {
  249. Debug.Log($"⚔️ Starting battle: {result.battleData.enemyCount} {result.battleData.enemyType}(s)");
  250. // TODO: Integrate with your battle system
  251. // This is where you would transition to the battle scene
  252. // or set up the battle data for your existing battle system
  253. if (result.battleData.enemyCharacterData != null)
  254. {
  255. Debug.Log($"🎯 Using enemy data: {result.battleData.enemyCharacterData.enemyName}");
  256. Debug.Log($"📊 Enemy stats: HP={result.battleData.enemyCharacterData.maxHealth}, " +
  257. $"AC={result.battleData.enemyCharacterData.armorClass}, " +
  258. $"Threat={result.battleData.enemyCharacterData.threatLevel}");
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Check if the system is currently handling a combat choice
  264. /// </summary>
  265. public bool IsHandlingCombat => isHandlingCombat;
  266. /// <summary>
  267. /// Cancel current combat handling (for cleanup)
  268. /// </summary>
  269. public void CancelCombatHandling()
  270. {
  271. if (isHandlingCombat)
  272. {
  273. isHandlingCombat = false;
  274. currentBattleData = null;
  275. currentContext = null;
  276. if (combatEventPopup != null)
  277. combatEventPopup.ForceClose();
  278. }
  279. }
  280. }