public Piece(int x, int y, int x0, int y0, int x1, int y1) { SetType(Type.FILL); SetDirection(Board.Direction.RIGHT); boardCoords = new BoardCoords(x, y); cursorCoords = new CursorCoords(x0, y0, x1, y1); }
private void InitializePieces() { int pieceLayer = LayerMask.NameToLayer("Pieces"); for (int playerNr = 0; playerNr < GameLogic.NumPlayers; playerNr++) { var playerColor = BoardCoords.playerOrder[playerNr]; var prefabName = "piece" + playerColor.Substring(0, 1).ToUpper() + playerColor.Substring(1); var prefab = Resources.Load(prefabName) as GameObject; for (int pieceNr = 0; pieceNr < GameLogic.NumPiecesPerPlayer; pieceNr++) { var pos = GetPieceCoords(playerNr, pieceNr); // The piece needs to be in an empty parent object so that its animations are local // to the parent's transformation. That way we can move the parent around and animation // of the piece will happen in the position of the parent and not the center of the board. var parent = new GameObject("pieceParent"); parent.transform.position = pos; var piece = Instantiate(prefab, pos, Quaternion.identity).gameObject; piece.transform.parent = parent.transform; piece.AddComponent <PieceAnimation>(); piece.SetActive(true); // Add animator and animation controller var animator = piece.AddComponent <Animator>(); animator.runtimeAnimatorController = Resources.Load <RuntimeAnimatorController>("Anim/pieceBase"); // To test that the animator works: //animator.SetBool("Jump", true); // Put all pieces on a layer, for raycasting. piece.layer = pieceLayer; foreach (Transform child in piece.transform) { child.gameObject.layer = pieceLayer; } m_pieces.Add(piece); } } return; Vector3 GetPieceCoords(int playerNr, int pieceNr) { var piecePos = m_gameLogic.GetPiecePos(playerNr * GameLogic.NumPiecesPerPlayer + pieceNr); if (piecePos < 0) { return(BoardCoords.getHomeCoords(playerNr, pieceNr)); } else { return(BoardCoords.getPositionCoords(piecePos)); } } }
IEnumerator PieceMove(GameLogic.Move move) { var piece = m_pieces[move.pieceNr]; Vector3 newCoords; if (move.toPos < GameLogic.BoardSize) { newCoords = BoardCoords.getPositionCoords(move.toPos); } else { newCoords = BoardCoords.getTargetCoords(m_gameLogic.CurrentPlayer, move.toPos % 1000); } GameObject bumpedPiece = null; Vector3? bumpTargetPos = null; if (move.pieceOnTargetPos.HasValue) { var bumpedPieceIdx = move.pieceOnTargetPos.Value; var bumpedOwner = m_gameLogic.WhosePlayerIsPiece(bumpedPieceIdx); var bumpHomePos = (-m_gameLogic.FindFreeHomePos(bumpedOwner)) - 1; bumpedPiece = m_pieces[bumpedPieceIdx]; bumpTargetPos = BoardCoords.getHomeCoords(bumpedOwner, bumpHomePos); } m_move = new MovingPiece() { piece = piece, start = piece.transform.parent.position, target = newCoords, phase = 0f }; yield return(new WaitForSeconds(m_jumpLength)); if (bumpedPiece != null) { bumpedPiece.transform.parent.position = bumpTargetPos.Value; } var result = m_gameLogic.ExecuteMove(move); if (result.gameOver != null) { Debug.Log("Game over"); GameOver(); } else { StartCoroutine(NextPlayer()); } }
/*** * Called whenever the player clicks a rune. * * @param rune The rune that was clicked. * */ public void OnRuneClick(Rune rune) { if (gameState != GameState.Ready) { return; } if (paused) { return; } if (clickedRune == null) { // A rune has not been clicked yet rune.ToggleGlow(true); clickedRune = rune; swappedRune = null; PlaySound(SoundClips.Select); } else { // A rune has already been clicked // Did the player click the same rune? if (clickedRune == rune) { return; } // Check if the runes are adjacent to each other if (BoardCoords.areCoordsAdjacent(clickedRune.coords, rune.coords)) { // A valid pair was selected, swap em' gameState = GameState.Swapping; swappedRune = rune; AnimateRuneSwap(clickedRune, swappedRune); } else { // An invalid pair was selected // De-select the current rune clickedRune.ToggleGlow(false); // Select the rune just clicked rune.ToggleGlow(true); clickedRune = rune; swappedRune = null; PlaySound(SoundClips.Select); } } }
public static bool areCoordsAdjacent(BoardCoords c1, BoardCoords c2) { // Are they on the same row and adjacent? if (c1.x == c2.x && (c2.y == c1.y - 1 || c2.y == c1.y + 1)) { return(true); } // Are they on the same column and adjacent? if (c1.y == c2.y && (c2.x == c1.x - 1 || c2.x == c1.x + 1)) { return(true); } return(false); }