CombatTravelEvent.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using UnityEngine;
  2. /// <summary>
  3. /// Combat encounter events - ambushes, bandits, wild animals, etc.
  4. /// More likely in forests and mountains, less likely on roads
  5. /// </summary>
  6. [CreateAssetMenu(fileName = "New Combat Event", menuName = "RPG/Travel Events/Combat Event")]
  7. public class CombatTravelEvent : TravelEvent
  8. {
  9. [Header("Combat Event Settings")]
  10. public int minEnemies = 1;
  11. public int maxEnemies = 3;
  12. [Tooltip("Drag EnemyCharacterData assets here")]
  13. public EnemyCharacterData[] possibleEnemies = new EnemyCharacterData[0];
  14. [Header("Escape Configuration")]
  15. public bool allowRunningAway = true;
  16. [Range(0f, 1f)]
  17. public float runAwaySuccessChance = 0.75f;
  18. public int runAwayHealthPenalty = 5;
  19. [Header("Run Away Messages")]
  20. [TextArea(2, 4)]
  21. public string[] successfulRunAwayMessages = {
  22. "Your party successfully escapes from the enemies!",
  23. "Quick thinking allows your party to avoid the confrontation!",
  24. "Your party slips away unnoticed by the enemies."
  25. };
  26. [TextArea(2, 4)]
  27. public string[] failedRunAwayMessages = {
  28. "The enemies catch up to your party! You take {damage} damage while trying to escape.",
  29. "Your escape attempt fails! The enemies pursue and wound your party for {damage} damage.",
  30. "The enemies block your escape route! Your party suffers {damage} damage in the failed attempt."
  31. };
  32. [Header("Combat Event Descriptions")]
  33. [TextArea(2, 4)]
  34. public string[] encounterDescriptions = {
  35. "Your party is ambushed by {enemyType}s!",
  36. "A group of {enemyType}s blocks your path!",
  37. "You stumble upon a {enemyType} camp!"
  38. };
  39. public override EventResult ExecuteEvent(TravelEventContext context)
  40. {
  41. int enemyCount = Random.Range(minEnemies, maxEnemies + 1);
  42. // Ensure we have valid enemies configured
  43. if (possibleEnemies == null || possibleEnemies.Length == 0)
  44. {
  45. Debug.LogError($"No enemies configured for combat event: {eventName}");
  46. return EventResult.NoEvent();
  47. }
  48. // Filter out null entries
  49. var validEnemies = System.Array.FindAll(possibleEnemies, e => e != null);
  50. if (validEnemies.Length == 0)
  51. {
  52. Debug.LogError($"All enemy references are null for combat event: {eventName}");
  53. return EventResult.NoEvent();
  54. }
  55. // Select random enemy
  56. EnemyCharacterData selectedEnemy = validEnemies[Random.Range(0, validEnemies.Length)];
  57. string enemyType = selectedEnemy.enemyName;
  58. // Generate description
  59. string description = encounterDescriptions[Random.Range(0, encounterDescriptions.Length)];
  60. description = description.Replace("{enemyType}", enemyType);
  61. // Try to use combat event integration for popup choice
  62. Debug.Log("🔍 Looking for CombatEventIntegration component...");
  63. // First try to find it by component type
  64. var integrationComponents = FindObjectsByType<MonoBehaviour>(FindObjectsSortMode.None);
  65. var combatIntegration = System.Array.Find(integrationComponents,
  66. comp => comp.GetType().Name == "CombatEventIntegration");
  67. if (combatIntegration == null)
  68. {
  69. // Try the reflection approach as fallback
  70. var type = System.Type.GetType("CombatEventIntegration");
  71. if (type != null)
  72. {
  73. combatIntegration = FindFirstObjectByType(type) as MonoBehaviour;
  74. }
  75. }
  76. if (combatIntegration != null)
  77. {
  78. Debug.Log($"✅ Found CombatEventIntegration: {combatIntegration.name}");
  79. // Create battle data for the popup
  80. var battleData = new BattleEventData
  81. {
  82. enemyCount = enemyCount,
  83. enemyType = enemyType,
  84. enemyCharacterData = selectedEnemy
  85. };
  86. // Use reflection to call ShowCombatChoicePopup with the new signature
  87. var method = combatIntegration.GetType().GetMethod("ShowCombatChoicePopup");
  88. if (method != null)
  89. {
  90. Debug.Log($"🎭 Calling ShowCombatChoicePopup with {enemyCount} {enemyType}(s) and combat event reference");
  91. method.Invoke(combatIntegration, new object[] { battleData, context, description, this });
  92. // Return a special result that indicates popup is handling the choice
  93. return new EventResult("Combat encounter detected...")
  94. {
  95. shouldStopTravel = true, // Stop travel while popup is showing
  96. startBattle = false, // Don't start battle yet - popup will handle it
  97. suppressMessage = true, // Don't show default message - popup handles display
  98. eventOccurred = true
  99. };
  100. }
  101. else
  102. {
  103. Debug.LogError("❌ ShowCombatChoicePopup method not found on CombatEventIntegration");
  104. }
  105. }
  106. else
  107. {
  108. Debug.LogWarning("⚠️ No CombatEventIntegration component found in scene");
  109. }
  110. // Fallback to immediate battle if no integration found
  111. Debug.LogWarning("⚠️ No CombatEventIntegration found, proceeding with immediate battle");
  112. var result = EventResult.SimpleBattle(description, enemyCount, enemyType);
  113. result.battleData.enemyCharacterData = selectedEnemy;
  114. return result;
  115. }
  116. /// <summary>
  117. /// Handle escape attempt for this specific combat event
  118. /// </summary>
  119. public EventResult HandleEscapeAttempt(TravelEventContext context)
  120. {
  121. if (!allowRunningAway)
  122. {
  123. Debug.LogWarning($"🚫 Running away is not allowed for this combat event: {eventName}");
  124. return null; // Force battle
  125. }
  126. Debug.Log($"🏃 Processing escape attempt for {eventName} (success chance: {runAwaySuccessChance:P})");
  127. // Roll for escape success
  128. bool escapeSuccessful = Random.value <= runAwaySuccessChance;
  129. if (escapeSuccessful)
  130. {
  131. return CreateSuccessfulEscapeResult();
  132. }
  133. else
  134. {
  135. return CreateFailedEscapeResult(context);
  136. }
  137. }
  138. /// <summary>
  139. /// Create result for successful escape
  140. /// </summary>
  141. private EventResult CreateSuccessfulEscapeResult()
  142. {
  143. string message = successfulRunAwayMessages[Random.Range(0, successfulRunAwayMessages.Length)];
  144. Debug.Log($"✅ Escape successful: {message}");
  145. return new EventResult(message)
  146. {
  147. shouldStopTravel = false, // Don't stop travel for successful escape
  148. startBattle = false,
  149. eventOccurred = true
  150. };
  151. }
  152. /// <summary>
  153. /// Create result for failed escape
  154. /// </summary>
  155. private EventResult CreateFailedEscapeResult(TravelEventContext context)
  156. {
  157. string message = failedRunAwayMessages[Random.Range(0, failedRunAwayMessages.Length)];
  158. message = message.Replace("{damage}", runAwayHealthPenalty.ToString());
  159. Debug.Log($"❌ Escape failed: {message}");
  160. var result = new EventResult(message)
  161. {
  162. shouldStopTravel = true, // Stop travel for failed escape
  163. startBattle = false, // Don't start battle, just apply penalty
  164. eventOccurred = true,
  165. healthChange = -runAwayHealthPenalty // Apply health penalty through EventResult system
  166. };
  167. return result;
  168. }
  169. void OnEnable()
  170. {
  171. // Set default values for combat events
  172. eventType = EventType.Combat;
  173. rarity = EventRarity.Common;
  174. // Combat events are more likely in dangerous terrain
  175. forestChance = 0.8f;
  176. mountainChance = 0.9f;
  177. plainsChance = 0.4f;
  178. roadChance = 0.2f; // Much less likely on roads
  179. townChance = 0.1f; // Very unlikely in towns
  180. villageChance = 0.3f;
  181. }
  182. }