Made it so that the player has to own all the properties in a colour to buy/sell.

This commit is contained in:
Anthony Berg
2020-08-29 01:32:07 +01:00
parent 3c5debf1b1
commit 6f4c8a85a3
6 changed files with 572 additions and 85 deletions

View File

@@ -39,7 +39,7 @@ public class ButtonHandler : MonoBehaviour
void OpenInventory()
{
inventoryClass.OpenInventory();
inventoryClass.UpdateInventory();
GameUI.SetActive(false);
PropertyUI.SetActive(true);
inventoryText.text = "Close Inventory";

View File

@@ -22,11 +22,26 @@ public class Inventory : MonoBehaviour
public Button buyHouse;
public TextMeshProUGUI buyHouseText;
public Button sellHouse;
public TextMeshProUGUI sellHouseText;
public Button mortgage;
public TextMeshProUGUI mortgageText;
private Button lastButton;
//This is the function that will be used when the user clicks to open the inventory.
public void OpenInventory()
public void UpdateInventory() //This is meant to update what is on the property panel when
{
if (inventoryPanel.activeSelf) //This checks if the inventory panel is open.
{
OpenInventory(); //This updates only the inventory panel.
}
else if (propertyPanel.activeSelf) //This checks if the property panel is open.
{
OpenProperties(lastButton); //This updates the properties panel.
}
}
public void OpenInventory() //This function renders the inventory pannel.
{
Player currentPlayer = main.board.players[main.board.currentPlayer]; //Initialises the current player to make the code look neater.
for (int i = 0; i < properties.Length; i++) //Checks through all of the buttons.
@@ -44,6 +59,7 @@ public class Inventory : MonoBehaviour
void OpenProperties(Button button) //This functions runs when a property has been clicked on in the inventory.
{
lastButton = button; //This caches the previously used button for updating when closed and reopened.
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.
@@ -63,9 +79,11 @@ public class Inventory : MonoBehaviour
propertyHouses.text = property.ParseHouses(); //This shows how many houses there are.
backButton.onClick.AddListener(CloseProperties); //This adds a listener to go back to the main menu screen for the inventory.
//These clears all of the previous listeners.
buyHouse.onClick.RemoveAllListeners();
sellHouse.onClick.RemoveAllListeners();
//These adds listeners to the sell and buy buttons for this specific property.
buyHouse.onClick.AddListener(() => BuyHouse(currentProperty, property.property_name));
sellHouse.onClick.AddListener(() => SellHouse(currentProperty, property.property_name));
@@ -93,6 +111,13 @@ public class Inventory : MonoBehaviour
break;
}
if (!main.board.players[main.board.currentPlayer].CheckColourSet(property.property_group)) //This checks if all of the colour group properties are owned.
{
//If they aren't all owned, the buy and sell buttons will be disabled.
buyHouse.interactable = false;
sellHouse.interactable = false;
}
//Buttons
if (!main.board.players[main.board.currentPlayer].ownedProperties[currentProperty].mortgage) //This checks if the property is not mortgaged.
{
@@ -109,47 +134,60 @@ public class Inventory : MonoBehaviour
propertyPanel.SetActive(true); //This shows the property specific menu.
}
void BuyHouse(int currentProperty, string propertyName)
void BuyHouse(int currentProperty, string propertyName) //This is the function for the buy button.
{
main.board.BuyHouseOnProperty(propertyName);
Property property = main.board.players[main.board.currentPlayer].ownedProperties[currentProperty];
propertyHouses.text = property.ParseHouses();
if (property.houses == 4)
main.board.BuyHouseOnProperty(propertyName); //This will pass the buy function in the main script.
Property property = main.board.players[main.board.currentPlayer].ownedProperties[currentProperty]; //This caches the current property information.
propertyHouses.text = property.ParseHouses(); //This changes the text for how many houses there are on the property.
switch (property.houses) //This now checks how many houses there are on the property.
{
buyHouseText.text = "Buy Hotel";
}
else if (property.houses > 4)
{
buyHouseText.text = "Buy Hotel";
buyHouse.interactable = false;
}
else
{
buyHouseText.text = "Buy House";
case 4: //This checks if there are enough houses to buy a hotel now.
buyHouseText.text = "Buy Hotel"; //This changes the text on the button to buying a hotel.
sellHouseText.text = "Sell House"; //This changes the text on the button to selling a house.
break;
case 5: //This checks if the property has reached its limit on buying properties.
buyHouseText.text = "Buy Hotel"; //This changes the text on the button to buying a hotel.
sellHouseText.text = "Sell Hotel"; //This changes the text on the button to selling a hotel.
buyHouse.interactable = false; //This disables the user from buying more houses.
break;
default:
buyHouseText.text = "Buy House"; //If there aren't enough for a hotel, the button will display Buy House.
sellHouseText.text = "Sell House"; //This changes the button text to selling a house.
break;
}
sellHouse.interactable = true;
sellHouse.interactable = true; //As when a property will be bought, it will immediately allow the user to sell their houses.
}
void SellHouse(int currentProperty, string propertyName)
void SellHouse(int currentProperty, string propertyName) //This is the function for the buy button.
{
main.board.SellHouseOnProperty(propertyName);
Property property = main.board.players[main.board.currentPlayer].ownedProperties[currentProperty];
propertyHouses.text = property.ParseHouses();
if (property.houses == 0)
{
sellHouse.interactable = false;
}
else if (property.houses < 4)
{
buyHouseText.text = "Buy House";
}
else if (property.houses >= 4)
{
buyHouseText.text = "Buy Hotel";
}
main.board.SellHouseOnProperty(propertyName); //This will pass the sell function in the main script.
buyHouse.interactable = true;
Property property = main.board.players[main.board.currentPlayer].ownedProperties[currentProperty]; //This caches the current property information.
propertyHouses.text = property.ParseHouses(); //This changes the text for how many houses there are on the property.
switch (property.houses) //This now checks how many houses there are on the property.
{
case 0: //This checks if there are 0 houses left.
buyHouseText.text = "Buy House"; //This changes the button text to buying a house.
sellHouseText.text = "Sell House"; //This changes the button text to selling a house.
sellHouse.interactable = false; //If there are no more houses, the player can't sell anymore, so the sell button will be disabled.
break;
case 4: //This is for when they have sold the hotel.
buyHouseText.text = "Buy Hotel"; //This changes the button text to buying a hotel.
sellHouseText.text = "Sell House"; //This changes the button text to selling a house.
break;
default:
buyHouseText.text = "Buy House"; //This changes the button text to buying a house.
sellHouseText.text = "Sell House"; //This changes the button text to selling a house.
break;
}
buyHouse.interactable = true; //As when a property is sold, it means that the houses aren't at its max and hence enables the buy button.
}
void Mortgage(int currentProperty) //This function runs when the Mortgage button has been pressed.
@@ -174,6 +212,7 @@ public class Inventory : MonoBehaviour
void CloseProperties() //This closes the property specific window and goes to the inventory main menu.
{
OpenInventory(); //This updates the inventory panel.
propertyPanel.SetActive(false); //This closes the property specific window.
inventoryPanel.SetActive(true); //This opens the inventory main menu.
}

View File

@@ -223,20 +223,20 @@ public class Board //Creating the class for the board mechanics.
return false; //Returns false if the property is not buybale.
}
public (int, int) FindOwner(string propertyName)
public (int, int) FindOwner(string propertyName) //This function is used to find the property and the owner of it.
{
for (int i = 0; i < players.Count; i++)
for (int i = 0; i < players.Count; i++) //This is the loop for the amount of players playing.
{
for (int j = 0; j < players[i].ownedProperties.Count; j++)
for (int j = 0; j < players[i].ownedProperties.Count; j++) //This is the loop for the amount of properties the currently searched player owns.
{
if (players[i].ownedProperties[j].property_name == propertyName)
if (players[i].ownedProperties[j].property_name == propertyName) //This checks if the property name parameter matches the one that the player owns.
{
return (i, j);
return (i, j); //It returns the player then the property position.
}
}
}
return (0, 0);
return (0, 0); //Default error.
}
public void BuyProperty()
@@ -270,90 +270,95 @@ public class Board //Creating the class for the board mechanics.
}
public void BuyHouseOnProperty(string propertyName)
public void BuyHouseOnProperty(string propertyName) //This function links the UI button and this class to buy properties.
{
var location = FindOwner(propertyName);
var location = FindOwner(propertyName); //This function finds the owner and the position of the property in the owner's list of properties.
Property property = players[location.Item1].ownedProperties[location.Item2];
Property property = players[location.Item1].ownedProperties[location.Item2]; //This caches the property information that was selected.
if (property.houses < 4)
if (!players[location.Item1].CheckColourSet(property.property_group)) //This checks if the player owns all of the properties in the group.
{
if (BuyHouse())
return; //This stops the function if they don't own all the properties in the group.
}
if (property.houses < 4) //This checks if the property has less than 4 houses.
{
if (BuyHouse()) //This then buys a house locally.
{
players[location.Item1].ownedProperties[location.Item2].addHouse();
players[location.Item1].ownedProperties[location.Item2].addHouse(); //This then buys the house in the Property class.
}
}
else if (property.houses == 4)
else if (property.houses == 4) //This checks if the property has enough houses to buy a hotel.
{
if (BuyHotel())
if (BuyHotel()) //This buys a hotel locally.
{
players[location.Item1].ownedProperties[location.Item2].addHouse();
players[location.Item1].ownedProperties[location.Item2].addHouse(); //This then buys the hotel in the Property class.
}
}
players[location.Item1].Pay(Convert.ToInt32(property.property_cost));
players[location.Item1].Pay(Convert.ToInt32(property.property_cost)); //This then makes the player pay for the house that they bought.
}
public void SellHouseOnProperty(string propertyName)
public void SellHouseOnProperty(string propertyName) //This function links the UI button and this class to sell properties.
{
var location = FindOwner(propertyName);
var location = FindOwner(propertyName); //This function finds the owner and the position of the property in the owner's list of properties.
Property property = players[location.Item1].ownedProperties[location.Item2];
Property property = players[location.Item1].ownedProperties[location.Item2]; //This caches the property information that was selected.
if (property.houses <= 4 && property.houses > 0)
if (property.houses <= 4 && property.houses > 0) //This checks if the properties has a house(s).
{
SellHouse();
players[location.Item1].ownedProperties[location.Item2].removeHouse();
SellHouse(); //This then locally sells the house(s) locally.
players[location.Item1].ownedProperties[location.Item2].removeHouse(); //This then removes the house in the Property class.
}
else if (property.houses == 5)
else if (property.houses == 5) //This checks if the property has a hotel.
{
if (SellHotel())
if (SellHotel()) //This then sells the hotel locally.
{
players[location.Item1].ownedProperties[location.Item2].removeHouse();
players[location.Item1].ownedProperties[location.Item2].removeHouse(); //This then removes the hotel in the Property class.
}
}
players[location.Item1].Pay(Convert.ToInt32(property.property_cost)/-2);
players[location.Item1].Pay(Convert.ToInt32(property.property_cost)/-2); //This then gives back the money to the player for half the house/hotel's cost.
}
private bool BuyHouse()
private bool BuyHouse() //This function is used to buy houses locally.
{
if (houses > 0)
if (houses > 0) //This checks if there are enough houses to buy.
{
houses--;
return true;
houses--; //This removes a house from the board as the house can be bought.
return true; //This says that the house can be bought.
}
return false;
return false; //This says that the house cannot be bought.
}
private void SellHouse()
private void SellHouse() //This function is used to sell a house locally.
{
houses++;
houses++; //This adds a house to the board as the house has been sold.
}
private bool BuyHotel()
private bool BuyHotel() //This function is used to buy a hotel locally.
{
if (hotels > 0)
if (hotels > 0) //This checks if there are enough hotels on the board.
{
houses += 4;
hotels--;
return true;
houses += 4; //This adds 4 houses to the board as the player only has a hotel now.
hotels--; //This removes a hotel from the board.
return true; //This says that the hotel can be bought.
}
return false;
return false; //This says that the hotel cannot be bought.
}
private bool SellHotel()
private bool SellHotel() //This function is used to sell a hotel locally.
{
if (houses >= 4)
if (houses >= 4) //This first checks if there are enough houses to sell the hotel.
{
houses -= 4;
hotels++;
return true;
houses -= 4; //This removes 4 houses from the board and gives it to the property.
hotels++; //This then adds a hotel to the board.
return true; //This returns true to say that the hotel can be sold.
}
return false;
return false; //This returns false to say that the hotel cannot be sold.
}
}

View File

@@ -1,6 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
public class RollDice : MonoBehaviour
@@ -54,4 +52,4 @@ public class RollDice : MonoBehaviour
main.board.MovePlayer(totalRoll);
}
}
}