EnemySelectionUI.cs 7.3 KB

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