예제 #1
0
    private void CreatePuzzleTiles()
    {
        // using the width and height variables create an array.
        TileDisplayArray = new GameObject[Width, Height];

        // set the scale and position values for this puzzle.
        Scale = new Vector3(1.0f / Width, 1.0f, 1.0f / Height);
        Tile.transform.localScale = Scale;

        // used to count the number of tiles and assign each tile a correct value.
        int TileValue = 0;

        // spawn the tiles into an array.
        for (int j = Height - 1; j >= 0; j--)
        {
            for (int i = 0; i < Width; i++)
            {
                // calculate the position of this tile all centred around Vector3(0.0f, 0.0f, 0.0f).
                Position = new Vector3(((Scale.x * (i + 0.5f)) - (Scale.x * (Width / 2.0f))) * (10.0f + SeperationBetweenTiles),
                                       0.0f,
                                       ((Scale.z * (j + 0.5f)) - (Scale.z * (Height / 2.0f))) * (10.0f + SeperationBetweenTiles));

                // set this location on the display grid.
                DisplayPositions.Add(Position);

                // spawn the object into play.
                TileDisplayArray[i, j] = Instantiate(Tile, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.Euler(90.0f, -180.0f, 0.0f)) as GameObject;
                TileDisplayArray[i, j].gameObject.transform.parent = this.transform;

                // set and increment the display number counter.
                ST_PuzzleTile thisTile = TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>();
                thisTile.ArrayLocation = new Vector2(i, j);
                thisTile.GridLocation  = new Vector2(i, j);
                thisTile.LaunchPositionCoroutine(Position);
                TileValue++;

                // create a new material using the defined shader.
                Material thisTileMaterial = new Material(PuzzleShader);

                // apply the puzzle image to it.
                thisTileMaterial.mainTexture = PuzzleImage;

                // set the offset and tile values for this material.
                thisTileMaterial.mainTextureOffset = new Vector2(1.0f / Width * i, 1.0f / Height * j);
                thisTileMaterial.mainTextureScale  = new Vector2(1.0f / Width, 1.0f / Height);

                // assign the new material to this tile for display.
                TileDisplayArray[i, j].GetComponent <Renderer>().material = thisTileMaterial;
            }
        }

        /*
         * // Enable an impossible puzzle for fun!
         * // switch the second and third grid location textures.
         * Material thisTileMaterial2 = TileDisplayArray[1,3].GetComponent<Renderer>().material;
         * Material thisTileMaterial3 = TileDisplayArray[2,3].GetComponent<Renderer>().material;
         * TileDisplayArray[1,3].GetComponent<Renderer>().material = thisTileMaterial3;
         * TileDisplayArray[2,3].GetComponent<Renderer>().material = thisTileMaterial2;
         */
    }
예제 #2
0
    private ST_PuzzleTile CheckIfWeCanMove(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        // check each movement direction
        if (CheckMoveLeft(Xpos, Ypos, thisTile) != thisTile)
        {
            return(CheckMoveLeft(Xpos, Ypos, thisTile));
        }

        if (CheckMoveRight(Xpos, Ypos, thisTile) != thisTile)
        {
            return(CheckMoveRight(Xpos, Ypos, thisTile));
        }

        if (CheckMoveDown(Xpos, Ypos, thisTile) != thisTile)
        {
            return(CheckMoveDown(Xpos, Ypos, thisTile));
        }

        if (CheckMoveUp(Xpos, Ypos, thisTile) != thisTile)
        {
            return(CheckMoveUp(Xpos, Ypos, thisTile));
        }

        return(thisTile);
    }
    private ST_PuzzleTile CheckIfWeCanMove(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        // check each movement direction
        if (CheckMoveLeft(Xpos, Ypos, thisTile) != thisTile)
        {
            tilePlace[EmptySpace % 3, EmptySpace / 3] = tilePlace[Xpos, Ypos];
            EmptySpace++;
            return(CheckMoveLeft(Xpos, Ypos, thisTile));
        }

        if (CheckMoveRight(Xpos, Ypos, thisTile) != thisTile)
        {
            tilePlace[EmptySpace % 3, EmptySpace / 3] = tilePlace[Xpos, Ypos];
            EmptySpace--;
            return(CheckMoveRight(Xpos, Ypos, thisTile));
        }

        if (CheckMoveDown(Xpos, Ypos, thisTile) != thisTile)
        {
            tilePlace[EmptySpace % 3, EmptySpace / 3] = tilePlace[Xpos, Ypos];
            EmptySpace += 3;
            return(CheckMoveDown(Xpos, Ypos, thisTile));
        }

        if (CheckMoveUp(Xpos, Ypos, thisTile) != thisTile)
        {
            tilePlace[EmptySpace % 3, EmptySpace / 3] = tilePlace[Xpos, Ypos];
            EmptySpace -= 3;
            return(CheckMoveUp(Xpos, Ypos, thisTile));
        }

        return(thisTile);
    }
