360 lines
11 KiB
C#
360 lines
11 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class BattleManager : MonoBehaviour
|
|
{
|
|
[Header("Battle Configuration")]
|
|
public bool battleStarted = false;
|
|
public float battleDuration = 300f; // 5 minutes max
|
|
public Transform playerSpawnArea;
|
|
public Transform enemySpawnArea;
|
|
|
|
[Header("Unit Management")]
|
|
public List<UnitController> playerUnits = new List<UnitController>();
|
|
public List<UnitController> enemyUnits = new List<UnitController>();
|
|
|
|
[Header("Strategy Presets")]
|
|
public BattleStrategy defaultPlayerStrategy;
|
|
public BattleStrategy defaultEnemyStrategy;
|
|
|
|
// Battle statistics
|
|
public int playerUnitsRemaining;
|
|
public int enemyUnitsRemaining;
|
|
public float battleTimer;
|
|
|
|
// Events
|
|
public System.Action<Team> OnBattleEnd;
|
|
public System.Action OnBattleStart;
|
|
|
|
private void Start()
|
|
{
|
|
InitializeBattle();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (battleStarted)
|
|
{
|
|
battleTimer += Time.deltaTime;
|
|
|
|
// Check win conditions
|
|
CheckBattleEnd();
|
|
|
|
// Auto-end battle after max duration
|
|
if (battleTimer >= battleDuration)
|
|
{
|
|
EndBattle(GetWinningTeam());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void InitializeBattle()
|
|
{
|
|
// Find all units in the scene
|
|
UnitController[] allUnits = FindObjectsByType<UnitController>(FindObjectsSortMode.None);
|
|
Debug.Log($"Found {allUnits.Length} units in the scene during initialization.");
|
|
|
|
foreach (UnitController unit in allUnits)
|
|
{
|
|
Debug.Log($"Processing unit: {unit.gameObject.name}, Team: {unit.team}");
|
|
|
|
if (unit.team == Team.Player)
|
|
{
|
|
playerUnits.Add(unit);
|
|
unit.OnUnitDeath += OnPlayerUnitDeath;
|
|
Debug.Log($"Added {unit.gameObject.name} to player units");
|
|
}
|
|
else if (unit.team == Team.Enemy)
|
|
{
|
|
enemyUnits.Add(unit);
|
|
unit.OnUnitDeath += OnEnemyUnitDeath;
|
|
Debug.Log($"Added {unit.gameObject.name} to enemy units");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Unit {unit.gameObject.name} has team set to {unit.team} - not Player or Enemy!");
|
|
}
|
|
}
|
|
|
|
playerUnitsRemaining = playerUnits.Count;
|
|
enemyUnitsRemaining = enemyUnits.Count;
|
|
|
|
Debug.Log($"Initialization complete: {playerUnitsRemaining} player units, {enemyUnitsRemaining} enemy units");
|
|
|
|
// Set default strategies if none assigned
|
|
if (defaultPlayerStrategy == null)
|
|
{
|
|
defaultPlayerStrategy = BattleStrategy.CreateHoldPositionStrategy();
|
|
Debug.Log("Created default player strategy: Hold Position");
|
|
}
|
|
if (defaultEnemyStrategy == null)
|
|
{
|
|
defaultEnemyStrategy = BattleStrategy.CreateChargeStrategy();
|
|
Debug.Log("Created default enemy strategy: Charge");
|
|
}
|
|
}
|
|
|
|
public void StartBattle()
|
|
{
|
|
if (battleStarted)
|
|
{
|
|
Debug.Log("Battle already started!");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"Starting battle with {playerUnits.Count} player units and {enemyUnits.Count} enemy units.");
|
|
|
|
battleStarted = true;
|
|
battleTimer = 0f;
|
|
|
|
// Apply strategies to units
|
|
ApplyStrategiesToUnits();
|
|
|
|
OnBattleStart?.Invoke();
|
|
|
|
Debug.Log("Battle Started! Player units: " + playerUnitsRemaining + ", Enemy units: " + enemyUnitsRemaining);
|
|
}
|
|
|
|
private void ApplyStrategiesToUnits()
|
|
{
|
|
Debug.Log($"Applying strategies to units. Default player strategy: {(defaultPlayerStrategy != null ? defaultPlayerStrategy.strategyName : "NULL")}, Default enemy strategy: {(defaultEnemyStrategy != null ? defaultEnemyStrategy.strategyName : "NULL")}");
|
|
|
|
// Apply strategies to player units
|
|
foreach (UnitController unit in playerUnits)
|
|
{
|
|
if (unit.assignedStrategy == null)
|
|
{
|
|
unit.SetStrategy(defaultPlayerStrategy);
|
|
Debug.Log($"Applied default player strategy to {unit.gameObject.name}");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"Unit {unit.gameObject.name} already has strategy: {unit.assignedStrategy.strategyName}");
|
|
}
|
|
}
|
|
|
|
// Apply strategies to enemy units
|
|
foreach (UnitController unit in enemyUnits)
|
|
{
|
|
if (unit.assignedStrategy == null)
|
|
{
|
|
unit.SetStrategy(defaultEnemyStrategy);
|
|
Debug.Log($"Applied default enemy strategy to {unit.gameObject.name}");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"Unit {unit.gameObject.name} already has strategy: {unit.assignedStrategy.strategyName}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetPlayerStrategy(BattleStrategy strategy)
|
|
{
|
|
defaultPlayerStrategy = strategy;
|
|
|
|
// Apply to existing units
|
|
foreach (UnitController unit in playerUnits)
|
|
{
|
|
if (unit != null && unit.gameObject.activeInHierarchy)
|
|
{
|
|
unit.SetStrategy(strategy);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetEnemyStrategy(BattleStrategy strategy)
|
|
{
|
|
defaultEnemyStrategy = strategy;
|
|
|
|
// Apply to existing units
|
|
foreach (UnitController unit in enemyUnits)
|
|
{
|
|
if (unit != null && unit.gameObject.activeInHierarchy)
|
|
{
|
|
unit.SetStrategy(strategy);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetUnitTypeStrategy(UnitType unitType, BattleStrategy strategy, Team team)
|
|
{
|
|
List<UnitController> targetUnits = (team == Team.Player) ? playerUnits : enemyUnits;
|
|
|
|
foreach (UnitController unit in targetUnits)
|
|
{
|
|
if (unit != null && unit.unitData != null && unit.unitData.unitType == unitType)
|
|
{
|
|
unit.SetStrategy(strategy);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register a newly spawned unit with the BattleManager
|
|
/// </summary>
|
|
public void RegisterUnit(UnitController unit)
|
|
{
|
|
if (unit == null)
|
|
{
|
|
Debug.LogError("Cannot register null unit!");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"Registering unit: {unit.gameObject.name}, Team: {unit.team}");
|
|
|
|
if (unit.team == Team.Player)
|
|
{
|
|
if (!playerUnits.Contains(unit))
|
|
{
|
|
playerUnits.Add(unit);
|
|
unit.OnUnitDeath += OnPlayerUnitDeath;
|
|
playerUnitsRemaining++;
|
|
|
|
// Apply current strategy if battle is active
|
|
if (battleStarted && defaultPlayerStrategy != null)
|
|
{
|
|
unit.SetStrategy(defaultPlayerStrategy);
|
|
}
|
|
|
|
Debug.Log($"Registered {unit.gameObject.name} to player units. Total: {playerUnits.Count}");
|
|
}
|
|
}
|
|
else if (unit.team == Team.Enemy)
|
|
{
|
|
if (!enemyUnits.Contains(unit))
|
|
{
|
|
enemyUnits.Add(unit);
|
|
unit.OnUnitDeath += OnEnemyUnitDeath;
|
|
enemyUnitsRemaining++;
|
|
|
|
// Apply current strategy if battle is active
|
|
if (battleStarted && defaultEnemyStrategy != null)
|
|
{
|
|
unit.SetStrategy(defaultEnemyStrategy);
|
|
}
|
|
|
|
Debug.Log($"Registered {unit.gameObject.name} to enemy units. Total: {enemyUnits.Count}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Unit {unit.gameObject.name} has team set to {unit.team} - not Player or Enemy!");
|
|
}
|
|
}
|
|
|
|
private void OnPlayerUnitDeath(UnitController unit)
|
|
{
|
|
playerUnitsRemaining--;
|
|
Debug.Log("Player unit died. Remaining: " + playerUnitsRemaining);
|
|
}
|
|
|
|
private void OnEnemyUnitDeath(UnitController unit)
|
|
{
|
|
enemyUnitsRemaining--;
|
|
Debug.Log("Enemy unit died. Remaining: " + enemyUnitsRemaining);
|
|
}
|
|
|
|
private void CheckBattleEnd()
|
|
{
|
|
if (playerUnitsRemaining <= 0)
|
|
{
|
|
EndBattle(Team.Enemy);
|
|
}
|
|
else if (enemyUnitsRemaining <= 0)
|
|
{
|
|
EndBattle(Team.Player);
|
|
}
|
|
}
|
|
|
|
private Team GetWinningTeam()
|
|
{
|
|
if (playerUnitsRemaining > enemyUnitsRemaining)
|
|
return Team.Player;
|
|
else if (enemyUnitsRemaining > playerUnitsRemaining)
|
|
return Team.Enemy;
|
|
else
|
|
return Team.Player; // Draw goes to player
|
|
}
|
|
|
|
private void EndBattle(Team winner)
|
|
{
|
|
if (!battleStarted) return;
|
|
|
|
battleStarted = false;
|
|
OnBattleEnd?.Invoke(winner);
|
|
|
|
Debug.Log("Battle Ended! Winner: " + winner);
|
|
Debug.Log("Final Scores - Player: " + playerUnitsRemaining + ", Enemy: " + enemyUnitsRemaining);
|
|
}
|
|
|
|
public void ResetBattle()
|
|
{
|
|
battleStarted = false;
|
|
battleTimer = 0f;
|
|
|
|
// Reset all units
|
|
foreach (UnitController unit in playerUnits)
|
|
{
|
|
if (unit != null)
|
|
{
|
|
unit.gameObject.SetActive(true);
|
|
unit.currentHealth = unit.unitData.health;
|
|
unit.currentState = UnitState.Idle;
|
|
}
|
|
}
|
|
|
|
foreach (UnitController unit in enemyUnits)
|
|
{
|
|
if (unit != null)
|
|
{
|
|
unit.gameObject.SetActive(true);
|
|
unit.currentHealth = unit.unitData.health;
|
|
unit.currentState = UnitState.Idle;
|
|
}
|
|
}
|
|
|
|
playerUnitsRemaining = playerUnits.Count;
|
|
enemyUnitsRemaining = enemyUnits.Count;
|
|
}
|
|
|
|
// Helper methods for UI
|
|
public List<BattleStrategy> GetPresetStrategies()
|
|
{
|
|
return new List<BattleStrategy>
|
|
{
|
|
BattleStrategy.CreateHoldPositionStrategy(),
|
|
BattleStrategy.CreateFlankStrategy(),
|
|
BattleStrategy.CreateChargeStrategy(),
|
|
BattleStrategy.CreateSpreadWideStrategy(),
|
|
BattleStrategy.CreateWaitForCloseStrategy()
|
|
};
|
|
}
|
|
|
|
public void AssignStrategyByText(string strategyText, Team team)
|
|
{
|
|
BattleStrategy strategy = ParseStrategyFromText(strategyText);
|
|
|
|
if (team == Team.Player)
|
|
SetPlayerStrategy(strategy);
|
|
else
|
|
SetEnemyStrategy(strategy);
|
|
}
|
|
|
|
private BattleStrategy ParseStrategyFromText(string text)
|
|
{
|
|
// Simple text parsing for strategy assignment
|
|
text = text.ToLower();
|
|
|
|
if (text.Contains("flank") || text.Contains("around"))
|
|
return BattleStrategy.CreateFlankStrategy();
|
|
else if (text.Contains("charge") || text.Contains("attack"))
|
|
return BattleStrategy.CreateChargeStrategy();
|
|
else if (text.Contains("spread") || text.Contains("wide"))
|
|
return BattleStrategy.CreateSpreadWideStrategy();
|
|
else if (text.Contains("wait") || text.Contains("close"))
|
|
return BattleStrategy.CreateWaitForCloseStrategy();
|
|
else
|
|
return BattleStrategy.CreateHoldPositionStrategy();
|
|
}
|
|
}
|