public void SetMinesOnFieldBlock(FieldBlock fieldBlock, GameObject exludeCell, bool Randomize) { int ncells = fieldSize * fieldSize * 6; // Set Mines int randMinesOnField = minesOnField; if (Randomize) { randMinesOnField = Random.Range(minesOnField - 1, minesOnField + 2); } if (randMinesOnField < 1) { randMinesOnField = 1; } if (randMinesOnField > ncells - 1) { randMinesOnField = ncells - 1; } for (int currMine = 0; currMine < randMinesOnField; currMine++) { while (true) { int mineCell = Random.Range(0, ncells - 1); OpenCell oCell = fieldBlock.cups[mineCell]; if ((!oCell.isMine) && (fieldBlock.cells[mineCell] != exludeCell)) { oCell.isMine = true; break; } } } }
public void CalcValuesInFieldBlock(FieldBlock fieldBlock) { int ncells = fieldSize * fieldSize * 6; for (int indCell = 0; indCell < ncells; indCell++) { OpenCell oCell = fieldBlock.cups[indCell]; //Clear cell Transform cellText = fieldBlock.cells[indCell].transform.Find("CellText(Clone)"); Transform cellMine = fieldBlock.cells[indCell].transform.Find("Mine(Clone)"); if (cellText != null) { DestroyImmediate(cellText.gameObject); } if (cellMine != null) { DestroyImmediate(cellMine.gameObject); } int nMinesInAdj = 0; for (int indAdj = 0; indAdj < adjOffsets.Length; indAdj++) { OpenCell adjCell = GetAdjOpenCell(oCell, indAdj); if ((adjCell != null) && (adjCell.isMine)) { nMinesInAdj++; } } oCell.cellValue = nMinesInAdj; Vector3 textOffset = new Vector3(0, 0.3f, 0); if (oCell.isUp) { textOffset.z = 0.5f; } else { textOffset.z = -0.5f; } if ((nMinesInAdj > 0) && (!oCell.isMine)) { GameObject textCell = Instantiate(CellText, fieldBlock.cells[indCell].transform.position + textOffset, CellText.transform.rotation) as GameObject; textCell.transform.parent = fieldBlock.cells[indCell].transform; //transform; textCell.GetComponent <Renderer>().material.SetTextureOffset("_MainTex", MakeTextureOffset(oCell.cellValue)); } if (oCell.isMine) { GameObject mineCell = Instantiate(Mine, fieldBlock.cells[indCell].transform.position + textOffset, Mine.transform.rotation) as GameObject; mineCell.transform.parent = fieldBlock.cells[indCell].transform; //transform; } } FieldBlock_OptimizeNumbersAndMines(fieldBlock); Resources.UnloadUnusedAssets(); }
public OpenCell GetAdjOpenCell(OpenCell srcCell, int adjIndex) { int upMod = 1; if (!srcCell.isUp) { upMod = -1; } Vector2 adjPos = new Vector2(srcCell.cellCoord.x + adjOffsets[adjIndex].x, srcCell.cellCoord.y + adjOffsets[adjIndex].y * upMod); int adjCellInd = CellsCoordToID(adjPos); if (!allCells.ContainsKey(adjCellInd)) { return(null); } Transform cellCup = allCells[adjCellInd].transform.Find("CellCup"); if (cellCup == null) { return(null); } return(cellCup.gameObject.GetComponent <OpenCell> ()); }
public void ViewMines() { foreach (int curr_cell in allCells.Keys) { Transform cell_cup = allCells[curr_cell].transform.Find("CellCup"); if (cell_cup != null) { OpenCell oCell = cell_cup.gameObject.GetComponent <OpenCell>(); if (oCell.isMine) { Color col = cell_cup.gameObject.GetComponent <Renderer>().material.color; col.a = 0.6f; cell_cup.gameObject.GetComponent <Renderer>().material.color = col; } } } }
void SetCellParams(bool isUp, int col, int colInRow, int row, int cellIndex, bool IsDownIndex, FieldBlock fBlock, bool SetCellType) { OpenCell oCell = fBlock.cells[cellIndex].transform.Find("CellCup").gameObject.GetComponent <OpenCell>(); fBlock.cups [cellIndex] = oCell; oCell.isUp = IsDownIndex ? !isUp : isUp; oCell.parentBlock = fBlock; Vector2 centralCellCoord = GetCentralCellCoordInBlock(fBlock.pos); int rowx = Mathf.RoundToInt(centralCellCoord.x) - fieldSize - row; int rowy = 0; if (IsDownIndex) { rowy = Mathf.RoundToInt(centralCellCoord.y) - fieldSize + row; } else { rowy = Mathf.RoundToInt(centralCellCoord.y) + fieldSize - row - 1; } oCell.cellCoord = new Vector2(rowx + col, rowy); allCells.Add(CellsCoordToID(oCell.cellCoord), fBlock.cells[cellIndex]); // Set Cell Type oCell.cellParams.uncheckAll(); if (SetCellType) { if (row == 0) { if (IsDownIndex) { oCell.cellParams.Bottom = true; } else { oCell.cellParams.Top = true; } } if (col <= 1) { if (IsDownIndex) { oCell.cellParams.LeftBottom = true; if ((col == 0) && (row == fieldSize - 1)) { oCell.cellParams.LeftTop = true; } } else { oCell.cellParams.LeftTop = true; if ((col == 0) && (row == fieldSize - 1)) { oCell.cellParams.LeftBottom = true; } } } if (col >= colInRow - 2) { if (IsDownIndex) { oCell.cellParams.RightBottom = true; if ((col == colInRow - 1) && (row == fieldSize - 1)) { oCell.cellParams.RightTop = true; } } else { oCell.cellParams.RightTop = true; if ((col == colInRow - 1) && (row == fieldSize - 1)) { oCell.cellParams.RightBottom = true; } } } } }
void FieldBlock_OptimizeNumbersAndMines(FieldBlock fBlock) { int ncells = fBlock.cells.Length; int nums_mines_Count = 0; for (int curr_cell = 0; curr_cell < ncells; curr_cell++) { OpenCell oCell = fBlock.cups[curr_cell]; if ((oCell.isMine) || (oCell.cellValue > 0)) { nums_mines_Count++; } } CombineInstance[] combo_nums_mines = new CombineInstance[nums_mines_Count]; Material shared_mat_num_mine = null; int curr_num_mine = 0; for (int curr_cell = 0; curr_cell < ncells; curr_cell++) { Transform cMine = fBlock.cells[curr_cell].transform.Find("Mine(Clone)"); Transform cNum = fBlock.cells[curr_cell].transform.Find("CellText(Clone)"); GameObject num_mine = null; if (cMine != null) { num_mine = cMine.gameObject; } if (cNum != null) { num_mine = cNum.gameObject; } if (num_mine != null) { MeshFilter num_mf = num_mine.GetComponent <MeshFilter>(); combo_nums_mines[curr_num_mine].mesh = num_mf.mesh; combo_nums_mines[curr_num_mine].transform = num_mf.transform.localToWorldMatrix; Vector3[] numb_vertices = combo_nums_mines[curr_num_mine].mesh.vertices; Vector2[] numb_uvs = new Vector2[numb_vertices.Length]; Vector2 numb_offset = num_mine.GetComponent <Renderer>().sharedMaterial.GetTextureOffset("_MainTex"); Vector2 numb_scale = num_mine.GetComponent <Renderer>().sharedMaterial.GetTextureScale("_MainTex"); numb_uvs[0] = new Vector2(numb_offset.x, numb_offset.y); numb_uvs[1] = new Vector2(numb_offset.x + numb_scale.x, numb_offset.y + numb_scale.y); numb_uvs[2] = new Vector2(numb_offset.x + numb_scale.x, numb_offset.y); numb_uvs[3] = new Vector2(numb_offset.x, numb_offset.y + numb_scale.y); combo_nums_mines[curr_num_mine].mesh.uv = numb_uvs; shared_mat_num_mine = num_mine.GetComponent <Renderer>().material; DestroyImmediate(num_mine); curr_num_mine++; } } if (fBlock.opto_mines != null) { DestroyImmediate(fBlock.opto_mines.GetComponent <MeshFilter> ().mesh); DestroyImmediate(fBlock.opto_mines); } fBlock.opto_mines = new GameObject(); fBlock.opto_mines.name = "OptimizedNumsAndMines"; fBlock.opto_mines.AddComponent <MeshFilter> (); fBlock.opto_mines.AddComponent <MeshRenderer> (); fBlock.opto_mines.GetComponent <MeshRenderer> ().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; fBlock.opto_mines.GetComponent <MeshRenderer> ().receiveShadows = false; shared_mat_num_mine.SetTextureOffset("_MainTex", new Vector2(0, 0)); shared_mat_num_mine.SetTextureScale("_MainTex", new Vector2(1, 1)); fBlock.opto_mines.GetComponent <Renderer> ().sharedMaterial = shared_mat_num_mine; fBlock.opto_mines.GetComponent <MeshFilter> ().mesh.CombineMeshes(combo_nums_mines); fBlock.opto_mines.transform.parent = transform; combo_nums_mines = null; }
void Update() { if (Application.platform == RuntimePlatform.Android) { if (Input.GetKeyDown(KeyCode.Escape)) { uiProcs.backButtonPress(); } if (Input.GetKeyDown(KeyCode.Menu)) { uiProcs.menuButtonPress(); } } if (gamePause) { return; } if (!gameOver && !winState && firstOpen) { time += Time.deltaTime; UpdateTimer(Mathf.RoundToInt(time)); } /*if ((Input.touchCount >= 2) && * ((Input.GetTouch(0).phase == TouchPhase.Moved) || (Input.GetTouch(1).phase == TouchPhase.Moved))) { * Vector2 t0 = Input.GetTouch(0).position; * Vector2 t1 = Input.GetTouch(1).position; * Vector2 diff_vec = t0 - t1; * float dist = Mathf.Sqrt(diff_vec.x*diff_vec.x + diff_vec.y*diff_vec.y); * if (Mathf.Abs (touch_dist) > 0){ * float diff = dist - touch_dist; * Vector3 single_vec = new Vector3(1,1,1); * transform.parent.localScale += (single_vec * diff/100); * touch_dist = dist; * }else{ * touch_dist = dist; * } * }else{ * touch_dist = 0; * }*/ if (Input.GetButtonDown("Fire1")) { if (Input.mousePosition.y > 60) { mousePressed = true; mouseDownPos = Input.mousePosition; prevFieldPos = fieldTransform.position; } movingFieldStart = false; } if (Input.GetButtonUp("Fire1")) { mousePressed = false; movingFieldStart = false; } if (mousePressed) { if ((Mathf.Abs(mouseDownPos.x - Input.mousePosition.x) > 15) || (Mathf.Abs(mouseDownPos.y - Input.mousePosition.y) > 15) || (movingFieldStart)) { movingFieldStart = true; GameObject currCell = fieldProcs.GetCurrentCell(); if (currCell != null) { OpenCell currOpenCell = currCell.GetComponentInChildren <OpenCell>(); if (currOpenCell != null) { currOpenCell.CancelOpen(); } } Vector3 old_pos = mainCamera.ScreenToWorldPoint(mouseDownPos); Vector3 new_pos = mainCamera.ScreenToWorldPoint(Input.mousePosition); /*Vector3 newPosition = new Vector3 (prevFieldPos.x - (mouseDownPos.x - Input.mousePosition.x) / 10, * 0, * prevFieldPos.z - (mouseDownPos.y - Input.mousePosition.y) / 10);*/ Vector3 newPosition = new Vector3(prevFieldPos.x - (old_pos.x - new_pos.x), 0, prevFieldPos.z - (old_pos.z - new_pos.z)); if (newPosition.x < minBound.x) { newPosition.x = minBound.x; } if (newPosition.z < minBound.z) { newPosition.z = minBound.z; } if (newPosition.x > maxBound.x) { newPosition.x = maxBound.x; } if (newPosition.z > maxBound.z) { newPosition.z = maxBound.z; } fieldTransform.position = newPosition; } } }
void Open() { if (fieldMover == null) { Start(); } bool isGameOver = fieldMover.GetGameOver(); bool isWin = fieldMover.GetWin(); if (isGameOver || isWin || isFlag) { return; } if (!fieldMover.IsFirstOpen()) { fieldProcs.SetMinesOnFieldBlock(parentBlock, transform.parent.gameObject, uiProcs.IsEndlessGame()); fieldProcs.CalcValuesInFieldBlock(parentBlock); fieldMover.FirstOpen(); } if (cellParams.haveBlockAdj()) { Vector2 pos = parentBlock.pos; if (cellParams.Top) { Vector2 adjPos = new Vector2(pos.x, pos.y + 1); CheckAdjFieldBlock(adjPos); } if (cellParams.Bottom) { Vector2 adjPos = new Vector2(pos.x, pos.y - 1); CheckAdjFieldBlock(adjPos); } if (cellParams.LeftTop) { Vector2 adjPos = new Vector2(pos.x - 1, pos.y + 1); CheckAdjFieldBlock(adjPos); } if (cellParams.LeftBottom) { Vector2 adjPos = new Vector2(pos.x - 1, pos.y); CheckAdjFieldBlock(adjPos); } if (cellParams.RightTop) { Vector2 adjPos = new Vector2(pos.x + 1, pos.y); CheckAdjFieldBlock(adjPos); } if (cellParams.RightBottom) { Vector2 adjPos = new Vector2(pos.x + 1, pos.y - 1); CheckAdjFieldBlock(adjPos); } fieldProcs.CalcValuesInFieldBlock(parentBlock); } isOpen = true; if ((cellValue == 0) && (!isMine)) { for (int i = 0; i < fieldProcs.adjOffsets.Length; i++) { OpenCell adjOpenCell = fieldProcs.GetAdjOpenCell(this, i); if ((adjOpenCell == null) || (adjOpenCell.isOpen)) { continue; } if (adjOpenCell.isFlag) { adjOpenCell.isFlag = false; uiProcs.ChangeFlagsCount(1); } adjOpenCell.Open(); } } if (isMine) { if (uiProcs.IsEndlessGame()) { fieldMover.SaveScore(); } fieldMover.SetGameOver(true); fieldProcs.ViewMines(); } else { uiProcs.IncOpenedCells(); } if ((!uiProcs.IsEndlessGame()) && (fieldMover.openedCells >= needcells)) { fieldMover.SaveScore(); fieldMover.SetWin(true); } gameObject.SetActive(false); }