mirror of
https://github.com/Earth-Genesis-Games/Ajedrez_Purgatorio.git
synced 2026-09-11 09:47:35 +00:00
- 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)
143 lines
4.2 KiB
C#
143 lines
4.2 KiB
C#
// ✅ GameManagerCombat.cs
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class GameManagerCombat : MonoBehaviour
|
|
{
|
|
public static GameManagerCombat Instance;
|
|
public PieceCombat[,] board = new PieceCombat[8, 8];
|
|
public bool whiteTurn = true;
|
|
public bool modoIA = true;
|
|
public PieceCombat selectedPiece = null;
|
|
private bool waitingForAIMove = false;
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else Destroy(gameObject);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (modoIA && !whiteTurn && !waitingForAIMove)
|
|
{
|
|
waitingForAIMove = true;
|
|
Invoke(nameof(MakeAIMove), 0.5f);
|
|
}
|
|
}
|
|
|
|
void MakeAIMove()
|
|
{
|
|
waitingForAIMove = false;
|
|
var pieces = board.Cast<PieceCombat>().Where(p => p != null && !p.isWhite).ToList();
|
|
System.Random rand = new();
|
|
|
|
foreach (var piece in pieces.OrderBy(_ => rand.Next()))
|
|
{
|
|
var moves = piece.GetAvailableMoves(board);
|
|
foreach (var move in moves.Where(m => board[m.x, m.y] != null && board[m.x, m.y].isWhite))
|
|
{
|
|
if (IsLegalMove(piece, move)) { MovePiece(piece, move); return; }
|
|
}
|
|
foreach (var move in moves)
|
|
{
|
|
if (IsLegalMove(piece, move)) { MovePiece(piece, move); return; }
|
|
}
|
|
}
|
|
Debug.Log("La IA no tiene movimientos legales.");
|
|
}
|
|
|
|
public void SelectPiece(PieceCombat piece)
|
|
{
|
|
if (selectedPiece == piece) { piece.HideMoves(); selectedPiece = null; return; }
|
|
if (piece.isWhite == whiteTurn)
|
|
{
|
|
selectedPiece?.HideMoves();
|
|
selectedPiece = piece;
|
|
piece.ShowMoves();
|
|
}
|
|
}
|
|
|
|
public void TryMoveTo(Vector2Int targetPos)
|
|
{
|
|
if (selectedPiece == null) return;
|
|
var available = selectedPiece.GetAvailableMoves(board);
|
|
if (!available.Contains(targetPos)) return;
|
|
|
|
Vector2Int oldPos = selectedPiece.currentPos;
|
|
PieceCombat captured = board[targetPos.x, targetPos.y];
|
|
board[oldPos.x, oldPos.y] = null;
|
|
board[targetPos.x, targetPos.y] = selectedPiece;
|
|
selectedPiece.currentPos = targetPos;
|
|
|
|
if (IsInCheck(selectedPiece.isWhite))
|
|
{
|
|
board[targetPos.x, targetPos.y] = captured;
|
|
board[oldPos.x, oldPos.y] = selectedPiece;
|
|
selectedPiece.currentPos = oldPos;
|
|
Debug.Log("Movimiento inválido: deja en jaque.");
|
|
return;
|
|
}
|
|
|
|
MovePiece(selectedPiece, targetPos);
|
|
selectedPiece.HideMoves();
|
|
selectedPiece = null;
|
|
}
|
|
|
|
public void MovePiece(PieceCombat piece, Vector2Int newPos)
|
|
{
|
|
Vector2Int oldPos = piece.currentPos;
|
|
PieceCombat captured = board[newPos.x, newPos.y];
|
|
if (captured != null) Destroy(captured.gameObject);
|
|
|
|
board[oldPos.x, oldPos.y] = null;
|
|
board[newPos.x, newPos.y] = piece;
|
|
piece.transform.position = new Vector3(newPos.x * BoardManagerCombat.Instance.tileSize, newPos.y * BoardManagerCombat.Instance.tileSize, -1f);
|
|
piece.currentPos = newPos;
|
|
piece.hasMoved = true;
|
|
|
|
whiteTurn = !whiteTurn;
|
|
}
|
|
|
|
bool IsLegalMove(PieceCombat piece, Vector2Int move)
|
|
{
|
|
Vector2Int oldPos = piece.currentPos;
|
|
PieceCombat target = board[move.x, move.y];
|
|
board[oldPos.x, oldPos.y] = null;
|
|
board[move.x, move.y] = piece;
|
|
piece.currentPos = move;
|
|
|
|
bool legal = !IsInCheck(piece.isWhite);
|
|
|
|
board[move.x, move.y] = target;
|
|
board[oldPos.x, oldPos.y] = piece;
|
|
piece.currentPos = oldPos;
|
|
return legal;
|
|
}
|
|
|
|
public bool IsInCheck(bool forWhite)
|
|
{
|
|
PieceCombat king = null;
|
|
foreach (var p in board)
|
|
{
|
|
if (p != null && p is KingCombat && p.isWhite == forWhite)
|
|
{
|
|
king = p;
|
|
break;
|
|
}
|
|
}
|
|
if (king == null) return false;
|
|
|
|
foreach (var p in board)
|
|
{
|
|
if (p != null && p.isWhite != forWhite)
|
|
{
|
|
var moves = p.GetAvailableMoves(board);
|
|
if (moves.Contains(king.currentPos)) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|