// Store all game objects with the BuildingBlock-tag using Parse IEnumerator SubmitSceneToParse_Local() { List <ParseBuildingBlock> newBlocks = new List <ParseBuildingBlock> (); var gameObjectBlocks = GameObject.FindGameObjectsWithTag("BuildingBlock"); foreach (var gameObjectBlock in gameObjectBlocks) { bool blockExists = false; //iterate all blocks, if a block does not exist then add it to newblocks array, otherwise update its values foreach (var block in AppModel.currentBlocks) { if (block.ObjectId == gameObjectBlock.GetComponent <BuildingBlock>().objectId) { if (block.posX != gameObjectBlock.transform.position.x || block.posY != gameObjectBlock.transform.position.y || block.rotZ != gameObjectBlock.transform.rotation.z) { block.posX = gameObjectBlock.transform.position.x; block.posY = gameObjectBlock.transform.position.y; block.rotZ = gameObjectBlock.transform.rotation.eulerAngles.z; block.SaveAsync().ContinueWith(t => { }); } blockExists = true; break; } } if (blockExists) { continue; } //add the block as a new item ParseBuildingBlock newBlock = new ParseBuildingBlock(); newBlock.matchId = AppModel.currentMatch.ObjectId; newBlock.originalPosX = gameObjectBlock.GetComponent <BuildingBlock>().originalPosX; newBlock.originalPosY = gameObjectBlock.GetComponent <BuildingBlock>().originalPosY; newBlock.originalRotZ = gameObjectBlock.GetComponent <BuildingBlock>().originalRotZ; newBlock.posX = gameObjectBlock.transform.position.x; newBlock.posY = gameObjectBlock.transform.position.y; newBlock.rotZ = gameObjectBlock.transform.rotation.eulerAngles.z; newBlock.index = gameObjectBlock.GetComponent <BuildingBlock>().index; newBlock.SetBuildingBlockType(gameObjectBlock.GetComponent <BuildingBlock>().buildingBlockType); newBlocks.Add(newBlock); } //if newblocks array has objects, upload them if (newBlocks.Count() > 0) { newBlocks.SaveAllAsync().ContinueWith(t => { }); } //change turn for the match object, then return to matches screen AppModel.currentMatch ["playerTurn"] = AppModel.currentOpponent; var updateTurn = AppModel.currentMatch.SaveAsync(); while (!updateTurn.IsCompleted) { yield return(null); } if (!updateTurn.IsCanceled && !updateTurn.IsFaulted) { headerText.text = "Waiting for " + AppModel.currentOpponent["displayName"].ToString(); refreshImage.gameObject.SetActive(true); refreshImage.transform.parent.gameObject.SetActive(true); } // Activate back button GameObject.Find("BackButton").GetComponent <Button>().interactable = true; }
// Instantiate building blocks using varible "result" // It is up to the caller to check so that result is valid IEnumerator InstantiateBlocks() { // Safety check! if (result != null) { Time.timeScale = 30f; // Check whos turn it is ParseUser playerTurn = AppModel.currentMatch["playerTurn"] as ParseUser; bool myTurn = false; if (!playerTurn.ObjectId.Equals(ParseUser.CurrentUser.ObjectId)) { headerText.text = "Waiting for " + AppModel.currentOpponent["displayName"].ToString(); refreshImage.gameObject.SetActive(true); refreshImage.transform.parent.gameObject.SetActive(true); } else { headerText.text = "Your turn against " + AppModel.currentOpponent["displayName"].ToString(); refreshImage.gameObject.SetActive(false); refreshImage.transform.parent.gameObject.SetActive(false); myTurn = true; } List <GameObject> newBuildingBlocksList = new List <GameObject>(); foreach (var item in result) { //Instantiate block from prefab ParseBuildingBlock pb = item as ParseBuildingBlock; Quaternion rotation = Quaternion.identity; Vector3 eulerAngles = rotation.eulerAngles; eulerAngles.z = pb.originalRotZ; rotation.eulerAngles = eulerAngles; GameObject newBuildingBlock = Instantiate(buildingBlocks[(int)pb.GetBuildingBlockType()], new Vector3(pb.originalPosX, pb.originalPosY, -1f), rotation) as GameObject; //Store Parse Object ID in prefab class BuildingBlock newBuildingBlockScript = newBuildingBlock.GetComponentInChildren <BuildingBlock>(); newBuildingBlockScript.objectId = pb.ObjectId; // Set building block to released (not swinging) newBuildingBlockScript.isHeldAtStart = false; newBuildingBlocksList.Add(newBuildingBlockScript.gameObject); nextBuildingBlockIndex++; // Add to next building block index so that we know the index for the next building block we place in the scene yield return(new WaitForSeconds(.01f)); int ctr = 0, listLength = newBuildingBlocksList.Count; while (ctr < listLength) { while (!MathUtilities.IsApproximately(newBuildingBlocksList[ctr].GetComponent <Rigidbody2D>().velocity.x, 0.0f) || !MathUtilities.IsApproximately(newBuildingBlocksList[ctr].GetComponent <Rigidbody2D>().velocity.y, 0.0f) || !MathUtilities.IsApproximately(newBuildingBlocksList[ctr].GetComponent <Rigidbody2D>().angularVelocity, 0.0f)) { // Break from coroutine and reset ctr in order to start over looping all the building blocks yield return(null); ctr = 0; continue; } ctr++; } } Time.timeScale = 1f; if (myTurn) { buildingBlockSlotMachine.GenerateNewBuildingBlocks(); } } }