int RandomSwitch(int pos, int moves) { var startPos = pos; int endPos = startPos; var count = moves; print("Moves " + moves); while (count > 0) { endPos = ChooseRandomNeighbor(pos); pos = endPos; count--; } // Check for valid switch (max. 2 rows/cols distance) if (Mathf.Abs(GetRow(startPos) - GetRow(endPos)) >= moves || Mathf.Abs(GetCol(startPos) - GetCol(endPos)) >= moves) { return(endPos); } else { switchDirection = Swipes.Direction.NONE; return(RandomSwitch(pos, moves)); } }
public void SwitchCubes(Cube one, Cube two, Swipes.Direction direction) { if (one == null || two == null || (one.pos == two.pos)) { finishedAnimation = true; return; } if (one.pos - 1 == two.pos || one.pos + 1 == two.pos || one.pos + LevelManager.instance.boardWidth == two.pos || one.pos - LevelManager.instance.boardWidth == two.pos || one.pos + LevelManager.instance.boardWidth - 1 == two.pos || one.pos + LevelManager.instance.boardWidth + 1 == two.pos || one.pos - LevelManager.instance.boardWidth - 1 == two.pos || one.pos - LevelManager.instance.boardWidth + 1 == two.pos) { var tempPos = one.pos; one.pos = two.pos; two.pos = tempPos; var distance = (one.gameObject.transform.position - two.gameObject.transform.position).magnitude; var speed = distance / Constants.CubeSwitchTime; var time = distance / speed; iTween.MoveTo(one.gameObject, iTween.Hash("position", two.gameObject.transform.position, "time", time, "easetype", iTween.EaseType.linear)); iTween.MoveTo(two.gameObject, iTween.Hash("position", one.gameObject.transform.position, "time", time, "easetype", iTween.EaseType.linear, "oncomplete", "SetFinished", "oncompletetarget", gameObject)); UpdateMoveInfo(Constants.Powerup.Switch); if (LevelManager.instance.RecordMove()) { var lvlSpawner = (LevelSpawner)spawner; lvlSpawner.RecordMove(new PowerupExe(Constants.Powerup.Switch, two, direction)); } } }
protected void AnimateHand(int cubePos, Vector3 offset, Swipes.Direction moveDirection, float length, int angle) { StopAnimation(); var cube = cubes.Where(c => c.pos == cubePos).FirstOrDefault(); var pos = cube.transform.position + new Vector3(offset.x * Constants.CubeSizeX, offset.y * Constants.CubeSizeY); animator.SetPositions(pos, moveDirection, length, angle); animator.Animate(true); }
bool IsValidRotatePosition(int pos, Swipes.Direction direction) { bool isValidPosition = false; switch (direction) { case Swipes.Direction.LEFT: /* * X X X X X X * X O O O O O * X O O O O O * X O O O O O */ isValidPosition = GetRow(pos) < height - 1 && GetCol(pos) > 0; break; case Swipes.Direction.RIGHT: /* * X X X X X X * O O O O O X * O O O O O X * O O O O O X */ isValidPosition = GetRow(pos) < height - 1 && GetCol(pos) < width - 1; break; } if (!isValidPosition || positions.Count < 2) { return(false); } // 3 2 // 1 0 <-- left: @arg pos = 0, right: @arg pos = 1 var pos0 = direction == Swipes.Direction.LEFT ? pos : pos + 1; var pos1 = direction == Swipes.Direction.LEFT ? pos - 1 : pos; var pos2 = direction == Swipes.Direction.LEFT ? pos + width : pos + 1 + width; var pos3 = direction == Swipes.Direction.LEFT ? pos - 1 + width : pos + width; var valid0 = cubePositions[GetRow(pos0), GetCol(pos0)] != 0; var valid1 = cubePositions[GetRow(pos1), GetCol(pos1)] != 0; var valid2 = cubePositions[GetRow(pos2), GetCol(pos2)] != 0; var valid3 = cubePositions[GetRow(pos3), GetCol(pos3)] != 0; return((valid0 && (valid1 || valid2 || valid3)) || (valid1 && (valid2 || valid3)) || (valid2 && valid3) ); }
bool IsValidSwitchRowPosition(int pos, Swipes.Direction direction) { bool isValidPosition = false; if (direction == Swipes.Direction.LEFT || direction == Swipes.Direction.RIGHT) { isValidPosition = DifferentCubesInRow(pos); } else if (direction == Swipes.Direction.UP || direction == Swipes.Direction.DOWN) { isValidPosition = DifferentCubesInCol(pos); } if (!isValidPosition) { return(false); } return(TotalMoves(pos, direction) > 1); }
public void MoveToCubeRelative(Cube cube, Swipes.Direction direction, float time) { if (cube == null) { return; } var x = cube.transform.position.x; var y = cube.transform.position.y; var z = cube.transform.position.z; var position = Vector3.zero; switch (direction) { case Swipes.Direction.UP: position = new Vector3(x, y + 0.74f, z); break; case Swipes.Direction.DOWN: position = new Vector3(x, y - 0.74f, z); break; case Swipes.Direction.LEFT: position = new Vector3(x - 0.8f, y, z); break; case Swipes.Direction.RIGHT: position = new Vector3(x + 0.8f, y, z); break; default: Debug.LogError("Relative position not defined!"); break; } if (position == Vector3.zero) { return; } MoveToPosition(cube, position, time); }
public Cube SpawnCubeRelative(Cube cube, Swipes.Direction direction) { if (cube != null) { var x = cube.transform.position.x; var y = cube.transform.position.y; var z = cube.transform.position.z; var position = Vector3.zero; switch (direction) { case Swipes.Direction.UP: position = new Vector3(x, y - 0.74f, z); break; case Swipes.Direction.DOWN: position = new Vector3(x, y + 0.74f, z); break; case Swipes.Direction.LEFT: position = new Vector3(x + 0.8f, y, z); break; case Swipes.Direction.RIGHT: position = new Vector3(x - 0.8f, y, z); break; default: Debug.LogError("Relative position not defined!"); break; } if (position == Vector3.zero) { return(null); } return(SafeInstantiate(cubePrefab, position, cube.transform.rotation)); } return(null); }
public void SetPositions(Vector3 startPos, Swipes.Direction direction, float length, int angle) { this.startPos = startPos; this.angle = angle; switch (direction) { case Swipes.Direction.LEFT: relEndPos = new Vector3(-length * Constants.CubeSizeX, 0); break; case Swipes.Direction.RIGHT: relEndPos = new Vector3(length * Constants.CubeSizeX, 0); break; case Swipes.Direction.UP: relEndPos = new Vector3(0, length * Constants.CubeSizeY); break; case Swipes.Direction.DOWN: relEndPos = new Vector3(0, -length * Constants.CubeSizeY); break; case Swipes.Direction.DOWN_LEFT: relEndPos = new Vector3(-length * Constants.CubeSizeX, -length * Constants.CubeSizeY); break; case Swipes.Direction.DOWN_RIGHT: relEndPos = new Vector3(length * Constants.CubeSizeX, -length * Constants.CubeSizeY); break; case Swipes.Direction.UP_LEFT: relEndPos = new Vector3(-length * Constants.CubeSizeX, length * Constants.CubeSizeY); break; case Swipes.Direction.UP_RIGHT: relEndPos = new Vector3(length * Constants.CubeSizeX, length * Constants.CubeSizeY); break; } }
public void TryExecute(Cube cube, Swipes.Direction direction) { try { if (LevelManager.instance.gameState == LevelManager.GameState.InTutorial) { if (LevelData.instance.tutorials [LevelManager.instance.currentTutorial - 1].moveDirections [LevelManager.instance.pulseIndex - 1] == direction) { Execute(cube, direction); LevelManager.instance.pulseIndex++; } else { //LevelManager.instance.lastPulseIndex = LevelManager.instance.pulseIndex - 1; //LevelManager.instance.pulseIndex = LevelManager.instance.lastPulseIndex + 1; } } else { Execute(cube, direction); } } catch (System.Exception ex) { Debug.LogError("BasePowerup: " + ex.GetBaseException()); } }
protected override void Execute(Cube cube, Swipes.Direction direction) { if (cube == null) { powerups.finishedAnimation = true; return; } Cube left = cube.neighbors[Constants.Left]; Cube leftUp = left != null ? cube.neighbors[Constants.Left].neighbors[Constants.Up] : null; Cube up = cube.neighbors[Constants.Up]; Cube right = cube.neighbors[Constants.Right]; Cube rightUp = right != null ? cube.neighbors[Constants.Right].neighbors[Constants.Up] : null; if (up == null) { powerups.finishedAnimation = true; return; } var tempCube = cube.neighbors; var tempUp = up.neighbors; powerup = Constants.Powerup.Rotate; var dir = direction == Swipes.Direction.LEFT ? Swipes.Direction.RIGHT : Swipes.Direction.LEFT; switch (direction) { case Swipes.Direction.LEFT: if (cube && left && leftUp && up) { var tempLeft = left.neighbors; var tempLeftUp = leftUp.neighbors; powerups.MoveToCube(cube, left, Constants.CubeSwitchTime); cube.pos--; cube.neighbors = tempLeft; powerups.MoveToCube(left, leftUp, Constants.CubeSwitchTime); left.pos += LevelManager.instance.boardWidth; left.neighbors = tempLeftUp; powerups.MoveToCube(leftUp, up, Constants.CubeSwitchTime); leftUp.pos++; leftUp.neighbors = tempUp; powerups.MoveToCube(up, cube, Constants.CubeSwitchTime); up.pos -= LevelManager.instance.boardWidth; up.neighbors = tempCube; powerups.UpdateMoveInfo(Constants.Powerup.Rotate); if (lvlSpawner != null) { positionsCopy.Reverse(); lvlSpawner.RecordMove(new PowerupExe(Constants.Powerup.Rotate, cubesCopy, positionsCopy, Swipes.Direction.RIGHT)); } } else { powerups.finishedAnimation = true; } break; case Swipes.Direction.RIGHT: if (cube && right && rightUp && up) { var tempRightUp = rightUp.neighbors; var tempRight = right.neighbors; powerups.MoveToCube(cube, right, Constants.CubeSwitchTime); cube.pos++; cube.neighbors = tempRight; powerups.MoveToCube(right, rightUp, Constants.CubeSwitchTime); right.pos += LevelManager.instance.boardWidth; right.neighbors = tempRightUp; powerups.MoveToCube(rightUp, up, Constants.CubeSwitchTime); rightUp.pos--; rightUp.neighbors = tempUp; powerups.MoveToCube(up, cube, Constants.CubeSwitchTime); up.pos -= LevelManager.instance.boardWidth; up.neighbors = tempCube; powerups.UpdateMoveInfo(Constants.Powerup.Rotate); if (lvlSpawner != null) { positionsCopy.Reverse(); lvlSpawner.RecordMove(new PowerupExe(Constants.Powerup.Rotate, cubesCopy, positionsCopy, Swipes.Direction.LEFT)); } } else { powerups.finishedAnimation = true; } break; default: Debug.LogError("Wrong direction!"); powerups.finishedAnimation = true; break; } }
private PowerupExe(int type, Swipes.Direction direction) { this.type = type; this.direction = direction; }
public PowerupExe(int type, Cube cube, Swipes.Direction direction) : this(type, direction) { this.cube = cube; }
public PowerupExe(int type, List <Cube> cubes, List <int> positions, Swipes.Direction direction) : this(type, direction) { this.cubes = new List <Cube>(cubes); this.positions = new List <int>(positions); }
public static void ShiftByOne <T>(this T[,] array, int pos, Swipes.Direction direction) { T[] row, col; T first, last; width = array.GetLength(1); switch (direction) { case Swipes.Direction.LEFT: row = array.GetRow(GetRowByPos(pos)); first = row [0]; for (int j = 0; j < row.Length - 1; j++) { row [j] = row [j + 1]; } row [row.Length - 1] = first; array.SetRow(GetRowByPos(pos), row); break; case Swipes.Direction.RIGHT: row = array.GetRow(GetRowByPos(pos)); last = row [row.Length - 1]; for (int l = row.Length - 1; l > 0; l--) { row [l] = row [l - 1]; } row [0] = last; array.SetRow(GetRowByPos(pos), row); break; case Swipes.Direction.UP: col = array.GetCol(GetColByPos(pos)); last = col [col.Length - 1]; for (int k = col.Length - 1; k > 0; k--) { col [k] = col [k - 1]; } col [0] = last; array.SetCol(GetColByPos(pos), col); break; case Swipes.Direction.DOWN: col = array.GetCol(GetColByPos(pos)); first = col [0]; for (int m = 0; m < col.Length - 1; m++) { col [m] = col [m + 1]; } col [col.Length - 1] = first; array.SetCol(GetColByPos(pos), col); break; } }
public override IEnumerator ExecutePowerup(int powerup, List <Cube> cubes, List <int> positions, Swipes.Direction direction) { var sp = GameObject.Find("EditorSpawner").GetComponent <LevelEditorSpawner> (); sp.UpdateBoard(); GestureDetector.ready = false; switch (powerup) { case 0: switch_.TryExecute(cubes[0], direction); break; case 1: rotate.GetDirectionAndExecute(cubes, positions); break; case 2: switchRow.TryExecute(cubes[0], direction); break; default: Debug.LogError("Powerup not defined!"); break; } yield return(new WaitForSeconds(Constants.CubeSwitchTime * 1.2f)); sp.UpdateBoard(); GestureDetector.ready = true; }
int TotalMoves(int pos, Swipes.Direction direction) { var totalMoves = 0; int startPos = -1; switch (direction) { case Swipes.Direction.LEFT: startPos = cubePositions[GetRow(pos), 0]; break; case Swipes.Direction.RIGHT: startPos = cubePositions[GetRow(pos), width - 1]; break; case Swipes.Direction.UP: startPos = cubePositions[height - 1, GetCol(pos)]; break; case Swipes.Direction.DOWN: startPos = cubePositions[0, GetCol(pos)]; break; } // Cube at the end if (cubePositions[GetRow(startPos), GetCol(startPos)] != 0) { if (direction == Swipes.Direction.LEFT || direction == Swipes.Direction.RIGHT) { totalMoves += width - 1; } else if (direction == Swipes.Direction.UP || direction == Swipes.Direction.DOWN) { totalMoves += height - 1; } } if (totalMoves > 0) { if (direction == Swipes.Direction.LEFT || direction == Swipes.Direction.RIGHT) { totalMoves += NonMainCubesInRow(pos) - 1; } else if (direction == Swipes.Direction.UP || direction == Swipes.Direction.DOWN) { totalMoves += NonMainCubesInCol(pos) - 1; } } else { if (direction == Swipes.Direction.LEFT || direction == Swipes.Direction.RIGHT) { totalMoves += NonMainCubesInRow(pos); } else if (direction == Swipes.Direction.UP || direction == Swipes.Direction.DOWN) { totalMoves += NonMainCubesInCol(pos); } } return(totalMoves); }
public virtual IEnumerator ExecutePowerup(int powerup, List <Cube> cubes, List <int> positions, Swipes.Direction direction) { /*if (tutSpawner != null) { * if (!tutSpawner.ValidProgress ()) { * LevelManager.instance.lastPulseIndex = LevelManager.instance.pulseIndex - 1; * LevelManager.instance.pulseIndex = LevelManager.instance.lastPulseIndex + 1; * tutSpawner.UpdateBoard (); * } * }*/ if (LevelManager.instance.movesLeft > 0 && finishedAnimation) { GestureDetector.ready = false; if (LevelManager.instance.moveInfo[powerup] > 0) { finishedAnimation = false; switch (powerup) { case 0: switch_.TryExecute(cubes[0], direction); break; case 1: rotate.GetDirectionAndExecute(cubes, positions); break; case 2: switchRow.TryExecute(cubes[0], direction); break; default: Debug.LogError("Powerup not defined!"); break; } } else if (LevelManager.instance.gameState == LevelManager.GameState.InLevel && spawner.powerupMovesLeft[powerup].gameObject.activeInHierarchy) { spawner.powerupMovesLeft[powerup].GetComponent <UIAnimations>().PulseSize(Colors.powerupMissing); } yield return(new WaitForSeconds(Constants.CubeSwitchTime)); finishedAnimation = true; spawner.UpdateBoard(); GestureDetector.ready = true; } }
bool UseSwitchRow() { var maxIterations = (width * height) / 2; var pos = Random.Range(0, maxPos); var direction = GetValidSwitchRowPosition(pos); int undoPos = 0; Swipes.Direction undoDir = Swipes.Direction.NONE; switch (direction) { case Swipes.Direction.LEFT: undoPos = GetRow(pos) * width + (width - 1); undoDir = Swipes.Direction.RIGHT; break; case Swipes.Direction.RIGHT: undoPos = GetRow(pos) * width; undoDir = Swipes.Direction.LEFT; break; case Swipes.Direction.UP: undoPos = GetCol(pos); undoDir = Swipes.Direction.DOWN; break; case Swipes.Direction.DOWN: undoPos = GetRow(maxPos) * width + GetCol(pos); undoDir = Swipes.Direction.UP; break; } while (direction == Swipes.Direction.NONE && maxIterations > 0) { pos = Random.Range(0, maxPos); direction = GetValidSwitchRowPosition(pos); print("Valid direction: " + direction); switch (direction) { case Swipes.Direction.LEFT: undoPos = GetRow(pos) * width + (width - 1); undoDir = Swipes.Direction.RIGHT; break; case Swipes.Direction.RIGHT: undoPos = GetRow(pos) * width; undoDir = Swipes.Direction.LEFT; break; case Swipes.Direction.UP: undoPos = GetCol(pos); undoDir = Swipes.Direction.DOWN; break; case Swipes.Direction.DOWN: undoPos = GetRow(maxPos) * width + GetCol(pos); undoDir = Swipes.Direction.UP; break; } maxIterations--; } if (maxIterations == 0) { // Change powerup Debug.LogError("Max iterations reached!"); //powerups[powerupOrder[i]] = Random.Range(0, 1 + 1) == 0 ? 0 : 1; return(false); } cubePositions.ShiftByOne(pos, direction); // Increment move info moveInfo[2]++; // Add solver move info (reversed) //Solver.instance.Add(new SwitchRowExe(undoPos, undoDir)); solverMoveInfo.Add("Switch row from " + GetRow(undoPos) + "|" + GetCol(undoPos) + " " + undoDir); return(true); }
protected override void Execute(Cube cube, Swipes.Direction direction) { powerups.SwitchCubes(cube, cube.neighbors [(int)direction], direction); }
int ChooseRandomNeighbor(int pos) { var direction = Random.Range(0, 8); var endPos = -1; switch (direction) { case 0: // Left if (pos - 1 >= 0 && GetRow(pos - 1) == GetRow(pos)) { endPos = pos - 1; } break; case 1: // Right if (pos + 1 <= maxPos && GetRow(pos + 1) == GetRow(pos)) { endPos = pos + 1; } break; case 2: // Up if (pos + width <= maxPos) { endPos = pos + width; } break; case 3: // Down if (pos - width >= 0) { endPos = pos - width; } break; case 4: // Up left if (pos + width - 1 <= maxPos && GetRow(pos + width - 1) == GetRow(pos) + 1) { endPos = pos + width - 1; } break; case 5: // Up right if (pos + width + 1 <= maxPos && GetRow(pos + width + 1) == GetRow(pos) + 1) { endPos = pos + width + 1; } break; case 6: // Down left if (pos - width - 1 >= 0 && GetRow(pos - width - 1) == GetRow(pos) - 1) { endPos = pos - width - 1; } break; case 7: // Down right if (pos - width + 1 >= 1 && GetRow(pos - width + 1) == GetRow(pos) - 1) { endPos = pos - width + 1; } break; } if (endPos == -1) { return(ChooseRandomNeighbor(pos)); } else { switchDirection = (Swipes.Direction)direction; return(endPos); } }
protected override void Execute(Cube cube, Swipes.Direction direction) { if (cube == null || (cube.pos.GetCol() != 0 && cube.pos.GetCol() != LevelManager.instance.boardWidth - 1 && cube.pos.GetRow() != 0 && cube.pos.GetRow() != LevelManager.instance.boardHeight - 1)) { powerups.finishedAnimation = true; return; } var cubesInRow = new List <Cube>(); cubesInRow.Add(cube); Swipes.Direction dir; switch (direction) { case Swipes.Direction.UP: dir = Swipes.Direction.DOWN; if (cube.pos.GetRow() != LevelManager.instance.boardHeight - 1) { powerups.finishedAnimation = true; return; } for (int i = 1; i < LevelManager.instance.boardHeight; i++) { cubesInRow.Add(cubesInRow[i - 1].neighbors[Constants.Down]); } break; case Swipes.Direction.DOWN: dir = Swipes.Direction.UP; if (cube.pos.GetRow() != 0) { powerups.finishedAnimation = true; return; } for (int i = 1; i < LevelManager.instance.boardHeight; i++) { cubesInRow.Add(cubesInRow[i - 1].neighbors[Constants.Up]); } break; case Swipes.Direction.LEFT: dir = Swipes.Direction.RIGHT; if (cube.pos.GetCol() != 0) { powerups.finishedAnimation = true; return; } for (int i = 1; i < LevelManager.instance.boardWidth; i++) { cubesInRow.Add(cubesInRow[i - 1].neighbors[Constants.Right]); } break; case Swipes.Direction.RIGHT: dir = Swipes.Direction.LEFT; if (cube.pos.GetCol() != LevelManager.instance.boardWidth - 1) { powerups.finishedAnimation = true; return; } for (int i = 1; i < LevelManager.instance.boardWidth; i++) { cubesInRow.Add(cubesInRow[i - 1].neighbors[Constants.Left]); } break; default: powerups.finishedAnimation = true; Debug.LogError("Wrong direction!"); return; } var colorArray = new Color[cubesInRow.Count]; for (int i = 0; i < cubesInRow.Count; i++) { colorArray[i] = cubesInRow[i].color; } // Spawn new cube at the end of the row and move towards the swipe direction Cube newCube = Powerups.spawner.SpawnCubeRelative(cubesInRow[cubesInRow.Count - 1], direction); newCube.SetColor(cubesInRow[0].color); newCube.pos = cubesInRow[cubesInRow.Count - 1].pos; newCube.SetNeighbors(); powerups.MoveToCubeRelative(newCube, direction, Constants.CubeSwitchTime); // Move cube at the start of the row into invisible object and destroy after certain time powerups.MoveToCubeRelative(cubesInRow[0], direction, Constants.CubeSwitchTime); Destroy(cubesInRow[0].gameObject, Constants.CubeSwitchTime); var amount = direction == Swipes.Direction.LEFT || direction == Swipes.Direction.RIGHT ? 1 : LevelManager.instance.boardWidth; for (int j = 1; j < cubesInRow.Count; j++) { powerups.MoveToCube(cubesInRow[j], cubesInRow[j - 1], Constants.CubeSwitchTime); // Change pos according to swipe direction cubesInRow[j].pos = direction == Swipes.Direction.LEFT || direction == Swipes.Direction.DOWN ? cubesInRow[j].pos - amount : cubesInRow[j].pos + amount; } powerup = Constants.Powerup.SwitchRow; powerups.UpdateMoveInfo(Constants.Powerup.SwitchRow); if (lvlSpawner != null) { lvlSpawner.RecordMove(new PowerupExe(Constants.Powerup.SwitchRow, newCube, dir)); } }
protected abstract void Execute(Cube cube, Swipes.Direction direction);