/// <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(); } } } }
/// <summary> /// Updates the list of selectable cases for the current pawn, /// according to the relative cases, the expansion, if it can jump /// and if it can dig. /// </summary> public void UpdateSelectableCases() { moveCases.Clear(); foreach (Move mv in moveCasesIn) { if (!mv.expansion) // No expansion, just once { Position posFinale = this.Position.board.GetPositionFromTo(this.Position.coo, mv.position); moveCases.Add(posFinale); } else // Expansion, so as many times as the limit { Position current = new Position(this.Position.board, this.Position.coo); bool canContinue = true; int[,] convert = new int[2, 2] { { 1, 0 }, { 0, 1 } }; int[,] tmpMatrix = new int[2, 2]; Vector2 newMove; int moveX; int moveY; IList <int[, ]> convertMatrices; for (int i = 0; i < mv.limit && canContinue; i++) { //convert the vector into the new coordinate system moveX = (int)(mv.position.x * convert[0, 0] + mv.position.y * convert[1, 0]); moveY = (int)(mv.position.x * convert[0, 1] + mv.position.y * convert[1, 1]); newMove = new Vector2(moveX, moveY); //prepare the list of matrices convertMatrices = new List <int[, ]>(); current = current.board.GetPositionFromTo(current.coo, newMove, ref convertMatrices); moveCases.Add(current); //rebuild the convert matrix for the next movement foreach (int[,] convertMatrix in convertMatrices) { tmpMatrix[0, 0] = convert[0, 0] * convertMatrix[0, 0] + convert[0, 1] * convertMatrix[1, 0]; tmpMatrix[0, 1] = convert[0, 0] * convertMatrix[0, 1] + convert[0, 1] * convertMatrix[1, 1]; tmpMatrix[1, 0] = convert[1, 0] * convertMatrix[0, 0] + convert[1, 1] * convertMatrix[1, 0]; tmpMatrix[1, 1] = convert[1, 0] * convertMatrix[0, 1] + convert[1, 1] * convertMatrix[1, 1]; convert = (int[, ])tmpMatrix.Clone(); } if (pp != null) { if ((pp.IsThereAPawn(current)) && !canJump) // if the pawn can't jump, then we don't go further in this position { canContinue = false; } } } } } // if canDig is activated for this pawn, then the square at the opposite of the current position is also an available movement if (canDig) { Vector3 origin = this.transform.position; // position of the current game object (pawn) Vector3 direction = this.Position.board.SquaresOfTheBoard[this.Position.coo].transform.up; // up of the square where the pawn is placed Ray rayToSelect = new Ray(origin, direction); // origin + direction make the ray RaycastHit hit = new RaycastHit(); // future object hit LayerMask mask = -1; if (Physics.Raycast(rayToSelect, out hit, float.MaxValue, mask.value)) // hit game object { SquareElement ce = hit.transform.gameObject.GetComponent <SquareElement>(); // the element just hit must be a square element if (ce.Position.board != this.Position.board) // the square element juste hit must be on a different board { moveCases.Add(new Position(ce.Position.board, ce.Position.coo)); // we add it to the possible move cases. } } } }