EnemySelectionUI.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. /// <summary>
  5. /// Simple IMGUI-based enemy selection UI for attack targeting
  6. /// Alternative to direct mouse targeting
  7. /// </summary>
  8. public class EnemySelectionUI : MonoBehaviour
  9. {
  10. [Header("Settings")]
  11. public KeyCode toggleKey = KeyCode.Tab;
  12. public bool useEnemyListMode = false; // Can be toggled in inspector
  13. [Header("UI Settings")]
  14. public float windowWidth = 300f;
  15. public float windowHeight = 400f;
  16. public float buttonHeight = 40f;
  17. private bool isVisible = false;
  18. private Character currentAttacker;
  19. private List<Character> availableEnemies = new List<Character>();
  20. private Character selectedEnemy;
  21. private Vector2 scrollPosition;
  22. // Events
  23. public event System.Action<Character> OnEnemySelected;
  24. public event System.Action OnSelectionCancelled;
  25. private GUIStyle windowStyle;
  26. private GUIStyle buttonStyle;
  27. private GUIStyle selectedButtonStyle;
  28. private GUIStyle labelStyle;
  29. private bool stylesInitialized = false;
  30. void Update()
  31. {
  32. // Toggle enemy list mode with Tab key
  33. if (Input.GetKeyDown(toggleKey))
  34. {
  35. useEnemyListMode = !useEnemyListMode;
  36. Debug.Log($"🎯 Enemy selection mode: {(useEnemyListMode ? "List UI" : "Direct Targeting")}");
  37. }
  38. // Close UI with Escape
  39. if (isVisible && Input.GetKeyDown(KeyCode.Escape))
  40. {
  41. HideEnemySelection();
  42. OnSelectionCancelled?.Invoke();
  43. }
  44. }
  45. void OnGUI()
  46. {
  47. if (!isVisible) return;
  48. InitializeStyles();
  49. DrawEnemySelectionWindow();
  50. }
  51. private void InitializeStyles()
  52. {
  53. if (stylesInitialized) return;
  54. windowStyle = new GUIStyle(GUI.skin.window);
  55. windowStyle.fontSize = 16;
  56. windowStyle.fontStyle = FontStyle.Bold;
  57. buttonStyle = new GUIStyle(GUI.skin.button);
  58. buttonStyle.fontSize = 14;
  59. buttonStyle.alignment = TextAnchor.MiddleLeft;
  60. buttonStyle.padding = new RectOffset(10, 10, 10, 10);
  61. selectedButtonStyle = new GUIStyle(buttonStyle);
  62. selectedButtonStyle.normal.background = buttonStyle.active.background;
  63. selectedButtonStyle.normal.textColor = Color.yellow;
  64. selectedButtonStyle.fontStyle = FontStyle.Bold;
  65. labelStyle = new GUIStyle(GUI.skin.label);
  66. labelStyle.fontSize = 14;
  67. labelStyle.fontStyle = FontStyle.Bold;
  68. labelStyle.alignment = TextAnchor.MiddleCenter;
  69. stylesInitialized = true;
  70. }
  71. private void DrawEnemySelectionWindow()
  72. {
  73. Rect windowRect = new Rect(
  74. Screen.width - windowWidth - 20,
  75. (Screen.height - windowHeight) / 2,
  76. windowWidth,
  77. windowHeight
  78. );
  79. GUI.Window(0, windowRect, DrawWindowContent,
  80. $"Select Target for {(currentAttacker ? currentAttacker.CharacterName : "Character")}",
  81. windowStyle);
  82. }
  83. private void DrawWindowContent(int windowID)
  84. {
  85. GUILayout.BeginVertical();
  86. // Instructions
  87. GUILayout.Label("Choose an enemy to attack:", labelStyle);
  88. GUILayout.Space(10);
  89. // Scroll area for enemy list
  90. scrollPosition = GUILayout.BeginScrollView(scrollPosition,
  91. GUILayout.ExpandHeight(true));
  92. foreach (Character enemy in availableEnemies)
  93. {
  94. if (enemy == null) continue;
  95. bool isSelected = (selectedEnemy == enemy);
  96. GUIStyle currentStyle = isSelected ? selectedButtonStyle : buttonStyle;
  97. // Create button content with enemy info
  98. string buttonText = $"{enemy.CharacterName}";
  99. if (enemy.TryGetComponent<Character>(out var character))
  100. {
  101. buttonText += $" (HP: {character.CurrentHealth}/{character.MaxHealth})";
  102. }
  103. if (GUILayout.Button(buttonText, currentStyle, GUILayout.Height(buttonHeight)))
  104. {
  105. SelectEnemy(enemy);
  106. }
  107. // Highlight selected enemy in the scene
  108. if (isSelected)
  109. {
  110. HighlightEnemy(enemy);
  111. }
  112. }
  113. GUILayout.EndScrollView();
  114. GUILayout.Space(10);
  115. // Action buttons
  116. GUILayout.BeginHorizontal();
  117. GUI.enabled = selectedEnemy != null;
  118. if (GUILayout.Button("Attack Selected", GUILayout.Height(30)))
  119. {
  120. ConfirmSelection();
  121. }
  122. GUI.enabled = true;
  123. if (GUILayout.Button("Cancel", GUILayout.Height(30)))
  124. {
  125. CancelSelection();
  126. }
  127. GUILayout.EndHorizontal();
  128. GUILayout.Space(5);
  129. GUILayout.Label("Press TAB to toggle targeting mode", GUI.skin.box);
  130. GUILayout.Label("Press ESC to cancel", GUI.skin.box);
  131. GUILayout.EndVertical();
  132. }
  133. public void ShowEnemySelection(Character attacker)
  134. {
  135. if (!useEnemyListMode)
  136. {
  137. Debug.Log("🎯 Enemy list mode disabled - use direct targeting");
  138. return;
  139. }
  140. currentAttacker = attacker;
  141. selectedEnemy = null;
  142. // Find all enemy characters
  143. RefreshEnemyList();
  144. if (availableEnemies.Count == 0)
  145. {
  146. Debug.LogWarning("No enemies found for targeting!");
  147. return;
  148. }
  149. isVisible = true;
  150. Debug.Log($"🎯 Enemy selection UI shown for {attacker.CharacterName} - {availableEnemies.Count} enemies available");
  151. }
  152. public void HideEnemySelection()
  153. {
  154. isVisible = false;
  155. selectedEnemy = null;
  156. ClearEnemyHighlights();
  157. Debug.Log("🎯 Enemy selection UI hidden");
  158. }
  159. private void RefreshEnemyList()
  160. {
  161. availableEnemies.Clear();
  162. // Find all characters tagged as enemies
  163. Character[] allCharacters = FindObjectsByType<Character>(FindObjectsSortMode.None);
  164. foreach (Character character in allCharacters)
  165. {
  166. if (character.CompareTag("Enemy") && character.CurrentHealth > 0)
  167. {
  168. availableEnemies.Add(character);
  169. }
  170. }
  171. // Sort by distance to attacker for convenience
  172. if (currentAttacker != null)
  173. {
  174. availableEnemies = availableEnemies
  175. .OrderBy(enemy => Vector3.Distance(currentAttacker.transform.position, enemy.transform.position))
  176. .ToList();
  177. }
  178. Debug.Log($"🎯 Found {availableEnemies.Count} available enemies");
  179. }
  180. private void SelectEnemy(Character enemy)
  181. {
  182. // Clear previous selection highlight
  183. if (selectedEnemy != null)
  184. {
  185. ClearEnemyHighlight(selectedEnemy);
  186. }
  187. selectedEnemy = enemy;
  188. Debug.Log($"🎯 Selected enemy: {enemy.CharacterName}");
  189. }
  190. private void ConfirmSelection()
  191. {
  192. if (selectedEnemy != null)
  193. {
  194. Debug.Log($"✅ Attack target confirmed: {selectedEnemy.CharacterName}");
  195. OnEnemySelected?.Invoke(selectedEnemy);
  196. HideEnemySelection();
  197. }
  198. }
  199. private void CancelSelection()
  200. {
  201. Debug.Log("❌ Enemy selection cancelled");
  202. OnSelectionCancelled?.Invoke();
  203. HideEnemySelection();
  204. }
  205. private void HighlightEnemy(Character enemy)
  206. {
  207. // Add visual highlight to the enemy (simple outline or color change)
  208. if (enemy != null)
  209. {
  210. // Use the character's existing visual state system
  211. enemy.SetVisualState(ActionDecisionState.NoAction); // Temporary highlight
  212. }
  213. }
  214. private void ClearEnemyHighlight(Character enemy)
  215. {
  216. if (enemy != null)
  217. {
  218. enemy.SetVisualState(ActionDecisionState.NoAction); // Reset to normal
  219. }
  220. }
  221. private void ClearEnemyHighlights()
  222. {
  223. foreach (Character enemy in availableEnemies)
  224. {
  225. ClearEnemyHighlight(enemy);
  226. }
  227. }
  228. void OnDestroy()
  229. {
  230. OnEnemySelected = null;
  231. OnSelectionCancelled = null;
  232. }
  233. }