Exemplo n.º 1
0
        void OnTileClicked(SlidingTile clickedTile)
        {
            if (_isComplete)
            {
                return;
            }

            Vector2 gridPos = GridPositionForTileIndex(clickedTile.Index);
            int     tileX   = (int)gridPos.x;
            int     tileY   = (int)gridPos.y;

            // poll all four directions for an opent spot
            bool left  = CanMoveLeft(tileX, tileY);
            bool right = CanMoveRight(tileX, tileY);
            bool up    = CanMoveUp(tileX, tileY);
            bool down  = CanMoveDown(tileX, tileY);

            bool canMoveToOpenSpace = (left || right || up || down);

            if (canMoveToOpenSpace)
            {
                SwapTileWithOpenSpace(clickedTile);
                CheckComplete();
            }
        }
Exemplo n.º 2
0
        void SwapTileWithOpenSpace(SlidingTile clickedTile)
        {
            int     newOpenTileIndex = clickedTile.Index;
            int     newTileIndex     = _currentOpenTile.Index;
            Vector2 newOpenGridPos   = GridPositionForTileIndex(newOpenTileIndex);
            Vector3 newTileGridPos   = GridPositionForTileIndex(newTileIndex);

            _gameBoard[(int)newOpenGridPos.x, (int)newOpenGridPos.y] = _currentOpenTile;
            _gameBoard[(int)newTileGridPos.x, (int)newTileGridPos.y] = clickedTile;

            Vector3 newOpenTilePosition = PositionForTileIndex(newOpenTileIndex);
            Vector3 newTilePosition     = PositionForTileIndex(newTileIndex);

            clickedTile.MoveToPosition(newTileIndex, newTilePosition);
            _currentOpenTile.MoveToPosition(newOpenTileIndex, newOpenTilePosition);
        }
Exemplo n.º 3
0
        void CreatePuzzleBoard(RectTransform parent, Texture2D puzzleImage)
        {
            _canvas     = parent;
            _boardImage = puzzleImage;

            _tileWidth  = (_canvas.sizeDelta.x / _gridWidth);
            _tileHeight = (_canvas.sizeDelta.y / _gridHeight);

            _gameBoard = new SlidingTile[_gridWidth, _gridHeight];

            for (int y = 0; y < _gridHeight; y++)
            {
                for (int x = 0; x < _gridWidth; x++)
                {
                    CreateTile(x, y);
                    SetTileSprite(x, y);
                }
            }

            _currentOpenTile = _gameBoard[0, 0];
            _currentOpenTile.gameObject.SetActive(false);

            StartCoroutine(RandomizeInitialLayout());
        }