public Vector3 GetTargetLocation(ST_PuzzleTileHyper thisTile) { // check if we can move this tile and get the position we can move to. ST_PuzzleTileHyper 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); }
private void CreatePuzzleTiles(int newPuzzleIndex) { // 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; //currentPuzzleImageIndex = Random.Range(0, PuzzleImages.Length - 1); currentPuzzleImageIndex = newPuzzleIndex; Texture puzzleImage = PuzzleImages[newPuzzleIndex]; // 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_PuzzleTileHyper thisTile = TileDisplayArray[i, j].GetComponent <ST_PuzzleTileHyper>(); 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; */ }