private ST_PuzzleTileHyper CheckIfWeCanMove(int Xpos, int Ypos, ST_PuzzleTileHyper 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); }
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 ST_PuzzleTileHyper CheckMoveUp(int Xpos, int Ypos, ST_PuzzleTileHyper 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); }
private ST_PuzzleTileHyper CheckMoveDown(int Xpos, int Ypos, ST_PuzzleTileHyper 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); }
private ST_PuzzleTileHyper CheckMoveRight(int Xpos, int Ypos, ST_PuzzleTileHyper 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); }
private ST_PuzzleTileHyper GetTileAtThisGridLocation(int x, int y, ST_PuzzleTileHyper 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_PuzzleTileHyper>().GridLocation.x == x) && (TileDisplayArray[i, j].GetComponent <ST_PuzzleTileHyper>().GridLocation.y == y)) { if (TileDisplayArray[i, j].GetComponent <ST_PuzzleTileHyper>().Active == false) { // return this tile active property. return(TileDisplayArray[i, j].GetComponent <ST_PuzzleTileHyper>()); } } } } return(thisTile); }
// 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_PuzzleTileHyper>().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_PuzzleTileHyper thisTile1 = TileDisplayArray[row_i, col_i].GetComponent <ST_PuzzleTileHyper>(); ST_PuzzleTileHyper thisTile2 = TileDisplayArray[row_j, col_j].GetComponent <ST_PuzzleTileHyper>(); //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); }
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; */ }