public void CreatePieceAndInitialize(Vector2Int squareCoords, TeamColor team, string type) { Piece newPiece = pieceCreator.CreatePiece(type).GetComponent <Piece>(); newPiece.SetData(squareCoords, team, board); Material teamMaterial = pieceCreator.GetTeamMaterial(team); newPiece.SetMaterial(teamMaterial); board.SetPieceOnBoard(squareCoords, newPiece); ChessPlayer currentPlayer = team == TeamColor.White ? whitePlayer : blackPlayer; currentPlayer.AddPiece(newPiece); }
/// <summary> /// Generates all twelve starting pieces for the player. /// </summary> /// <param name="player">the player to generate pieces for</param> public void GeneratePlacementPieces(Player player) { // Directions as ints int N = 0, NE = 1, SE = 2, S = 3, SW = 4, NW = 5; // List of pieces to place List <PieceData> pieces = new List <PieceData>(); PieceCreator pieceCreator = GameObject.Find("Taoex").GetComponent <PieceCreator>(); // Create the pieces for (int dir = 0; dir < 6; dir++) { pieces.Add(pieceCreator.CreatePiece(player.colour, dir, 2)); pieces.Add(pieceCreator.CreatePiece(player.colour, dir, 3)); } // Reference to the board for getting way cross coordinates TaoexBoard board = GameObject.Find("Taoex").GetComponent <TaoexBoard>(); Vector3 center = GameObject.Find("Compass").transform.position; Vector3[] crosses = new Vector3[6]; // Positions of the way crosses crosses[0] = board.GetTiles()[13, 5].BasePosition; // S crosses[1] = board.GetTiles()[7, 8].BasePosition; // SW crosses[2] = board.GetTiles()[7, 14].BasePosition; // NW crosses[3] = board.GetTiles()[13, 17].BasePosition; // N crosses[4] = board.GetTiles()[19, 14].BasePosition; // NE crosses[5] = board.GetTiles()[19, 8].BasePosition; // SE // Pieces per direction are in sets of two, so we need to offset them float offset = 0f; float angleOffset = 0f; // Counter for the piece number for the current direction int count = 0; foreach (PieceData piece in pieces) { // Setup the textures piece.SetupTextures(); // Placement piece component so that the piece can handle click events correctly player.PlacementPieces.Add(piece.getObj().AddComponent <PlacementPiece>()); piece.getObj().GetComponent <PlacementPiece>().Piece = piece; // Spawn position of the piece Vector3 spawnPosition; Transform pieceTrans = piece.getObj().transform; // Direction of the piece int direction = piece.direction; // Spawn position starts at the way cross spawnPosition = crosses[direction]; // Vector going outward from the center to the cross Vector3 centerToCross = spawnPosition - center; centerToCross.Normalize(); // Move the spawn position back and up spawnPosition += centerToCross * 500f; spawnPosition.y += 40f; // Spawn the piece texture there piece.getObj().transform.position = spawnPosition; // Make the piece look towards the center pieceTrans.LookAt(center); // Move along the side of the board using uvn coordinates pieceTrans.InverseTransformDirection(pieceTrans.position); pieceTrans.Translate(offset - 400f + playerPlacementOffset, 0, 0f); pieceTrans.TransformDirection(pieceTrans.position); // Rotate the pieces piece.getObj().transform.eulerAngles = new Vector3(0, angleOffset, 0); // Save the transform piece.getObj().GetComponent <PlacementPiece>().PlacementPosition = piece.getObj().transform.position; piece.getObj().GetComponent <PlacementPiece>().PlacementRotation = piece.getObj().transform.eulerAngles; // Increment the offset offset += 75f; count++; // If this is the second piece, reset offsets for the next set of two if (count % 2 == 0) { offset = 0f; } if (count % 2 == 0) { angleOffset += 60f; } } // Next player's offset playerPlacementOffset += 200f; }