// Update is called once per frame void Update() { if (TowerBuild.setUpDone) { // check if the player turn has changed if (currentPlayer != playerTurn || nextTurn) { currentPlayer = playerTurn; nextTurn = false; if (gameOver) { turnsTaken--; } turnsTaken++; recentlyUndo = false; recentlyRedo = false; // reset redo undo lists redoTowerPos = new List <Vector3>(); redoTowerRot = new List <Quaternion> (); undoTowerPos = tempPos; undoTowerRot = tempRot; tempPos = new List <Vector3> (); tempRot = new List <Quaternion> (); // get the list of blocklayers List <Transform> tower = TowerBuild.blkLayers; // in the beginning of the turn, get all the positions //print("FILLING IN TEMP"); for (int i = 0; i < tower.Count; i++) { // fill in the perFrame buffer for (int j = 0; j < 3; j++) { Transform currBlock; if (tower [i].childCount == 3 || j < tower [i].childCount) { currBlock = tower [i].GetChild(j); } // if one block is being held, retrieve that child else if (tower [i].childCount < 3) { currBlock = tower [i].GetChild(0); // retrieve the held block Stylus5 stylusGO = (Stylus5)FindObjectOfType(typeof(Stylus5)); if (stylusGO.interactingWith != null) { Transform missing = stylusGO.interactingWith.transform; if (j == missing.GetComponent <BlockInteraction> ().missIndex) { currBlock = missing; } } } else { currBlock = tower [i].GetChild(0); } Vector3 toAdd = new Vector3(0f, 0.001f * i, 0f); tempPos.Add(currBlock.position + toAdd); tempRot.Add(currBlock.rotation); } } } } GameObject.FindGameObjectWithTag("TurnsTaken").GetComponent <UnityEngine.UI.Text> ().text = "Turns taken: " + turnsTaken; if (!gameOver) { GameObject.FindGameObjectWithTag("PlayerText").GetComponent <UnityEngine.UI.Text>().text = "Player " + playerTurn + "'s turn"; } else { GameObject.FindGameObjectWithTag("PlayerText").GetComponent <UnityEngine.UI.Text>().text = "Player " + playerTurn + " has lost!"; } }
void Start() { stylusGO = (Stylus5)FindObjectOfType(typeof(Stylus5)); }
public static void undo() { if (TowerBuild.setUpDone && undoTowerPos.Count > 1) { // get the list of block layers List <Transform> tower = TowerBuild.blkLayers; // if we've already moved one step, we don't have to do anything if (!recentlyUndo) { // modify playerTurn, turnsTaken, currentPlayer, and recentlyUndo playerTurn = (playerTurn == 1 && Numbers.numPlayers == 2) ? 2 : 1; turnsTaken--; //if (gameOver) // turnsTaken--; currentPlayer = playerTurn; recentlyRedo = false; recentlyUndo = true; } // we shouldn't be in game over after an undo gameOver = false; GroundCollide.gameOver = false; // fill in the redo section for (int i = 0; i < tower.Count; i++) { // fill in the perFrame buffer for (int j = 0; j < 3; j++) { Transform currBlock; if (tower [i].childCount == 3 || j < tower [i].childCount) { currBlock = tower [i].GetChild(j); } // if one block is being held, retrieve that child else if (tower [i].childCount < 3) { currBlock = tower [i].GetChild(0); // retrieve the held block Stylus5 stylusGO = (Stylus5)FindObjectOfType(typeof(Stylus5)); if (stylusGO.interactingWith != null) { Transform missing = stylusGO.interactingWith.transform; if (j == missing.GetComponent <BlockInteraction> ().missIndex) { currBlock = missing; } } } else { currBlock = tower [i].GetChild(0); } Vector3 toAdd = new Vector3(0f, 0.001f * i, 0f); redoTowerPos.Add(currBlock.position + toAdd); redoTowerRot.Add(currBlock.rotation); } } // reset the whole tower to the last turn for (int i = 0; i < tower.Count; i++) { for (int j = 0; j < 3; j++) { Transform block = tower [i].GetChild(j); block.position = undoTowerPos [(i * 3) + j]; block.rotation = undoTowerRot [(i * 3) + j]; } } if (GroundCollide.lastRemovedBlock.GetComponent <BlockState> ().hitTheGround) { GroundCollide.lastRemovedBlock.GetComponent <BlockState> ().undoed = true; } GroundCollide.lastRemovedBlock.GetComponent <BlockState> ().hitTheGround = false; } else { print("undo failed"); } }
void OnCollisionEnter(Collision collision) { // check if block (one) hits the ground GameObject g = collision.gameObject; if (g.layer == 8) { BlockState bState = g.GetComponent <BlockState> (); if (!bState.hitTheGround) { bState.hitTheGround = true; print(bState.hitTheGround + " " + g.name + " has hit the ground"); lastRemovedBlock = g; // g.SetActive(false); startTimer = true; // if yes... // check if player holding block Stylus5 stylusGO = (Stylus5)FindObjectOfType(typeof(Stylus5)); if (stylusGO.interactingWith != null) { Transform heldBlock = stylusGO.interactingWith.transform; if (heldBlock == null) { // fail game gameOver = true; } } // check if another block hits ground in next 5 seconds if (Time.time - timeStart < 5.0 && check2ndCollision) { // fail game gameOver = true; } check2ndCollision = true; // check if top layer fo tower falls below a threshold Transform topLayer = TowerBuild.blkLayers [TowerBuild.blkLayers.Count - 1]; Transform topX, topY, topZ; if (topLayer.childCount == 3) { topX = topLayer.GetChild(0); topY = topLayer.GetChild(1); topZ = topLayer.GetChild(2); } else { topX = topLayer.GetChild(0); topY = topLayer.GetChild(1); if (stylusGO.interactingWith != null) { topZ = stylusGO.interactingWith.transform; } else { topZ = topLayer.GetChild(1); } } if (topLayer.position.y - topX.position.y > distThreshold || topLayer.position.y - topY.position.y > distThreshold || topLayer.position.y - topZ.position.y > distThreshold) { // fail game gameOver = true; } } } // when turn is done, set startTimer to false }