mirror of
https://github.com/smyalygames/monopoly.git
synced 2025-11-23 07:30:05 +01:00
Completed trading
This commit is contained in:
parent
fa91572726
commit
87d996e5b8
File diff suppressed because it is too large
Load Diff
@ -25,6 +25,7 @@ public class ButtonHandler : MonoBehaviour
|
|||||||
//Trade
|
//Trade
|
||||||
public GameObject TradeUI;
|
public GameObject TradeUI;
|
||||||
public Button tradeButton;
|
public Button tradeButton;
|
||||||
|
public Button tradeCompletedButton;
|
||||||
private bool tradeOpen = false;
|
private bool tradeOpen = false;
|
||||||
private Trade trade;
|
private Trade trade;
|
||||||
|
|
||||||
@ -138,6 +139,7 @@ public class ButtonHandler : MonoBehaviour
|
|||||||
inventory.onClick.AddListener(ToggleInventory);
|
inventory.onClick.AddListener(ToggleInventory);
|
||||||
cardButton.onClick.AddListener(CloseCard);
|
cardButton.onClick.AddListener(CloseCard);
|
||||||
tradeButton.onClick.AddListener(ToggleTrade);
|
tradeButton.onClick.AddListener(ToggleTrade);
|
||||||
|
tradeCompletedButton.onClick.AddListener(ToggleTrade);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuyPropertyClick() {
|
void BuyPropertyClick() {
|
||||||
|
|||||||
@ -894,21 +894,47 @@ public class Player
|
|||||||
|
|
||||||
public void BuyProperty(Property property) //This function allows the player to own a property.
|
public void BuyProperty(Property property) //This function allows the player to own a property.
|
||||||
{
|
{
|
||||||
int price = property.property_value;
|
int price = property.property_value; //Assigns the price of the property.
|
||||||
|
|
||||||
if (money - price >= 0)
|
if (money - price >= 0) //If the player can afford the property.
|
||||||
{
|
{
|
||||||
ownedProperties.Add(property); //Adds the property to the list of the player owned properties.
|
ownedProperties.Add(property); //Adds the property to the list of the player owned properties.
|
||||||
ownedProperties = MergeMethod.MergeSort(ownedProperties);
|
ownedProperties = MergeMethod.MergeSort(ownedProperties); //Performs a merge sort on the ownedProperties list
|
||||||
money -= price;
|
money -= price; //Removes the balance from the player.
|
||||||
}
|
}
|
||||||
else
|
else //If the player cannot afford the property.
|
||||||
{
|
{
|
||||||
Debug.Log("Error: You do not have enough money to pay for the property!");
|
Debug.LogError("You do not have enough money to pay for the property!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Trading
|
||||||
|
public void AddProperty(Property property)
|
||||||
|
{
|
||||||
|
ownedProperties.Add(property); //Adds the property to the list of the player owned properties.
|
||||||
|
ownedProperties = MergeMethod.MergeSort(ownedProperties); //Performs a merge sort on the ownedProperties list
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveProperty(Property property)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < ownedProperties.Count; i++) //This goes through all the properties in the list.
|
||||||
|
{
|
||||||
|
if (ownedProperties[i] == property) //Checks if the property is the same as the item in the list.
|
||||||
|
{
|
||||||
|
ownedProperties.RemoveAt(i); //Removes the property from the list.
|
||||||
|
ownedProperties = MergeMethod.MergeSort(ownedProperties); //Performs a merge sort on the ownedProperties list
|
||||||
|
Debug.Log(ownedProperties.Count);
|
||||||
|
return; //Ends the for loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateMoneyText()
|
||||||
|
{
|
||||||
|
textHandler.UpdateMoney(money);
|
||||||
|
}
|
||||||
|
|
||||||
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.
|
||||||
|
|||||||
@ -21,20 +21,48 @@ public class Trade : MonoBehaviour
|
|||||||
public Button playerSelectionNext; //This is a button to click next on the player selection screen.
|
public Button playerSelectionNext; //This is a button to click next on the player selection screen.
|
||||||
|
|
||||||
//Player information
|
//Player information
|
||||||
|
private Player player1Information; //This is just the class of information about player 1.
|
||||||
|
private Player player2Information; //This is just the class of information about player 1.
|
||||||
private List<Property> player1Properties; //This creates a list of all the properties from player 1.
|
private List<Property> player1Properties; //This creates a list of all the properties from player 1.
|
||||||
private List<Property> player2Properties; //This creates a list of all the properties from player 2.
|
private List<Property> player2Properties; //This creates a list of all the properties from player 2.
|
||||||
|
|
||||||
//Core Trading
|
//Displays
|
||||||
public List<Button> player1PropertiesButtons; //This is a list for all the buttons for the properties for player 1.
|
//Name Display
|
||||||
public List<Button> player2PropertiesButtons; //This is a list for all the buttons for the properties for player 2.
|
public TMP_Text player1Name; //This is the text used to show player 1's name on screen.
|
||||||
|
public TMP_Text player2Name; //This is the text used to show player 2's name on screen.
|
||||||
|
//Money Display
|
||||||
|
public TMP_Text player1MoneyText; //This is used to show how much money player 1 has.
|
||||||
|
public TMP_Text player2MoneyText; //This is used to show how much money player 2 has.
|
||||||
|
|
||||||
private List<bool> player1SelectedProperties = new List<bool>(); //This is the selected properties for player 1 to trade.
|
//Core Trading
|
||||||
private List<bool> player2SelectedProperties = new List<bool>(); //This is the selected properties for player 2 to trade.
|
//Properties
|
||||||
|
public List<Toggle> player1PropertiesButtons; //This is a list for all the buttons for the properties for player 1.
|
||||||
|
public List<Toggle> player2PropertiesButtons; //This is a list for all the buttons for the properties for player 2.
|
||||||
|
//Money
|
||||||
|
public TMP_InputField player1Money; //This is the amount taken from player 1.
|
||||||
|
private int player1MoneyInt; //This is the int version of player1Money text.
|
||||||
|
public TMP_InputField player2Money; //This is the amount taken from player 2.
|
||||||
|
private int player2MoneyInt; //This is the int version of player1Money text.
|
||||||
|
//Complete trade
|
||||||
|
public Button tradeButton; //This button is meant to finish the trade.
|
||||||
|
|
||||||
|
//Confirmation Box
|
||||||
|
public GameObject ConfirmationBox; //This is the confirmation box that will pop up.
|
||||||
|
public TMP_Text confirmationText; //This is the text that will ask if the player is sure about the trade.
|
||||||
|
public Button confirmationButtonYes; //This is if player 2 decides yes.
|
||||||
|
public Button confirmationButtonNo; //This is if player 2 doesn't want to trade.
|
||||||
|
|
||||||
|
//Completion
|
||||||
|
public GameObject CompletionBox;
|
||||||
|
|
||||||
//Function when opening the trade UI
|
//Function when opening the trade UI
|
||||||
public void OpenTrade()
|
public void OpenTrade()
|
||||||
{
|
{
|
||||||
PlayerSelector.SetActive(true); //This opens the player selector.
|
TradeMenu.SetActive(false); //This hides the trade menu (if it was open from before)
|
||||||
|
ConfirmationBox.SetActive(false); //This hides the confirmation box (if it was on from before)
|
||||||
|
CompletionBox.SetActive(false); //This hides the completion box (if it was on from before)
|
||||||
|
tradeButton.interactable = true; //This turns on the trade button (if it was disabled from before)
|
||||||
|
|
||||||
player1 = main.board.currentPlayer; //This gets the current player who wants to trade.
|
player1 = main.board.currentPlayer; //This gets the current player who wants to trade.
|
||||||
|
|
||||||
//Editing the dropdown.
|
//Editing the dropdown.
|
||||||
@ -48,6 +76,8 @@ public class Trade : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
playerSelectDropdown.AddOptions(playerNames); //Adds all of the player names to the dropdown list.
|
playerSelectDropdown.AddOptions(playerNames); //Adds all of the player names to the dropdown list.
|
||||||
|
|
||||||
|
PlayerSelector.SetActive(true); //This opens the player selector.
|
||||||
}
|
}
|
||||||
|
|
||||||
//This function goes to the next screen after the player selection screen.
|
//This function goes to the next screen after the player selection screen.
|
||||||
@ -59,7 +89,26 @@ public class Trade : MonoBehaviour
|
|||||||
player2 += 1; //This increments the ID of player2.
|
player2 += 1; //This increments the ID of player2.
|
||||||
}
|
}
|
||||||
|
|
||||||
//Getting the information about both players.
|
//Player Information (classes)
|
||||||
|
player1Information = main.board.players[player1]; //This is the class for player 1.
|
||||||
|
player2Information = main.board.players[player2]; //This is the class for player 2.
|
||||||
|
|
||||||
|
//Player names
|
||||||
|
player1Name.text = player1Information.name; //This sets player 1's name on screen.
|
||||||
|
player2Name.text = player2Information.name; //This sets player 2's name on screen.
|
||||||
|
|
||||||
|
//Player money
|
||||||
|
player1MoneyText.text = $"Total Money: {player1Information.money}"; //This sets player 1's balance on screen.
|
||||||
|
player2MoneyText.text = $"Total Money: {player2Information.money}"; //This sets player 2's balance on screen.
|
||||||
|
player1Money.text = ""; //This empties what was written into the input box.
|
||||||
|
player2Money.text = ""; //This empties what was written into the input box.
|
||||||
|
|
||||||
|
//Restricting the maximum value that can be input
|
||||||
|
//This is done by converting the int to a string, so the amount of characters can be counted.
|
||||||
|
player1Money.characterLimit = player1Information.money.ToString().Length; //This sets a soft limit on how much player 1 can spend.
|
||||||
|
player2Money.characterLimit = player2Information.money.ToString().Length; //This sets a soft limit on how much player 2 can spend.
|
||||||
|
|
||||||
|
//Getting the property information about both players.
|
||||||
player1Properties = main.board.players[player1].ownedProperties; //This gets all the properties owned by player 1.
|
player1Properties = main.board.players[player1].ownedProperties; //This gets all the properties owned by player 1.
|
||||||
player2Properties = main.board.players[player2].ownedProperties; //This gets all the properties owned by player 2.
|
player2Properties = main.board.players[player2].ownedProperties; //This gets all the properties owned by player 2.
|
||||||
|
|
||||||
@ -67,6 +116,7 @@ public class Trade : MonoBehaviour
|
|||||||
for (int i = 0; i < player1PropertiesButtons.Count; i++) //This goes through all of the buttons for Player 1.
|
for (int i = 0; i < player1PropertiesButtons.Count; i++) //This goes through all of the buttons for Player 1.
|
||||||
{
|
{
|
||||||
player1PropertiesButtons[i].interactable = false; //This makes all of the buttons inactive.
|
player1PropertiesButtons[i].interactable = false; //This makes all of the buttons inactive.
|
||||||
|
player1PropertiesButtons[i].isOn = false; //This removes all of the ticks on the buttons.
|
||||||
|
|
||||||
foreach (Property property in player1Properties) //This goes through all of player 1's owned properties.
|
foreach (Property property in player1Properties) //This goes through all of player 1's owned properties.
|
||||||
{
|
{
|
||||||
@ -82,6 +132,7 @@ public class Trade : MonoBehaviour
|
|||||||
for (int i = 0; i < player2PropertiesButtons.Count; i++) //This goes through all of the buttons for Player 2.
|
for (int i = 0; i < player2PropertiesButtons.Count; i++) //This goes through all of the buttons for Player 2.
|
||||||
{
|
{
|
||||||
player2PropertiesButtons[i].interactable = false; //This makes all of the buttons inactive.
|
player2PropertiesButtons[i].interactable = false; //This makes all of the buttons inactive.
|
||||||
|
player2PropertiesButtons[i].isOn = false; //This removes all of the ticks on the buttons.
|
||||||
|
|
||||||
foreach (Property property in player2Properties) //This goes through all of player 2's owned properties.
|
foreach (Property property in player2Properties) //This goes through all of player 2's owned properties.
|
||||||
{
|
{
|
||||||
@ -98,39 +149,116 @@ public class Trade : MonoBehaviour
|
|||||||
TradeMenu.SetActive(true); //This opens the trade menu.
|
TradeMenu.SetActive(true); //This opens the trade menu.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SelectProperty(int button, int player)
|
//Confirmation Boxes
|
||||||
|
private void StartConfirm() //This is when player 1 presses trade.
|
||||||
{
|
{
|
||||||
//This switch statement decides between which player the button has been assigned.
|
|
||||||
switch (player)
|
//Converting player1Money.text to an int.
|
||||||
|
if (player1Money.text != "")
|
||||||
{
|
{
|
||||||
case 1: //Player 1
|
player1MoneyInt = Convert.ToInt32(player1Money.text);
|
||||||
player1SelectedProperties[button] = !player1SelectedProperties[button]; //Switch between selection.
|
|
||||||
break;
|
|
||||||
case 2: //Player 2
|
|
||||||
player2SelectedProperties[button] = !player2SelectedProperties[button]; //Switch between selection.
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player1MoneyInt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Converting player2Money.text to an int.
|
||||||
|
if (player2Money.text != "")
|
||||||
|
{
|
||||||
|
player2MoneyInt = Convert.ToInt32(player2Money.text);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player2MoneyInt = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player1MoneyInt > player1Information.money || player2MoneyInt > player2Information.money) //This checks if the player has put too much money to trade.
|
||||||
|
{
|
||||||
|
return; //If they have, then don't let the player continue.
|
||||||
|
}
|
||||||
|
|
||||||
|
tradeButton.interactable = false; //This stops player 1 from being able to press trade multiple times.
|
||||||
|
confirmationButtonYes.interactable = true; //This allows the yes button to be pressed (if it was disabled from before)
|
||||||
|
confirmationText.text = $"{main.board.players[player2].name}\nAre you sure you want to complete the trade?";
|
||||||
|
ConfirmationBox.SetActive(true); //This shows the confirmation box.
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CancelConfirm() //This is if player 2 decides they don't want to trade.
|
||||||
|
{
|
||||||
|
ConfirmationBox.SetActive(false); //This hides the confirmation box.
|
||||||
|
tradeButton.interactable = true; //This enables player 2 to trade again.
|
||||||
|
}
|
||||||
|
|
||||||
|
//Finish the trades between players.
|
||||||
|
private void CompleteTrade()
|
||||||
|
{
|
||||||
|
confirmationButtonYes.interactable = false; //This disables the yes button to be pressed again, to prevent exploits.
|
||||||
|
|
||||||
|
//Money Trades
|
||||||
|
if (player1MoneyInt > 0) //This checks if player 1 is trading money. If so then...
|
||||||
|
{
|
||||||
|
//Take money away from player 1.
|
||||||
|
main.board.players[player1].Pay(player1MoneyInt);
|
||||||
|
//Give the money to player 2.
|
||||||
|
main.board.players[player2].Pay(-player1MoneyInt);
|
||||||
|
main.board.players[player1].UpdateMoneyText(); //This updates the amount of money player 1 has on screen.
|
||||||
|
}
|
||||||
|
if (player2MoneyInt > 0) //This checks if player 2 is trading money. If so then...
|
||||||
|
{
|
||||||
|
//Take money away from player 2.
|
||||||
|
main.board.players[player2].Pay(player2MoneyInt);
|
||||||
|
//Give the money to player 1.
|
||||||
|
main.board.players[player1].Pay(-player2MoneyInt);
|
||||||
|
main.board.players[player1].UpdateMoneyText(); //This updates the amount of money player 1 has on screen.
|
||||||
|
}
|
||||||
|
|
||||||
|
//Property Trades
|
||||||
|
//for player 1 to give to player 2
|
||||||
|
foreach (Toggle propertyButtons in player1PropertiesButtons) //This goes through all of the player buttons.
|
||||||
|
{
|
||||||
|
if (!propertyButtons.isOn) continue; //If the property is not selected, then skip searching through the owned property list.
|
||||||
|
foreach (Property property in player1Information.ownedProperties)
|
||||||
|
{
|
||||||
|
if (property.property_name == propertyButtons.name) //If the selected properties are matching then...
|
||||||
|
{
|
||||||
|
main.board.players[player1].RemoveProperty(property); //Takes away the property from Player 1.
|
||||||
|
main.board.players[player2].AddProperty(property); //Gives the property to Player 2.
|
||||||
|
break; //Stops the property for loop.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//for player 2 to give to player 1
|
||||||
|
foreach (Toggle propertyButtons in player2PropertiesButtons) //This goes through all of the player buttons.
|
||||||
|
{
|
||||||
|
if (!propertyButtons.isOn) continue; //If the property is not selected, then skip searching through the owned property list.
|
||||||
|
foreach (Property property in player2Information.ownedProperties)
|
||||||
|
{
|
||||||
|
if (property.property_name == propertyButtons.name) //If the selected properties are matching then...
|
||||||
|
{
|
||||||
|
main.board.players[player2].RemoveProperty(property); //Takes away the property from Player 2.
|
||||||
|
main.board.players[player1].AddProperty(property); //Gives the property to Player 1.
|
||||||
|
break; //Stops the property for loop.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Dealing with screens.
|
||||||
|
TradeMenu.SetActive(false);
|
||||||
|
ConfirmationBox.SetActive(false);
|
||||||
|
CompletionBox.SetActive(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Awake()
|
void Awake()
|
||||||
{
|
{
|
||||||
main = FindObjectOfType<Main>(); //This gets the data from the main file.
|
main = FindObjectOfType<Main>(); //This gets the data from the main file.
|
||||||
for (int i = 0; i < 28; i++)
|
|
||||||
{
|
|
||||||
player1SelectedProperties.Add(false);
|
|
||||||
player2SelectedProperties.Add(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
playerSelectionNext.onClick.AddListener(PlayerSelectNext); //This lets the next button work on the player selector.
|
playerSelectionNext.onClick.AddListener(PlayerSelectNext); //This lets the next button work on the player selector.
|
||||||
|
tradeButton.onClick.AddListener(StartConfirm); //This lets the player 1 ask player 2 if he wants to complete the trade.
|
||||||
for (int i = 0; i < player1PropertiesButtons.Count; i++) //This goes through all of the buttons in the main menu.
|
confirmationButtonNo.onClick.AddListener(CancelConfirm); //This button cancels the trade from player 2.
|
||||||
{
|
confirmationButtonYes.onClick.AddListener(CompleteTrade); //This button accepts the trade from player 2.
|
||||||
int i1 = i;
|
|
||||||
player1PropertiesButtons[i].onClick.AddListener(() => SelectProperty(i1, 1)); //This adds a specific menu for each of the buttons.
|
|
||||||
player2PropertiesButtons[i].onClick.AddListener(() => SelectProperty(i1, 2)); //This adds a specific menu for each of the buttons.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user