using UnityEngine; using UnityEngine.UI; using TMPro; using System.Collections.Generic; public class BattleUI : MonoBehaviour { [Header("UI References")] public GameObject strategyPanel; public TMP_InputField strategyInputField; public Button startBattleButton; public Button resetBattleButton; [Header("Strategy Buttons")] public Toggle holdPositionToggle; public Toggle flankToggle; public Toggle chargeToggle; public Toggle spreadWideToggle; public Toggle waitForCloseToggle; [Header("Unit Type Strategy")] public TMP_Dropdown infantryStrategyDropdown; public TMP_Dropdown cavalryStrategyDropdown; public TMP_Dropdown archerStrategyDropdown; [Header("Battle Info")] public TextMeshProUGUI battleStatusText; public TextMeshProUGUI playerUnitsText; public TextMeshProUGUI enemyUnitsText; public TextMeshProUGUI battleTimeText; [Header("Strategy Description")] public TextMeshProUGUI strategyDescriptionText; private BattleManager battleManager; private BattleStrategy selectedStrategy; private Dictionary unitTypeStrategies = new Dictionary(); private void Start() { InitializeUI(); FindBattleManager(); SetupEventListeners(); } private void Update() { UpdateBattleInfo(); } private void InitializeUI() { // Set default strategy selectedStrategy = BattleStrategy.CreateHoldPositionStrategy(); // Initialize dropdowns SetupStrategyDropdowns(); // Set initial UI state if (battleStatusText != null) battleStatusText.text = "Ready to Battle"; UpdateStrategyDescription(); } private void FindBattleManager() { battleManager = FindFirstObjectByType(); if (battleManager == null) { Debug.LogError("BattleManager not found in scene!"); } else { Debug.Log("BattleUI found BattleManager successfully!"); // Subscribe to battle events battleManager.OnBattleStart += OnBattleStarted; battleManager.OnBattleEnd += OnBattleEnded; } } private void SetupEventListeners() { // Strategy toggle listeners if (holdPositionToggle != null) holdPositionToggle.onValueChanged.AddListener((value) => { if (value) SelectStrategy(StrategyType.HoldPosition); }); if (flankToggle != null) flankToggle.onValueChanged.AddListener((value) => { if (value) SelectStrategy(StrategyType.Flank); }); if (chargeToggle != null) chargeToggle.onValueChanged.AddListener((value) => { if (value) SelectStrategy(StrategyType.Charge); }); if (spreadWideToggle != null) spreadWideToggle.onValueChanged.AddListener((value) => { if (value) SelectStrategy(StrategyType.SpreadWide); }); if (waitForCloseToggle != null) waitForCloseToggle.onValueChanged.AddListener((value) => { if (value) SelectStrategy(StrategyType.WaitForClose); }); // Button listeners if (startBattleButton != null) startBattleButton.onClick.AddListener(StartBattle); if (resetBattleButton != null) resetBattleButton.onClick.AddListener(ResetBattle); // Input field listener if (strategyInputField != null) strategyInputField.onEndEdit.AddListener(ParseStrategyInput); // Dropdown listeners if (infantryStrategyDropdown != null) infantryStrategyDropdown.onValueChanged.AddListener((value) => SetUnitTypeStrategy(UnitType.Infantry, value)); if (cavalryStrategyDropdown != null) cavalryStrategyDropdown.onValueChanged.AddListener((value) => SetUnitTypeStrategy(UnitType.Cavalry, value)); if (archerStrategyDropdown != null) archerStrategyDropdown.onValueChanged.AddListener((value) => SetUnitTypeStrategy(UnitType.Archer, value)); } private void SetupStrategyDropdowns() { List strategyNames = new List { "Hold Position", "Flank Attack", "Charge", "Spread Wide", "Wait for Close Range" }; if (infantryStrategyDropdown != null) { infantryStrategyDropdown.ClearOptions(); infantryStrategyDropdown.AddOptions(strategyNames); } if (cavalryStrategyDropdown != null) { cavalryStrategyDropdown.ClearOptions(); cavalryStrategyDropdown.AddOptions(strategyNames); cavalryStrategyDropdown.value = 1; // Default to Flank for cavalry } if (archerStrategyDropdown != null) { archerStrategyDropdown.ClearOptions(); archerStrategyDropdown.AddOptions(strategyNames); archerStrategyDropdown.value = 4; // Default to Wait for Close Range for archers } } private void SelectStrategy(StrategyType strategyType) { switch (strategyType) { case StrategyType.HoldPosition: selectedStrategy = BattleStrategy.CreateHoldPositionStrategy(); break; case StrategyType.Flank: selectedStrategy = BattleStrategy.CreateFlankStrategy(); break; case StrategyType.Charge: selectedStrategy = BattleStrategy.CreateChargeStrategy(); break; case StrategyType.SpreadWide: selectedStrategy = BattleStrategy.CreateSpreadWideStrategy(); break; case StrategyType.WaitForClose: selectedStrategy = BattleStrategy.CreateWaitForCloseStrategy(); break; } UpdateStrategyDescription(); } private void SetUnitTypeStrategy(UnitType unitType, int strategyIndex) { BattleStrategy strategy = null; switch (strategyIndex) { case 0: strategy = BattleStrategy.CreateHoldPositionStrategy(); break; case 1: strategy = BattleStrategy.CreateFlankStrategy(); break; case 2: strategy = BattleStrategy.CreateChargeStrategy(); break; case 3: strategy = BattleStrategy.CreateSpreadWideStrategy(); break; case 4: strategy = BattleStrategy.CreateWaitForCloseStrategy(); break; } if (strategy != null) { unitTypeStrategies[unitType] = strategy; if (battleManager != null) { battleManager.SetUnitTypeStrategy(unitType, strategy, Team.Player); } } } private void ParseStrategyInput(string input) { if (string.IsNullOrEmpty(input) || battleManager == null) return; // Parse the text input for strategy keywords battleManager.AssignStrategyByText(input, Team.Player); // Update the strategy description based on parsed strategy UpdateStrategyFromInput(input); } private void UpdateStrategyFromInput(string input) { input = input.ToLower(); if (input.Contains("flank")) { flankToggle.isOn = true; SelectStrategy(StrategyType.Flank); } else if (input.Contains("charge")) { chargeToggle.isOn = true; SelectStrategy(StrategyType.Charge); } else if (input.Contains("spread") || input.Contains("wide")) { spreadWideToggle.isOn = true; SelectStrategy(StrategyType.SpreadWide); } else if (input.Contains("wait") || input.Contains("close")) { waitForCloseToggle.isOn = true; SelectStrategy(StrategyType.WaitForClose); } else { holdPositionToggle.isOn = true; SelectStrategy(StrategyType.HoldPosition); } } private void UpdateStrategyDescription() { if (strategyDescriptionText != null && selectedStrategy != null) { strategyDescriptionText.text = selectedStrategy.description; } } private void StartBattle() { Debug.Log("StartBattle called in BattleUI"); if (battleManager == null) { Debug.LogError("Cannot start battle - BattleManager is null!"); return; } // Apply the selected main strategy if (selectedStrategy != null) { Debug.Log($"Applying selected strategy: {selectedStrategy.strategyName}"); battleManager.SetPlayerStrategy(selectedStrategy); } else { Debug.Log("No strategy selected, using default"); } // Apply unit-specific strategies foreach (var unitStrategy in unitTypeStrategies) { Debug.Log($"Applying unit-specific strategy for {unitStrategy.Key}: {unitStrategy.Value.strategyName}"); battleManager.SetUnitTypeStrategy(unitStrategy.Key, unitStrategy.Value, Team.Player); } // Start the battle Debug.Log("Calling battleManager.StartBattle()"); battleManager.StartBattle(); // Update UI if (strategyPanel != null) strategyPanel.SetActive(false); if (startBattleButton != null) startBattleButton.interactable = false; } private void ResetBattle() { if (battleManager != null) { battleManager.ResetBattle(); } // Reset UI if (strategyPanel != null) strategyPanel.SetActive(true); if (startBattleButton != null) startBattleButton.interactable = true; if (battleStatusText != null) battleStatusText.text = "Ready to Battle"; } private void UpdateBattleInfo() { if (battleManager == null) return; // Update unit counts if (playerUnitsText != null) playerUnitsText.text = "Player Units: " + battleManager.playerUnitsRemaining; if (enemyUnitsText != null) enemyUnitsText.text = "Enemy Units: " + battleManager.enemyUnitsRemaining; // Update battle time if (battleTimeText != null && battleManager.battleStarted) { int minutes = Mathf.FloorToInt(battleManager.battleTimer / 60f); int seconds = Mathf.FloorToInt(battleManager.battleTimer % 60f); battleTimeText.text = string.Format("Time: {0:00}:{1:00}", minutes, seconds); } } private void OnBattleStarted() { if (battleStatusText != null) battleStatusText.text = "Battle in Progress!"; } private void OnBattleEnded(Team winner) { if (battleStatusText != null) { battleStatusText.text = winner == Team.Player ? "Victory!" : "Defeat!"; } if (startBattleButton != null) startBattleButton.interactable = true; if (strategyPanel != null) strategyPanel.SetActive(true); } private void OnDestroy() { // Unsubscribe from events if (battleManager != null) { battleManager.OnBattleStart -= OnBattleStarted; battleManager.OnBattleEnd -= OnBattleEnded; } } }