/// <summary> /// Создать поле с фигурой /// </summary> /// <param name="parent">контейнер</param> /// <param name="position">позиция</param> /// <param name="size">размер</param> /// <param name="coord">позиция на доске</param> /// <param name="piece">фигура</param> /// <param name="onclick">действие по нажатию</param> /// <returns></returns> public static ChessPieceScript CreateChessPiece(Transform parent, Vector3 position, float size, BoardCoord coord, ChessPiece piece, UnityAction <BaseEventData> onclick) { if (piece == null) { throw new System.Exception("CreateChessPiece: параметр piece = null"); } string path = string.Format("Prefabs/{0}{1}", piece.PieceType.ToString(), piece.Color.ToString()); Object resource = Resources.Load(path); if (resource == null) { throw new System.Exception("Не найден ресурс " + path); } ChessPieceScript space = (Object.Instantiate(Resources.Load(path)) as GameObject).GetComponent <ChessPieceScript>(); space.SetCoordinates(coord); space.SetSize(size); space.transform.position = position; space.transform.SetParent(parent); space.Piece = piece; space.gameObject.name = string.Format("{0}_{1}", piece.PieceType.ToString(), piece.Color.ToString()); return(space); }
/// <summary> /// Обновить фигуры по их перемещению /// </summary> /// <param name="move">перемещение фигур</param> private void UpdateChessPieces(Stack <ChessMove> move) { if (move == null || move.Count == 0) { return; } ChessMove currentMove = move.Pop(); if (currentMove == null) { return; } ChessPieceScript pieceScript = FindChessPiece(currentMove.MovingPiece); if (!pieceScript) { Vector3 position = BoardCoordToTransformPosition(currentMove.From); pieceScript = UIBuilder.CreateChessPiece(PieceContainer, position, CurrentCellSize, currentMove.From, currentMove.MovingPiece, OnChessPieceClickHandler); pieceScript.gameObject.AddClickEventTrigger(OnChessPieceClickHandler); Destroy(FindChessPiece(currentMove.From).gameObject); } ChessPieceScript defeatedScript = FindChessPiece(currentMove.DefeatedPiece); /// скрыть захваченную фигуру if (defeatedScript) { defeatedScript.SetVisibility(false); } /// переместить фигуру if (pieceScript.Coordiantes != currentMove.To) { pieceScript.SetCoordinates(currentMove.To); Vector3 from = pieceScript.transform.position; Vector3 to = BoardCoordToTransformPosition(currentMove.To); StartCoroutine(ChessPieceMoveCoroutine(pieceScript, from, to, new UnityAction(() => { UpdateChessPieces(move); }))); } }