//Method to create a village for the player, this is attached a button that is only available if the player does not have a village //Once village is created (and database updated) the edit village scene is loaded public void CreateVillage() { if (PlayerCheck.isCollidingNow()) { //Show Toast WorldSceneToastMaker.showToast("You cannot build here as you are in an enemy zone", 2); return; } String id = vilRef.Push().Key; //Debug.Log(reference); //Debug.Log(reference.Child("villages")); //Debug.Log(id); String[] loc = location.text.Split(',');//lng then lat double lat = Convert.ToDouble(loc[1]); double lng = Convert.ToDouble(loc[0]); Villages vil = new Villages(user.Email, lat, lng, id); //reference.Child("villages").Child(key).Child("User").SetValueAsync(email); //vilRef.Child(id).Child("User").SetValueAsync(email); /* * string json = JsonUtility.ToJson(user); * * mDatabaseRef.Child("users").Child(userId).SetRawJsonValueAsync(json); */ String json = JsonUtility.ToJson(vil); vilRef.Child(id).SetRawJsonValueAsync(json); FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { DataSnapshot dataSnapshot = task.Result; // Do something with snapshot... foreach (DataSnapshot s in dataSnapshot.Children) { if (user.Email.Equals(s.Child("email").Value.ToString())) { //Debug.Log("Here: " + s.Child("email").Value + " " + s.Child("id").Value + " " + s.Child("villageID").Value + " " + s.Child("balance").Value); //Villages vil = new Villages(s.Child("email").Value.ToString(), s.Child("lat").Value.ToString(), s.Child("lng").Value.ToString()); Accounts acc = new Accounts(s.Child("email").Value.ToString(), s.Child("id").Value.ToString(), int.Parse(s.Child("balance").Value.ToString()), id); json = JsonUtility.ToJson(acc); reference.Child("users").Child(s.Child("id").Value.ToString()).SetRawJsonValueAsync(json); } } } }); //Change scene //SceneManager.LoadSceneAsync(2); Scenes.Load("BuildScene - Copy", "villageID", id); }
// Update is called once per frame // Exit if escape (or back, on mobile) is pressed. //If user login authenticated then world scene is loaded //If user registration is succeessful opens the login panel public void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } if (emailText != null) { email = emailText.text; } if (passwordText != null) { password = passwordText.text; } if (loginFin) { loginFin = false; AttemptingLoginPanel.SetActive(false); if (auth.CurrentUser == null) { Debug.Log("Error with sign in try again!"); toastMaker.showToast("There was an error, please try again", 2); //Error msg } else { Debug.Log("Sign in succesful!"); SceneManager.LoadSceneAsync(1); } } if (regFin) { regFin = false; AttemptingRegPanel.SetActive(false); if (regSuccess) { toastMaker.showToast("Account registered successfully. Redirected to Login page", 2); OpenLoginMenu(); regSuccess = false; } else { toastMaker.showToast("Account registration failed", 2); } } }
//Check if its the logged in users village if it is it loads the edit scene //Otherwise it loads the attack scene if the user has £500 or more private void OnMouseDown() { if (ownerEmail.Equals(email)) { Debug.Log("This is your village, you may edit it"); //Scenes.Load("BuildScene", "villageID", villageID); Scenes.Load("BuildScene - Copy", "villageID", villageID); //SceneManager.LoadScene(2); } else { //Check balance here int money = WorldScene.curBal; Debug.Log("current balance of attacking player " + money); if (money >= 500) { Debug.Log("This is not your village, you may attack it"); //Switch to attack scene //Scenes.Load("AttackScene", "villageID", villageID); Scenes.Load("AttackScene - Copy - Copy (2)", "villageID", villageID); } else { WorldSceneToastMaker.showToast("You do not have enough money to attack this village", 2); } } }
//Methods called upon pressing the attack scene shop (the zombie shop) //If balance of player is above the cost then the zombie prefab is spawned on the location of the player and a message is shown //Else a message stating the player cannot afford to purcahse public void zombie1Pressed() { // TODO build stuff... //Make sure not on top of UI int balance = userBalance; int cost = 100; if (cost > balance) { //Show Toast saying cannot afford ShopToastMaker.showToast("You can not afford to purchase Zombie Pack 1 for £100", 2); } else { balance = balance - cost; userBalance = balance; //Show Toast saying spent GameObject zombiesToSpawn = zombie1; GameObject g = (GameObject)Instantiate(zombiesToSpawn, player.transform.position, player.transform.rotation); g.transform.SetParent(ground); ShopToastMaker.showToast("You have purchased Zombie Pack 1 for £100", 2); } }
//Used to update the attack scene. each block of code within has been commented private void Update() { //Updates the health seen in the ui according to the players actual health if (playerHealth != null) { health.text = playerHealth.GetComponent <TextMesh>().text; } else { health.text = ""; } //Gets the users balance from the database at the start of the scene and stores in static variable (userBalance) FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync().ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { DataSnapshot dataSnapshot = task.Result; // Do something with snapshot... foreach (DataSnapshot s in dataSnapshot.Children) { if (email.Equals(s.Child("email").Value.ToString())) { //Debug.Log("LOOK HERE " + s.Child("balance").Value.ToString()); bal = "£" + s.Child("balance").Value.ToString(); if (!onlyOnce) { userBalance = int.Parse(s.Child("balance").Value.ToString()); onlyOnce = true; } } } } }); /* If the balance has been retrieved from the database then * it uses the static variable to update the text representing the players balance on the ui */ if (onlyOnce) { balanceText.text = "£" + userBalance.ToString(); } //Once the userID and villages turrets have been retreived from database the village is populated and the load panel is hidden if (loadDone && loadDone2) { for (int i = 0; i < positions.Count; i++) { ////// ///new stuff GameObject g; if (turretTypes[i] == 1) { g = (GameObject)Instantiate(turr1); } else if (turretTypes[i] == 2) { g = (GameObject)Instantiate(turr2); } else { g = (GameObject)Instantiate(turr3); } g.transform.position = positions[i]; g.transform.SetParent(ground); } loadingPanel.SetActive(false); loadDone = false; } //If village hub is destroyed then show the win panel else if the player is destroyed show the lose panel if (villageHub == null) { WinPanel.SetActive(true); //Stop player moving after killing village by disabling script MovementTest mTest = player.GetComponent <MovementTest>(); mTest.Stop(); if (!balChangedOnWinOrLose) { userBalance = userBalance + 500; balChangedOnWinOrLose = true; //Show Toast WinToastMaker.showToast("Congratulations! You have earned £500", 2); } } else if (player == null) { gameOverPanel.SetActive(true); if (!balChangedOnWinOrLose) { userBalance = userBalance - 500; balChangedOnWinOrLose = true; //Show Toast //toastMaker.setTextObj(LoseToastText); LoseToastMaker.showToast("You have failed to take over this village. Forced to pay out £500", 2); } } }