TravelEventTypes.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. }
  183. /// <summary>
  184. /// Trading encounter events - merchants, traders, caravans
  185. /// More likely on roads and near settlements
  186. /// </summary>
  187. [CreateAssetMenu(fileName = "New Trading Event", menuName = "RPG/Travel Events/Trading Event")]
  188. public class TradingTravelEvent : TravelEvent
  189. {
  190. [Header("Trading Event Settings")]
  191. public string[] merchantTypes = { "Traveling Merchant", "Caravan Trader", "Wandering Peddler" };
  192. public bool canHaveRareItems = true;
  193. [Range(0f, 1f)]
  194. public float rareItemChance = 0.3f;
  195. [Range(0.5f, 1.5f)]
  196. public float priceModifier = 1f;
  197. [Header("Trading Event Descriptions")]
  198. [TextArea(2, 4)]
  199. public string[] encounterDescriptions = {
  200. "You encounter a {merchantType} on the road.",
  201. "A {merchantType} approaches your party with goods to sell.",
  202. "You come across a {merchantType} resting by the roadside."
  203. };
  204. public override EventResult ExecuteEvent(TravelEventContext context)
  205. {
  206. string merchantType = merchantTypes[Random.Range(0, merchantTypes.Length)];
  207. string description = encounterDescriptions[Random.Range(0, encounterDescriptions.Length)];
  208. description = description.Replace("{merchantType}", merchantType);
  209. var result = EventResult.SimpleTrading(description, merchantType);
  210. result.tradingData.hasRareItems = canHaveRareItems && Random.value < rareItemChance;
  211. result.tradingData.priceModifier = priceModifier;
  212. return result;
  213. }
  214. void OnEnable()
  215. {
  216. // Set default values for trading events
  217. eventType = EventType.Trading;
  218. rarity = EventRarity.Common;
  219. // Trading events are more likely on roads and near settlements
  220. roadChance = 0.9f;
  221. townChance = 0.7f;
  222. villageChance = 0.8f;
  223. bridgeChance = 0.6f; // Common crossing points
  224. ferryChance = 0.5f;
  225. // Less likely in wilderness
  226. forestChance = 0.3f;
  227. mountainChance = 0.2f;
  228. plainsChance = 0.5f;
  229. }
  230. }
  231. /// <summary>
  232. /// Discovery events - finding treasures, resources, or hidden locations
  233. /// </summary>
  234. [CreateAssetMenu(fileName = "New Discovery Event", menuName = "RPG/Travel Events/Discovery Event")]
  235. public class DiscoveryTravelEvent : TravelEvent
  236. {
  237. [Header("Discovery Settings")]
  238. public int minGoldFound = 10;
  239. public int maxGoldFound = 100;
  240. public bool canFindItems = true;
  241. [Header("Discovery Descriptions")]
  242. [TextArea(2, 4)]
  243. public string[] discoveryDescriptions = {
  244. "Your party discovers a hidden cache containing {amount} gold!",
  245. "While exploring, you find an abandoned stash with {amount} gold pieces.",
  246. "A keen-eyed party member spots something valuable - {amount} gold!"
  247. };
  248. public override EventResult ExecuteEvent(TravelEventContext context)
  249. {
  250. int goldFound = Random.Range(minGoldFound, maxGoldFound + 1);
  251. string description = discoveryDescriptions[Random.Range(0, discoveryDescriptions.Length)];
  252. description = description.Replace("{amount}", goldFound.ToString());
  253. return new EventResult(description)
  254. {
  255. goldChange = goldFound
  256. };
  257. }
  258. void OnEnable()
  259. {
  260. eventType = EventType.Discovery;
  261. rarity = EventRarity.Uncommon;
  262. // More likely in remote areas
  263. forestChance = 0.7f;
  264. mountainChance = 0.8f;
  265. plainsChance = 0.4f;
  266. roadChance = 0.3f;
  267. townChance = 0.1f;
  268. }
  269. }
  270. /// <summary>
  271. /// Rest events - safe camping spots, inns, healing springs
  272. /// </summary>
  273. [CreateAssetMenu(fileName = "New Rest Event", menuName = "RPG/Travel Events/Rest Event")]
  274. public class RestTravelEvent : TravelEvent
  275. {
  276. [Header("Rest Settings")]
  277. public int healthRestored = 25;
  278. public bool restoresAllHealth = false;
  279. [Header("Rest Descriptions")]
  280. [TextArea(2, 4)]
  281. public string[] restDescriptions = {
  282. "Your party finds a peaceful grove perfect for resting and recovers {health} health.",
  283. "A natural spring provides refreshing water, restoring {health} health to your party.",
  284. "Your party discovers a safe camping spot and takes time to tend wounds, recovering {health} health."
  285. };
  286. public override EventResult ExecuteEvent(TravelEventContext context)
  287. {
  288. int actualHealing = restoresAllHealth ? 100 : healthRestored;
  289. string description = restDescriptions[Random.Range(0, restDescriptions.Length)];
  290. description = description.Replace("{health}", actualHealing.ToString());
  291. return new EventResult(description)
  292. {
  293. healthChange = actualHealing
  294. };
  295. }
  296. void OnEnable()
  297. {
  298. eventType = EventType.Rest;
  299. rarity = EventRarity.Common;
  300. // More likely in natural areas
  301. forestChance = 0.8f;
  302. riverChance = 0.9f;
  303. lakeChance = 0.9f;
  304. plainsChance = 0.6f;
  305. mountainChance = 0.5f;
  306. roadChance = 0.4f;
  307. townChance = 0.2f;
  308. }
  309. }
  310. /// <summary>
  311. /// Hazard events - natural dangers, traps, environmental challenges
  312. /// </summary>
  313. [CreateAssetMenu(fileName = "New Hazard Event", menuName = "RPG/Travel Events/Hazard Event")]
  314. public class HazardTravelEvent : TravelEvent
  315. {
  316. [Header("Hazard Settings")]
  317. public int minHealthLoss = 5;
  318. public int maxHealthLoss = 20;
  319. public int goldCost = 0; // Cost to avoid the hazard
  320. [Header("Hazard Descriptions")]
  321. [TextArea(2, 4)]
  322. public string[] hazardDescriptions = {
  323. "Your party gets caught in a sudden rockslide, losing {damage} health.",
  324. "Treacherous terrain causes injuries to your party members, resulting in {damage} health loss.",
  325. "A hidden pit trap catches one of your party members, causing {damage} damage."
  326. };
  327. public override EventResult ExecuteEvent(TravelEventContext context)
  328. {
  329. int damage = Random.Range(minHealthLoss, maxHealthLoss + 1);
  330. string description = hazardDescriptions[Random.Range(0, hazardDescriptions.Length)];
  331. description = description.Replace("{damage}", damage.ToString());
  332. return new EventResult(description)
  333. {
  334. healthChange = -damage,
  335. goldChange = -goldCost
  336. };
  337. }
  338. void OnEnable()
  339. {
  340. eventType = EventType.Hazard;
  341. rarity = EventRarity.Common;
  342. // More likely in dangerous terrain
  343. mountainChance = 0.9f;
  344. forestChance = 0.6f;
  345. riverChance = 0.7f;
  346. oceanChance = 0.8f;
  347. // Less likely in safe areas
  348. roadChance = 0.2f;
  349. townChance = 0.1f;
  350. villageChance = 0.2f;
  351. plainsChance = 0.4f;
  352. }
  353. }