public bool RoundWin() { int enemyCount = 0; int playerCount = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); if (chessTile) { if (chessTile.gameObject) { if (chessTile.gameObject.name.Contains("NPC")) { enemyCount++; } if (chessTile.gameObject.name.Contains("Player")) { playerCount++; } } } } } // 플레이어 0명이 아니고 적이 없으면 승리 return(playerCount != 0 && enemyCount == 0); }
public void DestroyPlayerList() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); if (chessTile) { if (chessTile.gameObject) { //if (chessTile.gameObject.name.Contains("Player")) { ChessCharacter character = chessTile.gameObject.GetComponent <ChessCharacter>(); if (character) { DestroyImmediate(character.GetHpBar().gameObject); } DestroyImmediate(chessTile.gameObject); } } } } } }
public void SpawnCharacter(string path, string name, int tileX, int tileY, eCharacter chessCharacterType, ChessCharacter.eCharacterType characterType) { GameObject character = Instantiate(Resources.Load(path)) as GameObject; character.name = name; ChessCharacter cCharacter = character.GetComponent <ChessCharacter>(); cCharacter.SetTilePosition(new Vector3Int(tileX, tileY, 0)); cCharacter.SetCharacterType(characterType); cCharacter.SetChessCharacterType(chessCharacterType); Debug.Log("Spawn in GameManager"); character.transform.SetParent(tilemap.transform); ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(tileX, tileY, 0)); if (chessTile != null) { chessTile.gameObject = character; } GameObject canvas = GameObject.Find("Canvas"); RectTransform canvasRect = canvas.GetComponent <RectTransform>(); //slider transform 세팅 GameObject sliderObject = Instantiate(Resources.Load("Prefabs/UI/HpBar"), hpbarPanel.transform) as GameObject; //sliderObject.transform.SetParent(canvas.transform, false); Slider slider = sliderObject.GetComponent <Slider>(); cCharacter.SetHpBar(slider); }
private bool BuyWaitCharacter(string path, string name, eCharacter characterType) { int waitPositionX = 0; while (waitPositionX < 8) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(waitPositionX, -1, 0)); if (chessTile != null) { if (chessTile.gameObject == null) { GameObject character = Instantiate(Resources.Load(path)) as GameObject; character.name = name; ChessCharacter cCharacter = character.GetComponent <ChessWaitCharacter>(); character.transform.SetParent(tilemap.transform); cCharacter.SetTilePosition(new Vector3Int(waitPositionX, -1, 0)); cCharacter.SetCharacterType(ChessCharacter.eCharacterType.WAIT); cCharacter.SetChessCharacterType(characterType); chessTile.gameObject = character; return(true); } } waitPositionX++; } return(false); }
public AbstractChessPiece(ChessBoard board, ChessColor color, ChessTile position, bool onBoard = true) { Board = board; Color = color; Position = position; InPlay = onBoard; }
public bool IsFinishRound() { int enemyCount = 0; int playerCount = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); if (chessTile) { if (chessTile.gameObject) { if (chessTile.gameObject.name.Contains("NPC")) { enemyCount++; } if (chessTile.gameObject.name.Contains("Player")) { playerCount++; } } } } } // 플레이어, 적 중 하나라로 0명이면 라운드 종료 return(playerCount == 0 || enemyCount == 0); }
public void StartMovement(Vector3 tilePos, ChessTile ns) { newSpot = ns; endMarker = tilePos; endMarker.y = this.transform.position.y; isMoving = true; }
public void PromotePawn(Pawn pawn, ChessTile promotionTile) { // Spawn the pawn promotion dialouge and forget about it GameObject promotionUI = Instantiate(pawn.isBlack ? _pawnPromotionUIPrefabBlack : _pawnPromotionUIPrefabWhite, promotionTile.transform); promotionUI.transform.localPosition = new Vector3(0f, 0.05f, pawn.isBlack ? -6.25f : 6.25f); promotionUI.GetComponent <PawnPromotionUI>().PromotionPawn = pawn; }
public void MovePiece(ChessTile newSpot) { // Checks if space piece is moving to is occupied, if so attacked piece is destroyed, // bool is true if it was a King, bool wasKing = false; if (newSpot.occupied) { if (newSpot.occupiedBy.isKing) { wasKing = true; } Destroy(newSpot.occupiedBy.gameObject); //newSpot.occupiedBy = selectedPiece; } // Starts LERP process for moved piece selectedPiece.StartMovement(newSpot.transform.position, newSpot); // Resets everything to do with selecting and moving a piece UnHighlightValidTiles(selectedPiece); selectedPiece.currentTile.occupied = false; selectedPiece.currentTile.occupiedBy = null; //selectedPiece.SetLocation(newSpot); selectedPiece.GetComponent <Renderer>().material.color = selectedPiece.originalColor; if (selectedPiece.isPawn) // if a pawn was moved make onStartingTile false, so it can only move twice once { selectedPiece.GetComponent <Pawn>().onStartingTile = false; // If pawn reaches opposite side of board, replace it with a Queen if (newSpot.coordinates.z == 0f) { CreateQueen(blackTeam, queenPrefab, selectedPiece.currentTile.coordinates, selectedPiece.team); Destroy(selectedPiece.gameObject); } else if (newSpot.coordinates.z == 7f) { CreateQueen(whiteTeam, queenPrefab, selectedPiece.currentTile.coordinates, selectedPiece.team); Destroy(selectedPiece.gameObject); } } // If King was destroyed, end game if (wasKing) { // Game Over Screen isGameOver = true; whiteTeam = null; blackTeam = null; uiManager.GameOver(); } //NewTurn(); }
public void SelectTile(ChessTile tile) { // Make the tile this belongs to the active board SelectBoard(tile.Board); // If there is an active piece and this is a legal move make the move then deactivate the piece if (_activeFigure != null && _potentialMoves.Contains(tile)) { MovePiece(_activeFigure, tile); _activeFigure = null; return; } foreach (ChessTile moveTile in _potentialMoves) { moveTile.Highlighted = false; // Unhighlights the previously highlighted moves } // Deselect the old tile and select the new one if (_activeTile != null) { _activeTile.Highlighted = false; } _activeTile = tile; _activeTile.Highlighted = true; // If there is a piece on this tile select it as well, otherwise deselect the active piece if (_activeTile.Figure != null) { _activeFigure = _activeTile.Figure; bool[,] legalMoves = _activeFigure.LegalMoves(); // Get the tile references for each legal move _potentialMoves = new List <ChessTile>(); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (legalMoves[x, y]) { _potentialMoves.Add(_activeChessBoard.Tiles[x, y]); } } } // Highlight those squares foreach (ChessTile moveTile in _potentialMoves) { moveTile.Highlighted = true; } } else { _activeFigure = null; } _activeChessBoard.ClearThreatArrows(); _activeChessBoard.DrawThreatArrowsToTile(_activeTile); }
void StartSettings_FormClosing(object sender, FormClosingEventArgs e) { stepTime = ss.StepTime; isWhite = ss.IsWhite; botVSbot = ss.BotVSbot; botsWar = ss.BotsWar; tile = new ChessTile(ss.BotVSbot, ss.IsWhite, ss.PawnWeightOpp1, ss.RookWeightOpp1, ss.BishopWeightOpp1, ss.KnightWeightOpp1, ss.QueenWeightOpp1, ss.KingWeightOpp1, ss.PawnWeightOpp2, ss.RookWeightOpp2, ss.BishopWeightOpp2, ss.KnightWeightOpp2, ss.QueenWeightOpp2, ss.KingWeightOpp2); }
// Moves Piece to a tile and tells tile who it is occupied by public void SetLocation(ChessTile tile) { currentTile = tile; transform.localPosition = tile.transform.localPosition; currentTile.occupied = true; currentTile.occupiedBy = this; this.transform.position += new Vector3(0, GetComponent <MeshFilter>().mesh.bounds.extents.y, 0); //print("Hello"); }
public void AddMoveIfLegal(int x, int y, ref ChessTile[,] tiles, ref bool[,] moveArray) { if (MoveOnBoard(x, y)) { ChessTile toTile = tiles[x, y]; bool noThreatAndEmpty = toTile.ThreatenedBy(isBlack).Count == 0 && !toTile.HasPiece(); bool canTake = toTile.HasEnemyPiece(isBlack) && toTile.Figure.ProtectedBy().Count == 0; moveArray[x, y] = noThreatAndEmpty || canTake; } }
private void OnMouseDown() { if (tile.moveable == true) { ChessBoard board = tile.GetBoard(); // simple lambda function to allow code reuse in the statements below System.Action <ChessPiece, ChessTile> movePiece = (p, t) => { board.GetBoardArray()[p.X, p.Y].Piece = null; // set the previous tile's piece information to null t.Piece = p; // store the piece into the tile p.Move(t.X, t.Y); // move the piece's game object into the tile's position }; //hardcoded castling algorithm if (board.pieceInfo is King && Mathf.Abs(tile.X - board.pieceInfo.X) > 1) { ChessTile t = (tile.X - board.pieceInfo.X > 0) ? board.GetBoardArray()[tile.X - 1, tile.Y] : board.GetBoardArray()[tile.X + 1, tile.Y]; Rook r = (tile.X - board.pieceInfo.X > 0) ? board.GetBoardArray()[tile.X + 1, tile.Y].Piece as Rook : board.GetBoardArray()[tile.X - 2, tile.Y].Piece as Rook; movePiece(r, t); movePiece(board.pieceInfo, tile); // move king } //hardcoded en passant algorithm else if (board.pieceInfo is Pawn && tile.Piece == null && tile.X != board.pieceInfo.X && ((Pawn)board.pieceInfo).promoted == null) { ChessTile t = (board.pieceInfo.GetTeam() == Team.White) ? board.GetBoardArray()[tile.X, tile.Y - 1] : board.GetBoardArray()[tile.X, tile.Y + 1]; board.RemoveFromBoard(t.Piece); // remove the piece from board t.Piece.Enabled = false; // disable the piece interaction t.Piece = null; // set the previous tile's piece information to null movePiece(board.pieceInfo, tile); } //hardcoded promotion algorithm else if (board.pieceInfo is Pawn && (tile.Y == 0 || tile.Y == board.GetBoardArray().GetLength(1) - 1) && ((Pawn)board.pieceInfo).promoted == null) { movePiece(board.pieceInfo, tile); Pawn p = board.pieceInfo as Pawn; board.uiPromotion.Show(board, p); } // normal move else if (board.pieceInfo != null) { movePiece(board.pieceInfo, tile); // move the piece } board.onMoveListener(); } }
// Creates a tile for the board private void CreateTile(IntVector2 coordinates, ChessTile tile, Color color) { ChessTile newTile = Instantiate(tile) as ChessTile; tiles[coordinates.x, coordinates.z] = newTile; newTile.coordinates = coordinates; newTile.originalColor = color; newTile.name = "Board Tile " + coordinates.x + ", " + coordinates.z; newTile.transform.parent = transform; newTile.transform.localPosition = new Vector3(coordinates.x - size.x * 0.5f + 0.5f, 0f, coordinates.z - size.z * 0.5f + 0.5f); }
public void ResetTilePath(string keyName) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); chessTile.ResetTileData(keyName); } } }
private ChessCharacter FindAdjacentTarget(int findRange) { _cCharacter.ClearPathFindQueue(); GameManager.gameInstance.ResetTilePath(_cCharacter.name); ChessTile startTile; startTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(_cCharacter.GetTilePosition()); startTile.SetPrevPathTileNodeMap(_cCharacter.name, startTile); _cCharacter.PushPathFindTile(startTile); ChessTile nextQueueTile = _cCharacter.PopPathFindTile(); while (nextQueueTile != null) //###남은 갯수 체크가 아닌 PopPathFindTile 반환 값으로 판별하도록 개선 가능 { ChessTile currentTile = nextQueueTile; //가장 인근의 캐릭터 반환 if (currentTile.gameObject != null && currentTile.GetDistanceWeight() != 0) { ChessCharacter targetCharacter = currentTile.gameObject.GetComponent <ChessCharacter>(); if (_cCharacter.GetCharacterType() != targetCharacter.GetCharacterType() && targetCharacter.GetCharacterType() != ChessCharacter.eCharacterType.WAIT) // 적일 때만 탐색 성공 시킨다. { return(targetCharacter); } } else if (currentTile.GetDistanceWeight() == findRange + 1) { return(null); } else { for (int i = 0; i < (int)ChessCharacter.Direction.MAXSIZE; i++) { ChessCharacter.Direction direction = (ChessCharacter.Direction)i; Vector3Int nextTilePos = currentTile.GetTilePosition() + _cCharacter.GetDirectionTileNext(direction); ChessTile nextTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(nextTilePos); if (_cCharacter.IsInWall(nextTilePos) && //맵 안에 있을 때 분기 nextTile.GetPrevPathTileNodeMap(_cCharacter.name) == null) // 이미 이전 타일 세팅 안되있을 때 분기 { nextTile.SetPrevPathTileNodeMap(_cCharacter.name, currentTile); nextTile.SetDistanceWeight(currentTile.GetDistanceWeight() + 1); _cCharacter.PushPathFindTile(nextTile); } } } nextQueueTile = _cCharacter.PopPathFindTile(); } return(null); }
public void AllTilesLog() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); if (chessTile != null) { Debug.Log("tile[" + j + "," + i + "] : " + tilemap.GetColliderType(chessTile.GetTilePosition())); } } } }
public void SetTileObject(Vector3Int tilePosition, GameObject gameObject) { if (tilePosition.x < 0 || tilePosition.x > 7 || tilePosition.y < 0 || tilePosition.y > 7) { //Debug.Log("Tile 범위 초과 : GetTileObject"); } ChessTile chessTile = tilemap.GetTile <ChessTile>(tilePosition); if (chessTile != null) { chessTile.gameObject = gameObject; } }
private void FindPath(Vector3Int targetTilePosition) { _cCharacter.ClearPathFindQueue(); _cCharacter.ClearPathStack(); GameManager.gameInstance.ResetTilePath(_cCharacter.name); ChessTile startTile; startTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(_cCharacter.GetTilePosition()); startTile.SetPrevPathTileNodeMap(_cCharacter.name, startTile); _cCharacter.PushPathFindTile(startTile); ChessTile nextQueueTile = _cCharacter.PopPathFindTile(); while (nextQueueTile != null) { ChessTile currentTile = nextQueueTile; //목표 타일에 도달하면 반환 if (currentTile.GetTilePosition() == targetTilePosition) { ChessTile pathTile = currentTile; while (pathTile.GetPrevPathTileNodeMap(_cCharacter.name) != null && pathTile.GetPrevPathTileNodeMap(_cCharacter.name) != pathTile) { _cCharacter.PushPathStackTile(pathTile); pathTile = pathTile.GetPrevPathTileNodeMap(_cCharacter.name); targetTile = pathTile; } Debug.Log("@@@@@path finish1!!!"); return; } else { for (int i = 0; i < (int)ChessCharacter.Direction.MAXSIZE; i++) { ChessCharacter.Direction direction = (ChessCharacter.Direction)i; Vector3Int nextTilePos = currentTile.GetTilePosition() + _cCharacter.GetDirectionTileNext(direction); ChessTile nextTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(nextTilePos); if (_cCharacter.IsInWall(nextTilePos) && nextTile.GetPrevPathTileNodeMap(_cCharacter.name) == null) { nextTile.SetPrevPathTileNodeMap(_cCharacter.name, currentTile); //Debug.Log("nextTile : " + nextTile.position + "direction : " + direction); _cCharacter.PushPathFindTile(nextTile); } } } nextQueueTile = _cCharacter.PopPathFindTile(); } }
/// <summary> /// Creates a Chessboard with the specified tileSize, height and with /// </summary> /// <returns>The chess board.</returns> /// <param name="boardParams">Board parameters.</param> public Dictionary <TileIndex, ChessTile> CreateChessBoard(ChessBoardParams boardParams) { var chessBoard = new Dictionary <TileIndex, ChessTile>(); for (int i = 0; i < boardParams.height; i++) { for (int j = 0; j < boardParams.width; j++) { ChessTile tile = ChessTile.Init(tilePrototype).SetPosition().SetColor(); TileIndex index = new TileIndex(j, i); chessBoard.Add(index, tile); } } return(chessBoard); }
public GameObject GetTileObject(Vector3Int tilePosition) { GameObject gameObject = null; if (tilePosition.x < 0 || tilePosition.x > 7 || tilePosition.y < 0 || tilePosition.y > 7) { return(null); } ChessTile chessTile = tilemap.GetTile <ChessTile>(tilePosition); if (chessTile != null) { gameObject = chessTile.gameObject; } return(gameObject); }
public override void SetPosition(ChessTile tile) { if (!HasMoved) // If this is the first move { int castleY = isBlack ? 7 : 0; ChessTile[,] tiles = tile.Board.Tiles; // Move the Queenside rook or kingside rook if castling if (tile.xCoord == 2) { tiles[0, castleY].Figure.SetPosition(tiles[3, castleY]); } else if (tile.xCoord == 6) { tiles[7, castleY].Figure.SetPosition(tiles[5, castleY]); } } base.SetPosition(tile); }
// Move this directly to a tile public virtual void SetPosition(ChessTile tile) { // Reposition this in Unity this.transform.parent = tile.transform; this.transform.localPosition = Vector3.zero; this.transform.localScale = Scale; // Any movement from a player removes their EnPassant tile if (isBlack) { tile.Board.BlackEnPassantTile = null; } else { tile.Board.WhiteEnPassantTile = null; } HasMoved = true; }
public bool CanMoveTile(Vector3Int tilePos) { if (tilePos.x < 0 || tilePos.x > 7 || tilePos.y < 0 || tilePos.y > 7) { return(false); } if ((int)tilemap.GetColliderType(tilePos) > (int)Tile.ColliderType.None) { return(false); } ChessTile chessTile = tilemap.GetTile <ChessTile>(tilePos); if (chessTile.gameObject != null) { return(false); } return(true); }
public void MovePiece(ChessFigure figure, ChessTile targetTile) { // Remove this piece from the old tile figure.Tile.Figure = null; // If there is a piece on the target tile destroy it if (targetTile.Figure != null) { Destroy(targetTile.Figure.gameObject); } // Move this piece to the new tile in Unity figure.SetPosition(targetTile); // Doubly link them: figure.Tile = targetTile; targetTile.Figure = figure; // Change the turn figure.Tile.Board.IsBlacksTurn = !figure.Tile.Board.IsBlacksTurn; }
public void SavePlayerList() { lastPlayerNameList.Clear(); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { ChessTile chessTile = tilemap.GetTile <ChessTile>(new Vector3Int(j, i, 0)); if (chessTile) { if (chessTile.gameObject) { if (chessTile.gameObject.name.Contains("Player")) { ChessCharacter character = chessTile.gameObject.GetComponent <ChessCharacter>(); lastPlayerNameList.Add(GetSpawnCharacterInfo(character.GetChessCharacterType(), chessTile.GetTilePosition().x, chessTile.GetTilePosition().y)); } } } } } }
private void _Generate() { bool alternate = true; for (int x = 0; x < chessTiles.GetLength(0); x++) { alternate = !alternate; for (int y = 0; y < chessTiles.GetLength(1); y++) { if (alternate) { chessTiles[x, y] = new ChessTile(this, x, y, GameManager.SpriteManager["whiteTile"]); } else { chessTiles[x, y] = new ChessTile(this, x, y, GameManager.SpriteManager["blackTile"]); } alternate = !alternate; } } gameMode(this, whitePieces, blackPieces, out whiteKing, out blackKing); }
// Special for pawn due to EnPassant logic and promotoion public override void SetPosition(ChessTile tile) { base.SetPosition(tile); // EnPassant Logic if (isBlack) { // If the pawn moved two tiles this enables EnPassant on the previous tile, otherwise it eliminates previous enpassant tile.Board.BlackEnPassantTile = Tile.yCoord - tile.yCoord == 2 ? tile.Board.Tiles[tile.xCoord, tile.yCoord + 1] : null; // If this was moving to an Enpassant tile delete the pawn on the correct tile if (tile.Board.WhiteEnPassantTile != null && tile.Board.WhiteEnPassantTile == tile) { Destroy(tile.Board.Tiles[tile.xCoord, tile.yCoord + 1].Figure.gameObject); } } else { // If the pawn moved two tiles this enables EnPassant on the previous tile, otherwise it eliminates previous enpassant tile.Board.WhiteEnPassantTile = tile.yCoord - Tile.yCoord == 2 ? tile.Board.Tiles[tile.xCoord, tile.yCoord - 1] : null; // If this was moving to an Enpassant tile delete the pawn on the correct tile if (tile.Board.BlackEnPassantTile != null && tile.Board.BlackEnPassantTile == tile) { Destroy(tile.Board.Tiles[tile.xCoord, tile.yCoord - 1].Figure.gameObject); } } // If the pawn has moved to the last rank open up the promotion window if (tile.yCoord % 7 == 0) { // This is ran before the two-way figure-tile coupling happens in the MovePiece() logic in GameController GameObject controller = GameObject.FindGameObjectWithTag("GameController"); controller.GetComponent <GameController>().PromotePawn(this, tile); } }
public void ReleaseOverTile(ChessTile tile) { //TODO: Piece Drag and drop logic }