| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.AI;
- using UnityEngine.Rendering.UI;
- public class GameManager : MonoBehaviour
- {
- public static GameManager Instance { get; private set; }
- public List<GameObject> playerCharacters = new List<GameObject>();
- public List<GameObject> enemyCharacters = new List<GameObject>();
- [Header("Decision Controller")]
- public PlayerDecisionController playerDecisionController;
- private Dictionary<GameObject, GameObject> enemySelectedTargets = new Dictionary<GameObject, GameObject>();
- public bool isContinuousRunActive = false; // Indicates if the game is in continuous run mode
- private const string ENEMY_LAYER_NAME = "Enemies";
- private int enemyLayerMask;
- private bool isActionExecutionPhaseActive;
- private bool waitingForAdvanceInput = false;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- // Initialize PlayerDecisionController if not set
- if (playerDecisionController == null)
- {
- playerDecisionController = FindFirstObjectByType<PlayerDecisionController>();
- if (playerDecisionController == null)
- {
- GameObject pdcGO = new GameObject("PlayerDecisionController");
- playerDecisionController = pdcGO.AddComponent<PlayerDecisionController>();
- }
- }
- int enemyLayer = LayerMask.NameToLayer(ENEMY_LAYER_NAME);
- if (enemyLayer == -1)
- {
- Debug.LogError($"Layer '{ENEMY_LAYER_NAME}' not found. Please create it in Project Settings > Tags and Layers, and assign your enemy prefabs to it.");
- // Disable functionality or handle error appropriately
- }
- else
- {
- enemyLayerMask = 1 << enemyLayer;
- }
- }
- else
- {
- Destroy(gameObject);
- return; // Avoid further initialization if this is not the singleton instance
- }
- }
- internal void StartBattle(List<GameObject> placedPlayerCharacters, List<GameObject> placedEnemyCharacters)
- {
- playerCharacters = placedPlayerCharacters;
- enemyCharacters = placedEnemyCharacters;
- // Refresh PlayerDecisionController with current player characters
- if (playerDecisionController != null)
- {
- playerDecisionController.RefreshPlayerCharacters();
- playerDecisionController.SetEnabled(false); // Disable initially until decision phase
- }
- // Start the battle logic here
- EnemiesDecideAction();
- StartCoroutine(MainBattleLoopCoroutine());
- }
- private IEnumerator MainBattleLoopCoroutine()
- {
- while (true) // Main battle loop
- {
- // --- Player Decision Phase ---
- Time.timeScale = 1f; // Ensure normal time scale for player decision phase
- // Check for completed movements before turn starts
- CheckAndResetCompletedMovements();
- // Keep existing actions, only allow new decisions for characters with no action
- playerDecisionController.UpdateVisualStates(); // Update colors: pink for no action, green for incomplete
- playerDecisionController.SetEnabled(true);
- // Show lines for any existing incomplete actions
- playerDecisionController.ShowActiveActionLines();
- // Wait for all players to make their initial decisions (only needed at turn start)
- while (!playerDecisionController.AllCharactersHaveActions())
- {
- yield return null; // Wait for player input through PlayerDecisionController
- }
- // Disable input during execution phase and hide action lines
- playerDecisionController.SetEnabled(false);
- playerDecisionController.HideActiveActionLines();
- // --- Pause point after decision (if not in continuous run mode) ---
- if (!isContinuousRunActive)
- {
- PauseGame();
- yield return new WaitUntil(() => Time.timeScale > 0 || isContinuousRunActive);
- }
- if (Time.timeScale == 0 && isContinuousRunActive)
- {
- ResumeGame();
- }
- // --- Action execution phase ---
- // yield return StartCoroutine(ExecutePlayerActionsCoroutine());
- yield return StartCoroutine(ExecuteAllActionsSimultaneously());
- // TODO: add ement action execution here for a full turn
- if (BattleUIManager.Instance != null)
- {
- BattleUIManager.Instance.SetButtonsInteractable(true);
- }
- EnemiesDecideAction(); // Enemies decide their actions after players have executed their actions
- // --- Post-Actions: Loop or Pause ---
- if (isContinuousRunActive)
- {
- if (Time.timeScale > 0)
- {
- yield return new WaitForSeconds(0.5f); // Brief delay if running
- }
- }
- else
- {
- PauseGame();
- }
- // Check for battle end conditions using proper alive/dead detection
- List<GameObject> alivePlayers = GetAlivePlayers();
- List<GameObject> aliveEnemies = GetAliveEnemies();
- // Debug: Log current battle state
- Debug.Log($"🔍 Battle state check: {alivePlayers.Count} alive players, {aliveEnemies.Count} alive enemies");
- if (alivePlayers.Count == 0 || aliveEnemies.Count == 0)
- {
- Debug.Log("🏁 BATTLE END DETECTED!");
- PauseGame();
- if (BattleUIManager.Instance != null)
- {
- BattleUIManager.Instance.DisableBattleControls(); // Disable UI controls
- }
- // Handle battle end logic
- if (alivePlayers.Count == 0)
- {
- Debug.Log("💀 PLAYER DEFEAT");
- HandlePlayerDefeat(playerCharacters);
- }
- else if (aliveEnemies.Count == 0)
- {
- Debug.Log("🏆 PLAYER VICTORY");
- HandlePlayerVictory(enemyCharacters);
- }
- break; // Exit the loop if battle is over
- }
- }
- }
- public List<GameObject> GetAllCharacters()
- {
- return playerCharacters.Concat(enemyCharacters).ToList();
- }
- /// <summary>
- /// Get all alive player characters
- /// </summary>
- public List<GameObject> GetAlivePlayers()
- {
- var alive = playerCharacters
- .Where(p => p != null)
- .Where(p =>
- {
- var character = p.GetComponent<Character>();
- bool isAlive = character != null && !character.IsDead;
- Debug.Log($"🔍 Player {p.name}: Character={character != null}, IsDead={character?.IsDead}, Alive={isAlive}");
- return isAlive;
- })
- .ToList();
- Debug.Log($"🔍 Total alive players: {alive.Count}/{playerCharacters.Count}");
- return alive;
- }
- /// <summary>
- /// Get all alive enemy characters
- /// </summary>
- public List<GameObject> GetAliveEnemies()
- {
- var alive = enemyCharacters
- .Where(e => e != null)
- .Where(e =>
- {
- var character = e.GetComponent<Character>();
- bool isAlive = character != null && !character.IsDead;
- Debug.Log($"🔍 Enemy {e.name}: Character={character != null}, IsDead={character?.IsDead}, Alive={isAlive}");
- return isAlive;
- })
- .ToList();
- Debug.Log($"🔍 Total alive enemies: {alive.Count}/{enemyCharacters.Count}");
- return alive;
- }
- /// <summary>
- /// Handle player victory (all enemies defeated)
- /// </summary>
- private void HandlePlayerVictory(List<GameObject> defeatedEnemies)
- {
- Debug.Log("🏆 Players have won the battle!");
- // Find or create the post-battle loot system
- var lootSystem = FindFirstObjectByType<PostBattleLootSystem>();
- if (lootSystem == null)
- {
- // Create loot system if it doesn't exist
- GameObject lootSystemGO = new GameObject("PostBattleLootSystem");
- lootSystem = lootSystemGO.AddComponent<PostBattleLootSystem>();
- }
- // Initialize and start looting
- lootSystem.InitializeLootSystem(defeatedEnemies);
- lootSystem.OnLootingComplete += OnLootingComplete;
- lootSystem.StartLooting();
- }
- /// <summary>
- /// Handle player defeat (all players dead)
- /// </summary>
- private void HandlePlayerDefeat(List<GameObject> defeatedPlayers)
- {
- Debug.Log("💀 All players have been defeated!");
- // Find or create the game over screen
- var gameOverScreen = FindFirstObjectByType<GameOverScreen>();
- if (gameOverScreen == null)
- {
- // Create game over screen if it doesn't exist
- GameObject gameOverGO = new GameObject("GameOverScreen");
- gameOverScreen = gameOverGO.AddComponent<GameOverScreen>();
- }
- // Show game over screen with statistics
- gameOverScreen.ShowGameOver(defeatedPlayers);
- }
- /// <summary>
- /// Called when looting is complete, proceed to end battle
- /// </summary>
- private void OnLootingComplete()
- {
- Debug.Log("🏆 Battle and looting complete, returning to exploration");
- // Save any updated team data back to the game state
- SaveBattleResults();
- // Return to MapScene2 (exploration scene)
- StartCoroutine(ReturnToExplorationScene());
- }
- /// <summary>
- /// Save battle results and return to exploration
- /// </summary>
- private System.Collections.IEnumerator ReturnToExplorationScene()
- {
- // Give a brief moment for any final UI updates
- yield return new WaitForSeconds(0.5f);
- // Load the exploration scene
- try
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene("MapScene2");
- }
- catch (System.Exception e)
- {
- Debug.LogError($"Failed to load MapScene2: {e.Message}");
- // Fallback - try to find and use battle setup
- var battleSetup = FindFirstObjectByType<EnhancedBattleSetup>();
- if (battleSetup != null)
- {
- battleSetup.EndBattleSession(true); // Player victory
- }
- else
- {
- Debug.LogWarning("No scene transition method available");
- }
- }
- }
- /// <summary>
- /// Save battle results back to persistent game state
- /// </summary>
- private void SaveBattleResults()
- {
- // Update team data with any changes from battle (health, items, etc.)
- var alivePlayerCharacters = GetAlivePlayers();
- // You could save character states here if needed
- Debug.Log($"💾 Saved battle results for {alivePlayerCharacters.Count} surviving players");
- }
- #region Debug and Testing Methods
- /// <summary>
- /// [DEBUG] Force battle end for testing post-battle systems
- /// </summary>
- [ContextMenu("Test Battle Victory")]
- public void TestBattleVictory()
- {
- Debug.Log("🧪 [TEST] Forcing battle victory to test post-battle loot system");
- // Kill all enemies for testing
- foreach (var enemy in enemyCharacters)
- {
- if (enemy != null)
- {
- var character = enemy.GetComponent<Character>();
- if (character != null && !character.IsDead)
- {
- // Force enemy death for testing
- character.TakeDamage(1000);
- }
- }
- }
- // Manually trigger victory
- HandlePlayerVictory(enemyCharacters);
- }
- /// <summary>
- /// [DEBUG] Force battle defeat for testing game over
- /// </summary>
- [ContextMenu("Test Battle Defeat")]
- public void TestBattleDefeat()
- {
- Debug.Log("🧪 [TEST] Forcing battle defeat to test game over screen");
- // Kill all players for testing
- foreach (var player in playerCharacters)
- {
- if (player != null)
- {
- var character = player.GetComponent<Character>();
- if (character != null && !character.IsDead)
- {
- // Force player death for testing
- character.TakeDamage(1000);
- }
- }
- }
- // Manually trigger defeat
- HandlePlayerDefeat(playerCharacters);
- }
- #endregion
- private IEnumerator ExecuteAllActionsSimultaneously()
- {
- isActionExecutionPhaseActive = true;
- var allActions = new Dictionary<GameObject, GameObject>();
- var movementActions = new Dictionary<GameObject, Vector3>();
- // Collect player actions from Character components
- foreach (GameObject playerGO in playerCharacters)
- {
- Character character = playerGO.GetComponent<Character>();
- if (character != null && character.actionData.hasTarget)
- {
- if (character.actionData.targetEnemy != null)
- {
- allActions[playerGO] = character.actionData.targetEnemy;
- }
- else if (character.actionData.targetPosition != Vector3.zero)
- {
- movementActions[playerGO] = character.actionData.targetPosition;
- }
- }
- }
- foreach (var action in enemySelectedTargets)
- {
- allActions[action.Key] = action.Value;
- }
- int playerActionsCount = allActions.Count - enemySelectedTargets.Count + movementActions.Count;
- enemySelectedTargets.Clear(); // Clear enemy targets as well
- if (allActions.Count == 0 && movementActions.Count == 0)
- {
- isActionExecutionPhaseActive = false;
- yield break;
- }
- // Configure All NavMeshAgents before starting movement
- foreach (var action in allActions)
- {
- GameObject actor = action.Key;
- if (actor == null)
- {
- continue;
- }
- Character character = actor.GetComponent<Character>();
- float attackRange = character != null ? character.GetAttackRange() : 2.0f;
- NavMeshAgent agent = actor.GetComponent<NavMeshAgent>();
- if (agent != null)
- {
- // For melee weapons (range <= 2), get much closer. For ranged, stay back more.
- float stoppingDistance = attackRange <= 2 ? 0.2f : Mathf.Max(0.5f, attackRange - 1.0f);
- agent.stoppingDistance = stoppingDistance;
- //
- agent.obstacleAvoidanceType = ObstacleAvoidanceType.HighQualityObstacleAvoidance;
- agent.avoidancePriority = UnityEngine.Random.Range(0, 100);
- // Start movement
- GameObject target = action.Value;
- if (target != null)
- {
- agent.SetDestination(target.transform.position);
- agent.isStopped = false;
- }
- }
- else
- {
- Debug.LogWarning($"NavMeshAgent not found or disabled on {actor.name}. Cannot move towards target.");
- }
- }
- // Handle pure movement actions (no enemy target)
- foreach (var movement in movementActions)
- {
- GameObject playerGO = movement.Key;
- Vector3 targetPosition = movement.Value;
- NavMeshAgent agent = playerGO.GetComponent<NavMeshAgent>();
- if (agent != null)
- {
- agent.SetDestination(targetPosition);
- agent.isStopped = false;
- }
- }
- // handle movement duration based on mode
- //
- if (allActions.Count > 0 || movementActions.Count > 0)
- {
- yield return StartCoroutine(MovementPhaseWithTargetTracking(allActions, movementActions, 1.0f));
- }
- // else
- // {
- // // If there are only attacks (no movement), still wait for 1 second
- // yield return new WaitForSeconds(1.0f);
- // }
- if (!isContinuousRunActive)
- {
- //
- //
- // Check for completed movement actions before showing lines
- CheckAndResetCompletedMovements();
- // Enable input and show lines for actions during pause - allow re-picking actions
- playerDecisionController.UpdateVisualStates(); // Update colors
- playerDecisionController.SetEnabled(true); // Allow action changes
- playerDecisionController.ShowActiveActionLines();
- PauseGame();
- waitingForAdvanceInput = true;
- yield return new WaitUntil(() => !waitingForAdvanceInput);
- // Disable input when advancing
- playerDecisionController.SetEnabled(false);
- ResumeGame();
- yield return new WaitForSeconds(1.0f);
- }
- foreach (var action in allActions)
- {
- GameObject actor = action.Key;
- GameObject target = action.Value;
- if (actor == null || target == null) continue;
- Character character = actor.GetComponent<Character>();
- float attackRange = character != null ? character.GetAttackRange() : 2.0f;
- float distance = Vector3.Distance(actor.transform.position, target.transform.position);
- if (distance <= attackRange)
- {
- // Execute the character's attack action
- character.ExecuteAction();
- }
- else
- {
- }
- }
- // Check movement completion and reset action if reached (movement actions are one-time)
- foreach (var movement in movementActions)
- {
- GameObject actor = movement.Key;
- Vector3 targetPosition = movement.Value;
- if (actor == null) continue;
- Character character = actor.GetComponent<Character>();
- if (character != null)
- {
- // Use horizontal distance only, ignore Y differences
- Vector3 actorPos = actor.transform.position;
- Vector2 actorPos2D = new Vector2(actorPos.x, actorPos.z);
- Vector2 targetPos2D = new Vector2(targetPosition.x, targetPosition.z);
- float horizontalDistance = Vector2.Distance(actorPos2D, targetPos2D);
- if (horizontalDistance <= 1.2f) // Movement completion threshold
- {
- character.actionData.Reset();
- character.SetVisualState(ActionDecisionState.NoAction);
- }
- }
- }
- isActionExecutionPhaseActive = false;
- BattleUIManager.Instance.SetButtonsInteractable(true);
- }
- private IEnumerator MovementPhaseWithTargetTracking(Dictionary<GameObject, GameObject> allActions, Dictionary<GameObject, Vector3> movementActions, float movementDuration)
- {
- float elapsedTime = 0f;
- // Store witch agents are still moving (not in attack range)
- Dictionary<NavMeshAgent, GameObject> activeMovements = new Dictionary<NavMeshAgent, GameObject>();
- Dictionary<NavMeshAgent, float> agentAttackRanges = new Dictionary<NavMeshAgent, float>();
- // Initialize active movements for attack actions
- foreach (var action in allActions)
- {
- GameObject actor = action.Key;
- GameObject target = action.Value;
- if (actor == null || target == null) continue;
- NavMeshAgent agent = actor.GetComponent<NavMeshAgent>();
- if (agent != null && agent.enabled)
- {
- // Get the character's attack range
- Character character = actor.GetComponent<Character>();
- float attackRange = character != null ? character.GetAttackRange() : 2.0f;
- // Configure agent for proper pathfinding around obstacles
- agent.obstacleAvoidanceType = ObstacleAvoidanceType.HighQualityObstacleAvoidance;
- agent.avoidancePriority = UnityEngine.Random.Range(0, 100); // Random priority to avoid clustering
- activeMovements[agent] = target;
- agentAttackRanges[agent] = attackRange;
- }
- }
- // Initialize active movements for pure movement actions
- foreach (var movement in movementActions)
- {
- GameObject actor = movement.Key;
- Vector3 targetPosition = movement.Value;
- if (actor == null) continue;
- NavMeshAgent agent = actor.GetComponent<NavMeshAgent>();
- if (agent != null && agent.enabled)
- {
- // For movement actions, use a small stopping distance to get closer
- float arrivalRange = 1.2f;
- // Configure agent for proper pathfinding around obstacles
- agent.obstacleAvoidanceType = ObstacleAvoidanceType.HighQualityObstacleAvoidance;
- agent.avoidancePriority = UnityEngine.Random.Range(0, 100);
- agent.stoppingDistance = 0.1f; // Get very close to the target
- // Create a dummy target GameObject at the position for consistency with the tracking system
- GameObject dummyTarget = new GameObject("MovementTarget");
- dummyTarget.transform.position = targetPosition;
- activeMovements[agent] = dummyTarget;
- agentAttackRanges[agent] = arrivalRange;
- }
- }
- while (elapsedTime < movementDuration && Time.timeScale > 0 && activeMovements.Count > 0)
- {
- var agentsToRemove = new List<NavMeshAgent>();
- foreach (var movement in activeMovements)
- {
- NavMeshAgent agent = movement.Key;
- GameObject target = movement.Value;
- float attackRange = agentAttackRanges[agent];
- if (agent == null || target == null || agent.gameObject == null)
- {
- agentsToRemove.Add(agent);
- continue;
- }
- float distanceToTarget = Vector3.Distance(agent.transform.position, target.transform.position);
- if (distanceToTarget <= attackRange)
- {
- agent.isStopped = true;
- agent.velocity = Vector3.zero;
- agentsToRemove.Add(agent);
- // If this is a movement target (dummy), check if we should reset the action
- if (target.name == "MovementTarget")
- {
- Character character = agent.GetComponent<Character>();
- if (character != null && character.actionData.targetPosition != Vector3.zero)
- {
- character.actionData.Reset();
- character.SetVisualState(ActionDecisionState.NoAction);
- }
- }
- }
- else if (!agent.isStopped)
- {
- // For movement targets, go directly to the position
- if (target.name == "MovementTarget")
- {
- agent.SetDestination(target.transform.position);
- // Check if agent has reached destination or is very close but stopped moving
- if (agent.pathStatus == NavMeshPathStatus.PathComplete &&
- (!agent.hasPath || agent.remainingDistance < 0.1f))
- {
- Character character = agent.GetComponent<Character>();
- if (character != null)
- {
- Vector3 actorPos = agent.transform.position;
- Vector3 targetPos = character.actionData.targetPosition;
- Vector2 actorPos2D = new Vector2(actorPos.x, actorPos.z);
- Vector2 targetPos2D = new Vector2(targetPos.x, targetPos.z);
- float horizontalDistance = Vector2.Distance(actorPos2D, targetPos2D);
- if (horizontalDistance <= 1.2f)
- {
- agent.isStopped = true;
- agentsToRemove.Add(agent);
- character.actionData.Reset();
- character.SetVisualState(ActionDecisionState.NoAction);
- }
- }
- }
- }
- else
- {
- // Update destination to track moving target, but stop just outside attack range
- Vector3 directionToTarget = (target.transform.position - agent.transform.position).normalized;
- Vector3 stoppingPosition = target.transform.position - directionToTarget * (attackRange * 0.9f);
- agent.SetDestination(stoppingPosition);
- }
- // Check if agent is stuck or can't reach destination
- if (agent.pathStatus == NavMeshPathStatus.PathInvalid)
- {
- Debug.LogWarning($"{agent.gameObject.name} cannot find path to {target.name}");
- agent.SetDestination(target.transform.position);
- }
- }
- }
- foreach (var agent in agentsToRemove)
- {
- activeMovements.Remove(agent);
- agentAttackRanges.Remove(agent);
- }
- yield return null; // wait one frame
- elapsedTime += Time.deltaTime;
- }
- // Stop any remaining moving agents and clean up dummy targets
- foreach (var movement in activeMovements)
- {
- if (movement.Key != null && movement.Key.enabled)
- {
- movement.Key.isStopped = true;
- movement.Key.velocity = Vector3.zero;
- }
- // Clean up dummy movement targets
- if (movement.Value != null && movement.Value.name == "MovementTarget")
- {
- Destroy(movement.Value);
- }
- }
- }
- public void AdvanceAction()
- {
- // Hide action lines when advancing
- if (playerDecisionController != null)
- {
- playerDecisionController.HideActiveActionLines();
- }
- if (waitingForAdvanceInput)
- {
- waitingForAdvanceInput = false;
- }
- else if (Time.timeScale == 0 && !isActionExecutionPhaseActive)
- {
- ResumeGame();
- }
- else
- {
- }
- }
- public void PauseGame()
- {
- Time.timeScale = 0f; // Pause the game
- BattleUIManager.Instance.SetButtonsInteractable(true);
- }
- public void ResumeGame()
- {
- Time.timeScale = 1f; // Resume the game
- }
- private void EnemiesDecideAction()
- {
- enemySelectedTargets.Clear();
- foreach (var enemy in enemyCharacters)
- {
- if (enemy == null)
- {
- Debug.LogWarning("Found null enemy in enemyCharacters list");
- continue;
- }
- if (playerCharacters.Count == 0)
- {
- Debug.LogWarning("No player characters available for enemies to target");
- continue;
- }
- // Logic for enemy action decision
- GameObject closestPlayer = null;
- float minDistance = float.MaxValue;
- List<GameObject> alivePlayers = playerCharacters.Where(player => !player.GetComponent<Character>().IsDead).ToList();
- foreach (var player in alivePlayers)
- {
- if (player == null) continue; // Safety check
- float distance = Vector3.Distance(enemy.transform.position, player.transform.position);
- if (distance < minDistance)
- {
- minDistance = distance;
- closestPlayer = player;
- }
- }
- if (closestPlayer != null)
- {
- // Set the enemy's action data to attack the target
- Character enemyCharacter = enemy.GetComponent<Character>();
- if (enemyCharacter != null)
- {
- enemyCharacter.actionData.SetAttackTarget(closestPlayer);
- }
- enemySelectedTargets[enemy] = closestPlayer; // Assign the closest player as the target
- }
- else
- {
- Debug.LogWarning($"{enemy.name} could not find a valid target");
- }
- enemy.GetComponent<Character>().SetVisualState(ActionDecisionState.EnemyAction);
- }
- }
- private void CheckAndResetCompletedMovements()
- {
- foreach (GameObject playerGO in playerCharacters)
- {
- Character character = playerGO.GetComponent<Character>();
- if (character != null && character.actionData.targetPosition != Vector3.zero && character.actionData.targetEnemy == null)
- {
- // This is a movement action, check if completed using horizontal distance only
- Vector3 playerPos = playerGO.transform.position;
- Vector3 targetPos = character.actionData.targetPosition;
- // Use only X and Z coordinates (ignore Y differences)
- Vector2 playerPos2D = new Vector2(playerPos.x, playerPos.z);
- Vector2 targetPos2D = new Vector2(targetPos.x, targetPos.z);
- float horizontalDistance = Vector2.Distance(playerPos2D, targetPos2D);
- if (horizontalDistance <= 1.2f) // Threshold for "close enough"
- {
- character.actionData.Reset();
- character.SetVisualState(ActionDecisionState.NoAction);
- }
- }
- }
- }
- }
|