예제 #4
0
    public Vector3 GetTargetLocation(ST_PuzzleTile thisTile)
    {
        // check if we can move this tile and get the position we can move to.
        ST_PuzzleTile MoveTo = CheckIfWeCanMove((int)thisTile.GridLocation.x, (int)thisTile.GridLocation.y, thisTile);

        if (MoveTo != thisTile)
        {
            // get the target position for this new tile.
            Vector3 TargetPos    = MoveTo.TargetPosition;
            Vector2 GridLocation = thisTile.GridLocation;
            thisTile.GridLocation = MoveTo.GridLocation;

            // move the empty tile into this tiles current position.
            MoveTo.LaunchPositionCoroutine(thisTile.TargetPosition);
            MoveTo.GridLocation = GridLocation;

            if (countMoves)
            {
                moveCounter++;

                if (MovesCounterDisplay != null)
                {
                    MovesCounterDisplay.text = moveCounter.ToString();
                }
            }

            // return the new target position.
            return(TargetPos);
        }

        // else return the tiles actual position (no movement).
        return(thisTile.TargetPosition);
    }
예제 #5
0
    private ST_PuzzleTile CheckMoveRight(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        // move right
        if ((Xpos + 1) < Width)
        {
            // we can move right, is the space currently being used?
            return(GetTileAtThisGridLocation(Xpos + 1, Ypos, thisTile));
        }

        return(thisTile);
    }
예제 #6
0
    private ST_PuzzleTile CheckMoveLeft(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        // move left
        if ((Xpos - 1) >= 0)
        {
            // we can move left, is the space currently being used?
            return(GetTileAtThisGridLocation(Xpos - 1, Ypos, thisTile));
        }

        return(thisTile);
    }
예제 #7
0
    private ST_PuzzleTile CheckMoveUp(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        // move up
        if ((Ypos + 1) < Height)
        {
            // we can move up, is the space currently being used?
            return(GetTileAtThisGridLocation(Xpos, Ypos + 1, thisTile));
        }

        return(thisTile);
    }
예제 #8
0
	private ST_PuzzleTile CheckMoveDown(int Xpos, int Ypos, ST_PuzzleTile thisTile)
	{
		// move down 
		if((Ypos - 1)  >= 0)
		{
			// we can move down, is the space currently being used?
			return GetTileAtThisGridLocation(Xpos, Ypos  - 1, thisTile);
		}
		
		return thisTile;
	}
예제 #9
0
    public bool CanPuzzleMove(ST_PuzzleTile thisTile)
    {
        ST_PuzzleTile MoveTo = CheckIfWeCanMove((int)thisTile.GridLocation.x, (int)thisTile.GridLocation.y, thisTile);

        if (MoveTo == thisTile)
        {
            return(false);
        }
        else
        {
            return(true);
        }
    }
예제 #10
0
    private ST_PuzzleTile GetTileByXY(int x, int y)
    {
        if (x < 0 || x > Width || y < 0 || y > Height)
        {
            return(null);
        }

        int i = x % Width;
        int j = y / Width;

        tileLast = TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>();
        return(tileLast);
    }
예제 #11
0
    private ST_PuzzleTile CheckIfWeCanMove1(int Xpos, int Ypos, ST_PuzzleTile thisTile)
    {
        int x = Xpos;
        int y = Ypos;

        if (GetTileByXY(x - 1, y) ||
            GetTileByXY(x + 1, y) ||
            GetTileByXY(x, y - 1) ||
            GetTileByXY(x, y + 1))
        {
            return(tileLast);
        }
        return(thisTile);
    }
