public void CollisionDetected(GameObject obj, GameObject obj2) { if (!turnedColumns.Contains(obj2)) { chooseNew = true; moveDict[obj] = false; columnInfo = new ColumnClass(obj2, null, rotationAxisDict[obj], 0); } else { if (obj2 == selectedColumn) { Debug.Log(obj2.name); wait = false; } moveDict[obj] = false; } }
// Update is called once per frame void Update() { if (begin) { prepareColumns(); } if (!transform.parent.GetComponent <ControllerScript>().countDB) { if (!startTurn) { startTurn = true; columnInfo = new ColumnClass(startColObj, null, new Vector3(0, 0, 0), 0); turnedColumns.Add(columnInfo.currentColumn); chooseNew = true; } if (Input.GetMouseButtonDown(0) && startTurn) { prevMousePos = mousePos; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { if (!turnedColumns.Contains(hit.transform.gameObject) && allColumns.Contains(hit.transform.gameObject)) { selectedColumn = hit.transform.gameObject; moveDict[selectedColumn] = true; //Debug.Log("3 " + selectedColumn.name); } } } if (selectedColumn != null && yyy > 0) { Vector3 rotPoint = new Vector3(0, 0, 0); rotPoint.x = selectedColumn.transform.position.x; rotPoint.y = selectedColumn.transform.position.y - selectedColumn.GetComponent <Collider>().bounds.size.y / 2; rotPoint.z = selectedColumn.transform.position.z; int index = 0; for (int i = 0; i < allColumns.Count; i++) { if (allColumns[i] == selectedColumn) { index = i; } } Vector3 rotAxisTemp = new Vector3(allColumns[index + 1].transform.position.z - allColumns[index - 1].transform.position.z, 0, allColumns[index - 1].transform.position.x - allColumns[index - 1].transform.position.x); selectedColumn.transform.RotateAround(rotPoint, rotAxisTemp, 1.0f); yyy -= 1; pos = selectedColumn.transform.position; rot = selectedColumn.transform.rotation; positionDict[selectedColumn] = selectedColumn.transform.position; rotationDict[selectedColumn] = selectedColumn.transform.rotation; } if (yyy == 0) { moveDict[selectedColumn] = false; if (!turnedColumns.Contains(selectedColumn)) { turnedColumns.Add(selectedColumn); } selectedColumn.GetComponent <Rigidbody>().velocity = Vector3.zero; selectedColumn.transform.position = positionDict[selectedColumn]; selectedColumn.transform.rotation = rotationDict[selectedColumn]; if (!wait) { stopTurning = true; } } if (xxx == 0) { foreach (GameObject elem in moveDict.Keys) { if (elem.name == endColumn) { game = false; moveDict[elem] = false; stopTurning = true; break; } } } else { foreach (GameObject elem in moveDict.Keys) { if (elem.name == endColumn) { if (moveDict[elem] == true) { xxx -= 1; } } } } if (game) { if (startTurn && columnInfo.currentColumn != null && chooseNew)// && !turn) { columnInfo = turnColumn(columnInfo); moveDict[columnInfo.currentColumn] = true; } } if (!stopTurning) { foreach (GameObject elem in moveDict.Keys) { if (moveDict[elem]) { if (!turnedColumns.Contains(elem)) { turnedColumns.Add(elem); } if (selectedColumn != null) { if (elem.name == "Cylinder (2)" && selectedColumn.name == "CylinderX") { direction = -direction; } } if (selectedColumn != null) { if (elem.name == "CylinderX" && selectedColumn.name == "CylinderX (1)") { direction = -direction; } } if (selectedColumn != null) { if (elem.name == "Cylinder3" && selectedColumn.name == "Cylinder") { direction = -direction; } } float degree = 1.0f * direction; elem.transform.RotateAround(rotationPointDict[elem], rotationAxisDict[elem], degree); if (selectedColumn != null) { if (elem.name == "Cylinder (2)" && selectedColumn.name == "CylinderX") { direction = -direction; } } if (selectedColumn != null) { if (elem.name == "CylinderX" && selectedColumn.name == "CylinderX (1)") { direction = -direction; } } if (selectedColumn != null) { if (elem.name == "Cylinder3" && selectedColumn.name == "Cylinder") { direction = -direction; } } positionDict[elem] = elem.transform.position; rotationDict[elem] = elem.transform.rotation; } else { elem.GetComponent <Rigidbody>().velocity = Vector3.zero; elem.transform.position = positionDict[elem]; elem.transform.rotation = rotationDict[elem]; } } } else { foreach (GameObject elem in moveDict.Keys) { elem.GetComponent <Rigidbody>().velocity = Vector3.zero; elem.transform.position = positionDict[elem]; elem.transform.rotation = rotationDict[elem]; } if (countPoints) { for (int i = 0; i < turnedColumns.Count; i++) { Material mat = turnedColumns[i].GetComponent <Renderer>().material; if (mat.color == Color.red) { winPoints += 1; } if (mat.color == Color.green) { loosePoints += 1; } } countPoints = false; score = winPoints - 0.5f * loosePoints; transform.parent.GetComponent <ControllerScript>().SecondRow(score); //UI.SetActive(true); } } if (chooseNew) { if (game) { columnInfo = new ColumnClass(columnInfo.nextColumn, null, rotationAxisDict[columnInfo.currentColumn], 0); chooseNew = false; } } } else { foreach (GameObject elem in allColumns) { elem.GetComponent <Rigidbody>().velocity = Vector3.zero; elem.transform.position = positionDict[elem]; elem.transform.rotation = rotationDict[elem]; } } }
ColumnClass turnColumn(ColumnClass colInfo) { GameObject column = colInfo.currentColumn; GameObject nextColumn = colInfo.nextColumn; Vector3 prevRotAxis = colInfo.rotAxis; if (column != selectedColumn) { float minD = 10000.0f; float d = 0.0f; float x = column.transform.position.x; float y = column.transform.position.y; float z = column.transform.position.z; float xAxis = 0.0f; float zAxis = 0.0f; rotationPoint.x = x; rotationPoint.y = y - column.GetComponent <Collider>().bounds.size.y / 2; rotationPoint.z = z; if (columns.Count > 0) { foreach (GameObject elem in columns) { d = Mathf.Sqrt(Mathf.Pow(elem.transform.position.x - x, 2) + Mathf.Pow(elem.transform.position.z - z, 2)); if (d < minD) { minD = d; nextColumn = elem; xAxis = elem.transform.position.x - x; zAxis = elem.transform.position.z - z; } } if (Mathf.Abs(zAxis) > Mathf.Abs(xAxis)) { xAxis = xAxis / zAxis; zAxis = zAxis / zAxis; } else { zAxis = zAxis / xAxis; xAxis = xAxis / xAxis; } columns.Remove(nextColumn); Vector3 rotAxis = new Vector3(0, 0, 0); rotAxis = new Vector3(-zAxis, 0, xAxis); rotationAxisDict[column] = rotAxis; return(new ColumnClass(column, nextColumn, rotAxis, direction)); } else { moveDict[column] = true; rotationAxisDict[column] = prevRotAxis; xxx = 90; return(new ColumnClass(column, null, prevRotAxis, direction)); } } else { game = false; return(new ColumnClass(column, null, prevRotAxis, direction)); } }