added buy button

This commit is contained in:
Anthony Berg
2020-08-16 00:03:48 +01:00
parent 39c86bf8d2
commit 12cbc174bc
56 changed files with 4834 additions and 1439 deletions

View File

@@ -1,43 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Waypoints : MonoBehaviour
{
public int roll;
int position = 0;
public GameObject[] waypoints;
float rotSpeed;
public float speed;
double WPradius = 0.001;
private properties properties;
public void UpdateRoll(int passedRoll)
{
roll += passedRoll;
if (roll >= 40)
{
roll -= 40;
}
properties.currentProperty(roll);
}
void Awake()
{
properties = GameObject.FindObjectOfType<properties>();
}
void Update()
{
if ((Vector3.Distance(waypoints[position].transform.position, transform.position) < WPradius) && position != roll)
{
//Debug.Log(waypoints[current]);
position++;
if (position >= waypoints.Length)
{
position = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[position].transform.position, Time.deltaTime * speed);
}
}

View File

@@ -1,40 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class board : MonoBehaviour
{
public class Board //Creating the class for the board mechanics.
{
public int houses; //Initialising houses
public int hotels; //Initialising hotels
// List<properties.Property> cards = new List<properties.Property>;
public Board()
{
houses = 32; //Defining the amount of houses - They have a finite amount
hotels = 12; //Defining the amount of hotels - They have a finite amount
}
}
// Start is called before the first frame update
void Start()
{
//Property cards = new Property("cunt cunt cunt", "test", 200, 400);
//Debug.Log(cards.ToString());
}
// Update is called once per frame
void Update()
{
}
}

8
Assets/Scripts/menu.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 06ecabaa422195443b2951d3144b714b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,33 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelLoader : MonoBehaviour
{
public GameObject loadingScreen; //Gets the loading screen
public Slider slider; //Gets the slider
public void LoadLevel (string sceneName)
{
StartCoroutine(LoadAsynchronously(sceneName)); //Starts the LoadAsynchronously function
}
IEnumerator LoadAsynchronously (string sceneName)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName); //Loads the monopoly board.
loadingScreen.SetActive(true); //Shows the loading screen.
while (!operation.isDone) //Runs the while loop whilst the level is loading.
{
float progress = Mathf.Clamp01(operation.progress / .9f); //Makes the unity loading value a nicer value as the original is 0-0.9
slider.value = progress; //Sets the loading bar progress
yield return null;
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 74cd34bd689e47d48862579cdb566e89
guid: 89cbcb58c8de8f14baaea829e70e2cb7
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,61 @@
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
using UnityEngine.UI;
public class MainMenu : MonoBehaviour
{
public Button PlayButton; //Imports the play button
private string existingProperties; //The variable for the properties
void Start() {
if (!PropertiesHandler.checkExists()) //Checks if the properties file doesn't exist.
{
StartCoroutine(GetProperties()); //Downloads the properties json.
}
else
{
enabled = false; //Stops the update function.
PlayButton.interactable = true; //Enables the play button
}
}
void Update()
{
if (existingProperties != null) //Checks if the data has been downloaded
{
PropertiesHandler.SaveProperties(existingProperties); //Saves the downloaded data
PlayButton.interactable = true; //Enables the play button
enabled = false; //Stops the update loop
}
}
public void QuitGame()
{
Debug.Log("QUIT!");
Application.Quit();
}
IEnumerator GetProperties()
{
UnityWebRequest www = UnityWebRequest.Get(Domain.subDomain("includes/get-properties.inc.php"));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
string json = www.downloadHandler.text;
existingProperties = json;
//existingProperties = JsonConvert.DeserializeObject<List<Property>>(json);
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d799e8e3a27dfe948be91afe90af4cad
guid: c9ea757deac30a2408c803a5493f2481
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class PropertiesHandler
{
public static void SaveProperties(string data)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/properties.smyal";
FileStream stream = new FileStream(path, FileMode.Create);
formatter.Serialize(stream, data);
stream.Close();
}
public static string LoadProperties()
{
string path = Application.persistentDataPath + "/properties.smyal";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
string data = formatter.Deserialize(stream) as string;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
public static bool checkExists()
{
string path = Application.persistentDataPath + "/properties.smyal";
return File.Exists(path);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1d7c35c48be71348b27864809b64564
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 13c555507e313474c818c020a1e0db4a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonHandler : MonoBehaviour
{
public Button rollDice;
public GameObject buyButton;
public Button buyButtonButton;
public void disableRollDice()
{
rollDice.interactable = false;
}
public void enableRollDice()
{
rollDice.interactable = true;
}
public void disableBuying()
{
buyButton.SetActive(false);
}
public void enableBuying()
{
buyButton.SetActive(true);
}
private Main main;
void Awake()
{
main = FindObjectOfType<Main>();
}
void Start()
{
buyButtonButton.onClick.AddListener(TaskOnClick);
}
void TaskOnClick() {
main.board.BuyProperty();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9e7eff7f50ac448409bf9cf21f66bfcb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,28 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class FileHandler
{
public static string LoadProperties()
{
string path = Application.persistentDataPath + "/properties.smyal";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
string data = formatter.Deserialize(stream) as string;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path);
return null;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f725fed145261e4e8ab9a3c6895d8ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,264 @@
using System;
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine.UI;
public class Property
{
public int property_id; //This is the position/id of the property
public string property_name;//This is the name of the property
public string property_group; //This is the group the property is in
public int property_value; //This is how much it costs to buy the property
public int? property_cost; //This is how much it costs to buy a house/hotel
public int? property_rent; //This is the rent if the player that doesn't land on it has to pay
public int? property_house1; //This is the rent if it has 1 house
public int? property_house2; //This is the rent if it has 2 houses
public int? property_house3; //This is the rent if it has 3 houses
public int? property_house4; //This is the rent if it has 4 houses
public int? property_hotel; //This is the rent if it has a hotel
public int houses = 0; //How many houses it has. (Can have up to 4)
public bool hotel = false; //Whether it has a hotel or not. (Can only have 1)
public bool mortgage = false; //Whether the property has been mortgaged.
public void addHouse() //Used to add a house.
{
if (!mortgage && (houses < 4) && !hotel) //Checks if there is not a mortgage and there are less than 4 houses and that there isn't a hotel.
{
houses += 1; //Adds a house to the property.
}
}
public void removeHouse() //Used to remove a house.
{
if (houses > 0) //Checks if there is at least 1 house on the property.
{
houses -= 1; //Removes the house from the property.
}
}
public void addHotel() //Used to add a hotel.
{
if (houses == 4) //Checks if the user has enough houses.
{
houses = 0; //Removes all the houses.
hotel = true; //Creates the hotel.
}
}
public void removeHotel() //Removes the hotel.
{
if (hotel) //Checks if they have a hotel already.
{
houses = 4; //Gives the user back their 4 houses.
hotel = false; //Removes the hotel.
}
}
public void mortgageProperty() //Mortgages the property.
{
if (!hotel && (houses == 0)) //Checks if there is not a hotel and if there are no houses.
{
mortgage = true; //Mortgages the property.
}
}
public void unmortgageProperty() //Removes the mortgage on the property.
{
if (mortgage) //Checks if the property has been mortgaged.
{
mortgage = false; //Removes the mortgage.
}
}
public bool isBuyable() //This returns a boolean if the property is buyable
{
return property_value != 0; //This checks if the value of the property is set and then returns a boolean
}
}
public class Board //Creating the class for the board mechanics.
{
public int houses; //Initialising houses
public int hotels; //Initialising hotels
public int totalProperties; //Defining how many properties can exist.
private TextHandler textHandler;
private ButtonHandler buttonHandler;
private List<Player> players;
private int currentPlayer = 0;
public List<Property> existingProperties;
public List<Property> avaliableProperties = new List<Property>(); //Has a list of all the available properties.
public Board(List<Player> players, List<Property> properties)
{
this.players = players;
Debug.Log(this.players.Count);
textHandler = GameObject.FindObjectOfType<TextHandler>();
buttonHandler = GameObject.FindObjectOfType<ButtonHandler>();
existingProperties = properties; //This sets all of the properties that exist
houses = 32; //Defining the amount of houses - They have a finite amount
hotels = 12; //Defining the amount of hotels - They have a finite amount
totalProperties = 28;
for (int i=0; i < properties.Count; i++)
{
if (properties[i].isBuyable())
{
avaliableProperties.Add(properties[i]);
}
}
}
public void MovePlayer(int player, int roll)
{
players[player].Move(roll);
textHandler.updateRoll(roll);
int money = players[player].money;
textHandler.updateMoney(money);
string propertyName = existingProperties[players[player].position].property_name;
int propertyValue = existingProperties[players[player].position].property_value;
if (propertyValue != 0)
{
textHandler.updateProperty(propertyName, propertyValue);
}
else
{
textHandler.updateTile(propertyName); //property_value
}
buttonHandler.disableRollDice();
}
public bool CheckBuyable(int position) //This checks if the property can be bought by the user.
{
Property property = existingProperties[position]; //Gets the property the player is currently at.
for (int i = 0; i < avaliableProperties.Count; i++) //Checks through all of the properties that are buyable using a linear search
{
if (property.property_name == avaliableProperties[i].property_name) //Checks if the name exists in the available properties that can be purchased.
{
buttonHandler.enableBuying(); //If it can, it will return true and break from the function
return true; //Returns true if the player can buy the property.
}
}
buttonHandler.disableBuying(); //If the name is not found, the property has not been found.
buttonHandler.enableRollDice(); //Lets the player continue moving if they can't buy the property.
return false; //Returns false if the player can't buy the property.
}
public void BuyProperty()
{
int position = players[currentPlayer].position; //This is the current position of the player for the property.
Property property = existingProperties[position]; //This gets the property that the player is buying
for (int i = 0; i < avaliableProperties.Count; i++) //Checks through all of the properties that are buyable using a linear search
{
if (property.property_name == avaliableProperties[i].property_name) //Checks if the name exists in the available properties that can be purchased.
{
avaliableProperties.RemoveAt(i); //Removes the property from the list.
players[currentPlayer].BuyProperty(property); //This buys the property in the player class
textHandler.updateMoney(players[currentPlayer].money); //This updates the amount of money the player has.
buttonHandler.disableBuying();
buttonHandler.enableRollDice();
return;
}
}
Debug.Log("The property cannot be bought!");
}
}
public class Player
{
private string name;
private int playerNumber;
public int money; //Initializes the variable for money.
public int position = 0; //Positions vary from 0-39 (40 squares on the board) (Go is 0)
public List<Property> ownedProperties = new List<Property>();
public GameObject player;
private Movement movement;
public Player(string playerName, int playerNumber, GameObject player)
{
name = playerName;
this.playerNumber = playerNumber;
money = 1500; //Set the default starting money.
this.player = player;
movement = GameObject.FindObjectOfType<Movement>();
}
public void Move(int roll) //This moves the player a certain length (what they got from rolling the dice).
{
position += roll; //Add the position with what was rolled.
if (position >= 40) //If the player has reached or passed go then...
{
position -= 40; //As the player has gone round the board once, it removes the fact that it has gone around the board once.
money += 200; //Collect money as they pass go.
}
movement.Move(position, playerNumber);
//return position; //Returns where the player needs to move to on the board
}
public void GoToJail() //If the player needs to go to jail.
{
position = 40; //Special position for jail.
}
public void GetOutOfJail(int length) //If the player is going out of jail.
{
position = 10; //Moves the player out of jail.
//Move(length); //Then moves the player.
}
public void BuyProperty(Property property) //This function allows the player to own a property.
{
int price = property.property_value;
if (money - price >= 0)
{
ownedProperties.Add(property); //Adds the property to the list of the player owned properties.
money -= price;
}
else
{
Debug.Log("Error: You do not have enough money to pay for the property!");
}
}
}
public class Main : MonoBehaviour
{
private List<Property> existingProperties;
public GameObject[] waypoints;
public Board board;
//Player variables
public List<Player> players = new List<Player>();
private void Awake()
{
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<List<Property>>(FileHandler.LoadProperties());
board = new Board(players, existingProperties);
}
}

View File

@@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Movement : MonoBehaviour
{
public int roll;
public bool movement = false;
private int position = 0;
public GameObject[] waypoints;
public GameObject[] players;
public int currentPlayer;
float rotSpeed;
public float speed;
double WPradius = 0.001;
private Main main;
public void Move(int _roll, int playerNumber)
{
roll = _roll;
movement = true;
currentPlayer = playerNumber;
}
void Awake()
{
main = FindObjectOfType<Main>();
}
void Update()
{
if (!movement) return;
if ((Vector3.Distance(waypoints[position].transform.position, players[currentPlayer].transform.position) < WPradius) && position != roll)
{
//Debug.Log(waypoints[current]);
position++;
if (position >= waypoints.Length)
{
position = 0;
}
} else if ((Vector3.Distance(waypoints[position].transform.position, players[currentPlayer].transform.position) < WPradius) && position == roll)
{
main.board.CheckBuyable(roll);
movement = false;
}
players[currentPlayer].transform.position = Vector3.MoveTowards(players[currentPlayer].transform.position,
waypoints[position].transform.position, Time.deltaTime * speed);
}
}

View File

@@ -8,14 +8,13 @@ public class RollDice : MonoBehaviour
System.Random random = new System.Random();
private int current;
private Waypoints waypoints;
private Main main;
public Button m_RollDice;
void Awake()
{
waypoints = GameObject.FindObjectOfType<Waypoints>();
main = FindObjectOfType<Main>();
}
// Start is called before the first frame update
@@ -24,15 +23,8 @@ public class RollDice : MonoBehaviour
m_RollDice.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
}
void TaskOnClick() {
void TaskOnClick() {
current = random.Next(1, 7) + random.Next(1, 7);
Debug.Log("Rolled: " + current);
waypoints.UpdateRoll(current);
}
main.board.MovePlayer(0, current);
}
}

View File

@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using TMPro;
using UnityEngine;
public class TextHandler : MonoBehaviour
{
public TextMeshProUGUI propertyInfo;
private string propertyName;
[CanBeNull] private string propertyValue;
public TextMeshProUGUI Roll;
private string roll;
public TextMeshProUGUI Money;
private string money;
public void updateTile (string propertyName)
{
this.propertyName = propertyName;
propertyInfo.text = $"Current tile: {this.propertyName}";
}
public void updateProperty (string propertyName, int? propertyValue)
{
this.propertyName = propertyName;
this.propertyValue = propertyValue.ToString();
propertyInfo.text = $"Current property: {this.propertyName}\nValue: {this.propertyValue}";
}
public void updateRoll(int roll)
{
this.roll = roll.ToString();
Roll.text = $"Rolled: {this.roll}";
}
public void updateMoney(int money)
{
this.money = money.ToString();
Money.text = $"Money: {this.money}";
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ac9f796ef9df9f4fbdedbe3e1f47473
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,57 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
public class Player
{
public int money; //Initializes the variable for money.
public int position = 0; //Positions vary from 0-39 (40 squares on the board) (Go is 0)
public Player()
{
money = 1500;
}
public void Move(int length) //This moves the player a certain length (what they got from rolling the dice).
{
if (position + length < 40) //Checks if the player will not pass go.
{
position += length; //Adds the length that the player needs to move.
}
else //If they pass go.
{
position = length - (39 - position); //Makes it so that the position is
money += 200; //Collect money as they pass go.
}
}
public void goToJail() //If the player needs to go to jail.
{
position = 40; //Special position for jail.
}
public void getOutOfJail(int length) //If the player is going out of jail.
{
position = 10; //Moves the player out of jail.
Move(length); //Then moves the player.
}
}
// Start is called before the first frame update
void Start()
{
Debug.Log(Domain.subDomain("Yes"));
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -1,150 +0,0 @@
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
public class Property
{
/*
public string title; //The name of the property.
public string group; //The colour, station or utility.
public int value; //How much it costs to buy.
public int houseCost; //How much it costs to buy a house on that property.
*/
public int property_id;
public string property_name;
public string property_group;
public int? property_value;
public int? property_cost;
public int? property_rent;
public int? property_house1;
public int? property_house2;
public int? property_house3;
public int? property_house4;
public int? property_hotel;
public int houses = 0; //How many houses it has. (Can have up to 4)
public bool hotel = false; //Whether it has a hotel or not. (Can only have 1)
public bool mortgage = false; //Whether the property has been mortgaged.
/*
public Property(string name, string importGroup, int importValue, int house) //For houses.
{
//Initialising all the variables.
title = name;
group = importGroup;
value = importValue;
houseCost = house;
}
public Property(string name, string importGroup, int importValue) //For stations or utility.
{
//Initialising all the variables.
title = name;
group = importGroup;
value = importValue;
}
*/
public void addHouse() //Used to add a house.
{
if (!mortgage && (houses < 4) && !hotel) //Checks if there is not a mortgage and there are less than 4 houses and that there isn't a hotel.
{
houses += 1; //Adds a house to the property.
}
}
public void removeHouse() //Used to remove a house.
{
if (houses > 0) //Checks if there is at least 1 house on the property.
{
houses -= 1; //Removes the house from the property.
}
}
public void addHotel() //Used to add a hotel.
{
if (houses == 4) //Checks if the user has enough houses.
{
houses = 0; //Removes all the houses.
hotel = true; //Creates the hotel.
}
}
public void removeHotel() //Removes the hotel.
{
if (hotel) //Checks if they have a hotel already.
{
houses = 4; //Gives the user back their 4 houses.
hotel = false; //Removes the hotel.
}
}
public void mortgageProperty() //Mortgages the property.
{
if (!hotel && (houses == 0)) //Checks if there is not a hotel and if there are no houses.
{
mortgage = true; //Mortgages the property.
}
}
public void unmortgageProperty() //Removes the mortgage on the property.
{
if (mortgage) //Checks if the property has been mortgaged.
{
mortgage = false; //Removes the mortgage.
}
}
public string printTitle() {
return property_name;
}
}
public class properties : MonoBehaviour
{
public List<Property> data;
int test;
// Start is called before the first frame update
void Start()
{
StartCoroutine(GetProperties());
}
void Update()
{
if (data != null && test == 0)
{
Debug.Log(data[0].printTitle());
test++;
}
}
IEnumerator GetProperties()
{
UnityWebRequest www = UnityWebRequest.Get(Domain.subDomain("includes/get-properties.inc.php"));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
string json = www.downloadHandler.text;
data = JsonConvert.DeserializeObject<List<Property>>(json);
}
}
public void currentProperty(int propertyNum)
{
Debug.Log(data[propertyNum].printTitle());
}
}