Files
Ajedrez_Purgatorio/Assets/Game/Scripts/Mono/Pieces/King.cs
T
jimmyabv b539aa403e fix: resolve compilation errors, add tests, fix configs, update docs
- Fix 9 CS0101 duplicate classes between Mono/ and Mono/Classic/
  - BoardManager -> ClassicBoardManager, SquareClick -> ClassicSquareClick
  - Delete duplicate Piece/Pawn/King/Queen/Rook/Bishop/Knight from Classic/
  - Merge GetAttackSquares into Piece base class with overrides
- Fix King castling: reject castling through/while in check
- Assign URP pipeline to QualitySettings (6 levels) and GraphicsSettings
- Fix template identifiers (DefaultCompany -> EarthGenesisGames)
- Delete orphaned SampleScene.unity and duplicate URP assets
- Fix test assembly reference (Assembly-CSharp -> AjedrezPurgatorio.Runtime)
- Remove 11 unused packages (multiplayer, visualscripting, terrain, etc.)
- Update BoardInputSystemSpike for ClassicSquareClick
- Write unit tests: CheckDetector, DrawDetector, MoveValidator
- Fix BalanceConfig JSON structure to match C# class fields
- Update stale documentation (~30 files)
  - architecture.md, ADR-0001, roadmap, gate-check, EPIC.md
  - PROJECT_STATUS.md, BLOQUEADORES_JUGABILIDAD.md
  - tech-debt-register.md
- Add Unity Editor setup guide (production/UNITY_EDITOR_SETUP.md)
2026-08-17 20:40:02 -03:00

101 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using AjedrezPurgatorio.Data;
public class King : Piece
{
[Header("Dead King")]
[Tooltip("If set, this King is a Dead King with player data")]
public DeadKingData deadKingData = null;
public bool IsDeadKing => deadKingData != null;
public override List<Vector2Int> GetAvailableMoves(Piece[,] board)
{
List<Vector2Int> moves = new List<Vector2Int>();
Vector2Int[] directions = {
Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right,
new Vector2Int(1, 1), new Vector2Int(-1, 1), new Vector2Int(1, -1), new Vector2Int(-1, -1)
};
foreach (var dir in directions)
{
Vector2Int pos = currentPos + dir;
if (IsInsideBoard(pos))
{
var target = board[pos.x, pos.y];
if (target == null || target.isWhite != isWhite)
moves.Add(pos);
}
}
bool kingInCheck = false;
if (GameManager.Instance != null)
kingInCheck = GameManager.Instance.IsInCheck(isWhite);
else if (GameManagerClassic.Instance != null)
kingInCheck = GameManagerClassic.Instance.IsInCheck(isWhite);
if (!hasMoved && !kingInCheck)
{
int y = currentPos.y;
if (CanCastle(board, 7, y, new int[] { 5, 6 }))
moves.Add(new Vector2Int(6, y));
if (CanCastle(board, 0, y, new int[] { 1, 2, 3 }))
moves.Add(new Vector2Int(2, y));
}
return moves;
}
public override List<Vector2Int> GetAttackSquares(Piece[,] board)
{
List<Vector2Int> attacks = new List<Vector2Int>();
Vector2Int[] directions = {
Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right,
new Vector2Int(1, 1), new Vector2Int(-1, 1), new Vector2Int(1, -1), new Vector2Int(-1, -1)
};
foreach (var dir in directions)
{
Vector2Int pos = currentPos + dir;
if (IsInsideBoard(pos)) attacks.Add(pos);
}
return attacks;
}
private bool CanCastle(Piece[,] board, int rookX, int y, int[] middleSquares)
{
Piece rook = board[rookX, y];
if (rook == null || !(rook is Rook) || rook.hasMoved || rook.isWhite != isWhite)
return false;
foreach (int x in middleSquares)
{
if (board[x, y] != null) return false;
}
int checkX1 = (rookX == 7) ? 5 : 3;
int checkX2 = (rookX == 7) ? 6 : 2;
if (IsSquareAttackedBy(board, new Vector2Int(checkX1, y), !isWhite)) return false;
if (IsSquareAttackedBy(board, new Vector2Int(checkX2, y), !isWhite)) return false;
return true;
}
private bool IsSquareAttackedBy(Piece[,] board, Vector2Int pos, bool byWhite)
{
foreach (var piece in board)
{
if (piece != null && piece.isWhite == byWhite)
{
if (piece.GetAttackSquares(board).Contains(pos))
return true;
}
}
return false;
}
}