PlayerDecisionController .cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. using System.Collections.Generic;
  2. using Unity.VisualScripting;
  3. using UnityEngine;
  4. public class PlayerDecisionController : MonoBehaviour
  5. {
  6. [Header("References")]
  7. public LayerMask enemyLayerMask = 1 << 10; // Enemy layer
  8. public LayerMask playerLayerMask = 1 << 9; // Player layer
  9. [Header("State")]
  10. private Character selectedCharacter;
  11. private bool isDragging = false;
  12. private Vector3 dragStartPosition;
  13. [Header("Settings")]
  14. public float enemySnapDistance = 1f;
  15. private List<Character> playerCharacters = new List<Character>();
  16. private TargetingLine targetingLine;
  17. private Camera mainCamera;
  18. private bool isEnabled = true;
  19. private List<TargetingLine> activeActionLines = new List<TargetingLine>();
  20. void Awake()
  21. {
  22. mainCamera = Camera.main;
  23. targetingLine = GetComponent<TargetingLine>();
  24. if (targetingLine == null)
  25. {
  26. targetingLine = gameObject.AddComponent<TargetingLine>();
  27. }
  28. }
  29. void Start()
  30. {
  31. InitializePlayerCharacters();
  32. }
  33. void Update()
  34. {
  35. HandleInput();
  36. }
  37. private void InitializePlayerCharacters()
  38. {
  39. // Get player characters from GameManager if available
  40. if (GameManager.Instance != null)
  41. {
  42. playerCharacters.Clear();
  43. foreach (GameObject playerGO in GameManager.Instance.playerCharacters)
  44. {
  45. Character character = playerGO.GetComponent<Character>();
  46. if (character != null)
  47. {
  48. playerCharacters.Add(character);
  49. }
  50. }
  51. }
  52. else
  53. {
  54. // Fallback: find all characters (for testing without GameManager)
  55. Character[] characters = FindObjectsByType<Character>(FindObjectsSortMode.None);
  56. playerCharacters.AddRange(characters);
  57. }
  58. foreach (Character character in playerCharacters)
  59. {
  60. character.actionData.Reset();
  61. character.SetVisualState(ActionDecisionState.NoAction);
  62. }
  63. }
  64. private void HandleInput()
  65. {
  66. if (!isEnabled) return;
  67. if (Input.GetMouseButtonDown(0)) // Left click
  68. {
  69. HandleLeftClickDown();
  70. }
  71. else if (Input.GetMouseButton(0) && isDragging) // Left drag
  72. {
  73. HandleLeftDrag();
  74. }
  75. else if (Input.GetMouseButtonUp(0)) // Left release
  76. {
  77. HandleLeftClickUp();
  78. }
  79. else if (Input.GetMouseButtonDown(1)) // Right click
  80. {
  81. HandleRightClick();
  82. }
  83. }
  84. /// <summary>
  85. /// Check if we're currently in move targeting mode (move action selected but not yet targeted)
  86. /// </summary>
  87. private bool IsInMoveTargetingMode()
  88. {
  89. if (selectedCharacter == null) return false;
  90. var actionData = selectedCharacter.GetEnhancedActionData<EnhancedCharacterActionData>();
  91. return actionData != null &&
  92. actionData.actionType == BattleActionType.Move &&
  93. actionData.state == ActionDecisionState.NoAction;
  94. }
  95. private void HandleLeftClickDown()
  96. {
  97. Vector3 mouseWorldPosition = GetMouseWorldPosition();
  98. Character clickedCharacter = GetPlayerCharacterAtPosition(mouseWorldPosition);
  99. if (clickedCharacter != null)
  100. {
  101. // Start drag mode from character - this allows both move and attack by dragging
  102. selectedCharacter = clickedCharacter;
  103. isDragging = true;
  104. dragStartPosition = clickedCharacter.transform.position;
  105. Debug.Log($"🖱️ Starting drag from character {clickedCharacter.CharacterName}");
  106. // Clear any existing action lines before starting new targeting
  107. ClearActionLineForCharacter(clickedCharacter);
  108. // Start targeting line for drag operations
  109. targetingLine.StartTargeting(dragStartPosition);
  110. // CinemachineCameraController.Instance.FocusOnCharacter(clickedCharacter.transform);
  111. }
  112. else
  113. {
  114. // Clicked empty space - start drag if we have a selected character and are in move mode
  115. if (selectedCharacter != null && IsInMoveTargetingMode())
  116. {
  117. isDragging = true;
  118. targetingLine.StartTargeting(selectedCharacter.transform.position);
  119. Debug.Log($"🎯 Started move targeting for {selectedCharacter.CharacterName}");
  120. }
  121. }
  122. }
  123. private void HandleLeftDrag()
  124. {
  125. if (selectedCharacter == null) return;
  126. Vector3 mouseWorldPos = GetMouseWorldPosition();
  127. GameObject enemyAtMouse = GetEnemyAtPosition(mouseWorldPos);
  128. if (enemyAtMouse != null)
  129. {
  130. // Snap to enemy
  131. Vector3 enemyPos = enemyAtMouse.transform.position;
  132. targetingLine.UpdateTargeting(dragStartPosition, enemyPos, true);
  133. }
  134. else
  135. {
  136. // Follow mouse
  137. targetingLine.UpdateTargeting(dragStartPosition, mouseWorldPos, false);
  138. }
  139. }
  140. private void HandleLeftClickUp()
  141. {
  142. Vector3 mouseWorldPos = GetMouseWorldPosition();
  143. GameObject enemyAtMouse = GetEnemyAtPosition(mouseWorldPos);
  144. // Handle different cases based on current state
  145. if (isDragging && selectedCharacter != null)
  146. {
  147. // We're in active targeting mode
  148. if (enemyAtMouse != null)
  149. {
  150. // Attack target selected
  151. selectedCharacter.actionData.SetAttackTarget(enemyAtMouse);
  152. UpdateEnhancedActionData(selectedCharacter, BattleActionType.Attack, enemyAtMouse, mouseWorldPos);
  153. selectedCharacter.SetVisualState(selectedCharacter.actionData.state);
  154. Debug.Log($"⚔️ Attack target set for {selectedCharacter.CharacterName}");
  155. }
  156. else
  157. {
  158. // Check if user actually dragged (minimum distance threshold) OR if we're in action wheel move mode
  159. float dragDistance = Vector3.Distance(dragStartPosition, mouseWorldPos);
  160. const float minDragDistance = 0.5f; // Minimum distance to consider it a drag vs click
  161. bool isActionWheelMove = IsInMoveTargetingMode();
  162. bool isDragMove = dragDistance > minDragDistance;
  163. if (isActionWheelMove || isDragMove)
  164. {
  165. // Move target selected
  166. selectedCharacter.actionData.SetMoveTarget(mouseWorldPos);
  167. UpdateEnhancedActionData(selectedCharacter, BattleActionType.Move, null, mouseWorldPos);
  168. selectedCharacter.SetVisualState(selectedCharacter.actionData.state);
  169. if (isActionWheelMove)
  170. {
  171. Debug.Log($"👟 Move target set for {selectedCharacter.CharacterName} (action wheel mode)");
  172. }
  173. else
  174. {
  175. Debug.Log($"👟 Move target set for {selectedCharacter.CharacterName} (dragged {dragDistance:F1} units)");
  176. }
  177. }
  178. else
  179. {
  180. // User just clicked on character without dragging - don't set any action
  181. Debug.Log($"🖱️ Character {selectedCharacter.CharacterName} selected (clicked without dragging)");
  182. }
  183. }
  184. // Notify BattleActionIntegration that targeting is complete
  185. var integration = FindFirstObjectByType<BattleActionIntegration>();
  186. if (integration != null)
  187. {
  188. integration.OnTargetingComplete(selectedCharacter);
  189. }
  190. // Cleanup targeting
  191. targetingLine.StopTargeting();
  192. isDragging = false;
  193. selectedCharacter = null; // Clear selection after action is set
  194. }
  195. else if (selectedCharacter != null)
  196. {
  197. // Just a character selection click - notify integration but don't set actions
  198. var integration = FindFirstObjectByType<BattleActionIntegration>();
  199. if (integration != null)
  200. {
  201. integration.OnTargetingComplete(selectedCharacter);
  202. }
  203. Debug.Log($"👆 Character {selectedCharacter.CharacterName} selected (no action set)");
  204. // Don't clear selectedCharacter here - let integration handle it
  205. }
  206. // Cleanup
  207. targetingLine.StopTargeting();
  208. selectedCharacter = null;
  209. isDragging = false;
  210. }
  211. private void HandleRightClick()
  212. {
  213. if (isDragging && selectedCharacter != null)
  214. {
  215. // Cancel current action
  216. selectedCharacter.actionData.Reset();
  217. selectedCharacter.SetVisualState(ActionDecisionState.NoAction);
  218. targetingLine.StopTargeting();
  219. selectedCharacter = null;
  220. isDragging = false;
  221. }
  222. else
  223. {
  224. // Right click on character to reset their action
  225. Vector3 mouseWorldPos = GetMouseWorldPosition();
  226. Character clickedCharacter = GetPlayerCharacterAtPosition(mouseWorldPos);
  227. if (clickedCharacter != null)
  228. {
  229. clickedCharacter.actionData.Reset();
  230. clickedCharacter.SetVisualState(ActionDecisionState.NoAction);
  231. }
  232. }
  233. }
  234. private Vector3 GetMouseWorldPosition()
  235. {
  236. Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  237. RaycastHit hit;
  238. // Raycast against a ground plane or existing colliders
  239. if (Physics.Raycast(ray, out hit, 200f))
  240. {
  241. Debug.Log($"🌍 Mouse world position: {hit.point} (hit: {hit.collider.gameObject.name})");
  242. return hit.point;
  243. }
  244. // Fallback: project onto a horizontal plane at y=0
  245. Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
  246. if (groundPlane.Raycast(ray, out float distance))
  247. {
  248. Vector3 position = ray.GetPoint(distance);
  249. Debug.Log($"🌍 Mouse world position (fallback plane): {position}");
  250. return position;
  251. }
  252. Debug.Log($"❌ Could not determine mouse world position");
  253. return Vector3.zero;
  254. }
  255. private Character GetPlayerCharacterAtPosition(Vector3 worldPosition)
  256. {
  257. Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  258. RaycastHit hit;
  259. if (Physics.Raycast(ray, out hit, 200f, playerLayerMask))
  260. {
  261. return hit.collider.GetComponent<Character>();
  262. }
  263. // Try raycast without layer mask to see what we're hitting
  264. if (Physics.Raycast(ray, out hit, 200f))
  265. {
  266. }
  267. else
  268. {
  269. }
  270. return null;
  271. }
  272. private GameObject GetEnemyAtPosition(Vector3 worldPosition)
  273. {
  274. Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  275. RaycastHit hit;
  276. if (Physics.Raycast(ray, out hit, 200f, enemyLayerMask))
  277. {
  278. Debug.Log($"🎯 Enemy detected: {hit.collider.gameObject.name} on layer {hit.collider.gameObject.layer}");
  279. return hit.collider.gameObject;
  280. }
  281. // Debug: Check what we're hitting without layer mask
  282. if (Physics.Raycast(ray, out hit, 200f))
  283. {
  284. Debug.Log($"🔍 Hit object: {hit.collider.gameObject.name} on layer {hit.collider.gameObject.layer} (not enemy layer)");
  285. }
  286. else
  287. {
  288. Debug.Log($"❌ No raycast hit detected at mouse position");
  289. }
  290. return null;
  291. }
  292. public bool AllCharactersHaveActions()
  293. {
  294. foreach (var character in playerCharacters)
  295. {
  296. // Characters need actions if they have no action set
  297. if (character.actionData.state == ActionDecisionState.NoAction)
  298. return false;
  299. }
  300. return true;
  301. }
  302. public void ResetAllCharacterActions()
  303. {
  304. foreach (var character in playerCharacters)
  305. {
  306. character.actionData.Reset();
  307. character.SetVisualState(ActionDecisionState.NoAction);
  308. }
  309. }
  310. public void UpdateVisualStates()
  311. {
  312. foreach (var character in playerCharacters)
  313. {
  314. // Update visual states based on current action status
  315. if (character.actionData.state == ActionDecisionState.NoAction)
  316. {
  317. character.SetVisualState(ActionDecisionState.NoAction); // Pink
  318. }
  319. else if (character.HasActionSelected())
  320. {
  321. character.SetVisualState(ActionDecisionState.ActionSelected); // Green
  322. }
  323. }
  324. }
  325. public void RefreshPlayerCharacters()
  326. {
  327. InitializePlayerCharacters();
  328. }
  329. public void SetEnabled(bool enabled)
  330. {
  331. isEnabled = enabled;
  332. if (!enabled)
  333. {
  334. if (isDragging)
  335. {
  336. // Cancel any current dragging operation
  337. targetingLine.StopTargeting();
  338. selectedCharacter = null;
  339. isDragging = false;
  340. }
  341. // Don't automatically hide action lines when disabling - let caller decide
  342. }
  343. }
  344. /// <summary>
  345. /// Check if PlayerDecisionController is currently in targeting/dragging mode
  346. /// </summary>
  347. public bool IsInTargetingMode => isDragging && selectedCharacter != null;
  348. /// <summary>
  349. /// Get the currently selected character (for action wheel integration)
  350. /// </summary>
  351. public Character GetSelectedCharacter() => selectedCharacter;
  352. /// <summary>
  353. /// Reset the controller state (for debugging/recovery)
  354. /// </summary>
  355. public void ResetState()
  356. {
  357. Debug.Log("🔄 PlayerDecisionController: Resetting state");
  358. if (isDragging && targetingLine != null)
  359. {
  360. targetingLine.StopTargeting();
  361. }
  362. selectedCharacter = null;
  363. isDragging = false;
  364. isEnabled = true;
  365. Debug.Log("✅ PlayerDecisionController: State reset complete");
  366. }
  367. public void ShowActiveActionLines()
  368. {
  369. HideActiveActionLines(); // Clear any existing lines first
  370. foreach (var character in playerCharacters)
  371. {
  372. if (character.HasActionSelected() && !character.IsActionComplete())
  373. {
  374. GameObject lineObject = new GameObject($"ActionLine_{character.name}");
  375. TargetingLine line = lineObject.AddComponent<TargetingLine>();
  376. activeActionLines.Add(line);
  377. Vector3 startPos = character.transform.position + Vector3.up * 0.5f;
  378. if (character.actionData.targetEnemy != null)
  379. {
  380. Vector3 endPos = character.actionData.targetEnemy.transform.position + Vector3.up * 0.5f;
  381. line.StartTargeting(startPos);
  382. line.UpdateTargeting(startPos, endPos, true); // true for enemy target (red line)
  383. }
  384. else if (character.actionData.targetPosition != Vector3.zero)
  385. {
  386. line.StartTargeting(startPos);
  387. line.UpdateTargeting(startPos, character.actionData.targetPosition, false); // false for movement (blue line)
  388. }
  389. }
  390. }
  391. }
  392. public void HideActiveActionLines()
  393. {
  394. foreach (var line in activeActionLines)
  395. {
  396. if (line != null)
  397. {
  398. line.StopTargeting();
  399. Destroy(line.gameObject);
  400. }
  401. }
  402. activeActionLines.Clear();
  403. }
  404. public void ClearActionLineForCharacter(Character character)
  405. {
  406. for (int i = activeActionLines.Count - 1; i >= 0; i--)
  407. {
  408. var line = activeActionLines[i];
  409. if (line != null && line.gameObject.name == $"ActionLine_{character.name}")
  410. {
  411. line.StopTargeting();
  412. Destroy(line.gameObject);
  413. activeActionLines.RemoveAt(i);
  414. break;
  415. }
  416. }
  417. }
  418. private void UpdateEnhancedActionData(Character character, BattleActionType actionType, GameObject targetEnemy, Vector3 targetPosition)
  419. {
  420. // Check if character has enhanced action data
  421. var enhancedData = character.GetEnhancedActionData<EnhancedCharacterActionData>();
  422. if (enhancedData != null)
  423. {
  424. Debug.Log($"🔄 Updating enhanced action data for {character.CharacterName}: {actionType}");
  425. // Update the enhanced action data to match the targeting result
  426. enhancedData.actionType = actionType;
  427. enhancedData.state = ActionDecisionState.ActionSelected;
  428. if (actionType == BattleActionType.Attack && targetEnemy != null)
  429. {
  430. enhancedData.targetEnemy = targetEnemy;
  431. enhancedData.targetPosition = Vector3.zero;
  432. Debug.Log($"🗡️ Enhanced data updated for attack on {targetEnemy.name}");
  433. }
  434. else if (actionType == BattleActionType.Move)
  435. {
  436. enhancedData.targetPosition = targetPosition;
  437. enhancedData.targetEnemy = null;
  438. Debug.Log($"👟 Enhanced data updated for move to {targetPosition}");
  439. }
  440. }
  441. else
  442. {
  443. Debug.Log($"ℹ️ No enhanced action data found for {character.CharacterName}");
  444. }
  445. }
  446. /// <summary>
  447. /// Manually start targeting mode for a specific character and action type
  448. /// Called by the action wheel system
  449. /// </summary>
  450. public void StartTargetingForCharacter(Character character, BattleActionType actionType)
  451. {
  452. Debug.Log($"🎯 StartTargetingForCharacter called: {character.CharacterName} -> {actionType}");
  453. selectedCharacter = character;
  454. dragStartPosition = character.transform.position;
  455. // Store the action type for later reference
  456. var enhancedData = character.GetEnhancedActionData<EnhancedCharacterActionData>();
  457. if (enhancedData == null)
  458. {
  459. enhancedData = new EnhancedCharacterActionData();
  460. character.SetEnhancedActionData(enhancedData);
  461. }
  462. enhancedData.actionType = actionType;
  463. enhancedData.state = ActionDecisionState.NoAction; // Waiting for target
  464. // Start targeting mode - user needs to click to set target
  465. isDragging = true;
  466. // Clear any existing action lines before starting new targeting
  467. ClearActionLineForCharacter(character);
  468. // Start the targeting line
  469. targetingLine.StartTargeting(dragStartPosition);
  470. Debug.Log($"✅ Targeting mode active for {character.CharacterName} - {actionType}");
  471. }
  472. /// <summary>
  473. /// Cancel current targeting operation
  474. /// </summary>
  475. public void CancelTargeting()
  476. {
  477. if (isDragging && selectedCharacter != null)
  478. {
  479. selectedCharacter.actionData.Reset();
  480. selectedCharacter.SetVisualState(ActionDecisionState.NoAction);
  481. targetingLine.StopTargeting();
  482. selectedCharacter = null;
  483. isDragging = false;
  484. Debug.Log("❌ Targeting cancelled");
  485. }
  486. }
  487. }