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;
         */
    }
    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);
    }
Exemple #3
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);
        }
    }
    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);
    }
Exemple #5
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;
                }
            }
        }
    }