Added a mortgage function that now works

This commit is contained in:
Anthony Berg 2020-08-26 01:32:12 +01:00
parent 168e2943fc
commit 0bcb724c04
4 changed files with 140 additions and 48 deletions

View File

@ -1234,7 +1234,8 @@ MonoBehaviour:
propertyColour: {fileID: 697882661} propertyColour: {fileID: 697882661}
buyHouse: {fileID: 569742455} buyHouse: {fileID: 569742455}
sellHouse: {fileID: 505820772} sellHouse: {fileID: 505820772}
Mortgage: {fileID: 516041487} mortgage: {fileID: 516041487}
mortgageText: {fileID: 1679217085}
--- !u!1 &314548389 --- !u!1 &314548389
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -1,27 +1,25 @@
using System.Collections; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine; using UnityEngine;
public static class FileHandler 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"; 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 (File.Exists(path)) //If the file exists..
{ {
BinaryFormatter formatter = new BinaryFormatter(); BinaryFormatter formatter = new BinaryFormatter(); //Gets the binary formatter to convert it into plain text
FileStream stream = new FileStream(path, FileMode.Open); FileStream stream = new FileStream(path, FileMode.Open); //Opens the properties.smyal file
string data = formatter.Deserialize(stream) as string; string data = formatter.Deserialize(stream) as string; //It decodes the binary to a string
stream.Close(); 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; return null;
} }
} }

View File

@ -9,6 +9,7 @@ public class Inventory : MonoBehaviour
{ {
private Main main; private Main main;
private Player currentPlayer;
//Inventory //Inventory
public GameObject inventoryPanel; public GameObject inventoryPanel;
@ -21,7 +22,8 @@ public class Inventory : MonoBehaviour
public Image propertyColour; public Image propertyColour;
public Button buyHouse; public Button buyHouse;
public Button sellHouse; 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. //This is the function that will be used when the user clicks to open the inventory.
public void OpenInventory() 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); Property property = new Property(); //This is used for the property information to make the code look neater.
propertyPanel.SetActive(true); 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.
Debug.Log(button.name);
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.
}
} }
void CloseProperties() //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.
{ {
propertyPanel.SetActive(false); mortgage.onClick.RemoveAllListeners(); //This removes all previous onClick listeners.
inventoryPanel.SetActive(true); 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 Mortgage(int currentProperty) //This function runs when the Mortgage button has been pressed.
{
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<Main>(); main = FindObjectOfType<Main>();
} }
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.
} }
} }

View File

@ -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. mortgage = true; //Mortgages the property.
} return true; //This returns true if done successfully.
} }
public void unmortgageProperty() //Removes the mortgage on the property. return false; //This returns false if the property couldn't be mortgaged.
}
public bool unmortgageProperty() //Removes the mortgage on the property.
{ {
if (mortgage) //Checks if the property has been mortgaged. if (mortgage) //Checks if the property has been mortgaged.
{ {
mortgage = false; //Removes the mortgage. 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 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 Debug.Log("The property cannot be bought!"); //Prints that theres an error
} }
public void Mortgage()
{
//TODO
}
} }
public class Player 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 private int playerNumber; //This is the player number in the queue
public int money; //Initializes the variable for money. public int money; //Initializes the variable for money.
public int position; //Positions vary from 0-39 (40 squares on the board) (Go is 0) 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<Property> ownedProperties; //This is the list of properties that the player owns. public List<Property> ownedProperties; //This is the list of properties that the player owns.
public GameObject player; public GameObject player;
private Movement movement; private Movement movement;
private TextHandler textHandler;
public Player(string playerName, int playerNumber, GameObject player) 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 this.player = player; //This links the object that the player is linked to in the game
ownedProperties = new List<Property>(); ownedProperties = new List<Property>();
movement = GameObject.FindObjectOfType<Movement>(); //This finds the movement script in the game movement = GameObject.FindObjectOfType<Movement>(); //This finds the movement script in the game
textHandler = GameObject.FindObjectOfType<TextHandler>(); //Finds the text handler script
} }
public void Move(int roll) //This moves the player a certain length (what they got from rolling the dice). 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!"); 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. 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. 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++) 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); 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 public class Main : MonoBehaviour
{ {
private List<Property> existingProperties; private List<Property> existingProperties;
public GameObject[] waypoints; public GameObject[] waypoints; //These are all the predefined waypoints on the board.
public Board board; public Board board;
//Player variables //Player variables
public List<Player> players = new List<Player>(); public List<Player> players = new List<Player>(); //Creates a list for all the players playing in the game.
private void Awake() private void Awake()
{ {
//Adds the players to the game
players.Add(new Player("smyalygames", 0, GameObject.Find("/Players/Player1"))); players.Add(new Player("smyalygames", 0, GameObject.Find("/Players/Player1")));
players.Add(new Player("coomer", 1, GameObject.Find("/Players/Player2"))); players.Add(new Player("coomer", 1, GameObject.Find("/Players/Player2")));
Debug.Log(players[0].player.name); Debug.Log(players[0].player.name); //This is just checking if the player has been assigned.
existingProperties = JsonConvert.DeserializeObject<List<Property>>(FileHandler.LoadProperties()); existingProperties = JsonConvert.DeserializeObject<List<Property>>(FileHandler.LoadProperties()); //This loads via JSON all the properties from a file which was originally downloaded from a server.
board = new Board(players, existingProperties); board = new Board(players, existingProperties); //Creates the board class.
} }
} }