1614 lines
59 KiB
C#
1614 lines
59 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace Guerra2D
|
||
{
|
||
public enum Team { Friendly, Enemy }
|
||
public enum GamePhase { FriendlyDeployment, EnemyDeployment, Battle, Victory }
|
||
|
||
public enum UnitSize
|
||
{
|
||
Section,
|
||
Platoon,
|
||
Company,
|
||
BattalionGroup,
|
||
Regiment,
|
||
Brigade,
|
||
Division
|
||
}
|
||
|
||
[Serializable]
|
||
public sealed class UnitDefinition
|
||
{
|
||
public string Id;
|
||
public string Name;
|
||
public string Abbreviation;
|
||
public string Description;
|
||
public int MaxHealth;
|
||
public int Movement;
|
||
public int Range;
|
||
public int Damage;
|
||
public int MaxAmmunition;
|
||
|
||
public UnitDefinition(string id, string name, string abbreviation, string description,
|
||
int maxHealth, int movement, int range, int damage, int maxAmmunition)
|
||
{
|
||
Id = id;
|
||
Name = name;
|
||
Abbreviation = abbreviation;
|
||
Description = description;
|
||
MaxHealth = maxHealth;
|
||
Movement = movement;
|
||
Range = range;
|
||
Damage = damage;
|
||
MaxAmmunition = maxAmmunition;
|
||
}
|
||
}
|
||
|
||
[Serializable]
|
||
public sealed class BattleUnit
|
||
{
|
||
public int Id;
|
||
public Team Team;
|
||
public UnitDefinition Definition;
|
||
public Vector2Int Position;
|
||
public int Health;
|
||
public bool Acted;
|
||
public UnitSize Size;
|
||
public int Ammunition;
|
||
|
||
public BattleUnit(
|
||
int id,
|
||
Team team,
|
||
UnitDefinition definition,
|
||
Vector2Int position,
|
||
UnitSize size)
|
||
{
|
||
Id = id;
|
||
Team = team;
|
||
Definition = definition;
|
||
Position = position;
|
||
Health = definition.MaxHealth;
|
||
Acted = false;
|
||
Size = size;
|
||
Ammunition = definition.MaxAmmunition;
|
||
}
|
||
}
|
||
|
||
public sealed class MilitaryWarGame : MonoBehaviour
|
||
{
|
||
private const int DefaultColumns = 40;
|
||
private const int DefaultRows = 20;
|
||
private const int MinColumns = 10;
|
||
private const int MaxColumns = 80;
|
||
private const int MinRows = 8;
|
||
private const int MaxRows = 40;
|
||
private const int MaxUnitsPerSide = 20;
|
||
|
||
private int columns = DefaultColumns;
|
||
private int rows = DefaultRows;
|
||
|
||
private readonly List<UnitDefinition> definitions = new List<UnitDefinition>();
|
||
private readonly List<BattleUnit> units = new List<BattleUnit>();
|
||
private readonly Dictionary<string, Texture2D> symbolCache = new Dictionary<string, Texture2D>();
|
||
|
||
private GamePhase phase = GamePhase.FriendlyDeployment;
|
||
private Team activeTeam = Team.Friendly;
|
||
private UnitDefinition selectedDefinition;
|
||
private BattleUnit selectedUnit;
|
||
private int nextUnitId = 1;
|
||
private int turnNumber = 1;
|
||
private string status = "Jogador 1: escolha uma unidade e coloque-a na zona azul.";
|
||
private Vector2 paletteScroll;
|
||
private Vector2 piecesMenuScroll;
|
||
private bool piecesMenuOpen = false;
|
||
private bool battlefieldSizeMenuOpen = false;
|
||
private string columnsInput = DefaultColumns.ToString();
|
||
private string rowsInput = DefaultRows.ToString();
|
||
|
||
private string healthInput = "";
|
||
private string movementInput = "";
|
||
private string rangeInput = "";
|
||
private string damageInput = "";
|
||
private string ammunitionInput = "";
|
||
private int sizeInputIndex = 0;
|
||
|
||
private bool ammunitionTransferMode = false;
|
||
private string transferAmountInput = "1";
|
||
|
||
private BattleUnit infoUnit;
|
||
private bool unitInfoOpen = false;
|
||
private Rect unitInfoRect = new Rect(0f, 0f, 285f, 255f);
|
||
|
||
private GUIStyle titleStyle;
|
||
private GUIStyle headingStyle;
|
||
private GUIStyle bodyStyle;
|
||
private GUIStyle smallStyle;
|
||
private GUIStyle centeredStyle;
|
||
private GUIStyle cellLabelStyle;
|
||
private GUIStyle unitSizeStyle;
|
||
private GUIStyle overlayTitleStyle;
|
||
private Texture2D whiteTexture;
|
||
|
||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
|
||
private static void Bootstrap()
|
||
{
|
||
if (FindFirstObjectByType<MilitaryWarGame>() != null)
|
||
return;
|
||
|
||
GameObject game = new GameObject("Guerra 2D - Controlador");
|
||
game.AddComponent<MilitaryWarGame>();
|
||
DontDestroyOnLoad(game);
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
BuildDefinitions();
|
||
selectedDefinition = definitions[0];
|
||
sizeInputIndex = (int)UnitSize.Section;
|
||
LoadDefinitionEditor(selectedDefinition);
|
||
whiteTexture = MakeSolidTexture(Color.white);
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
foreach (Texture2D texture in symbolCache.Values)
|
||
{
|
||
if (texture != null)
|
||
Destroy(texture);
|
||
}
|
||
|
||
if (whiteTexture != null)
|
||
Destroy(whiteTexture);
|
||
}
|
||
|
||
private void BuildDefinitions()
|
||
{
|
||
definitions.Clear();
|
||
definitions.Add(new UnitDefinition("infantry", "Infantaria", "INF", "", 10, 3, 1, 4, 8));
|
||
definitions.Add(new UnitDefinition("armor", "Carros de Combate", "BLD", "", 16, 4, 1, 6, 6));
|
||
definitions.Add(new UnitDefinition("artillery", "Artilharia", "ART", "", 8, 2, 5, 6, 5));
|
||
definitions.Add(new UnitDefinition("recon", "Reconhecimento", "REC", "", 8, 6, 2, 3, 7));
|
||
definitions.Add(new UnitDefinition("engineer", "Engenharia", "ENG", "", 11, 3, 1, 4, 6));
|
||
definitions.Add(new UnitDefinition("airdef", "Defesa Aérea", "DA", "", 9, 3, 4, 5, 5));
|
||
definitions.Add(new UnitDefinition("hq", "Posto de Comando", "QG", "", 7, 2, 1, 2, 4));
|
||
definitions.Add(new UnitDefinition("logistics", "Logística", "LOG", "", 8, 3, 1, 2, 20));
|
||
definitions.Add(new UnitDefinition("medical", "Saúde", "MED", "", 7, 3, 1, 1, 3));
|
||
definitions.Add(new UnitDefinition("signals", "Transmissões", "TRS", "", 7, 3, 2, 2, 4));
|
||
}
|
||
|
||
private void EnsureStyles()
|
||
{
|
||
if (titleStyle != null)
|
||
return;
|
||
|
||
titleStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = 22,
|
||
fontStyle = FontStyle.Bold,
|
||
alignment = TextAnchor.MiddleLeft,
|
||
normal = { textColor = new Color(0.93f, 0.95f, 0.98f) }
|
||
};
|
||
headingStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = 15,
|
||
fontStyle = FontStyle.Bold,
|
||
normal = { textColor = new Color(0.92f, 0.94f, 0.97f) }
|
||
};
|
||
bodyStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = 13,
|
||
wordWrap = true,
|
||
normal = { textColor = new Color(0.87f, 0.89f, 0.92f) }
|
||
};
|
||
smallStyle = new GUIStyle(bodyStyle) { fontSize = 11 };
|
||
centeredStyle = new GUIStyle(bodyStyle) { alignment = TextAnchor.MiddleCenter };
|
||
cellLabelStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = 10,
|
||
fontStyle = FontStyle.Bold,
|
||
alignment = TextAnchor.LowerCenter,
|
||
normal = { textColor = Color.white }
|
||
};
|
||
unitSizeStyle = new GUIStyle(GUI.skin.label)
|
||
{
|
||
fontSize = 11,
|
||
fontStyle = FontStyle.Bold,
|
||
alignment = TextAnchor.UpperCenter,
|
||
normal = { textColor = new Color(0.03f, 0.04f, 0.05f) }
|
||
};
|
||
overlayTitleStyle = new GUIStyle(titleStyle)
|
||
{
|
||
fontSize = 28,
|
||
alignment = TextAnchor.MiddleCenter
|
||
};
|
||
}
|
||
|
||
private void OnGUI()
|
||
{
|
||
EnsureStyles();
|
||
|
||
Event currentEvent = Event.current;
|
||
|
||
if (unitInfoOpen &&
|
||
currentEvent.type == EventType.MouseDown)
|
||
{
|
||
CloseUnitInfo();
|
||
}
|
||
|
||
DrawBackground();
|
||
|
||
bool deployment = phase == GamePhase.FriendlyDeployment || phase == GamePhase.EnemyDeployment;
|
||
bool expandedMenu = deployment && (piecesMenuOpen || battlefieldSizeMenuOpen);
|
||
float panelWidth = expandedMenu
|
||
? Mathf.Clamp(Screen.width * 0.24f, 300f, 380f)
|
||
: Mathf.Clamp(Screen.width * 0.17f, 225f, 285f);
|
||
|
||
Rect panelRect = new Rect(14f, 14f, panelWidth, Screen.height - 28f);
|
||
Rect boardArea = new Rect(panelRect.xMax + 14f, 14f, Screen.width - panelRect.width - 42f, Screen.height - 28f);
|
||
|
||
DrawPanel(panelRect);
|
||
DrawBoard(boardArea);
|
||
|
||
if (unitInfoOpen && infoUnit != null)
|
||
DrawUnitInfoPopup();
|
||
}
|
||
|
||
private void DrawBackground()
|
||
{
|
||
Color old = GUI.color;
|
||
GUI.color = new Color(0.055f, 0.07f, 0.09f);
|
||
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), whiteTexture);
|
||
GUI.color = old;
|
||
}
|
||
|
||
private void DrawPanel(Rect rect)
|
||
{
|
||
DrawFilledRect(rect, new Color(0.09f, 0.115f, 0.14f));
|
||
GUI.Box(rect, GUIContent.none);
|
||
|
||
GUILayout.BeginArea(new Rect(rect.x + 14f, rect.y + 12f, rect.width - 28f, rect.height - 24f));
|
||
GUILayout.Label("GUERRA 2D", titleStyle, GUILayout.Height(30));
|
||
GUILayout.Label(GetPhaseTitle(), headingStyle);
|
||
GUILayout.Space(5);
|
||
GUILayout.Label(status, bodyStyle, GUILayout.Height(48));
|
||
|
||
GUILayout.Space(4);
|
||
DrawTeamSummary();
|
||
GUILayout.Space(8);
|
||
|
||
if (phase == GamePhase.FriendlyDeployment || phase == GamePhase.EnemyDeployment)
|
||
DrawDeploymentPanel();
|
||
else if (phase == GamePhase.Battle)
|
||
DrawBattlePanel();
|
||
else
|
||
DrawVictoryPanel();
|
||
|
||
GUILayout.EndArea();
|
||
}
|
||
|
||
private string GetPhaseTitle()
|
||
{
|
||
return phase switch
|
||
{
|
||
GamePhase.FriendlyDeployment => "Colocação — Nossas Forças",
|
||
GamePhase.EnemyDeployment => "Colocação — Inimigo",
|
||
GamePhase.Battle => $"Batalha — Turno {turnNumber}: {TeamName(activeTeam)}",
|
||
GamePhase.Victory => "Fim da batalha",
|
||
_ => string.Empty
|
||
};
|
||
}
|
||
|
||
private void DrawTeamSummary()
|
||
{
|
||
int friendly = CountUnits(Team.Friendly);
|
||
int enemy = CountUnits(Team.Enemy);
|
||
GUILayout.BeginHorizontal();
|
||
GUI.color = FriendlyColor();
|
||
GUILayout.Box($"NOSSAS FORÇAS\n{friendly}/{MaxUnitsPerSide}", GUILayout.Height(44));
|
||
GUI.color = EnemyColor();
|
||
GUILayout.Box($"INIMIGO\n{enemy}/{MaxUnitsPerSide}", GUILayout.Height(44));
|
||
GUI.color = Color.white;
|
||
GUILayout.EndHorizontal();
|
||
}
|
||
|
||
private void DrawDeploymentPanel()
|
||
{
|
||
DrawBattlefieldSizeMenu();
|
||
GUILayout.Space(6);
|
||
|
||
string toggleLabel = piecesMenuOpen ? "PEÇAS ▲" : "PEÇAS ▼";
|
||
Color previousBackground = GUI.backgroundColor;
|
||
GUI.backgroundColor = piecesMenuOpen
|
||
? new Color(0.36f, 0.63f, 0.88f)
|
||
: new Color(0.78f, 0.82f, 0.86f);
|
||
|
||
bool newMenuState = GUILayout.Toggle(
|
||
piecesMenuOpen,
|
||
toggleLabel,
|
||
GUI.skin.button,
|
||
GUILayout.Height(42)
|
||
);
|
||
|
||
GUI.backgroundColor = previousBackground;
|
||
|
||
if (newMenuState != piecesMenuOpen)
|
||
{
|
||
piecesMenuOpen = newMenuState;
|
||
if (piecesMenuOpen)
|
||
{
|
||
battlefieldSizeMenuOpen = false;
|
||
piecesMenuScroll = Vector2.zero;
|
||
}
|
||
|
||
selectedUnit = null;
|
||
|
||
status = piecesMenuOpen
|
||
? "Menu de peças aberto. Selecione um símbolo militar."
|
||
: "Menu de peças fechado. Carregue em PEÇAS para voltar a abrir.";
|
||
}
|
||
|
||
if (piecesMenuOpen)
|
||
{
|
||
GUILayout.Space(6);
|
||
|
||
float completeMenuHeight = Mathf.Clamp(
|
||
Screen.height * 0.58f,
|
||
300f,
|
||
620f
|
||
);
|
||
|
||
piecesMenuScroll = GUILayout.BeginScrollView(
|
||
piecesMenuScroll,
|
||
GUI.skin.box,
|
||
GUILayout.Height(completeMenuHeight)
|
||
);
|
||
|
||
GUILayout.Label("Galeria de unidades", headingStyle);
|
||
GUILayout.Label(
|
||
"Selecione um símbolo, edite os atributos e depois clique numa célula da sua zona de colocação.",
|
||
smallStyle,
|
||
GUILayout.Height(38)
|
||
);
|
||
|
||
DrawSelectedDefinitionEditor();
|
||
GUILayout.Space(8);
|
||
|
||
GUILayout.Label("Tipos de unidade", headingStyle);
|
||
|
||
foreach (UnitDefinition definition in definitions)
|
||
{
|
||
bool selected = selectedDefinition == definition;
|
||
Color previous = GUI.backgroundColor;
|
||
GUI.backgroundColor = selected
|
||
? new Color(0.75f, 0.82f, 0.43f)
|
||
: Color.white;
|
||
|
||
GUILayout.BeginHorizontal(GUI.skin.box, GUILayout.Height(70));
|
||
Texture2D symbol = GetSymbolTexture(activeTeam, definition.Id);
|
||
GUILayout.Label(symbol, GUILayout.Width(58), GUILayout.Height(58));
|
||
GUILayout.BeginVertical();
|
||
|
||
if (GUILayout.Button(definition.Name, GUILayout.Height(25)))
|
||
{
|
||
selectedDefinition = definition;
|
||
selectedUnit = null;
|
||
LoadDefinitionEditor(definition);
|
||
status = $"{definition.Name} selecionada. Pode editar os atributos ou colocá-la no campo.";
|
||
}
|
||
|
||
GUILayout.Label(
|
||
$"PV {definition.MaxHealth} MOV {definition.Movement} ALC {definition.Range} " +
|
||
$"DANO {definition.Damage} MUN {definition.MaxAmmunition}",
|
||
smallStyle
|
||
);
|
||
GUILayout.Label(
|
||
definition.Description,
|
||
smallStyle,
|
||
GUILayout.Height(27)
|
||
);
|
||
|
||
GUILayout.EndVertical();
|
||
GUILayout.EndHorizontal();
|
||
|
||
GUI.backgroundColor = previous;
|
||
}
|
||
|
||
GUILayout.EndScrollView();
|
||
}
|
||
|
||
GUILayout.Space(8);
|
||
|
||
GUI.enabled = selectedUnit != null && selectedUnit.Team == activeTeam;
|
||
if (GUILayout.Button("Remover unidade selecionada", GUILayout.Height(32)))
|
||
RemoveSelectedUnit();
|
||
GUI.enabled = true;
|
||
|
||
int ownCount = CountUnits(activeTeam);
|
||
GUI.enabled = ownCount > 0;
|
||
string finishLabel = phase == GamePhase.FriendlyDeployment
|
||
? "Concluir colocação azul"
|
||
: "Concluir colocação vermelha e iniciar batalha";
|
||
|
||
if (GUILayout.Button(finishLabel, GUILayout.Height(42)))
|
||
{
|
||
piecesMenuOpen = false;
|
||
FinishDeployment();
|
||
}
|
||
|
||
GUI.enabled = true;
|
||
|
||
if (GUILayout.Button("Limpar a colocação deste jogador", GUILayout.Height(28)))
|
||
ClearTeam(activeTeam);
|
||
}
|
||
|
||
private void DrawSelectedDefinitionEditor()
|
||
{
|
||
if (selectedDefinition == null)
|
||
return;
|
||
|
||
Color previousBackground = GUI.backgroundColor;
|
||
GUI.backgroundColor = new Color(0.30f, 0.48f, 0.62f);
|
||
|
||
GUILayout.BeginVertical(GUI.skin.box);
|
||
GUILayout.Label($"EDITAR — {selectedDefinition.Name}", headingStyle);
|
||
|
||
DrawIntegerField("PV:", ref healthInput);
|
||
DrawIntegerField("Movimento:", ref movementInput);
|
||
DrawIntegerField("Alcance:", ref rangeInput);
|
||
DrawIntegerField("Dano:", ref damageInput);
|
||
DrawIntegerField("Munições:", ref ammunitionInput);
|
||
|
||
GUILayout.Space(4);
|
||
GUILayout.Label(
|
||
selectedUnit != null
|
||
? "Tamanho desta unidade colocada:"
|
||
: "Tamanho da próxima unidade:",
|
||
bodyStyle
|
||
);
|
||
|
||
string[] sizeOptions =
|
||
{
|
||
"•• Secção",
|
||
"••• Pelotão",
|
||
"I Companhia",
|
||
"II Batalhão/Grupo",
|
||
"III Regimento",
|
||
"X Brigada",
|
||
"XX Divisão"
|
||
};
|
||
|
||
sizeInputIndex = GUILayout.SelectionGrid(
|
||
sizeInputIndex,
|
||
sizeOptions,
|
||
1,
|
||
GUILayout.Height(7 * 27)
|
||
);
|
||
|
||
UnitSize previewSize = (UnitSize)Mathf.Clamp(
|
||
sizeInputIndex,
|
||
0,
|
||
Enum.GetValues(typeof(UnitSize)).Length - 1
|
||
);
|
||
|
||
GUILayout.Label(
|
||
$"Selecionado: {GetUnitSizeIndicator(previewSize)} — {GetUnitSizeName(previewSize)}",
|
||
smallStyle
|
||
);
|
||
|
||
GUILayout.Space(4);
|
||
|
||
if (GUILayout.Button("APLICAR ATRIBUTOS", GUILayout.Height(34)))
|
||
ApplyDefinitionEditor();
|
||
|
||
GUILayout.Label(
|
||
"Ex.: alcance 4 permite atacar até 4 casas de distância.",
|
||
smallStyle,
|
||
GUILayout.Height(28)
|
||
);
|
||
|
||
GUILayout.EndVertical();
|
||
GUI.backgroundColor = previousBackground;
|
||
}
|
||
|
||
private void DrawIntegerField(string label, ref string value)
|
||
{
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label(label, bodyStyle, GUILayout.Width(92));
|
||
value = GUILayout.TextField(value, 3, GUILayout.Height(27));
|
||
GUILayout.EndHorizontal();
|
||
}
|
||
|
||
private void LoadDefinitionEditor(UnitDefinition definition)
|
||
{
|
||
if (definition == null)
|
||
return;
|
||
|
||
healthInput = definition.MaxHealth.ToString();
|
||
movementInput = definition.Movement.ToString();
|
||
rangeInput = definition.Range.ToString();
|
||
damageInput = definition.Damage.ToString();
|
||
ammunitionInput = definition.MaxAmmunition.ToString();
|
||
}
|
||
|
||
private void ApplyDefinitionEditor()
|
||
{
|
||
if (selectedDefinition == null)
|
||
{
|
||
status = "Selecione primeiro um tipo de unidade.";
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(healthInput, out int health) ||
|
||
!int.TryParse(movementInput, out int movement) ||
|
||
!int.TryParse(rangeInput, out int range) ||
|
||
!int.TryParse(damageInput, out int damage) ||
|
||
!int.TryParse(ammunitionInput, out int ammunition))
|
||
{
|
||
status = "PV, movimento, alcance, dano e munições têm de ser números inteiros.";
|
||
return;
|
||
}
|
||
|
||
health = Mathf.Clamp(health, 1, 999);
|
||
movement = Mathf.Clamp(movement, 0, 99);
|
||
range = Mathf.Clamp(range, 1, 99);
|
||
damage = Mathf.Clamp(damage, 0, 999);
|
||
ammunition = Mathf.Clamp(ammunition, 0, 999);
|
||
|
||
selectedDefinition.MaxHealth = health;
|
||
selectedDefinition.Movement = movement;
|
||
selectedDefinition.Range = range;
|
||
selectedDefinition.Damage = damage;
|
||
selectedDefinition.MaxAmmunition = ammunition;
|
||
|
||
UnitSize chosenSize = (UnitSize)Mathf.Clamp(
|
||
sizeInputIndex,
|
||
0,
|
||
Enum.GetValues(typeof(UnitSize)).Length - 1
|
||
);
|
||
|
||
if (selectedUnit != null &&
|
||
selectedUnit.Team == activeTeam &&
|
||
selectedUnit.Definition == selectedDefinition)
|
||
{
|
||
selectedUnit.Size = chosenSize;
|
||
}
|
||
|
||
foreach (BattleUnit unit in units)
|
||
{
|
||
if (unit.Definition == selectedDefinition)
|
||
{
|
||
unit.Health = selectedDefinition.MaxHealth;
|
||
unit.Ammunition = Mathf.Min(
|
||
unit.Ammunition,
|
||
selectedDefinition.MaxAmmunition
|
||
);
|
||
}
|
||
}
|
||
|
||
LoadDefinitionEditor(selectedDefinition);
|
||
|
||
status =
|
||
selectedUnit != null
|
||
? $"{selectedDefinition.Name}: atributos atualizados e tamanho desta unidade definido como " +
|
||
$"{GetUnitSizeName(selectedUnit.Size)}."
|
||
: $"{selectedDefinition.Name}: PV {health}, MOV {movement}, ALC {range}, " +
|
||
$"DANO {damage}, MUN {ammunition}. " +
|
||
$"A próxima unidade será criada como {GetUnitSizeName(chosenSize)}.";
|
||
}
|
||
|
||
private void DrawBattlefieldSizeMenu()
|
||
{
|
||
string toggleLabel = battlefieldSizeMenuOpen
|
||
? "CAMPO DE BATALHA ▲"
|
||
: "CAMPO DE BATALHA ▼";
|
||
|
||
Color previousBackground = GUI.backgroundColor;
|
||
GUI.backgroundColor = battlefieldSizeMenuOpen
|
||
? new Color(0.36f, 0.63f, 0.88f)
|
||
: new Color(0.78f, 0.82f, 0.86f);
|
||
|
||
bool newMenuState = GUILayout.Toggle(
|
||
battlefieldSizeMenuOpen,
|
||
toggleLabel,
|
||
GUI.skin.button,
|
||
GUILayout.Height(42)
|
||
);
|
||
|
||
GUI.backgroundColor = previousBackground;
|
||
|
||
if (newMenuState != battlefieldSizeMenuOpen)
|
||
{
|
||
battlefieldSizeMenuOpen = newMenuState;
|
||
|
||
if (battlefieldSizeMenuOpen)
|
||
{
|
||
piecesMenuOpen = false;
|
||
columnsInput = columns.ToString();
|
||
rowsInput = rows.ToString();
|
||
status = "Introduza o número de colunas e linhas do campo de batalha.";
|
||
}
|
||
else
|
||
{
|
||
status = "Menu do tamanho do campo fechado.";
|
||
}
|
||
}
|
||
|
||
if (!battlefieldSizeMenuOpen)
|
||
return;
|
||
|
||
GUILayout.Space(6);
|
||
GUILayout.BeginVertical(GUI.skin.box);
|
||
GUILayout.Label("Configuração do campo", headingStyle);
|
||
GUILayout.Label(
|
||
$"Valores permitidos: {MinColumns}–{MaxColumns} colunas e {MinRows}–{MaxRows} linhas.",
|
||
smallStyle,
|
||
GUILayout.Height(28)
|
||
);
|
||
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label("Horizontal:", bodyStyle, GUILayout.Width(92));
|
||
columnsInput = GUILayout.TextField(columnsInput, 3, GUILayout.Height(28));
|
||
GUILayout.EndHorizontal();
|
||
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label("Vertical:", bodyStyle, GUILayout.Width(92));
|
||
rowsInput = GUILayout.TextField(rowsInput, 3, GUILayout.Height(28));
|
||
GUILayout.EndHorizontal();
|
||
|
||
GUILayout.Space(6);
|
||
|
||
if (GUILayout.Button("Aplicar tamanho", GUILayout.Height(36)))
|
||
ApplyBattlefieldSize();
|
||
|
||
if (GUILayout.Button("Repor 40 × 20", GUILayout.Height(30)))
|
||
{
|
||
columnsInput = DefaultColumns.ToString();
|
||
rowsInput = DefaultRows.ToString();
|
||
ApplyBattlefieldSize();
|
||
}
|
||
|
||
GUILayout.Label(
|
||
"Ao aplicar um novo tamanho, as peças já colocadas são removidas e a colocação recomeça pelas Nossas Forças.",
|
||
smallStyle,
|
||
GUILayout.Height(48)
|
||
);
|
||
|
||
GUILayout.EndVertical();
|
||
}
|
||
|
||
private void ApplyBattlefieldSize()
|
||
{
|
||
if (!int.TryParse(columnsInput, out int requestedColumns) ||
|
||
!int.TryParse(rowsInput, out int requestedRows))
|
||
{
|
||
status = "Introduza apenas números inteiros para o tamanho do campo.";
|
||
return;
|
||
}
|
||
|
||
requestedColumns = Mathf.Clamp(requestedColumns, MinColumns, MaxColumns);
|
||
requestedRows = Mathf.Clamp(requestedRows, MinRows, MaxRows);
|
||
|
||
columns = requestedColumns;
|
||
rows = requestedRows;
|
||
columnsInput = columns.ToString();
|
||
rowsInput = rows.ToString();
|
||
|
||
units.Clear();
|
||
nextUnitId = 1;
|
||
turnNumber = 1;
|
||
phase = GamePhase.FriendlyDeployment;
|
||
activeTeam = Team.Friendly;
|
||
selectedDefinition = definitions[0];
|
||
selectedUnit = null;
|
||
ammunitionTransferMode = false;
|
||
paletteScroll = Vector2.zero;
|
||
|
||
status =
|
||
$"Campo alterado para {columns} × {rows}. " +
|
||
$"A zona de colocação tem {GetDeploymentDepth()} colunas em cada extremidade.";
|
||
}
|
||
|
||
private void DrawBattlePanel()
|
||
{
|
||
GUILayout.Label("Comandos", headingStyle);
|
||
GUILayout.Label("As unidades das duas forças estão sempre visíveis. Selecione uma unidade da equipa ativa e depois mova ou ataque uma unidade adversária.", bodyStyle, GUILayout.Height(72));
|
||
|
||
if (selectedUnit != null)
|
||
{
|
||
GUILayout.BeginVertical(GUI.skin.box);
|
||
GUILayout.Label(selectedUnit.Definition.Name, headingStyle);
|
||
GUILayout.Label($"Equipa: {TeamName(selectedUnit.Team)}", bodyStyle);
|
||
GUILayout.Label($"Vida: {selectedUnit.Health}/{selectedUnit.Definition.MaxHealth}", bodyStyle);
|
||
GUILayout.Label($"Movimento: {selectedUnit.Definition.Movement} | Alcance: {selectedUnit.Definition.Range} | Dano: {selectedUnit.Definition.Damage}", bodyStyle);
|
||
GUILayout.Label(
|
||
$"Munições: {selectedUnit.Ammunition}/{selectedUnit.Definition.MaxAmmunition}",
|
||
bodyStyle
|
||
);
|
||
GUILayout.Label(
|
||
$"Tamanho: {GetUnitSizeIndicator(selectedUnit.Size)} — {GetUnitSizeName(selectedUnit.Size)}",
|
||
bodyStyle
|
||
);
|
||
GUILayout.Label(selectedUnit.Acted ? "Estado: já atuou neste turno" : "Estado: pronta", bodyStyle);
|
||
|
||
GUILayout.Space(6);
|
||
GUILayout.BeginHorizontal();
|
||
GUILayout.Label("Transferir:", bodyStyle, GUILayout.Width(75));
|
||
transferAmountInput = GUILayout.TextField(
|
||
transferAmountInput,
|
||
3,
|
||
GUILayout.Width(55),
|
||
GUILayout.Height(26)
|
||
);
|
||
GUILayout.Label("munições", smallStyle);
|
||
GUILayout.EndHorizontal();
|
||
|
||
GUI.enabled =
|
||
!selectedUnit.Acted &&
|
||
selectedUnit.Ammunition > 0;
|
||
|
||
if (GUILayout.Button(
|
||
ammunitionTransferMode
|
||
? "Cancelar transferência"
|
||
: "Transferir para unidade adjacente",
|
||
GUILayout.Height(32)))
|
||
{
|
||
ammunitionTransferMode = !ammunitionTransferMode;
|
||
status = ammunitionTransferMode
|
||
? "Clique numa unidade aliada adjacente para transferir munições."
|
||
: "Transferência de munições cancelada.";
|
||
}
|
||
|
||
GUI.enabled = true;
|
||
GUILayout.EndVertical();
|
||
}
|
||
else
|
||
{
|
||
GUILayout.Label("Nenhuma unidade selecionada.", centeredStyle, GUILayout.Height(70));
|
||
}
|
||
|
||
GUILayout.FlexibleSpace();
|
||
if (GUILayout.Button("Terminar turno", GUILayout.Height(46)))
|
||
EndTurn();
|
||
if (GUILayout.Button("Reiniciar partida", GUILayout.Height(30)))
|
||
ResetGame();
|
||
}
|
||
|
||
private void DrawVictoryPanel()
|
||
{
|
||
GUILayout.FlexibleSpace();
|
||
GUILayout.Label(status, overlayTitleStyle, GUILayout.Height(90));
|
||
GUILayout.Space(20);
|
||
if (GUILayout.Button("Nova partida", GUILayout.Height(50)))
|
||
ResetGame();
|
||
GUILayout.FlexibleSpace();
|
||
}
|
||
|
||
private void DrawBoard(Rect area)
|
||
{
|
||
float headerHeight = 54f;
|
||
float availableWidth = area.width - 20f;
|
||
float availableHeight = area.height - headerHeight - 20f;
|
||
float cellSize = Mathf.Floor(Mathf.Min(availableWidth / columns, availableHeight / rows));
|
||
cellSize = Mathf.Max(10f, cellSize);
|
||
float boardWidth = cellSize * columns;
|
||
float boardHeight = cellSize * rows;
|
||
float startX = area.x + Mathf.Max(10f, (area.width - boardWidth) * 0.5f);
|
||
float startY = area.y + headerHeight + Mathf.Max(6f, (availableHeight - boardHeight) * 0.5f);
|
||
|
||
Rect header = new Rect(area.x, area.y, area.width, 44f);
|
||
DrawFilledRect(header, new Color(0.09f, 0.115f, 0.14f));
|
||
GUI.Box(header, GUIContent.none);
|
||
GUI.Label(
|
||
new Rect(header.x + 14f, header.y + 3f, header.width - 28f, 22f),
|
||
$"CAMPO DE BATALHA — {columns} × {rows}",
|
||
headingStyle
|
||
);
|
||
GUI.Label(
|
||
new Rect(header.x + 14f, header.y + 24f, header.width - 28f, 18f),
|
||
$"{columns} colunas × {rows} linhas | Todas as unidades permanecem visíveis",
|
||
smallStyle
|
||
);
|
||
|
||
for (int row = 0; row < rows; row++)
|
||
{
|
||
for (int column = 0; column < columns; column++)
|
||
{
|
||
Vector2Int position = new Vector2Int(column, row);
|
||
Rect cellRect = new Rect(startX + column * cellSize, startY + row * cellSize, cellSize, cellSize);
|
||
DrawCell(cellRect, position, cellSize);
|
||
}
|
||
}
|
||
|
||
GUI.Label(new Rect(startX, startY + boardHeight + 2f, boardWidth, 18f),
|
||
"A T", smallStyle);
|
||
}
|
||
|
||
private void DrawCell(Rect rect, Vector2Int position, float cellSize)
|
||
{
|
||
BattleUnit unit = UnitAt(position);
|
||
Color cellColor = BaseCellColor(position);
|
||
|
||
if (phase == GamePhase.Battle && selectedUnit != null && selectedUnit.Team == activeTeam && !selectedUnit.Acted)
|
||
{
|
||
if (unit == null && IsLegalMove(selectedUnit, position))
|
||
cellColor = new Color(0.20f, 0.52f, 0.28f);
|
||
else if (unit != null && unit.Team != activeTeam && IsUnitVisibleToActivePlayer(unit) && IsLegalAttack(selectedUnit, unit))
|
||
cellColor = new Color(0.72f, 0.25f, 0.20f);
|
||
}
|
||
|
||
if (selectedUnit != null && selectedUnit.Position == position)
|
||
cellColor = new Color(0.78f, 0.66f, 0.20f);
|
||
|
||
DrawFilledRect(rect, cellColor);
|
||
GUI.Box(rect, GUIContent.none);
|
||
|
||
Event currentEvent = Event.current;
|
||
|
||
if (currentEvent.type == EventType.MouseDown &&
|
||
currentEvent.button == 1 &&
|
||
rect.Contains(currentEvent.mousePosition))
|
||
{
|
||
if (unit != null)
|
||
OpenUnitInfo(unit, currentEvent.mousePosition);
|
||
|
||
currentEvent.Use();
|
||
}
|
||
else if (GUI.Button(rect, GUIContent.none, GUIStyle.none))
|
||
{
|
||
HandleCellClick(position);
|
||
}
|
||
|
||
if (unit != null)
|
||
{
|
||
bool hideUnit = !IsUnitVisibleToActivePlayer(unit);
|
||
if (!hideUnit)
|
||
{
|
||
float iconPadding = Mathf.Max(2f, cellSize * 0.09f);
|
||
Rect iconRect = new Rect(rect.x + iconPadding, rect.y + iconPadding,
|
||
rect.width - iconPadding * 2f, rect.height - iconPadding * 2f - 10f);
|
||
GUI.DrawTexture(iconRect, GetSymbolTexture(unit.Team, unit.Definition.Id), ScaleMode.ScaleToFit, true);
|
||
|
||
float sizeLabelHeight = Mathf.Clamp(cellSize * 0.22f, 10f, 18f);
|
||
GUI.Label(
|
||
new Rect(iconRect.x, iconRect.y, iconRect.width, sizeLabelHeight),
|
||
GetUnitSizeIndicator(unit.Size),
|
||
unitSizeStyle
|
||
);
|
||
|
||
GUI.Label(new Rect(rect.x + 1f, rect.yMax - 14f, rect.width - 2f, 13f),
|
||
$"{unit.Health}PV | {unit.Ammunition}M", cellLabelStyle);
|
||
if (unit.Acted && phase == GamePhase.Battle)
|
||
{
|
||
DrawFilledRect(new Rect(rect.xMax - 9f, rect.y + 2f, 7f, 7f), new Color(0.25f, 0.25f, 0.25f));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void CloseUnitInfo()
|
||
{
|
||
unitInfoOpen = false;
|
||
infoUnit = null;
|
||
}
|
||
|
||
private void OpenUnitInfo(BattleUnit unit, Vector2 mousePosition)
|
||
{
|
||
infoUnit = unit;
|
||
unitInfoOpen = true;
|
||
|
||
float popupWidth = 285f;
|
||
float popupHeight = 255f;
|
||
float x = Mathf.Clamp(mousePosition.x + 12f, 8f, Screen.width - popupWidth - 8f);
|
||
float y = Mathf.Clamp(mousePosition.y + 12f, 8f, Screen.height - popupHeight - 8f);
|
||
unitInfoRect = new Rect(x, y, popupWidth, popupHeight);
|
||
}
|
||
|
||
private void DrawUnitInfoPopup()
|
||
{
|
||
if (infoUnit == null)
|
||
{
|
||
unitInfoOpen = false;
|
||
return;
|
||
}
|
||
|
||
DrawFilledRect(unitInfoRect, new Color(0.10f, 0.13f, 0.16f, 0.98f));
|
||
GUI.Box(unitInfoRect, GUIContent.none);
|
||
|
||
GUILayout.BeginArea(new Rect(
|
||
unitInfoRect.x + 12f,
|
||
unitInfoRect.y + 10f,
|
||
unitInfoRect.width - 24f,
|
||
unitInfoRect.height - 20f
|
||
));
|
||
|
||
GUILayout.Label(infoUnit.Definition.Name, headingStyle);
|
||
GUILayout.Label(
|
||
$"{infoUnit.Definition.Abbreviation} | {TeamName(infoUnit.Team)}",
|
||
smallStyle
|
||
);
|
||
|
||
GUILayout.Space(4);
|
||
GUILayout.Label(
|
||
$"Tamanho: {GetUnitSizeIndicator(infoUnit.Size)} — " +
|
||
$"{GetUnitSizeName(infoUnit.Size)}",
|
||
bodyStyle
|
||
);
|
||
GUILayout.Label(
|
||
$"Vida: {infoUnit.Health}/{infoUnit.Definition.MaxHealth}",
|
||
bodyStyle
|
||
);
|
||
GUILayout.Label($"Movimento: {infoUnit.Definition.Movement}", bodyStyle);
|
||
GUILayout.Label($"Alcance: {infoUnit.Definition.Range}", bodyStyle);
|
||
GUILayout.Label($"Dano: {infoUnit.Definition.Damage}", bodyStyle);
|
||
GUILayout.Label(
|
||
$"Munições: {infoUnit.Ammunition}/{infoUnit.Definition.MaxAmmunition}",
|
||
bodyStyle
|
||
);
|
||
|
||
if (!string.IsNullOrWhiteSpace(infoUnit.Definition.Description))
|
||
{
|
||
GUILayout.Space(4);
|
||
GUILayout.Label(infoUnit.Definition.Description, smallStyle);
|
||
}
|
||
|
||
GUILayout.EndArea();
|
||
}
|
||
|
||
private bool IsUnitVisibleToActivePlayer(BattleUnit unit)
|
||
{
|
||
// Nesta versão, todas as unidades são sempre visíveis durante
|
||
// a colocação e durante a batalha.
|
||
return unit != null;
|
||
}
|
||
|
||
private Color BaseCellColor(Vector2Int position)
|
||
{
|
||
bool alternate = (position.x + position.y) % 2 == 0;
|
||
int deploymentDepth = GetDeploymentDepth();
|
||
if (position.x < deploymentDepth)
|
||
return alternate ? new Color(0.14f, 0.27f, 0.38f) : new Color(0.12f, 0.23f, 0.33f);
|
||
if (position.x >= columns - deploymentDepth)
|
||
return alternate ? new Color(0.39f, 0.18f, 0.18f) : new Color(0.33f, 0.15f, 0.15f);
|
||
return alternate ? new Color(0.25f, 0.28f, 0.25f) : new Color(0.21f, 0.24f, 0.21f);
|
||
}
|
||
|
||
private void HandleCellClick(Vector2Int position)
|
||
{
|
||
if (phase == GamePhase.FriendlyDeployment || phase == GamePhase.EnemyDeployment)
|
||
HandleDeploymentClick(position);
|
||
else if (phase == GamePhase.Battle)
|
||
HandleBattleClick(position);
|
||
}
|
||
|
||
private void HandleDeploymentClick(Vector2Int position)
|
||
{
|
||
BattleUnit clicked = UnitAt(position);
|
||
|
||
if (clicked != null)
|
||
{
|
||
if (clicked.Team == activeTeam)
|
||
{
|
||
selectedUnit = clicked;
|
||
selectedDefinition = clicked.Definition;
|
||
LoadDefinitionEditor(clicked.Definition);
|
||
sizeInputIndex = (int)clicked.Size;
|
||
status = $"{clicked.Definition.Name} selecionada. Pode alterar o tamanho, reposicionar ou remover.";
|
||
}
|
||
else
|
||
{
|
||
status = "Esta unidade pertence ao outro jogador e não pode ser alterada.";
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (!IsDeploymentCell(activeTeam, position))
|
||
{
|
||
int deploymentDepth = GetDeploymentDepth();
|
||
status = activeTeam == Team.Friendly
|
||
? $"As Nossas Forças só podem ser colocadas nas primeiras {deploymentDepth} colunas azuis."
|
||
: $"O Inimigo só pode ser colocado nas últimas {deploymentDepth} colunas vermelhas.";
|
||
return;
|
||
}
|
||
|
||
if (selectedUnit != null && selectedUnit.Team == activeTeam)
|
||
{
|
||
selectedUnit.Position = position;
|
||
status = $"{selectedUnit.Definition.Name} reposicionada.";
|
||
selectedUnit = null;
|
||
return;
|
||
}
|
||
|
||
if (CountUnits(activeTeam) >= MaxUnitsPerSide)
|
||
{
|
||
status = $"Limite de {MaxUnitsPerSide} unidades atingido.";
|
||
return;
|
||
}
|
||
|
||
if (selectedDefinition == null)
|
||
{
|
||
status = "Escolha primeiro um tipo de unidade na galeria.";
|
||
return;
|
||
}
|
||
|
||
UnitSize newUnitSize = (UnitSize)Mathf.Clamp(
|
||
sizeInputIndex,
|
||
0,
|
||
Enum.GetValues(typeof(UnitSize)).Length - 1
|
||
);
|
||
|
||
units.Add(new BattleUnit(
|
||
nextUnitId++,
|
||
activeTeam,
|
||
selectedDefinition,
|
||
position,
|
||
newUnitSize
|
||
));
|
||
|
||
status =
|
||
$"{selectedDefinition.Name} ({GetUnitSizeName(newUnitSize)}) colocada. " +
|
||
"Pode escolher outro tamanho e continuar a adicionar unidades.";
|
||
}
|
||
|
||
private void HandleBattleClick(Vector2Int position)
|
||
{
|
||
BattleUnit clicked = UnitAt(position);
|
||
|
||
if (ammunitionTransferMode)
|
||
{
|
||
HandleAmmunitionTransfer(clicked);
|
||
return;
|
||
}
|
||
|
||
if (selectedUnit == null)
|
||
{
|
||
SelectBattleUnit(clicked);
|
||
return;
|
||
}
|
||
|
||
if (clicked != null && clicked.Team == activeTeam)
|
||
{
|
||
SelectBattleUnit(clicked);
|
||
return;
|
||
}
|
||
|
||
if (selectedUnit.Team != activeTeam || selectedUnit.Acted)
|
||
{
|
||
status = "Escolha uma unidade da equipa ativa que ainda não tenha atuado.";
|
||
selectedUnit = null;
|
||
return;
|
||
}
|
||
|
||
if (clicked == null)
|
||
{
|
||
if (IsLegalMove(selectedUnit, position))
|
||
{
|
||
selectedUnit.Position = position;
|
||
selectedUnit.Acted = true;
|
||
status = $"{selectedUnit.Definition.Name} movimentou-se e concluiu a sua ação.";
|
||
selectedUnit = null;
|
||
}
|
||
else
|
||
{
|
||
status = "Movimento inválido: destino ocupado ou fora do alcance de movimento.";
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (clicked.Team != activeTeam)
|
||
{
|
||
if (selectedUnit.Ammunition <= 0)
|
||
{
|
||
status = "Esta unidade não tem munições para atacar.";
|
||
return;
|
||
}
|
||
|
||
if (IsLegalAttack(selectedUnit, clicked))
|
||
{
|
||
clicked.Health -= selectedUnit.Definition.Damage;
|
||
selectedUnit.Ammunition--;
|
||
selectedUnit.Acted = true;
|
||
string attackerName = selectedUnit.Definition.Name;
|
||
if (clicked.Health <= 0)
|
||
{
|
||
string targetName = clicked.Definition.Name;
|
||
if (infoUnit == clicked)
|
||
{
|
||
CloseUnitInfo();
|
||
}
|
||
units.Remove(clicked);
|
||
status = $"{attackerName} destruiu {targetName}.";
|
||
}
|
||
else
|
||
{
|
||
status = $"{attackerName} atacou {clicked.Definition.Name}: {clicked.Health} PV restantes.";
|
||
}
|
||
selectedUnit = null;
|
||
CheckVictory();
|
||
}
|
||
else
|
||
{
|
||
status = "O alvo está fora do alcance desta unidade.";
|
||
}
|
||
}
|
||
}
|
||
|
||
private void HandleAmmunitionTransfer(BattleUnit target)
|
||
{
|
||
if (selectedUnit == null)
|
||
{
|
||
ammunitionTransferMode = false;
|
||
status = "Selecione primeiro a unidade que vai fornecer as munições.";
|
||
return;
|
||
}
|
||
|
||
if (target == null)
|
||
{
|
||
ammunitionTransferMode = false;
|
||
status = "Transferência cancelada: não existe uma unidade nessa célula.";
|
||
return;
|
||
}
|
||
|
||
if (target == selectedUnit)
|
||
{
|
||
ammunitionTransferMode = false;
|
||
status = "Transferência cancelada.";
|
||
return;
|
||
}
|
||
|
||
if (target.Team != selectedUnit.Team)
|
||
{
|
||
status = "Só pode transferir munições para uma unidade aliada.";
|
||
return;
|
||
}
|
||
|
||
if (Manhattan(selectedUnit.Position, target.Position) > 1)
|
||
{
|
||
status = "As unidades têm de estar em células adjacentes para transferir munições.";
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(transferAmountInput, out int requestedAmount) ||
|
||
requestedAmount <= 0)
|
||
{
|
||
status = "Introduza uma quantidade válida de munições para transferir.";
|
||
return;
|
||
}
|
||
|
||
int targetCapacity =
|
||
target.Definition.MaxAmmunition - target.Ammunition;
|
||
|
||
int transferred = Mathf.Min(
|
||
requestedAmount,
|
||
selectedUnit.Ammunition,
|
||
targetCapacity
|
||
);
|
||
|
||
if (transferred <= 0)
|
||
{
|
||
status = targetCapacity <= 0
|
||
? "A unidade de destino já tem a capacidade máxima de munições."
|
||
: "A unidade fornecedora não possui munições disponíveis.";
|
||
return;
|
||
}
|
||
|
||
selectedUnit.Ammunition -= transferred;
|
||
target.Ammunition += transferred;
|
||
selectedUnit.Acted = true;
|
||
ammunitionTransferMode = false;
|
||
|
||
status =
|
||
$"{selectedUnit.Definition.Name} transferiu {transferred} munições para " +
|
||
$"{target.Definition.Name}.";
|
||
|
||
selectedUnit = null;
|
||
}
|
||
|
||
private void SelectBattleUnit(BattleUnit unit)
|
||
{
|
||
if (unit == null)
|
||
{
|
||
selectedUnit = null;
|
||
status = "Nenhuma unidade nessa célula.";
|
||
return;
|
||
}
|
||
if (unit.Team != activeTeam)
|
||
{
|
||
status = "Não pode selecionar uma unidade adversária como unidade ativa.";
|
||
return;
|
||
}
|
||
selectedUnit = unit;
|
||
status = unit.Acted
|
||
? $"{unit.Definition.Name} já atuou neste turno."
|
||
: $"{unit.Definition.Name} selecionada: MOV {unit.Definition.Movement}, " +
|
||
$"ALC {unit.Definition.Range}, MUN {unit.Ammunition}/{unit.Definition.MaxAmmunition}.";
|
||
}
|
||
|
||
private bool IsLegalMove(BattleUnit unit, Vector2Int destination)
|
||
{
|
||
return UnitAt(destination) == null && Manhattan(unit.Position, destination) <= unit.Definition.Movement;
|
||
}
|
||
|
||
private bool IsLegalAttack(BattleUnit attacker, BattleUnit target)
|
||
{
|
||
// O alcance é medido em casas pela distância horizontal + vertical.
|
||
// Exemplo: alcance 4 permite atacar qualquer inimigo até 4 casas.
|
||
int distance = Manhattan(attacker.Position, target.Position);
|
||
|
||
return target.Team != attacker.Team &&
|
||
distance <= attacker.Definition.Range;
|
||
}
|
||
|
||
private static int Manhattan(Vector2Int a, Vector2Int b)
|
||
{
|
||
return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y);
|
||
}
|
||
|
||
private void RemoveSelectedUnit()
|
||
{
|
||
if (selectedUnit == null)
|
||
return;
|
||
string removed = selectedUnit.Definition.Name;
|
||
if (infoUnit == selectedUnit)
|
||
{
|
||
CloseUnitInfo();
|
||
}
|
||
units.Remove(selectedUnit);
|
||
selectedUnit = null;
|
||
status = $"{removed} removida da colocação.";
|
||
}
|
||
|
||
private void FinishDeployment()
|
||
{
|
||
ammunitionTransferMode = false;
|
||
selectedUnit = null;
|
||
|
||
if (phase == GamePhase.FriendlyDeployment)
|
||
{
|
||
phase = GamePhase.EnemyDeployment;
|
||
activeTeam = Team.Enemy;
|
||
selectedDefinition = definitions[0];
|
||
LoadDefinitionEditor(selectedDefinition);
|
||
piecesMenuOpen = false;
|
||
status = "Jogador 2: coloque as Forças Inimigas na zona vermelha. As unidades azuis permanecem visíveis.";
|
||
return;
|
||
}
|
||
|
||
phase = GamePhase.Battle;
|
||
activeTeam = Team.Friendly;
|
||
turnNumber = 1;
|
||
ResetActedForTeam(activeTeam);
|
||
status = "Nossas Forças iniciam. Todas as unidades estão visíveis.";
|
||
}
|
||
|
||
private void EndTurn()
|
||
{
|
||
ammunitionTransferMode = false;
|
||
selectedUnit = null;
|
||
activeTeam = activeTeam == Team.Friendly ? Team.Enemy : Team.Friendly;
|
||
if (activeTeam == Team.Friendly)
|
||
turnNumber++;
|
||
|
||
ResetActedForTeam(activeTeam);
|
||
status = $"Turno de {TeamName(activeTeam)}. Todas as unidades permanecem visíveis.";
|
||
}
|
||
|
||
private void CheckVictory()
|
||
{
|
||
bool friendlyAlive = CountUnits(Team.Friendly) > 0;
|
||
bool enemyAlive = CountUnits(Team.Enemy) > 0;
|
||
if (friendlyAlive && enemyAlive)
|
||
return;
|
||
|
||
phase = GamePhase.Victory;
|
||
status = friendlyAlive ? "VITÓRIA DAS NOSSAS FORÇAS" : "VITÓRIA DO INIMIGO";
|
||
selectedUnit = null;
|
||
}
|
||
|
||
private void ResetGame()
|
||
{
|
||
units.Clear();
|
||
nextUnitId = 1;
|
||
turnNumber = 1;
|
||
phase = GamePhase.FriendlyDeployment;
|
||
activeTeam = Team.Friendly;
|
||
selectedDefinition = definitions[0];
|
||
LoadDefinitionEditor(selectedDefinition);
|
||
selectedUnit = null;
|
||
ammunitionTransferMode = false;
|
||
transferAmountInput = "1";
|
||
CloseUnitInfo();
|
||
piecesMenuScroll = Vector2.zero;
|
||
piecesMenuOpen = false;
|
||
battlefieldSizeMenuOpen = false;
|
||
status = "Jogador 1: escolha uma unidade e coloque-a na zona azul.";
|
||
}
|
||
|
||
private void ClearTeam(Team team)
|
||
{
|
||
units.RemoveAll(unit => unit.Team == team);
|
||
selectedUnit = null;
|
||
status = $"Colocação de {TeamName(team)} limpa.";
|
||
}
|
||
|
||
private void ResetActedForTeam(Team team)
|
||
{
|
||
foreach (BattleUnit unit in units)
|
||
{
|
||
if (unit.Team == team)
|
||
unit.Acted = false;
|
||
}
|
||
}
|
||
|
||
private int CountUnits(Team team)
|
||
{
|
||
int count = 0;
|
||
foreach (BattleUnit unit in units)
|
||
{
|
||
if (unit.Team == team)
|
||
count++;
|
||
}
|
||
return count;
|
||
}
|
||
|
||
private BattleUnit UnitAt(Vector2Int position)
|
||
{
|
||
foreach (BattleUnit unit in units)
|
||
{
|
||
if (unit.Position == position)
|
||
return unit;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private bool IsDeploymentCell(Team team, Vector2Int position)
|
||
{
|
||
int deploymentDepth = GetDeploymentDepth();
|
||
return team == Team.Friendly
|
||
? position.x < deploymentDepth
|
||
: position.x >= columns - deploymentDepth;
|
||
}
|
||
|
||
private int GetDeploymentDepth()
|
||
{
|
||
return Mathf.Clamp(columns / 8, 2, 5);
|
||
}
|
||
|
||
private static string GetUnitSizeName(UnitSize size)
|
||
{
|
||
return size switch
|
||
{
|
||
UnitSize.Section => "Secção",
|
||
UnitSize.Platoon => "Pelotão",
|
||
UnitSize.Company => "Companhia",
|
||
UnitSize.BattalionGroup => "Batalhão/Grupo",
|
||
UnitSize.Regiment => "Regimento",
|
||
UnitSize.Brigade => "Brigada",
|
||
UnitSize.Division => "Divisão",
|
||
_ => "Desconhecido"
|
||
};
|
||
}
|
||
|
||
private static string GetUnitSizeIndicator(UnitSize size)
|
||
{
|
||
return size switch
|
||
{
|
||
UnitSize.Section => "••",
|
||
UnitSize.Platoon => "•••",
|
||
UnitSize.Company => "I",
|
||
UnitSize.BattalionGroup => "II",
|
||
UnitSize.Regiment => "III",
|
||
UnitSize.Brigade => "X",
|
||
UnitSize.Division => "XX",
|
||
_ => string.Empty
|
||
};
|
||
}
|
||
|
||
private static string TeamName(Team team)
|
||
{
|
||
return team == Team.Friendly ? "Nossas Forças" : "Inimigo";
|
||
}
|
||
|
||
private static Color FriendlyColor() => new Color(0.32f, 0.68f, 0.96f);
|
||
private static Color EnemyColor() => new Color(0.96f, 0.38f, 0.32f);
|
||
|
||
private void DrawFilledRect(Rect rect, Color color)
|
||
{
|
||
Color old = GUI.color;
|
||
GUI.color = color;
|
||
GUI.DrawTexture(rect, whiteTexture);
|
||
GUI.color = old;
|
||
}
|
||
|
||
private Texture2D GetSymbolTexture(Team team, string type)
|
||
{
|
||
string key = team + "_" + type;
|
||
if (symbolCache.TryGetValue(key, out Texture2D cached))
|
||
return cached;
|
||
|
||
Texture2D texture = BuildMilitarySymbol(team, type, 96);
|
||
symbolCache[key] = texture;
|
||
return texture;
|
||
}
|
||
|
||
private static Texture2D MakeSolidTexture(Color color)
|
||
{
|
||
Texture2D texture = new Texture2D(1, 1, TextureFormat.RGBA32, false);
|
||
texture.SetPixel(0, 0, color);
|
||
texture.Apply();
|
||
return texture;
|
||
}
|
||
|
||
private static Texture2D BuildMilitarySymbol(Team team, string type, int size)
|
||
{
|
||
Texture2D texture = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
||
{
|
||
filterMode = FilterMode.Bilinear,
|
||
wrapMode = TextureWrapMode.Clamp
|
||
};
|
||
Color transparent = new Color(0f, 0f, 0f, 0f);
|
||
Color fill = team == Team.Friendly ? new Color(0.36f, 0.70f, 0.98f, 0.92f) : new Color(0.98f, 0.39f, 0.34f, 0.92f);
|
||
Color line = new Color(0.04f, 0.06f, 0.08f, 1f);
|
||
|
||
Color[] pixels = new Color[size * size];
|
||
for (int i = 0; i < pixels.Length; i++) pixels[i] = transparent;
|
||
texture.SetPixels(pixels);
|
||
|
||
if (team == Team.Friendly)
|
||
{
|
||
FillRect(texture, 10, 20, size - 11, size - 21, fill);
|
||
DrawRect(texture, 10, 20, size - 11, size - 21, line, 4);
|
||
}
|
||
else
|
||
{
|
||
FillDiamond(texture, size / 2, size / 2, size / 2 - 8, fill);
|
||
DrawDiamond(texture, size / 2, size / 2, size / 2 - 8, line, 4);
|
||
}
|
||
|
||
DrawInnerIcon(texture, type, line, size);
|
||
texture.Apply();
|
||
return texture;
|
||
}
|
||
|
||
private static void DrawInnerIcon(Texture2D texture, string type, Color color, int size)
|
||
{
|
||
int l = 25, r = size - 26, b = 30, t = size - 31;
|
||
int cx = size / 2, cy = size / 2;
|
||
switch (type)
|
||
{
|
||
case "infantry":
|
||
DrawLine(texture, 10, 20, size - 11, size - 21, color, 4);
|
||
DrawLine(texture, 10, size - 21, size - 11, 20, color, 4);
|
||
break;
|
||
case "armor":
|
||
DrawEllipse(texture, cx, cy, 25, 13, color, 4);
|
||
break;
|
||
case "artillery":
|
||
FillCircle(texture, cx, cy, 10, color);
|
||
//DrawLine(texture, l, cy, r, cy, color, 3);
|
||
break;
|
||
case "recon":
|
||
DrawLine(texture, 2, size - 3, size - 3, 2, color, 5);
|
||
FillCircle(texture, 2, size - 3, 4, color);
|
||
FillCircle(texture, size - 3, 2, 4, color);
|
||
break;
|
||
case "engineer":
|
||
DrawLine(texture, l, b, l, t, color, 4);
|
||
DrawLine(texture, l, t, r, t, color, 4);
|
||
DrawLine(texture, l, cy, r - 8, cy, color, 4);
|
||
DrawLine(texture, l, b, r, b, color, 4);
|
||
break;
|
||
case "airdef":
|
||
DrawLine(texture, l, b, cx, t, color, 4);
|
||
DrawLine(texture, cx, t, r, b, color, 4);
|
||
DrawLine(texture, l + 8, cy - 3, r - 8, cy - 3, color, 4);
|
||
break;
|
||
case "hq":
|
||
DrawLine(texture, cx, b, cx, t, color, 4);
|
||
DrawLine(texture, cx, t, r - 4, t - 10, color, 4);
|
||
break;
|
||
case "logistics":
|
||
DrawRect(texture, l + 6, b + 4, r - 6, t - 4, color, 4);
|
||
DrawLine(texture, l + 8, b + 6, r - 8, t - 6, color, 3);
|
||
break;
|
||
case "medical":
|
||
FillRect(texture, cx - 5, b + 4, cx + 5, t - 4, color);
|
||
FillRect(texture, l + 6, cy - 5, r - 6, cy + 5, color);
|
||
break;
|
||
case "signals":
|
||
DrawLine(texture, cx + 6, t, cx - 8, cy + 4, color, 5);
|
||
DrawLine(texture, cx - 8, cy + 4, cx + 5, cy + 4, color, 5);
|
||
DrawLine(texture, cx + 5, cy + 4, cx - 8, b, color, 5);
|
||
break;
|
||
}
|
||
}
|
||
|
||
private static void FillRect(Texture2D tex, int x0, int y0, int x1, int y1, Color c)
|
||
{
|
||
for (int y = Mathf.Max(0, y0); y <= Mathf.Min(tex.height - 1, y1); y++)
|
||
for (int x = Mathf.Max(0, x0); x <= Mathf.Min(tex.width - 1, x1); x++)
|
||
tex.SetPixel(x, y, c);
|
||
}
|
||
|
||
private static void DrawRect(Texture2D tex, int x0, int y0, int x1, int y1, Color c, int thickness)
|
||
{
|
||
for (int i = 0; i < thickness; i++)
|
||
{
|
||
DrawLine(tex, x0 + i, y0 + i, x1 - i, y0 + i, c, 1);
|
||
DrawLine(tex, x1 - i, y0 + i, x1 - i, y1 - i, c, 1);
|
||
DrawLine(tex, x1 - i, y1 - i, x0 + i, y1 - i, c, 1);
|
||
DrawLine(tex, x0 + i, y1 - i, x0 + i, y0 + i, c, 1);
|
||
}
|
||
}
|
||
|
||
private static void FillDiamond(Texture2D tex, int cx, int cy, int radius, Color c)
|
||
{
|
||
for (int y = -radius; y <= radius; y++)
|
||
{
|
||
int half = radius - Mathf.Abs(y);
|
||
for (int x = -half; x <= half; x++)
|
||
SafeSet(tex, cx + x, cy + y, c);
|
||
}
|
||
}
|
||
|
||
private static void DrawDiamond(Texture2D tex, int cx, int cy, int radius, Color c, int thickness)
|
||
{
|
||
for (int i = 0; i < thickness; i++)
|
||
{
|
||
int r = radius - i;
|
||
DrawLine(tex, cx, cy + r, cx + r, cy, c, 1);
|
||
DrawLine(tex, cx + r, cy, cx, cy - r, c, 1);
|
||
DrawLine(tex, cx, cy - r, cx - r, cy, c, 1);
|
||
DrawLine(tex, cx - r, cy, cx, cy + r, c, 1);
|
||
}
|
||
}
|
||
|
||
private static void DrawEllipse(Texture2D tex, int cx, int cy, int rx, int ry, Color c, int thickness)
|
||
{
|
||
for (int degree = 0; degree < 360; degree++)
|
||
{
|
||
float rad = degree * Mathf.Deg2Rad;
|
||
for (int i = 0; i < thickness; i++)
|
||
{
|
||
int x = Mathf.RoundToInt(cx + (rx - i) * Mathf.Cos(rad));
|
||
int y = Mathf.RoundToInt(cy + (ry - i) * Mathf.Sin(rad));
|
||
SafeSet(tex, x, y, c);
|
||
}
|
||
}
|
||
}
|
||
|
||
private static void FillCircle(Texture2D tex, int cx, int cy, int radius, Color c)
|
||
{
|
||
int rr = radius * radius;
|
||
for (int y = -radius; y <= radius; y++)
|
||
for (int x = -radius; x <= radius; x++)
|
||
if (x * x + y * y <= rr)
|
||
SafeSet(tex, cx + x, cy + y, c);
|
||
}
|
||
|
||
private static void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, Color c, int thickness)
|
||
{
|
||
int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
||
int dy = -Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
||
int err = dx + dy;
|
||
while (true)
|
||
{
|
||
int radius = Mathf.Max(0, thickness / 2);
|
||
for (int oy = -radius; oy <= radius; oy++)
|
||
for (int ox = -radius; ox <= radius; ox++)
|
||
SafeSet(tex, x0 + ox, y0 + oy, c);
|
||
if (x0 == x1 && y0 == y1) break;
|
||
int e2 = 2 * err;
|
||
if (e2 >= dy) { err += dy; x0 += sx; }
|
||
if (e2 <= dx) { err += dx; y0 += sy; }
|
||
}
|
||
}
|
||
|
||
private static void SafeSet(Texture2D tex, int x, int y, Color c)
|
||
{
|
||
if (x >= 0 && y >= 0 && x < tex.width && y < tex.height)
|
||
tex.SetPixel(x, y, c);
|
||
}
|
||
}
|
||
} |