TravelEventSystemSetup.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using UnityEngine;
  2. /// <summary>
  3. /// Setup script to easily add the Travel Event System to your existing travel setup
  4. /// This component should be added to the same GameObject as TeamTravelSystem
  5. /// </summary>
  6. public class TravelEventSystemSetup : MonoBehaviour
  7. {
  8. [Header("Event System Setup")]
  9. [Tooltip("Automatically create and configure the travel event system")]
  10. public bool autoSetupEventSystem = true;
  11. [Header("Event Configuration")]
  12. [Range(0f, 1f)]
  13. [Tooltip("Base chance for events to occur (15% recommended)")]
  14. public float eventChance = 0.15f;
  15. [Range(1, 10)]
  16. [Tooltip("Check for events every N tiles traveled")]
  17. public int tilesPerCheck = 3;
  18. [Tooltip("Enable debug logging for events")]
  19. public bool enableDebugLogs = false;
  20. [Header("Default Events")]
  21. [Tooltip("Create default ScriptableObject events automatically")]
  22. public bool createDefaultEvents = true;
  23. void Start()
  24. {
  25. if (autoSetupEventSystem)
  26. {
  27. SetupEventSystem();
  28. }
  29. }
  30. [ContextMenu("Setup Event System")]
  31. public void SetupEventSystem()
  32. {
  33. // Check if TravelEventSystem already exists
  34. TravelEventSystem existingSystem = GetComponent<TravelEventSystem>();
  35. if (existingSystem != null)
  36. {
  37. ConfigureExistingSystem(existingSystem);
  38. return;
  39. }
  40. // Add TravelEventSystem component
  41. TravelEventSystem eventSystem = gameObject.AddComponent<TravelEventSystem>();
  42. // Configure the system
  43. eventSystem.enableEvents = true;
  44. eventSystem.baseEventChance = eventChance;
  45. eventSystem.tilesPerEventCheck = tilesPerCheck;
  46. eventSystem.showDebugLogs = enableDebugLogs;
  47. // Verify integration
  48. VerifyIntegration();
  49. }
  50. private void ConfigureExistingSystem(TravelEventSystem eventSystem)
  51. {
  52. eventSystem.baseEventChance = eventChance;
  53. eventSystem.tilesPerEventCheck = tilesPerCheck;
  54. eventSystem.showDebugLogs = enableDebugLogs;
  55. }
  56. private void VerifyIntegration()
  57. {
  58. // Check for required components
  59. TeamTravelSystem travelSystem = GetComponent<TeamTravelSystem>();
  60. if (travelSystem == null)
  61. {
  62. Debug.LogError("❌ TeamTravelSystem not found! TravelEventSystem requires TeamTravelSystem on the same GameObject.");
  63. return;
  64. }
  65. SimpleTeamPlacement teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
  66. if (teamPlacement == null)
  67. {
  68. Debug.LogWarning("⚠️ SimpleTeamPlacement not found in scene. Events may not work properly.");
  69. }
  70. MapMaker2 mapMaker = FindFirstObjectByType<MapMaker2>();
  71. if (mapMaker == null)
  72. {
  73. Debug.LogWarning("⚠️ MapMaker2 not found in scene. Events may not work properly.");
  74. }
  75. TravelEventSystem eventSystem = GetComponent<TravelEventSystem>();
  76. if (eventSystem != null && eventSystem.availableEvents.Count == 0)
  77. {
  78. Debug.LogWarning("⚠️ No events assigned to TravelEventSystem. Create and assign travel events for the system to work.");
  79. }
  80. }
  81. [ContextMenu("Test Event System")]
  82. public void TestEventSystem()
  83. {
  84. TravelEventSystem eventSystem = GetComponent<TravelEventSystem>();
  85. if (eventSystem == null)
  86. {
  87. Debug.LogError("❌ No TravelEventSystem found. Run Setup Event System first.");
  88. return;
  89. }
  90. eventSystem.TriggerEventCheck();
  91. }
  92. void OnValidate()
  93. {
  94. // Clamp values
  95. eventChance = Mathf.Clamp01(eventChance);
  96. tilesPerCheck = Mathf.Clamp(tilesPerCheck, 1, 10);
  97. }
  98. }
  99. /// <summary>
  100. /// Helper script to create example travel events for testing
  101. /// Add this to any GameObject to quickly create test events
  102. /// </summary>
  103. public class TravelEventExampleCreator : MonoBehaviour
  104. {
  105. [ContextMenu("Create Example Events")]
  106. public void CreateExampleEvents()
  107. {
  108. // This demonstrates how to create events at runtime for testing
  109. CreateExampleAmbushEvent();
  110. CreateExampleMerchantEvent();
  111. CreateExampleDiscoveryEvent();
  112. }
  113. private void CreateExampleAmbushEvent()
  114. {
  115. var ambushEvent = ScriptableObject.CreateInstance<CombatTravelEvent>();
  116. ambushEvent.eventName = "Test Forest Ambush";
  117. ambushEvent.eventDescription = "A test ambush event for debugging";
  118. ambushEvent.eventType = EventType.Combat;
  119. ambushEvent.rarity = EventRarity.Common;
  120. // Set terrain preferences
  121. ambushEvent.forestChance = 0.9f;
  122. ambushEvent.roadChance = 0.2f;
  123. ambushEvent.townChance = 0.1f;
  124. // Add to event system if available
  125. TravelEventSystem eventSystem = FindFirstObjectByType<TravelEventSystem>();
  126. if (eventSystem != null)
  127. {
  128. eventSystem.availableEvents.Add(ambushEvent);
  129. }
  130. }
  131. private void CreateExampleMerchantEvent()
  132. {
  133. var merchantEvent = ScriptableObject.CreateInstance<TradingTravelEvent>();
  134. merchantEvent.eventName = "Test Traveling Merchant";
  135. merchantEvent.eventDescription = "A test merchant event for debugging";
  136. merchantEvent.eventType = EventType.Trading;
  137. merchantEvent.rarity = EventRarity.Common;
  138. // Set terrain preferences
  139. merchantEvent.roadChance = 0.8f;
  140. merchantEvent.townChance = 0.7f;
  141. merchantEvent.forestChance = 0.3f;
  142. // Add to event system if available
  143. TravelEventSystem eventSystem = FindFirstObjectByType<TravelEventSystem>();
  144. if (eventSystem != null)
  145. {
  146. eventSystem.availableEvents.Add(merchantEvent);
  147. }
  148. }
  149. private void CreateExampleDiscoveryEvent()
  150. {
  151. var discoveryEvent = ScriptableObject.CreateInstance<DiscoveryTravelEvent>();
  152. discoveryEvent.eventName = "Test Hidden Treasure";
  153. discoveryEvent.eventDescription = "A test discovery event for debugging";
  154. discoveryEvent.eventType = EventType.Discovery;
  155. discoveryEvent.rarity = EventRarity.Uncommon;
  156. // Set terrain preferences
  157. discoveryEvent.forestChance = 0.7f;
  158. discoveryEvent.mountainChance = 0.8f;
  159. discoveryEvent.roadChance = 0.2f;
  160. // Add to event system if available
  161. TravelEventSystem eventSystem = FindFirstObjectByType<TravelEventSystem>();
  162. if (eventSystem != null)
  163. {
  164. eventSystem.availableEvents.Add(discoveryEvent);
  165. }
  166. }
  167. }