예제 #12
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray = new Ray(cam.transform.position, cam.transform.forward);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 2.0f))
            {
                ST_PuzzleTile temp = hit.collider.GetComponent <ST_PuzzleTile>();

                // Call detectedItem.onFocus() or whatever so that updates the UI instead of the player class
                if (temp != null)
                {
                    // get the puzzle display and return the new target location from this tile.
                    temp.LaunchPositionCoroutine(GameObject.Find("Slide Puzzle").GetComponent <ST_PuzzleDisplay>().GetTargetLocation(temp));
                }
            }
        }

        if (Input.GetKeyDown("e"))
        {
            if (itemDetector.getDetectedItem() != null && itemDetector.getDetectedItem().isActive)
            {
                itemDetector.getDetectedItem().onInteract();
            }
        }
        else if (Input.GetKeyDown("1"))
        {
            Inventory.instance.setActiveItem(0);
        }
        else if (Input.GetKeyDown("2"))
        {
            Inventory.instance.setActiveItem(1);
        }
        else if (Input.GetKeyDown("3"))
        {
            Inventory.instance.setActiveItem(2);
        }
        else if (Input.GetKeyDown("4"))
        {
            Inventory.instance.setActiveItem(3);
        }
        else if (Input.GetKeyDown("5"))
        {
            Inventory.instance.setActiveItem(4);
        }
    }
예제 #13
0
    private ST_PuzzleTile GetTileAtThisGridLocation(int x, int y, ST_PuzzleTile thisTile)
    {
        for (int j = Height - 1; j >= 0; j--)
        {
            for (int i = 0; i < Width; i++)
            {
                // check if this tile has the correct grid display location.
                if ((TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>().GridLocation.x == x) &&
                    (TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>().GridLocation.y == y))
                {
                    if (TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>().Active == false)
                    {
                        // return this tile active property.
                        return(TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>());
                    }
                }
            }
        }

        return(thisTile);
    }
예제 #14
0
    public Vector3 GetTargetLocation(ST_PuzzleTile thisTile)
    {
        // check if we can move this tile and get the position we can move to.
        ST_PuzzleTile MoveTo = CheckIfWeCanMove((int)thisTile.GridLocation.x, (int)thisTile.GridLocation.y, thisTile);

        if (MoveTo != thisTile)
        {
            // get the target position for this new tile.
            Vector3 TargetPos    = MoveTo.TargetPosition;
            Vector2 GridLocation = thisTile.GridLocation;
            thisTile.GridLocation = MoveTo.GridLocation;

            // move the empty tile into this tiles current position.
            MoveTo.LaunchPositionCoroutine(thisTile.TargetPosition, moveSpeed);
            MoveTo.GridLocation = GridLocation;

            // return the new target position.
            return(TargetPos);
        }

        // else return the tiles actual position (no movement).
        return(thisTile.TargetPosition);
    }
예제 #15
0
    /*
     * private Vector2 ConvertIndexToGrid(int i)
     * {
     *  int x = i % Width;
     *  int y = i / Width;
     *  return new Vector2(x, y);
     * }
     */

    // REFACTOR 1 ]


    private void CreatePuzzleTiles2()
    {
        // using the width and height variables create an array.
        TileDisplayArray = new GameObject[Width, Height];

        // set the scale and position values for this puzzle.
        Scale = new Vector3(1.0f / Width, 1.0f, 1.0f / Height);
        Tile.transform.localScale = Scale;

        var newIndexes        = PuzzleManager.PuzzleData.puzzleField;
        var newIndexesCounter = 0;

        // spawn the tiles into an array.
        for (int j = 0; j < Height; j++)
        {
            for (int i = 0; i < Width; i++)
            {
                var newIndex       = newIndexes[newIndexesCounter++];
                var newCoordinates = ConvertIndexToGrid(newIndex);
                var x = (int)newCoordinates.x;
                var y = (int)newCoordinates.y;

                // calculate the position of this tile all centred around Vector3(0.0f, 0.0f, 0.0f).
                Position = new Vector3(((Scale.x * (i + 0.5f)) - (Scale.x * (Width / 2.0f))) * (10.0f + SeperationBetweenTiles),
                                       0.0f,
                                       ((Scale.z * (j + 0.5f)) - (Scale.z * (Height / 2.0f))) * (10.0f + SeperationBetweenTiles));

                // set this location on the display grid.
                DisplayPositions.Add(Position);

                // spawn the object into play.
                TileDisplayArray[i, j] = Instantiate(Tile, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.Euler(90.0f, -180.0f, 0.0f)) as GameObject;
                TileDisplayArray[i, j].gameObject.transform.parent = this.transform;

                // set and increment the display number counter.
                ST_PuzzleTile thisTile = TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>();
                thisTile.ArrayLocation   = new Vector2(x, y);
                thisTile.GridLocation    = new Vector2(i, j);
                thisTile.CorrectLocation = thisTile.ArrayLocation == thisTile.GridLocation;
                thisTile.LaunchPositionCoroutine(Position);

                // create a new material using the defined shader.
                Material thisTileMaterial = new Material(PuzzleShader);

                // apply the puzzle image to it.
                thisTileMaterial.mainTexture = PuzzleImage;

                // set the offset and tile values for this material.
                thisTileMaterial.mainTextureOffset = new Vector2(1.0f / Width * x, 1.0f / Height * y);
                thisTileMaterial.mainTextureScale  = new Vector2(1.0f / Width, 1.0f / Height);

                // assign the new material to this tile for display.
                TileDisplayArray[i, j].GetComponent <Renderer>().material = thisTileMaterial;

                if (newIndex == 0)
                {
                    thisTile.Active = false;
                }
            }
        }
    }
