} // end co-routine checkCycle IEnumerator revertSwap(int x, int y, int mousePositionX, int mousePositionY, MatchObjectController oldSelectedObject, MatchObjectController newSelectedObject, GameObject Swap) { yield return(new WaitForSeconds(0.25f)); grid[x][y] = grid[mousePositionX][mousePositionY]; oldSelectedObject.xPos = x; oldSelectedObject.yPos = y; oldSelectedObject.swap = true; grid[mousePositionX][mousePositionY] = Swap; newSelectedObject.xPos = mousePositionX; newSelectedObject.yPos = mousePositionY; newSelectedObject.swap = true; newSelectedObject.SortUp(); if (!mouseDeactive) { mouseActive = true; } } // end co-routine revertSwap
} // end function start void Update() { // get mouse position relative to icons position on screen mouseVector = camera.ScreenToWorldPoint(Input.mousePosition); mousePositionX = Mathf.RoundToInt(mouseVector.x); mousePositionY = Mathf.RoundToInt(mouseVector.y); if (Input.GetMouseButtonUp(0)) { mouseDown = false; } // if the user clicks if ((Input.GetMouseButtonDown(0) || mouseDown) && mousePositionX >= 0 && mousePositionX < gridX && mousePositionY >= 0 && mousePositionY < gridY && mouseActive) { mouseDown = true; if (Selected.Count == 0) { // switch to selected sprite grid[mousePositionX][mousePositionY].GetComponent <MatchObjectController>().SetSelected(); Selected.Add(mousePositionX); Selected.Add(mousePositionY); } else if (Selected[0] == mousePositionX && (Selected[1] == (mousePositionY - 1) || Selected[1] == (mousePositionY + 1)) || Selected[1] == mousePositionY && (Selected[0] == (mousePositionX - 1) || Selected[0] == (mousePositionX + 1))) { mouseActive = false; mouseDown = false; MatchObjectController oldSelectedObject = grid[Selected[0]][Selected[1]].GetComponent <MatchObjectController>(); MatchObjectController newSelectedObject = grid[mousePositionX][mousePositionY].GetComponent <MatchObjectController>(); oldSelectedObject.ClearSelected(); GameObject Swap = grid[mousePositionX][mousePositionY]; grid[mousePositionX][mousePositionY] = grid[Selected[0]][Selected[1]]; oldSelectedObject.xPos = mousePositionX; oldSelectedObject.yPos = mousePositionY; oldSelectedObject.swap = true; oldSelectedObject.SortUp(); grid[Selected[0]][Selected[1]] = Swap; newSelectedObject.xPos = Selected[0]; newSelectedObject.yPos = Selected[1]; newSelectedObject.swap = true; if (checkMatch()) { StartCoroutine(checkCycle()); } else { StartCoroutine(revertSwap(Selected[0], Selected[1], mousePositionX, mousePositionY, oldSelectedObject, newSelectedObject, Swap)); } Swap = null; Selected.Clear(); Selected.TrimExcess(); } else if (mousePositionX != Selected[0] || mousePositionY != Selected[1]) { // switch back to unselected sprite grid[Selected[0]][Selected[1]].GetComponent <MatchObjectController>().ClearSelected(); Selected.Clear(); Selected.TrimExcess(); } } } // end function Update