| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// Enhanced Travel Event System that provides player choice for combat encounters
- /// Integrates CombatEventPopup for better user experience
- /// </summary>
- public class EnhancedTravelEventSystem : MonoBehaviour
- {
- [Header("Enhanced Event System")]
- public CombatEventPopup combatEventPopup;
- public bool allowRunningAway = true;
- [Range(0f, 1f)]
- public float runAwaySuccessChance = 0.75f;
- public int runAwayHealthPenalty = 5;
- [Header("Run Away Messages")]
- [TextArea(2, 4)]
- public string[] successfulRunAwayMessages = {
- "Your party successfully escapes from the enemies!",
- "Quick thinking allows your party to avoid the confrontation!",
- "Your party slips away unnoticed by the enemies."
- };
- [TextArea(2, 4)]
- public string[] failedRunAwayMessages = {
- "The enemies catch up to your party! You take {damage} damage while trying to escape.",
- "Your escape attempt fails! The enemies pursue and wound your party for {damage} damage.",
- "The enemies block your escape route! Your party suffers {damage} damage in the failed attempt."
- };
- // Component references
- private TravelEventSystem originalEventSystem;
- private TravelEventUI eventUI;
- // Current combat state
- private BattleEventData pendingBattleData;
- private TravelEventContext pendingContext;
- private bool waitingForCombatDecision = false;
- void Start()
- {
- // Find required components
- originalEventSystem = FindFirstObjectByType<TravelEventSystem>();
- eventUI = FindFirstObjectByType<TravelEventUI>();
- if (combatEventPopup == null)
- combatEventPopup = FindFirstObjectByType<CombatEventPopup>();
- if (originalEventSystem == null)
- {
- Debug.LogError("❌ Enhanced Travel Event System requires a TravelEventSystem component!");
- enabled = false;
- return;
- }
- if (combatEventPopup == null)
- {
- Debug.LogError("❌ Enhanced Travel Event System requires a CombatEventPopup component!");
- enabled = false;
- return;
- }
- // Setup combat popup callback
- combatEventPopup.OnCombatDecision += HandleCombatDecision;
- }
- void OnDestroy()
- {
- if (combatEventPopup != null)
- combatEventPopup.OnCombatDecision -= HandleCombatDecision;
- }
- /// <summary>
- /// Enhanced combat event handler that shows popup before battle
- /// Call this instead of the original HandleBattleEvent
- /// </summary>
- public void HandleEnhancedBattleEvent(BattleEventData battleData, TravelEventContext context, string eventDescription)
- {
- if (battleData == null || waitingForCombatDecision)
- return;
- // Store pending battle data
- pendingBattleData = battleData;
- pendingContext = context;
- waitingForCombatDecision = true;
- // Show combat popup
- combatEventPopup.ShowCombatEncounter(battleData, context, eventDescription);
- }
- /// <summary>
- /// Handle the player's combat decision from the popup
- /// </summary>
- private void HandleCombatDecision(bool chooseToAttack)
- {
- if (!waitingForCombatDecision || pendingBattleData == null)
- return;
- waitingForCombatDecision = false;
- if (chooseToAttack)
- {
- HandleAttackChoice();
- }
- else
- {
- HandleRunAwayChoice();
- }
- // Clear pending data
- pendingBattleData = null;
- pendingContext = null;
- }
- /// <summary>
- /// Handle when player chooses to attack
- /// </summary>
- private void HandleAttackChoice()
- {
- // Call the original battle setup logic
- if (originalEventSystem != null)
- {
- // Use reflection to call the private HandleBattleEvent method
- var method = originalEventSystem.GetType().GetMethod("HandleBattleEvent",
- System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- if (method != null)
- {
- method.Invoke(originalEventSystem, new object[] { pendingBattleData, pendingContext });
- }
- else
- {
- Debug.LogError("❌ Could not find HandleBattleEvent method in TravelEventSystem");
- // Fallback: just log the battle setup
- LogBattleSetup(pendingBattleData);
- }
- }
- }
- /// <summary>
- /// Handle when player chooses to run away
- /// </summary>
- private void HandleRunAwayChoice()
- {
- if (!allowRunningAway)
- {
- HandleAttackChoice();
- return;
- }
- bool escapeSuccessful = Random.value <= runAwaySuccessChance;
- if (escapeSuccessful)
- {
- HandleSuccessfulEscape();
- }
- else
- {
- HandleFailedEscape();
- }
- }
- /// <summary>
- /// Handle successful escape attempt
- /// </summary>
- private void HandleSuccessfulEscape()
- {
- string message = successfulRunAwayMessages[Random.Range(0, successfulRunAwayMessages.Length)];
- // Show escape message
- if (eventUI != null)
- {
- eventUI.ShowEventMessage(message, EventType.Discovery, false);
- }
- // Continue travel (no resource penalties for successful escape)
- ResumeTravelAfterEvent();
- }
- /// <summary>
- /// Handle failed escape attempt
- /// </summary>
- private void HandleFailedEscape()
- {
- string message = failedRunAwayMessages[Random.Range(0, failedRunAwayMessages.Length)];
- message = message.Replace("{damage}", runAwayHealthPenalty.ToString());
- // Apply health penalty
- ApplyRunAwayPenalty();
- // Show failure message
- if (eventUI != null)
- {
- eventUI.ShowEventMessage(message, EventType.Hazard, false);
- }
- // Start combat anyway
- StartCoroutine(DelayedCombatStart());
- }
- /// <summary>
- /// Apply penalties for failed escape attempt
- /// </summary>
- private void ApplyRunAwayPenalty()
- {
- // This would integrate with your character health system
- Debug.Log($"❤️ Party takes {runAwayHealthPenalty} damage from failed escape attempt");
- // TODO: Actually apply health damage to party members
- // Example: TeamHealthManager.ApplyDamageToParty(runAwayHealthPenalty);
- }
- /// <summary>
- /// Resume travel after successful escape
- /// </summary>
- private void ResumeTravelAfterEvent()
- {
- // The travel system should continue normally
- Debug.Log("🚗 Resuming travel after event...");
- // No need to stop travel for successful escapes
- }
- /// <summary>
- /// Start combat after a delay (for failed escape)
- /// </summary>
- private IEnumerator DelayedCombatStart()
- {
- yield return new WaitForSeconds(3f); // Wait for escape message to be read
- Debug.Log("⚔️ Starting combat after failed escape...");
- HandleAttackChoice();
- }
- /// <summary>
- /// Fallback battle setup logging
- /// </summary>
- private void LogBattleSetup(BattleEventData battleData)
- {
- Debug.Log($"⚔️ Setting up battle: {battleData.enemyCount} {battleData.enemyType}(s)");
- if (battleData.enemyCharacterData != null)
- {
- Debug.Log($"🎯 Using enemy data: {battleData.enemyCharacterData.enemyName}");
- Debug.Log($"📊 Enemy stats: HP={battleData.enemyCharacterData.maxHealth}, " +
- $"AC={battleData.enemyCharacterData.armorClass}, " +
- $"Threat={battleData.enemyCharacterData.threatLevel}");
- }
- // TODO: Actually set up battle scene transition
- Debug.Log("🎬 Transitioning to battle scene...");
- }
- /// <summary>
- /// Check if the system is currently waiting for player input
- /// </summary>
- public bool IsWaitingForCombatDecision => waitingForCombatDecision;
- /// <summary>
- /// Cancel pending combat (for cleanup)
- /// </summary>
- public void CancelPendingCombat()
- {
- if (waitingForCombatDecision)
- {
- waitingForCombatDecision = false;
- pendingBattleData = null;
- pendingContext = null;
- if (combatEventPopup != null)
- combatEventPopup.ForceClose();
- }
- }
- }
|