예제 #16
0
    // Randomize a 2D array.
    private IEnumerator randomize()
    {
        //don't count these moves for the user
        countMoves = false;

        //yield return new WaitForSeconds(2.0f);

        // hide a puzzle tile (one is always missing to allow the puzzle movement).
        TileDisplayArray[Random.Range(0, Width), Random.Range(0, Height)].GetComponent <ST_PuzzleTile>().Active = false;

        // Get the dimensions.
        int num_rows  = TileDisplayArray.GetUpperBound(0) + 1;
        int num_cols  = TileDisplayArray.GetUpperBound(1) + 1;
        int num_cells = num_rows * num_cols;

        for (int x = 0; x < num_cells * 2; x++)
        {
            // Randomize the array.
            System.Random rand = new System.Random();
            for (int i = 0; i < num_cells - 1; i++)
            {
                // Pick a random cell between i and the end of the array.
                int j = rand.Next(i, num_cells);

                // Convert to row/column indexes.
                int row_i = i / num_cols;
                int col_i = i % num_cols;
                int row_j = j / num_cols;
                int col_j = j % num_cols;

                // Swap cells i and j.


                ST_PuzzleTile thisTile1 = TileDisplayArray[row_i, col_i].GetComponent <ST_PuzzleTile>();
                ST_PuzzleTile thisTile2 = TileDisplayArray[row_j, col_j].GetComponent <ST_PuzzleTile>();
                //Vector2 tempArrayLocation1 = thisTile1.ArrayLocation;
                //Vector2 tempGridLocation1 = thisTile1.GridLocation;
                //Vector3 localPosition = thisTile1.transform.localPosition;

                thisTile1.ExecuteAdditionalMove(40f);
                thisTile2.ExecuteAdditionalMove(40f);

                yield return(new WaitForSeconds(0.0001f));

                //thisTile1.transform.localPosition = thisTile2.transform.localPosition;
                //thisTile2.transform.localPosition = localPosition;

                //thisTile1.ArrayLocation = thisTile2.ArrayLocation;
                //thisTile1.GridLocation = thisTile2.GridLocation;

                //thisTile2.ArrayLocation = tempArrayLocation1;
                //thisTile2.GridLocation = tempGridLocation1;

                //GameObject tempTile1 = TileDisplayArray[row_i, col_i];
                //TileDisplayArray[row_i, col_i] = TileDisplayArray[row_j, col_j];
                //TileDisplayArray[row_j, col_j] = tempTile1;

                //thisTile1.LaunchPositionCoroutine(thisTile2.transform.localPosition);
                //thisTile2.LaunchPositionCoroutine(localPosition);
            }
        }

        Complete = false;

        moveCounter = 0;
        countMoves  = true;

        MovesCounterDisplay.text = "0";

        // continually check for the correct answer.
        StartCoroutine(CheckForComplete());
        gameStarted = true;
        startTime   = Time.time;

        yield return(null);
    }