using UnityEngine;
///
/// Setup script to easily add the Travel Event System to your existing travel setup
/// This component should be added to the same GameObject as TeamTravelSystem
///
public class TravelEventSystemSetup : MonoBehaviour
{
[Header("Event System Setup")]
[Tooltip("Automatically create and configure the travel event system")]
public bool autoSetupEventSystem = true;
[Header("Event Configuration")]
[Range(0f, 1f)]
[Tooltip("Base chance for events to occur (15% recommended)")]
public float eventChance = 0.15f;
[Range(1, 10)]
[Tooltip("Check for events every N tiles traveled")]
public int tilesPerCheck = 3;
[Tooltip("Enable debug logging for events")]
public bool enableDebugLogs = false;
[Header("Default Events")]
[Tooltip("Create default ScriptableObject events automatically")]
public bool createDefaultEvents = true;
void Start()
{
if (autoSetupEventSystem)
{
SetupEventSystem();
}
}
[ContextMenu("Setup Event System")]
public void SetupEventSystem()
{
// Check if TravelEventSystem already exists
TravelEventSystem existingSystem = GetComponent();
if (existingSystem != null)
{
ConfigureExistingSystem(existingSystem);
return;
}
// Add TravelEventSystem component
TravelEventSystem eventSystem = gameObject.AddComponent();
// Configure the system
eventSystem.enableEvents = true;
eventSystem.baseEventChance = eventChance;
eventSystem.tilesPerEventCheck = tilesPerCheck;
eventSystem.showDebugLogs = enableDebugLogs;
// Verify integration
VerifyIntegration();
}
private void ConfigureExistingSystem(TravelEventSystem eventSystem)
{
eventSystem.baseEventChance = eventChance;
eventSystem.tilesPerEventCheck = tilesPerCheck;
eventSystem.showDebugLogs = enableDebugLogs;
}
private void VerifyIntegration()
{
// Check for required components
TeamTravelSystem travelSystem = GetComponent();
if (travelSystem == null)
{
Debug.LogError("❌ TeamTravelSystem not found! TravelEventSystem requires TeamTravelSystem on the same GameObject.");
return;
}
SimpleTeamPlacement teamPlacement = FindFirstObjectByType();
if (teamPlacement == null)
{
Debug.LogWarning("⚠️ SimpleTeamPlacement not found in scene. Events may not work properly.");
}
MapMaker2 mapMaker = FindFirstObjectByType();
if (mapMaker == null)
{
Debug.LogWarning("⚠️ MapMaker2 not found in scene. Events may not work properly.");
}
TravelEventSystem eventSystem = GetComponent();
if (eventSystem != null && eventSystem.availableEvents.Count == 0)
{
Debug.LogWarning("⚠️ No events assigned to TravelEventSystem. Create and assign travel events for the system to work.");
}
}
[ContextMenu("Test Event System")]
public void TestEventSystem()
{
TravelEventSystem eventSystem = GetComponent();
if (eventSystem == null)
{
Debug.LogError("❌ No TravelEventSystem found. Run Setup Event System first.");
return;
}
eventSystem.TriggerEventCheck();
}
void OnValidate()
{
// Clamp values
eventChance = Mathf.Clamp01(eventChance);
tilesPerCheck = Mathf.Clamp(tilesPerCheck, 1, 10);
}
}
///
/// Helper script to create example travel events for testing
/// Add this to any GameObject to quickly create test events
///
public class TravelEventExampleCreator : MonoBehaviour
{
[ContextMenu("Create Example Events")]
public void CreateExampleEvents()
{
// This demonstrates how to create events at runtime for testing
CreateExampleAmbushEvent();
CreateExampleMerchantEvent();
CreateExampleDiscoveryEvent();
}
private void CreateExampleAmbushEvent()
{
var ambushEvent = ScriptableObject.CreateInstance();
ambushEvent.eventName = "Test Forest Ambush";
ambushEvent.eventDescription = "A test ambush event for debugging";
ambushEvent.eventType = EventType.Combat;
ambushEvent.rarity = EventRarity.Common;
// Set terrain preferences
ambushEvent.forestChance = 0.9f;
ambushEvent.roadChance = 0.2f;
ambushEvent.townChance = 0.1f;
// Add to event system if available
TravelEventSystem eventSystem = FindFirstObjectByType();
if (eventSystem != null)
{
eventSystem.availableEvents.Add(ambushEvent);
}
}
private void CreateExampleMerchantEvent()
{
var merchantEvent = ScriptableObject.CreateInstance();
merchantEvent.eventName = "Test Traveling Merchant";
merchantEvent.eventDescription = "A test merchant event for debugging";
merchantEvent.eventType = EventType.Trading;
merchantEvent.rarity = EventRarity.Common;
// Set terrain preferences
merchantEvent.roadChance = 0.8f;
merchantEvent.townChance = 0.7f;
merchantEvent.forestChance = 0.3f;
// Add to event system if available
TravelEventSystem eventSystem = FindFirstObjectByType();
if (eventSystem != null)
{
eventSystem.availableEvents.Add(merchantEvent);
}
}
private void CreateExampleDiscoveryEvent()
{
var discoveryEvent = ScriptableObject.CreateInstance();
discoveryEvent.eventName = "Test Hidden Treasure";
discoveryEvent.eventDescription = "A test discovery event for debugging";
discoveryEvent.eventType = EventType.Discovery;
discoveryEvent.rarity = EventRarity.Uncommon;
// Set terrain preferences
discoveryEvent.forestChance = 0.7f;
discoveryEvent.mountainChance = 0.8f;
discoveryEvent.roadChance = 0.2f;
// Add to event system if available
TravelEventSystem eventSystem = FindFirstObjectByType();
if (eventSystem != null)
{
eventSystem.availableEvents.Add(discoveryEvent);
}
}
}