// Use this for initialization void Start() { ST_PuzzleDisplay.PuzzleMoves = 0; ST_PuzzleDisplay.CanMove = false; ST_PuzzleDisplay.CanCount = false; ST_PuzzleDisplay.Moves.Clear(); //Texture2D encryptImg = PuzzleImage as Texture2D; Instance = this; AudioSource = GetComponent <AudioSource>(); PuzzleImage = PuzzleImages[UnityEngine.Random.Range(0, PuzzleImages.Length)]; PuzzleImage = Steganography.Encode(PuzzleImage, PuzzleManager.CurrentHash); // create the games puzzle tiles from the provided image. CreatePuzzleTiles2(); // mix up the puzzle. //StartCoroutine(JugglePuzzle()); //SetInitialState(PuzzleManager.PuzzleData.field); StartCoroutine(CheckForComplete()); CanMove = true; CanCount = true; LetsgoPopup.EnableFor(1); GameTimerUpdater.StartTimer(); }
// new ] // REFACTOR 2 ] private void SetInitialState(List <int> state) { int maxIndex = (Height * Width) - 1; if (state.Count - 1 == maxIndex) { Vector3[] positions = new Vector3[maxIndex + 1]; for (int i = 0; i <= maxIndex; i++) { var xy = ConvertIndexToGrid(i); var tile = TileDisplayArray[(int)xy.x, (int)xy.y]; var tilePosition = tile.GetComponent <ST_PuzzleTile>().TargetPosition; positions[i] = new Vector3(tilePosition.x, tilePosition.y, tilePosition.z); } GameObject[,] newTiles = new GameObject[Width, Height]; for (int newIndex = 0; newIndex <= maxIndex; newIndex++) { var oldIndex = state[newIndex]; var newCoordinates = ConvertIndexToGrid(newIndex); var oldCoordinates = ConvertIndexToGrid(oldIndex); if (oldIndex == -1) { var oldTile = TileDisplayArray[(int)newCoordinates.x, (int)newCoordinates.y]; newTiles[(int)newCoordinates.x, (int)newCoordinates.y] = oldTile; oldTile.GetComponent <ST_PuzzleTile>().Active = false; } else { var oldTile = TileDisplayArray[(int)oldCoordinates.x, (int)oldCoordinates.y]; newTiles[(int)newCoordinates.x, (int)newCoordinates.y] = oldTile; oldTile.GetComponent <ST_PuzzleTile>().GridLocation = new Vector2(newCoordinates.x, newCoordinates.y); var position = positions[newIndex]; oldTile.GetComponent <ST_PuzzleTile>().transform.localPosition = new Vector3(position.x, position.y, position.z); oldTile.GetComponent <ST_PuzzleTile>().TargetPosition = new Vector3(position.x, position.y, position.z); } } TileDisplayArray = newTiles; StartCoroutine(CheckForComplete()); CanMove = true; CanCount = true; LetsgoPopup.EnableFor(1); GameTimerUpdater.StartTimer(); return; } throw new ArgumentException("The initial state size does not match with puzzle size", nameof(state)); }
protected void Update() { if (Input.GetKeyUp(KeyCode.F)) { Rotate(RubikFaceType.Front, true); } if (Input.GetKeyUp(KeyCode.L)) { Rotate(RubikFaceType.Left, false); } if (Input.GetKeyUp(KeyCode.R)) { Rotate(RubikFaceType.Right, false); } if (Input.GetKeyUp(KeyCode.T)) { Rotate(RubikFaceType.Top, false); } if (Input.GetKeyUp(KeyCode.B)) { Rotate(RubikFaceType.Bottom, false); } if (Input.GetKeyUp(KeyCode.U)) { Rotate(RubikFaceType.Back, false); } if (_shuffleMode) { if (_shuffleCounter < _numberOfShuffles && _canMove) { var type = (RubikFaceType)_moveTypes.GetValue(UnityEngine.Random.Range(0, _moveTypes.Length - 1)); var clockwise = (UnityEngine.Random.value <= 0.5f); _isShuffling = true; Rotate(type, clockwise, () => { _shuffleCounter++; _isShuffling = false; }); } } if (_shuffleMode && _shuffleCounter >= _numberOfShuffles) { GameTimerUpdater.StartTimer(); _shuffleMode = false; LetsgoPopup.EnableFor(1); ControlsManager.CanMove = true; } }
protected void Awake() { _audioSource = GetComponent <AudioSource>(); StartCoroutine(WebServiceManager.GetTopScores((scores) => { int index = 0; foreach (var topScore in scores) { LeaderboardManager.Instance.SetEntry(index, topScore.PlayerAddress, topScore.Score); index++; } })); Grid = new MatchingGrid((int)Size.x, (int)Size.y, TilePrefabs, Border, Angle, BorderBall, this, ref Image); LetsGoPopup.EnableFor(1); GameTimerUpdater.StartTimer(); }
private IEnumerator JugglePuzzle() { ShufflingPopup.EnableFor(4); yield return(new WaitForSeconds(1.0f)); int tileToHideX = UnityEngine.Random.Range(0, Width); int tileToHideY = UnityEngine.Random.Range(0, Height); // hide a puzzle tile (one is always missing to allow the puzzle movement). TileDisplayArray[tileToHideX, tileToHideY].GetComponent <ST_PuzzleTile>().Active = false; yield return(new WaitForSeconds(1.0f)); int howManyShuffles = UnityEngine.Random.Range(20, 35); for (int k = 0; k < howManyShuffles; k++) { // use random to position each puzzle section in the array delete the number once the space is filled. for (int j = 0; j < Height; j++) { for (int i = 0; i < Width; i++) { // attempt to execute a move for this tile. TileDisplayArray[i, j].GetComponent <ST_PuzzleTile>().ExecuteAdditionalMove(); yield return(new WaitForSeconds(0.02f)); } } } // continually check for the correct answer. StartCoroutine(CheckForComplete()); yield return(null); CanMove = true; CanCount = true; LetsgoPopup.EnableFor(1); GameTimerUpdater.StartTimer(); }
protected void Update() { if (Input.GetKeyUp(KeyCode.F)) { Rotate(RubikFaceType.Front, true); } if (Input.GetKeyUp(KeyCode.L)) { Rotate(RubikFaceType.Left, false); } if (Input.GetKeyUp(KeyCode.R)) { Rotate(RubikFaceType.Right, false); } if (Input.GetKeyUp(KeyCode.T)) { Rotate(RubikFaceType.Top, false); } if (Input.GetKeyUp(KeyCode.B)) { Rotate(RubikFaceType.Bottom, false); } if (Input.GetKeyUp(KeyCode.U)) { Rotate(RubikFaceType.Back, false); } // SHUFFLE [ if (_shuffleMode) { if (_shuffleCounter < _numberOfShuffles && _canMove) { var mv = PuzzleManager.PuzzleData.puzzleField[_shuffleCounter]; // var mv = UnityEngine.Random.Range(0, _moveTypes.Length - 1); if (mv >= _moveTypes.Length) { // todo: fix it // error(), but continue because validation on hipr-restful Debug.Log("Incorrect puzzle puzzleId=" + PuzzleManager.PuzzleData.puzzleId + "mv=" + mv); mv = 0; } var type = (RubikFaceType)_moveTypes.GetValue(mv); var clockwise = (UnityEngine.Random.value <= 0.5f); _isShuffling = true; Rotate(type, clockwise, () => { _shuffleCounter++; _isShuffling = false; }); } } if (_shuffleMode && _shuffleCounter >= _numberOfShuffles) { GameTimerUpdater.StartTimer(); _shuffleMode = false; LetsgoPopup.EnableFor(1); ControlsManager.CanMove = true; } // SHUFFLE ] }