SimpleActionWheel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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
  66. GUI.color = Color.white;
  67. GUIStyle titleStyle = new GUIStyle(GUI.skin.label);
  68. titleStyle.fontSize = 24; // Larger font
  69. titleStyle.alignment = TextAnchor.MiddleCenter;
  70. titleStyle.normal.textColor = Color.white;
  71. titleStyle.fontStyle = FontStyle.Bold;
  72. Rect titleRect = new Rect(center.x - 150, center.y - wheelRadius - 60, 300, 40);
  73. // Draw background for title text
  74. GUI.color = new Color(0, 0, 0, 0.8f);
  75. GUI.Box(titleRect, "");
  76. GUI.color = Color.white;
  77. GUI.Label(titleRect, $"{currentCharacter.CharacterName} - Choose Action", titleStyle);
  78. // Draw action buttons with better spacing
  79. int actionCount = actionTypes.Length;
  80. float angleStep = 360f / actionCount;
  81. for (int i = 0; i < actionCount; i++)
  82. {
  83. float angle = i * angleStep - 90f; // Start from top
  84. Vector2 buttonPos = GetPositionOnCircle(center, angle, wheelRadius * 0.75f); // Moved closer to center
  85. DrawActionButton(buttonPos, actionTypes[i], actionLabels[i], actionColors[i]);
  86. }
  87. // Draw improved instructions
  88. GUIStyle instructionStyle = new GUIStyle(GUI.skin.label);
  89. instructionStyle.fontSize = 16;
  90. instructionStyle.alignment = TextAnchor.MiddleCenter;
  91. instructionStyle.normal.textColor = Color.white;
  92. instructionStyle.fontStyle = FontStyle.Bold;
  93. Rect instructionRect = new Rect(center.x - 200, center.y + wheelRadius + 40, 400, 25);
  94. // Background for instructions
  95. GUI.color = new Color(0, 0, 0, 0.8f);
  96. GUI.Box(instructionRect, "");
  97. GUI.color = Color.white;
  98. GUI.Label(instructionRect, "Click action or press ESC to cancel", instructionStyle);
  99. GUI.color = Color.white;
  100. }
  101. private void DrawActionButton(Vector2 position, BattleActionType actionType, string label, Color color)
  102. {
  103. Rect buttonRect = new Rect(position.x - buttonSize / 2, position.y - buttonSize / 2, buttonSize, buttonSize);
  104. // Check if mouse is over button
  105. bool isHovered = buttonRect.Contains(Event.current.mousePosition);
  106. // Draw button with improved visuals
  107. Color buttonColor = isHovered ? Color.Lerp(color, Color.white, 0.4f) : color;
  108. // Draw button shadow
  109. GUI.color = new Color(0, 0, 0, 0.5f);
  110. Rect shadowRect = new Rect(buttonRect.x + 3, buttonRect.y + 3, buttonRect.width, buttonRect.height);
  111. GUI.Box(shadowRect, "");
  112. // Draw button background
  113. GUI.color = buttonColor;
  114. GUI.Box(buttonRect, "");
  115. // Draw button border (simple outline)
  116. GUI.color = isHovered ? Color.white : new Color(0.8f, 0.8f, 0.8f);
  117. GUI.Box(new Rect(buttonRect.x - 1, buttonRect.y - 1, buttonRect.width + 2, buttonRect.height + 2), "");
  118. // Draw button text
  119. GUIStyle buttonStyle = new GUIStyle(GUI.skin.label);
  120. buttonStyle.fontSize = 14;
  121. buttonStyle.alignment = TextAnchor.MiddleCenter;
  122. buttonStyle.normal.textColor = isHovered ? Color.black : Color.white;
  123. buttonStyle.fontStyle = FontStyle.Bold;
  124. buttonStyle.wordWrap = true;
  125. GUI.color = Color.white;
  126. GUI.Label(buttonRect, label, buttonStyle);
  127. // Handle button click using GUI.Button for easier event handling
  128. GUI.color = Color.clear; // Make button invisible
  129. if (GUI.Button(buttonRect, "", GUIStyle.none))
  130. {
  131. OnActionButtonClicked(actionType);
  132. }
  133. GUI.color = Color.white;
  134. }
  135. private void DrawCircle(Vector2 center, float radius, Color color)
  136. {
  137. GUI.color = color;
  138. float size = radius * 2;
  139. Rect circleRect = new Rect(center.x - radius, center.y - radius, size, size);
  140. // Simple circle approximation using GUI.DrawTexture with a circular texture
  141. // For simplicity, we'll just draw a square for now
  142. GUI.Box(circleRect, "");
  143. GUI.color = Color.white;
  144. }
  145. private Vector2 GetPositionOnCircle(Vector2 center, float angle, float radius)
  146. {
  147. float radian = angle * Mathf.Deg2Rad;
  148. float x = center.x + Mathf.Cos(radian) * radius;
  149. float y = center.y + Mathf.Sin(radian) * radius;
  150. return new Vector2(x, y);
  151. }
  152. private void OnActionButtonClicked(BattleActionType actionType)
  153. {
  154. Debug.Log($"🎯 Action button clicked: {actionType}");
  155. Debug.Log($"🎯 OnActionSelected event has {(OnActionSelected != null ? OnActionSelected.GetInvocationList().Length : 0)} subscribers");
  156. OnActionSelected?.Invoke(actionType);
  157. Debug.Log($"🎯 Event invoked, hiding wheel...");
  158. HideWheel();
  159. }
  160. public void ShowWheelForCharacter(Character character)
  161. {
  162. Debug.Log($"🎮 SimpleActionWheel.ShowWheelForCharacter called with: {(character != null ? character.CharacterName : "NULL")}");
  163. if (character == null)
  164. {
  165. Debug.LogWarning("Cannot show action wheel: character is null");
  166. return;
  167. }
  168. currentCharacter = character;
  169. isVisible = true;
  170. Debug.Log($"🎮 Simple action wheel shown for {character.CharacterName} - isVisible: {isVisible}");
  171. }
  172. public void HideWheel()
  173. {
  174. isVisible = false;
  175. currentCharacter = null;
  176. Debug.Log("🎮 Simple action wheel hidden");
  177. }
  178. private Character FindSelectedCharacter()
  179. {
  180. // Find the currently selected character
  181. var characters = FindObjectsByType<Character>(FindObjectsSortMode.None);
  182. foreach (var character in characters)
  183. {
  184. if (character.CompareTag("Player"))
  185. {
  186. // For now, return the first player character found
  187. // In a real implementation, you'd check which one is actually selected
  188. return character;
  189. }
  190. }
  191. return null;
  192. }
  193. void OnDestroy()
  194. {
  195. OnActionSelected = null;
  196. }
  197. }