/** * @brief This function adds the trapezoid that collides and recursively calls each of its children to add to the * grid system as well. * */ void addSelfAndChildren(GameObject trapezoidObject, int side, int row) { grid[side, row] = trapezoidObject; if (trapezoidObject.transform.childCount == 4) { TrapezoidTransform childScript = trapezoidObject.transform.GetChild(3).GetComponent <TrapezoidTransform> (); childScript.move = false; childScript.gravityFall = false; childScript.side = side; childScript.row = row + 1; addSelfAndChildren(trapezoidObject.transform.GetChild(3).gameObject, side, row + 1); } }
/** * @brief This function continues the game after a floodfill is performed. * It checks if a gameObject on the grid has nothing beneath it. If it doesn't, then the * object and its children are removed from the grid, parented to the hexagon (so that it can * rotate with the hexagon), and move towards the hexagon to move down however many * rows necessary. Game over conditions are also checked! * */ void ContinueFlowOfGame() { for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 1; j < grid.GetLength(1); j++) { //if the current position on the grid is not null (a game object) and there //is no game object beneath it (the grid is null). if (grid [i, j - 1] == null && grid [i, j] != null) { //Move that particular object, parent it to the canvas and remove its children from the grid as well. Transform objectTransform = grid [i, j].transform; objectTransform.SetParent(HexagonBehaviour.blackHexagon.transform); TrapezoidTransform objectScript = grid [i, j].GetComponent <TrapezoidTransform> (); objectScript.move = true; objectScript.gravityFall = true; grid [i, j] = null; for (int k = j + 1; k < grid.GetLength(1); k++) { //if the children chain stops (where the null value is reached from the j value). if (grid [i, k] == null) { break; } else { //remove it from from the grid (since it moves with its parent). objectScript = grid[i, k].GetComponent <TrapezoidTransform> (); objectScript.move = true; grid [i, k] = null; } } } } } // Checks all sides after floodfill to check if 8 are stacked. If they are, load "game over" screen. // It also repositions the top five high scores if the final score of the game belongs in it locally. for (int i = 0; i < grid.GetLength(0); i++) { bool redFlag = false; for (int j = 0; j < grid.GetLength(1); j++) { if (grid [i, j] == null) { redFlag = true; } } //Checks if the column if (redFlag == true) { continue; } else { int topScore = PlayerPrefs.GetInt("1st Place", 0); int secondScore = PlayerPrefs.GetInt("2nd Place", 0); int thirdScore = PlayerPrefs.GetInt("3rd Place", 0); int fourthScore = PlayerPrefs.GetInt("4th Place", 0); int fifthScore = PlayerPrefs.GetInt("5th Place", 0); if (score > topScore) { PlayerPrefs.SetInt("5th Place", PlayerPrefs.GetInt("4th Place", 0)); PlayerPrefs.SetInt("4th Place", PlayerPrefs.GetInt("3rd Place", 0)); PlayerPrefs.SetInt("3rd Place", PlayerPrefs.GetInt("2nd Place", 0)); PlayerPrefs.SetInt("2nd Place", PlayerPrefs.GetInt("1st Place", 0)); PlayerPrefs.SetInt("1st Place", score); } else if (topScore >= score && score > secondScore) { PlayerPrefs.SetInt("5th Place", PlayerPrefs.GetInt("4th Place", 0)); PlayerPrefs.SetInt("4th Place", PlayerPrefs.GetInt("3rd Place", 0)); PlayerPrefs.SetInt("3rd Place", PlayerPrefs.GetInt("2nd Place", 0)); PlayerPrefs.SetInt("2nd Place", score); } else if (secondScore >= score && score > thirdScore) { PlayerPrefs.SetInt("5th Place", PlayerPrefs.GetInt("4th Place", 0)); PlayerPrefs.SetInt("4th Place", PlayerPrefs.GetInt("3rd Place", 0)); PlayerPrefs.SetInt("3rd Place", score); } else if (thirdScore >= score && score > fourthScore) { PlayerPrefs.SetInt("5th Place", PlayerPrefs.GetInt("4th Place", 0)); PlayerPrefs.SetInt("4th Place", score); } else if (fourthScore >= score && score > fifthScore) { PlayerPrefs.SetInt("5th Place", score); } //Resets score score = 0; Application.LoadLevel("GameOver"); } } }
/** * @brief The purpose of this function is to detect a collision between two different * rigidbodies with colliders inside of the child rectangles for the particular trapezoid. * @detail This detects whether a collision with the black hexagon or any of the child trapezoids take place. * If the gameObject that is hit is the black hexagon, the gameObject that this script is referring to * will become a child of the hexagon. If not, then it will become a child of whichever trapezoid it hits. **/ void OnCollisionEnter2D(Collision2D coll) { gameObject.GetComponentInParent <Rigidbody2D> ().velocity = Vector2.zero; TrapezoidTransform swagger = gameObject.GetComponentInParent <TrapezoidTransform>(); swagger.move = false; GameObject blackHexagon = GameObject.Find("Black Hexagon"); if (coll.gameObject.name.Equals("Black Hexagon")) { swagger.gravityFall = false; gameObject.transform.parent.SetParent(coll.transform); Vector3 areaOne = Quaternion.Euler(0, 0, -60) * (HexagonBehaviour.areaZero - blackHexagon.transform.position) + blackHexagon.transform.position; Vector3 areaTwo = Quaternion.Euler(0, 0, -120) * (HexagonBehaviour.areaZero - blackHexagon.transform.position) + blackHexagon.transform.position; Vector3 areaThree = Quaternion.Euler(0, 0, -180) * (HexagonBehaviour.areaZero - blackHexagon.transform.position) + blackHexagon.transform.position; Vector3 areaFour = Quaternion.Euler(0, 0, -240) * (HexagonBehaviour.areaZero - blackHexagon.transform.position) + blackHexagon.transform.position; Vector3 areaFive = Quaternion.Euler(0, 0, -300) * (HexagonBehaviour.areaZero - blackHexagon.transform.position) + blackHexagon.transform.position; Vector2 zeroArea = HexagonBehaviour.areaZero; Vector2 oneArea = areaOne; Vector2 twoArea = areaTwo; Vector2 threeArea = areaThree; Vector2 fourArea = areaFour; Vector2 fiveArea = areaFive; if (Physics2D.OverlapCircle(zeroArea, 3) != null && Physics2D.OverlapCircle(zeroArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 0; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } else if (Physics2D.OverlapCircle(oneArea, 3) != null && Physics2D.OverlapCircle(oneArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 1; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } else if (Physics2D.OverlapCircle(twoArea, 3) != null && Physics2D.OverlapCircle(twoArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 2; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } else if (Physics2D.OverlapCircle(threeArea, 3) != null && Physics2D.OverlapCircle(threeArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 3; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } else if (Physics2D.OverlapCircle(fourArea, 3) != null && Physics2D.OverlapCircle(fourArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 4; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } else if (Physics2D.OverlapCircle(fiveArea, 3) != null && Physics2D.OverlapCircle(fiveArea, 3).gameObject == gameObject) { TrapezoidTransform trapezoidScript = gameObject.GetComponentInParent <TrapezoidTransform> (); trapezoidScript.side = 5; trapezoidScript.row = 0; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [trapezoidScript.side, trapezoidScript.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(trapezoidScript.row); connectedGameObjectsSide.Add(trapezoidScript.side); destroyAllAroundHexagon(trapezoidScript.side, trapezoidScript.row); } else { //grid [trapezoidScript.side,trapezoidScript.row] = gameObject as GameObject; addSelfAndChildren(gameObject.transform.parent.gameObject, trapezoidScript.side, trapezoidScript.row); for (int i = trapezoidScript.row; i < grid.GetLength(1); i++) { if (grid [trapezoidScript.side, i] == null) { continue; } else { enteringSide = trapezoidScript.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } } ContinueFlowOfGame(); } } else if (coll.gameObject.transform.IsChildOf(blackHexagon.transform) && (gameObject.transform.parent.transform.childCount == 3) && (!gameObject.transform.IsChildOf(blackHexagon.transform) || ((gameObject.GetComponentInParent <TrapezoidTransform> ().move == true || gameObject.GetComponentInParent <TrapezoidTransform>().gravityFall == true) && gameObject.transform.IsChildOf(blackHexagon.transform)))) { GameObject topOfStack = topObject(coll.gameObject); //gameObject.transform.parent.SetParent (coll.transform); gameObject.transform.parent.SetParent(topOfStack.transform); gameObject.transform.parent.transform.position = topOfStack.transform.position + topOfStack.transform.up * 5.50f; TrapezoidTransform referenceScript = topOfStack.GetComponent <TrapezoidTransform> (); swagger.side = referenceScript.side; swagger.row = referenceScript.row + 1; if (gameObject.transform.parent.name.Equals("RainbowTrapezoid(Clone)")) { grid [swagger.side, swagger.row] = gameObject.transform.parent.gameObject as GameObject; connectedGameObjectsRow.Add(swagger.row); connectedGameObjectsSide.Add(swagger.side); destroyAllAroundRegular(swagger.side, swagger.row); } else { swagger.gravityFall = false; addSelfAndChildren(gameObject.transform.parent.gameObject, swagger.side, swagger.row); for (int i = swagger.row; i < grid.GetLength(1); i++) { if (grid [swagger.side, i] == null) { continue; } else { enteringSide = swagger.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } //checks every row and column of the grid after all blocks destroyed from floodfills of all children ContinueFlowOfGame(); } } else if (coll.gameObject.transform.IsChildOf(blackHexagon.transform) && (gameObject.transform.parent.transform.childCount == 4) && (gameObject.GetComponentInParent <TrapezoidTransform> ().gravityFall == true && gameObject.transform.parent.transform.IsChildOf(blackHexagon.transform))) { gameObject.transform.parent.SetParent(coll.transform); TrapezoidTransform reference = coll.gameObject.GetComponent <TrapezoidTransform> (); swagger.side = reference.side; swagger.row = reference.row + 1; swagger.gravityFall = false; addSelfAndChildren(gameObject.transform.parent.gameObject, swagger.side, swagger.row); for (int i = swagger.row; i < grid.GetLength(1); i++) { if (grid [swagger.side, i] == null) { continue; } else { enteringSide = swagger.side; enteringRow = i; if (!grid [swagger.side, i].name.Equals("BlackTrapezoid(Clone)")) { floodfill(swagger.side, i, grid [swagger.side, i].name); } } } //checks every row and column of the grid after all blocks destroyed from floodfills of all children ContinueFlowOfGame(); } }