/// <summary> /// Move a pawn from its square to the other square /// </summary> /// <param name="pawn">the pawn we want to move</param> /// <param name="square">the square where we want to move the pawn</param> public bool Move(PawnElement pawn, ChessElement square) { // It is a possible move for this Pawn Element if (pawn.MoveCases.Contains(square.Position)) { // Check if there is a pawn at the destination => if yes, we can only eat it (if it's on a different team) if (positionPawns.GetPositions[square.Position] != null) { PawnElement pe = positionPawns.GetPositions[square.Position].GetComponent <PawnElement>(); // We can only eat if the teams are different if (pe.GetTeam != pawn.GetTeam) { eat.Eat(positionPawns.GetPositions, square.Position); // The pawn is eaten but the actual movement is yet to be done } else { // the pawn cannot be eaten => the movement can't be finished return(false); } } positionPawns.GetPositions[square.Position] = pawn.gameObject; positionPawns.GetPositions[pawn.Position] = null; //change the position that the pawn knows pawn.Position = square.Position; positionPawn.PutPawnIntoPosition(pawn.gameObject); pawn.UpdateSelectableCases(); return(true); } return(false); }
/// <summary> /// Put a game object at its right position on the chessboard /// </summary> /// <param name="pawnToPositionate">the gameobject to positionate</param> public void PutPawnIntoPosition(GameObject pawnToPositionate) { Position pos = pawnToPositionate.GetComponent <ChessElement>().Position; Vector2 position = pos.coo; GameObject square = pos.board.GetSquare(position); Vector3 p = square.transform.position; int x = (int)p.x; int y = (int)p.y; int z = (int)p.z; pawnToPositionate.transform.position = new Vector3(x, y, z); pawnToPositionate.transform.up = -square.transform.up; // The orientation is unchanged, must be set in the prefab // About the rotation PawnElement pawnElement = pawnToPositionate.GetComponent <PawnElement>(); if (pawnElement != null) { if (pawnElement.GetTeam != 0) { pawnToPositionate.transform.Rotate(new Vector3(0, 180, 0)); } } }
/// <summary> /// Catch the input of the mouse and use it /// to select an object /// </summary> void Update() { //Fire1 is the button for selecting if (Input.GetButtonDown("Fire1")) { bool aSelectionHasBeenDone = select[0].LaunchSelect(Input.mousePosition); //If we select a pawn, the possible movement will have a special color if (aSelectionHasBeenDone) { //a new selection has been done, so we remove the previous one ResetPossibleMovementColor(); if (select[0].HasSthSelected) { PawnElement element1 = select[0].LastSelected.GetComponent <PawnElement>(); //put a special color on the squares you can move foreach (Position pos in element1.MoveCases) { pos.board.GetSquare(pos.coo).GetComponent <Renderer>().material.color = possibleSquaresColor; } } } //we need to have the first select activated to select a second gameobject //and we need to have selected a gameobject before this frame if (select[0].HasSthSelected && !aSelectionHasBeenDone) { select[1].LaunchSelect(Input.mousePosition); } //we remove the selection of the second gameobject if the first is unselected and //if a second gameobject has been selected else if (select[1].HasSthSelected) { select[1].RemoveSelection(); } } //Submit is the button for the validation of our movement if (Input.GetButtonDown("Submit")) { //we have to select 2 gameobject before the validation of the movement if (select[0].HasSthSelected && select[1].HasSthSelected) { PawnElement element1 = select[0].LastSelected.GetComponent <PawnElement>(); if (element1.GetTeam == teamTurn.GetTeamTurn) { SquareElement element2 = select[1].LastSelected.GetComponent <SquareElement>(); //do the move; eat the pawn at the selected square if needed if (move.Move(element1, element2)) { //we notify to all the observers that a move has been done NotifyAll(); } ResetAllSelection(); } } } }
public override bool CanSelect(GameObject newSelected) { if (base.CanSelect(newSelected)) { PawnElement pElem = newSelected.GetComponent <PawnElement>(); return(pElem.GetTeam == teamTurn.GetTeamTurn); } return(false); }
/// <summary> /// Eats the pawn at the given position. Makes it disappear from the board. /// Checking if the pawn is on a different pawn is done before. /// </summary> /// <param name="allPawns"> list of all the pawns</param> /// <param name="pos">the position of the pawn to eat</param> public void Eat(IDictionary <Position, GameObject> allPawns, Position pos) { GameObject go; if (allPawns.TryGetValue(pos, out go)) { // Checking if the pawn to be eaten is the target if (go == goal.GetGoal1 || go == goal.GetGoal2) { goal.ToDisplay = "La partie est finie, "; PawnElement pe = go.GetComponent <PawnElement>(); goal.ToDisplay += "le joueur " + pe.GetTeam + " a gagné !"; // TODO Stop the game ? } // Destroying the game object Destroy(go); // Destroying the GO does not set the position null apparently, so allPawns[pos] = null; } }