diff --git a/Assets/Scenes/monopoly.unity b/Assets/Scenes/monopoly.unity index 3583f23..f7418fe 100644 --- a/Assets/Scenes/monopoly.unity +++ b/Assets/Scenes/monopoly.unity @@ -1234,7 +1234,8 @@ MonoBehaviour: propertyColour: {fileID: 697882661} buyHouse: {fileID: 569742455} sellHouse: {fileID: 505820772} - Mortgage: {fileID: 516041487} + mortgage: {fileID: 516041487} + mortgageText: {fileID: 1679217085} --- !u!1 &314548389 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/monopoly/FileHandler.cs b/Assets/Scripts/monopoly/FileHandler.cs index 70bef7d..6df67c8 100644 --- a/Assets/Scripts/monopoly/FileHandler.cs +++ b/Assets/Scripts/monopoly/FileHandler.cs @@ -1,27 +1,25 @@ -using System.Collections; -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Runtime.Serialization.Formatters.Binary; using UnityEngine; public static class FileHandler { - public static string LoadProperties() + public static string LoadProperties() //This function loads the properties from a file. { - string path = Application.persistentDataPath + "/properties.smyal"; - if (File.Exists(path)) + string path = Application.persistentDataPath + "/properties.smyal"; //This finds the predefined path in LocalLow and tries to find the JSON file in binary. + if (File.Exists(path)) //If the file exists.. { - BinaryFormatter formatter = new BinaryFormatter(); - FileStream stream = new FileStream(path, FileMode.Open); + BinaryFormatter formatter = new BinaryFormatter(); //Gets the binary formatter to convert it into plain text + FileStream stream = new FileStream(path, FileMode.Open); //Opens the properties.smyal file - string data = formatter.Deserialize(stream) as string; - stream.Close(); + string data = formatter.Deserialize(stream) as string; //It decodes the binary to a string + stream.Close(); //The file is now closed. - return data; + return data; //The data is returned. } - else + else //If the file didn't exist... { - Debug.LogError("Save file not found in " + path); + Debug.LogError("Save file not found in " + path); //Sends an error message to the console. return null; } } diff --git a/Assets/Scripts/monopoly/Inventory.cs b/Assets/Scripts/monopoly/Inventory.cs index 019fb14..66f9de3 100644 --- a/Assets/Scripts/monopoly/Inventory.cs +++ b/Assets/Scripts/monopoly/Inventory.cs @@ -9,6 +9,7 @@ public class Inventory : MonoBehaviour { private Main main; + private Player currentPlayer; //Inventory public GameObject inventoryPanel; @@ -21,7 +22,8 @@ public class Inventory : MonoBehaviour public Image propertyColour; public Button buyHouse; public Button sellHouse; - public Button Mortgage; + public Button mortgage; + public TextMeshProUGUI mortgageText; //This is the function that will be used when the user clicks to open the inventory. public void OpenInventory() @@ -40,32 +42,88 @@ public class Inventory : MonoBehaviour } } - void OpenProperties(Button button) + void OpenProperties(Button button) //This functions runs when a property has been clicked on in the inventory. { - inventoryPanel.SetActive(false); - propertyPanel.SetActive(true); - Debug.Log(button.name); + Property property = new Property(); //This is used for the property information to make the code look neater. + int currentProperty = 50; //This is used for identifying the property in the ownedProperties list - 50 is used if the property wasn't found for some reason. + + for (int i = 0; i < main.board.players[main.board.currentPlayer].ownedProperties.Count; i++) //This checks through the current player's owned properties. + { + if (main.board.players[main.board.currentPlayer].ownedProperties[i].property_name == button.name) //This checks if owned property matches with the button pressed's name. + { + property = main.board.players[main.board.currentPlayer].ownedProperties[i]; //The clicked on property is set to the variable property. + currentProperty = i; //The current property identifier is set. + break; //This stops the for loop prematurely as the search is done. + } + } + + //Initialising the texts: + /* + * TODO: Add House, Remove House + * buyHouse, sellHouse + */ + propertyName.text = property.property_name; //This sets the name of the property title on screen. + propertyColour.color = button.image.color; //This changes the background colour of the title to the colour of the button that was pressed. + + backButton.onClick.AddListener(CloseProperties); //This adds a listener to go back to the main menu screen for the inventory. + + //Buttons + if (!main.board.players[main.board.currentPlayer].ownedProperties[currentProperty].mortgage) //This checks if the property is not mortgaged. + { + mortgage.onClick.RemoveAllListeners(); //This removes all previous onClick listeners. + mortgage.onClick.AddListener(() => Mortgage(currentProperty)); //This adds a click listener to mortgage the property. + } + else //If the property is already mortgaged then... + { + mortgage.onClick.RemoveAllListeners(); //This removes all previous onClick listeners. + mortgage.onClick.AddListener(() => Unmortgage(currentProperty)); //This adds a click listener to unmortgage the property. + } + + inventoryPanel.SetActive(false); //This hides the inventory main menu. + propertyPanel.SetActive(true); //This shows the property specific menu. } - void CloseProperties() + void Mortgage(int currentProperty) //This function runs when the Mortgage button has been pressed. { - propertyPanel.SetActive(false); - inventoryPanel.SetActive(true); + if (main.board.players[main.board.currentPlayer].Mortgage(currentProperty)) //This runs the mortgage function for the property and checks if it was done so successfully. + { + mortgageText.text = "Unmortgage"; //This changes the button from Mortgage to Unmortgage. + mortgage.onClick.RemoveAllListeners(); //This removes all previous onClick listeners. + mortgage.onClick.AddListener(() => Unmortgage(currentProperty)); //This adds a click listener to unmortgage the property. + } + } + + void Unmortgage(int currentProperty) //This function runs when the Unmortgage button has been pressed. + { + if (main.board.players[main.board.currentPlayer].Unmortgage(currentProperty)) //This runs the unmortgage command for the property and checks if it was done so successfully. + { + mortgageText.text = "Mortgage"; //This changes the button from Unmortgage to Mortgage. + mortgage.onClick.RemoveAllListeners(); //This removes all previous onClick listeners. + mortgage.onClick.AddListener(() => Mortgage(currentProperty)); //This adds a click listener to mortgage the property. + } + } + + void CloseProperties() //This closes the property specific window and goes to the inventory main menu. + { + propertyPanel.SetActive(false); //This closes the property specific window. + inventoryPanel.SetActive(true); //This opens the inventory main menu. } - private void Awake() + void Awake() { main = FindObjectOfType
(); } - private void Start() + void Start() { - for (int i = 0; i < properties.Length; i++) + for (int i = 0; i < properties.Length; i++) //This goes through all of the buttons in the main menu. { - properties[i].onClick.AddListener(delegate { OpenProperties(properties[i]); }); + int i1 = i; + properties[i].onClick.AddListener(() => OpenProperties(properties[i1])); //This adds a specific menu for each of the buttons. } - backButton.onClick.AddListener(CloseProperties); + backButton.onClick.AddListener(CloseProperties); //This adds the function to go back to the main inventory page on the property specific window. } + } diff --git a/Assets/Scripts/monopoly/Main.cs b/Assets/Scripts/monopoly/Main.cs index fa56682..1d5615d 100644 --- a/Assets/Scripts/monopoly/Main.cs +++ b/Assets/Scripts/monopoly/Main.cs @@ -70,20 +70,26 @@ public class Property } } - public void mortgageProperty() //Mortgages the property. + public bool mortgageProperty() //Mortgages the property. { - if (!hotel && (houses == 0)) //Checks if there is not a hotel and if there are no houses. + if (!hotel && (houses == 0) && !mortgage) //Checks if there is not a hotel and if there are no houses. { mortgage = true; //Mortgages the property. + return true; //This returns true if done successfully. } + + return false; //This returns false if the property couldn't be mortgaged. } - public void unmortgageProperty() //Removes the mortgage on the property. + public bool unmortgageProperty() //Removes the mortgage on the property. { if (mortgage) //Checks if the property has been mortgaged. { mortgage = false; //Removes the mortgage. + return true; //This returns true if done successfully. } + + return false; //This returns false if the property couldn't be unmortgaged. } public bool isBuyable() //This returns a boolean if the property is buyable @@ -226,16 +232,11 @@ public class Board //Creating the class for the board mechanics. Debug.Log("The property cannot be bought!"); //Prints that theres an error } - - public void Mortgage() - { - //TODO - } } public class Player { - private string name; //This is the username of the player + public string name; //This is the username of the player private int playerNumber; //This is the player number in the queue public int money; //Initializes the variable for money. public int position; //Positions vary from 0-39 (40 squares on the board) (Go is 0) @@ -243,6 +244,7 @@ public class Player public List ownedProperties; //This is the list of properties that the player owns. public GameObject player; private Movement movement; + private TextHandler textHandler; public Player(string playerName, int playerNumber, GameObject player) { @@ -254,6 +256,7 @@ public class Player this.player = player; //This links the object that the player is linked to in the game ownedProperties = new List(); movement = GameObject.FindObjectOfType(); //This finds the movement script in the game + textHandler = GameObject.FindObjectOfType(); //Finds the text handler script } public void Move(int roll) //This moves the player a certain length (what they got from rolling the dice). @@ -304,10 +307,9 @@ public class Player { Debug.Log("Error: You do not have enough money to pay for the property!"); } + } - - public bool CheckColourSet(string colour) //Checks if the player has a whole colour set. { int required = 3; //This is the number of properties needed to own to buy houses. @@ -320,19 +322,51 @@ public class Player for (int i = 0; i < ownedProperties.Count && counter != required; i++) { - if (ownedProperties[i].property_group == colour) + if (ownedProperties[i].property_group == colour) //Checks if the owned property is in the same colour group. { - counter++; + counter++; //Increments the counter if a property was found to be owned. } } return (counter == required); - } - public void Pay(int fee) + public void Pay(float fee) //This function makes the user pay. { - money -= fee; + money -= Convert.ToInt32(fee); //This deducts the money from the user's balance. + textHandler.updateMoney(money); //This updates the text on the screen for the user. + } + + public bool Mortgage(int currentProperty) //This is used for mortgaging a property. + { + if (currentProperty == 50) //Checks if there was an error - 50 is an error code. + { + return false; //Breaks the function and says that it wasn't completed. + } + + if (ownedProperties[currentProperty].mortgageProperty()) //Mortgages the property and if done successfully.. + { + Pay(ownedProperties[currentProperty].property_value / -2); //Gives the user 50% of what the property is worth. (/-2 makes it positive in the Pay function) + return true; //Says that mortgaging has been done successfully. + } + + return false; //Says that mortgaging has not been done successfully. + } + + public bool Unmortgage(int currentProperty) //This is used for unmortgaging a property. + { + if (currentProperty == 50) //Checks if there was an error - 50 is an error code. + { + return false; //Breaks the function and says that it wasn't completed. + } + + if (ownedProperties[currentProperty].unmortgageProperty()) //Unmortgages the property and if done successfully.. + { + Pay((ownedProperties[currentProperty].property_value / 2) * 1.1f); //Makes the user pay what they got in mortgage plus a 10% interest + return true; //Says that mortgaging has been done successfully. + } + + return false; //Says that mortgaging has not been done successfully. } } @@ -409,19 +443,20 @@ public static class MergeMethod public class Main : MonoBehaviour { private List existingProperties; - public GameObject[] waypoints; + public GameObject[] waypoints; //These are all the predefined waypoints on the board. public Board board; //Player variables - public List players = new List(); + public List players = new List(); //Creates a list for all the players playing in the game. private void Awake() { + //Adds the players to the game players.Add(new Player("smyalygames", 0, GameObject.Find("/Players/Player1"))); players.Add(new Player("coomer", 1, GameObject.Find("/Players/Player2"))); - Debug.Log(players[0].player.name); - existingProperties = JsonConvert.DeserializeObject>(FileHandler.LoadProperties()); - board = new Board(players, existingProperties); + Debug.Log(players[0].player.name); //This is just checking if the player has been assigned. + existingProperties = JsonConvert.DeserializeObject>(FileHandler.LoadProperties()); //This loads via JSON all the properties from a file which was originally downloaded from a server. + board = new Board(players, existingProperties); //Creates the board class. } }