void DrawPath(ItemCube cube) { path.Clear(); int res = CalcScorePath(cube); foreach (ItemCube c in path) { c.Points = res; } }
void Start() { if (SoundSwitch) { SoundSwitch.Status = Prefs.SoundStatus; } string board = Progress.Data.Board; for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { ItemCube cube = CreateCube(i, j); int idx = i * Columns + j; if (!string.IsNullOrEmpty(board) && board.Length > idx) { int cl = 0; if (int.TryParse(board[idx].ToString(), out cl)) { cube.SetColor(cl); } //Debug.Log(board[idx].ToString()); } Board.Add(BoardIndex(i, j), cube); } } score = Progress.Data.Score; if (ScoreText) { ScoreText.text = score.ToString(); } //Create weighted list WeightColors.Add(Weighted.Create(0.5f, 1)); WeightColors.Add(Weighted.Create(2, 2)); WeightColors.Add(Weighted.Create(2, 3)); WeightColors.Add(Weighted.Create(0.5f, 4)); NewMove(); }
/// <summary> /// Create new cube /// </summary> /// <param name="col"></param> /// <param name="row"></param> /// <returns>new ItemCube</returns> ItemCube CreateCube(int col, int row) { GameObject obj = Instantiate(ItemPrefab); obj.transform.SetParent(transform); obj.transform.localPosition = new Vector3(col * DistanceX, 0, row * DistanceY); ItemCube cube = obj.GetComponent <ItemCube>(); if (!cube) { Debug.LogError("ItemCube not present!"); Destroy(obj); return(null); } cube.col = col; cube.row = row; return(cube); }
/// <summary> /// Calculate score in neighbours /// Recursive call /// </summary> /// <param name="col"></param> /// <param name="row"></param> /// <param name="color"></param> /// <returns></returns> int CalcScorePath(int col, int row, int color) { if (col < 0 || col >= Columns) { return(0); } if (row < 0 || row >= Rows) { return(0); } ItemCube cube = GetCube(col, row); if (!cube) { return(0); } if (path.Contains(cube)) { return(0); } if (cube.color != color) { return(0); } path.Add(cube); int result = 1; foreach (Vector2 neighbour in neighbours) { result += CalcScorePath(col + (int)neighbour.x, row + (int)neighbour.y, color); } return(result); }
/// <summary> /// Calculate score in neighbours by cube /// </summary> /// <param name="cube"></param> /// <returns></returns> int CalcScorePath(ItemCube cube) { return(CalcScorePath(cube.col, cube.row, cube.color)); }
void Update() { if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonUp(0) && !Input.GetMouseButton(0)) { //No mouse interaction do nothing return; } if (Helper.isPointerOverUI()) { //Mouse is ower GUI //Debug.Log("GUI"); return; } if (Input.GetMouseButtonUp(0)) { //Release mouse foreach (ItemCube c in Board.Values) { if (c.Points > 2) { Score += c.Points; } c.ClearScore(); } //Save score Progress.Data.Score = score; if (Progress.Data.BestScore < score) { Progress.Data.BestScore = score; } //Save board Progress.Data.Board = ""; for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows; j++) { ItemCube cb = GetCube(i, j); Progress.Data.Board = Progress.Data.Board + cb.color; } } Progress.Save(); if (lastCube) { lastCube.Selected = false; } lastCube = null; int free = CalcFreeMoves(); if (free == 0) { //TODO: finish level FinishLevel(); } else { NewMove(); } return; } Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (!Physics.Raycast(ray, out hit)) { //No object unde mouse //Debug.Log("No object"); return; } ItemCube cube = hit.collider.GetComponent <ItemCube>(); if (!cube) { //No itemCube uder mouse //Debug.Log("No cube object"); return; } if (cube.color != 0) { //We have hammer booste if (Input.GetMouseButtonDown(0) && boosterStatus == 1) { SoundManager.Play(SoundKind.Hammer); boosterStatus = 0; cube.DestroyColor(); } //Cube with color; //Debug.Log("Click on colored cube"); return; } //Debug.Log(hit.collider.transform.parent.name + " cube:" + cube); if (Input.GetMouseButtonDown(0)) { //Begin drag //Debug.Log("Rotate cube:" + cube); int c = GetColor(); if (c > -1) { cube.Rotate(MoveDirection.Right, c); if (lastCube) { lastCube.Selected = false; } lastCube = cube; if (lastCube) { lastCube.Selected = true; } DrawPath(cube); } return; } if (lastCube == null) { //No start cube return; } int distRow = Mathf.Abs(lastCube.row - cube.row); int distCol = Mathf.Abs(lastCube.col - cube.col); if ( distRow > 1 || distCol > 1 || distRow != 1 && distCol != 1 || distRow == 1 && distCol == 1 ) { //Not valid distance //Debug.Log("Not valid distance last:" + lastCube + " current:" + cube); return; } int cc = GetColor(); if (cc < 0) { return; } MoveDirection dir = cube.col > lastCube.col ? MoveDirection.Right : cube.col < lastCube.col ? MoveDirection.Left : cube.row < lastCube.row ? MoveDirection.Down : MoveDirection.Up; cube.Rotate(dir, cc); DrawPath(cube); if (lastCube) { lastCube.Selected = false; } lastCube = cube; if (lastCube) { lastCube.Selected = true; } }