SimpleActionWheel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using UnityEngine;
  2. using System;
  3. /// <summary>
  4. /// Simple IMGUI-based action wheel that doesn't require any Canvas setup
  5. /// Perfect for testing and prototyping the battle action system
  6. /// </summary>
  7. public class SimpleActionWheel : MonoBehaviour
  8. {
  9. [Header("Settings")]
  10. public KeyCode toggleKey = KeyCode.Q;
  11. public bool showDebugGUI = true;
  12. private Character currentCharacter;
  13. private bool isVisible = false;
  14. private Vector2 wheelCenter;
  15. private float wheelRadius = 120f; // Increased for better spacing
  16. private float buttonSize = 70f; // Larger buttons for better visibility
  17. // Events
  18. public event Action<BattleActionType> OnActionSelected;
  19. private BattleActionType[] actionTypes =
  20. {
  21. BattleActionType.Attack,
  22. BattleActionType.Move,
  23. BattleActionType.UseItem,
  24. BattleActionType.CastSpell,
  25. BattleActionType.Defend,
  26. BattleActionType.RunAway
  27. };
  28. private string[] actionLabels =
  29. {
  30. "⚔️ Attack",
  31. "👟 Move",
  32. "🧪 Item",
  33. "✨ Spell",
  34. "🛡️ Defend",
  35. "💨 Run"
  36. };
  37. private Color[] actionColors =
  38. {
  39. Color.red,
  40. Color.green,
  41. Color.blue,
  42. Color.magenta,
  43. Color.yellow,
  44. Color.gray
  45. };
  46. void Update()
  47. {
  48. if (isVisible && Input.GetKeyDown(KeyCode.Escape))
  49. {
  50. HideWheel();
  51. }
  52. }
  53. void OnGUI()
  54. {
  55. if (!isVisible || currentCharacter == null) return;
  56. // Set up GUI
  57. GUI.depth = -1000; // Ensure it's on top
  58. // Draw background
  59. Vector2 center = new Vector2(Screen.width / 2, Screen.height / 2);
  60. wheelCenter = center;
  61. // Draw wheel background with better visibility
  62. DrawCircle(center, wheelRadius + 30, new Color(0, 0, 0, 0.8f)); // Darker background
  63. DrawCircle(center, wheelRadius + 25, Color.white);
  64. DrawCircle(center, wheelRadius + 20, new Color(0.2f, 0.2f, 0.2f, 0.9f)); // Inner background
  65. // Draw title with better contrast and auto-sizing
  66. GUI.color = Color.white;
  67. GUIStyle titleStyle = new GUIStyle(GUI.skin.label);
  68. titleStyle.fontSize = 24; // Starting font size
  69. titleStyle.alignment = TextAnchor.MiddleCenter;
  70. titleStyle.normal.textColor = Color.white;
  71. titleStyle.fontStyle = FontStyle.Bold;
  72. string titleText = $"{currentCharacter.CharacterName} - Choose Action";
  73. Rect titleRect = new Rect(center.x - 200, center.y - wheelRadius - 60, 400, 40);
  74. // Auto-size text to fit the available width
  75. Vector2 textSize = titleStyle.CalcSize(new GUIContent(titleText));
  76. if (textSize.x > titleRect.width)
  77. {
  78. // Calculate scale factor to fit text
  79. float scaleFactor = titleRect.width / textSize.x;
  80. titleStyle.fontSize = Mathf.Max(14, Mathf.RoundToInt(titleStyle.fontSize * scaleFactor));
  81. }
  82. // Draw background for title text
  83. GUI.color = new Color(0, 0, 0, 0.8f);
  84. GUI.Box(titleRect, "");
  85. GUI.color = Color.white;
  86. GUI.Label(titleRect, titleText, titleStyle);
  87. // Draw action buttons with better spacing
  88. int actionCount = actionTypes.Length;
  89. float angleStep = 360f / actionCount;
  90. for (int i = 0; i < actionCount; i++)
  91. {
  92. float angle = i * angleStep - 90f; // Start from top
  93. Vector2 buttonPos = GetPositionOnCircle(center, angle, wheelRadius * 0.75f); // Moved closer to center
  94. DrawActionButton(buttonPos, actionTypes[i], actionLabels[i], actionColors[i]);
  95. }
  96. // Draw improved instructions
  97. GUIStyle instructionStyle = new GUIStyle(GUI.skin.label);
  98. instructionStyle.fontSize = 16;
  99. instructionStyle.alignment = TextAnchor.MiddleCenter;
  100. instructionStyle.normal.textColor = Color.white;
  101. instructionStyle.fontStyle = FontStyle.Bold;
  102. Rect instructionRect = new Rect(center.x - 250, center.y + wheelRadius + 40, 500, 25);
  103. // Background for instructions
  104. GUI.color = new Color(0, 0, 0, 0.8f);
  105. GUI.Box(instructionRect, "");
  106. GUI.color = Color.white;
  107. GUI.Label(instructionRect, "Click action, ESC to cancel, or Q to change action later", instructionStyle);
  108. GUI.color = Color.white;
  109. }
  110. private void DrawActionButton(Vector2 position, BattleActionType actionType, string label, Color color)
  111. {
  112. Rect buttonRect = new Rect(position.x - buttonSize / 2, position.y - buttonSize / 2, buttonSize, buttonSize);
  113. // Check if mouse is over button
  114. bool isHovered = buttonRect.Contains(Event.current.mousePosition);
  115. // Draw button with improved visuals
  116. Color buttonColor = isHovered ? Color.Lerp(color, Color.white, 0.4f) : color;
  117. // Draw button shadow
  118. GUI.color = new Color(0, 0, 0, 0.5f);
  119. Rect shadowRect = new Rect(buttonRect.x + 3, buttonRect.y + 3, buttonRect.width, buttonRect.height);
  120. GUI.Box(shadowRect, "");
  121. // Draw button background
  122. GUI.color = buttonColor;
  123. GUI.Box(buttonRect, "");
  124. // Draw button border (simple outline)
  125. GUI.color = isHovered ? Color.white : new Color(0.8f, 0.8f, 0.8f);
  126. GUI.Box(new Rect(buttonRect.x - 1, buttonRect.y - 1, buttonRect.width + 2, buttonRect.height + 2), "");
  127. // Draw button text
  128. GUIStyle buttonStyle = new GUIStyle(GUI.skin.label);
  129. buttonStyle.fontSize = 14;
  130. buttonStyle.alignment = TextAnchor.MiddleCenter;
  131. buttonStyle.normal.textColor = isHovered ? Color.black : Color.white;
  132. buttonStyle.fontStyle = FontStyle.Bold;
  133. buttonStyle.wordWrap = true;
  134. GUI.color = Color.white;
  135. GUI.Label(buttonRect, label, buttonStyle);
  136. // Handle button click using GUI.Button for easier event handling
  137. GUI.color = Color.clear; // Make button invisible
  138. if (GUI.Button(buttonRect, "", GUIStyle.none))
  139. {
  140. OnActionButtonClicked(actionType);
  141. }
  142. GUI.color = Color.white;
  143. }
  144. private void DrawCircle(Vector2 center, float radius, Color color)
  145. {
  146. GUI.color = color;
  147. float size = radius * 2;
  148. Rect circleRect = new Rect(center.x - radius, center.y - radius, size, size);
  149. // Simple circle approximation using GUI.DrawTexture with a circular texture
  150. // For simplicity, we'll just draw a square for now
  151. GUI.Box(circleRect, "");
  152. GUI.color = Color.white;
  153. }
  154. private Vector2 GetPositionOnCircle(Vector2 center, float angle, float radius)
  155. {
  156. float radian = angle * Mathf.Deg2Rad;
  157. float x = center.x + Mathf.Cos(radian) * radius;
  158. float y = center.y + Mathf.Sin(radian) * radius;
  159. return new Vector2(x, y);
  160. }
  161. private void OnActionButtonClicked(BattleActionType actionType)
  162. {
  163. Debug.Log($"🎯 Action button clicked: {actionType}");
  164. Debug.Log($"🎯 OnActionSelected event has {(OnActionSelected != null ? OnActionSelected.GetInvocationList().Length : 0)} subscribers");
  165. OnActionSelected?.Invoke(actionType);
  166. Debug.Log($"🎯 Event invoked, hiding wheel...");
  167. HideWheel();
  168. }
  169. public void ShowWheelForCharacter(Character character)
  170. {
  171. Debug.Log($"🎮 SimpleActionWheel.ShowWheelForCharacter called with: {(character != null ? character.CharacterName : "NULL")}");
  172. if (character == null)
  173. {
  174. Debug.LogWarning("Cannot show action wheel: character is null");
  175. return;
  176. }
  177. currentCharacter = character;
  178. isVisible = true;
  179. Debug.Log($"🎮 Simple action wheel shown for {character.CharacterName} - isVisible: {isVisible}");
  180. }
  181. public void HideWheel()
  182. {
  183. isVisible = false;
  184. currentCharacter = null;
  185. Debug.Log("🎮 Simple action wheel hidden");
  186. }
  187. private Character FindSelectedCharacter()
  188. {
  189. // Find the currently selected character
  190. var characters = FindObjectsByType<Character>(FindObjectsSortMode.None);
  191. foreach (var character in characters)
  192. {
  193. if (character.CompareTag("Player"))
  194. {
  195. // For now, return the first player character found
  196. // In a real implementation, you'd check which one is actually selected
  197. return character;
  198. }
  199. }
  200. return null;
  201. }
  202. void OnDestroy()
  203. {
  204. OnActionSelected = null;
  205. }
  206. }