From 9cb342dd42536f697d1b1b79e5f7c9db3f88aef5 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Tue, 24 Nov 2020 21:11:44 +0000 Subject: [PATCH] Added the ability to add multiple players --- .idea/.idea.Monopoly/.idea/indexLayout.xml | 1 - Assets/Scenes/menu.unity | 55 ++++++++-------------- Assets/Scenes/monopoly.unity | 35 +++++++------- Assets/Scripts/GameSettings.cs | 7 +-- Assets/Scripts/menu/LevelLoader.cs | 17 +++++-- Assets/Scripts/monopoly/Main.cs | 25 ++++++++-- Assets/Scripts/monopoly/Movement.cs | 6 ++- 7 files changed, 78 insertions(+), 68 deletions(-) diff --git a/.idea/.idea.Monopoly/.idea/indexLayout.xml b/.idea/.idea.Monopoly/.idea/indexLayout.xml index c25d7b8..f42b9a7 100644 --- a/.idea/.idea.Monopoly/.idea/indexLayout.xml +++ b/.idea/.idea.Monopoly/.idea/indexLayout.xml @@ -3,7 +3,6 @@ - Assets Library/PackageCache/com.unity.collab-proxy@1.3.9 Library/PackageCache/com.unity.ext.nunit@1.0.0 Library/PackageCache/com.unity.ide.rider@1.2.1 diff --git a/Assets/Scenes/menu.unity b/Assets/Scenes/menu.unity index 8364460..276fadb 100644 --- a/Assets/Scenes/menu.unity +++ b/Assets/Scenes/menu.unity @@ -135,7 +135,7 @@ GameObject: - component: {fileID: 239567296} - component: {fileID: 239567295} m_Layer: 5 - m_Name: Text (TMP) + m_Name: Players Text m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -974,7 +974,7 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 1824002928} + - m_Target: {fileID: 0} m_TargetAssemblyTypeName: m_MethodName: LoadLevel m_Mode: 5 @@ -1569,6 +1569,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 820348872} + - component: {fileID: 820348873} m_Layer: 5 m_Name: Play m_TagString: Untagged @@ -1598,6 +1599,22 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &820348873 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 820348871} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 89cbcb58c8de8f14baaea829e70e2cb7, type: 3} + m_Name: + m_EditorClassIdentifier: + playersInput: {fileID: 1814574228} + play: {fileID: 2129375864} + loadingScreen: {fileID: 821978300} + slider: {fileID: 952453483} --- !u!1 &821978300 GameObject: m_ObjectHideFlags: 0 @@ -3010,7 +3027,7 @@ GameObject: - component: {fileID: 1814574229} - component: {fileID: 1814574228} m_Layer: 5 - m_Name: InputField (TMP) + m_Name: Players Input m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -3180,7 +3197,6 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1824002929} - - component: {fileID: 1824002928} m_Layer: 0 m_Name: LoadLevel m_TagString: Untagged @@ -3188,21 +3204,6 @@ GameObject: m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 ---- !u!114 &1824002928 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1824002927} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 89cbcb58c8de8f14baaea829e70e2cb7, type: 3} - m_Name: - m_EditorClassIdentifier: - playersInput: {fileID: 0} - loadingScreen: {fileID: 821978300} - slider: {fileID: 952453483} --- !u!4 &1824002929 Transform: m_ObjectHideFlags: 0 @@ -3806,7 +3807,6 @@ GameObject: - component: {fileID: 2129375866} - component: {fileID: 2129375865} - component: {fileID: 2129375864} - - component: {fileID: 2129375867} m_Layer: 5 m_Name: PlayButton m_TagString: Untagged @@ -3915,21 +3915,6 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2129375862} m_CullTransparentMesh: 0 ---- !u!114 &2129375867 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2129375862} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 89cbcb58c8de8f14baaea829e70e2cb7, type: 3} - m_Name: - m_EditorClassIdentifier: - playersInput: {fileID: 0} - loadingScreen: {fileID: 0} - slider: {fileID: 0} --- !u!1 &2143598660 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/monopoly.unity b/Assets/Scenes/monopoly.unity index 8a2e92b..96ff794 100644 --- a/Assets/Scenes/monopoly.unity +++ b/Assets/Scenes/monopoly.unity @@ -358,11 +358,9 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 4.4997625, y: 10.148768, z: -4.430161} m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: - - {fileID: 1272828669} - - {fileID: 305116344} + m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 7 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &85078179 GameObject: @@ -1041,7 +1039,7 @@ GameObject: - component: {fileID: 305116342} - component: {fileID: 305116341} m_Layer: 0 - m_Name: Player2 + m_Name: PlayerTemplate m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -1117,11 +1115,11 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 305116340} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -0.15476274, y: 0.051231384, z: 0.043160915} + m_LocalPosition: {x: 4.345, y: 10.2, z: -4.387} m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} m_Children: [] - m_Father: {fileID: 61794250} - m_RootOrder: 1 + m_Father: {fileID: 0} + m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &310476693 GameObject: @@ -1222,7 +1220,7 @@ RectTransform: - {fileID: 929752267} - {fileID: 653887086} m_Father: {fileID: 0} - m_RootOrder: 6 + m_RootOrder: 8 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -4160,7 +4158,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 8 + m_RootOrder: 10 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &810784022 GameObject: @@ -5233,7 +5231,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 7 + m_RootOrder: 9 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &983314467 MonoBehaviour: @@ -5247,6 +5245,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ede8e3ed967bcfe42886d342fcf3d755, type: 3} m_Name: m_EditorClassIdentifier: + playerParentGameObject: {fileID: 61794249} + playersGameObjects: [] + playerTemplate: {fileID: 305116340} --- !u!114 &983314468 MonoBehaviour: m_ObjectHideFlags: 0 @@ -5303,9 +5304,7 @@ MonoBehaviour: - {fileID: 399889870} - {fileID: 724284469} - {fileID: 1679278720} - players: - - {fileID: 1272828665} - - {fileID: 305116340} + players: [] currentPlayer: 0 speed: 10 --- !u!1 &985089322 @@ -7109,7 +7108,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!65 &1272828666 BoxCollider: m_ObjectHideFlags: 0 @@ -7180,11 +7179,11 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1272828665} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -0.15476274, y: 0.051231384, z: 0.043160915} + m_LocalPosition: {x: 4.345, y: 10.2, z: -4.387} m_LocalScale: {x: 0.25, y: 0.25, z: 0.25} m_Children: [] - m_Father: {fileID: 61794250} - m_RootOrder: 0 + m_Father: {fileID: 0} + m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1316371467 GameObject: diff --git a/Assets/Scripts/GameSettings.cs b/Assets/Scripts/GameSettings.cs index 1dde0d7..ca6d8af 100644 --- a/Assets/Scripts/GameSettings.cs +++ b/Assets/Scripts/GameSettings.cs @@ -1,7 +1,4 @@ -using System.Collections; -using System.Collections.Generic; - -public static class GameSettings +public static class GameSettings { - public static int players; + public static int players = 2; } diff --git a/Assets/Scripts/menu/LevelLoader.cs b/Assets/Scripts/menu/LevelLoader.cs index aeb6eb1..7307151 100644 --- a/Assets/Scripts/menu/LevelLoader.cs +++ b/Assets/Scripts/menu/LevelLoader.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; @@ -10,13 +11,21 @@ public class LevelLoader : MonoBehaviour { public TMP_InputField playersInput; //This is for how many players the user has selected. + public Button play; //This is for the play button. public GameObject loadingScreen; //Gets the loading screen public Slider slider; //Gets the slider - - public void LoadLevel (string sceneName) + + void Start() + { + play.onClick.AddListener(LoadLevel); + } + + public void LoadLevel() { StartCoroutine(UpdateTable()); //Starts the LoadAsynchronously function - StartCoroutine(LoadAsynchronously(sceneName)); //Starts the LoadAsynchronously function + GameSettings.players = Convert.ToInt32(playersInput.text); //This sets the amount of players that has been set to play in the game. + Debug.Log(GameSettings.players); + StartCoroutine(LoadAsynchronously("monopoly")); //Starts the LoadAsynchronously function } IEnumerator UpdateTable() diff --git a/Assets/Scripts/monopoly/Main.cs b/Assets/Scripts/monopoly/Main.cs index 7a66b9e..86281c1 100644 --- a/Assets/Scripts/monopoly/Main.cs +++ b/Assets/Scripts/monopoly/Main.cs @@ -427,6 +427,7 @@ public class Board //Creating the class for the board mechanics. } players[location.Item1].Pay(Convert.ToInt32(property.property_cost)); //This then makes the player pay for the house that they bought. + textHandler.UpdateMoney(players[location.Item1].money); //Updates the money on the UI. } public void SellHouseOnProperty(string propertyName) //This function links the UI button and this class to sell properties. @@ -449,6 +450,7 @@ public class Board //Creating the class for the board mechanics. } 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. + textHandler.UpdateMoney(players[location.Item1].money); //Updates the money on the UI. } private bool BuyHouse() //This function is used to buy houses locally. @@ -789,6 +791,7 @@ public class Player 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) + textHandler.UpdateMoney(money); //Updates the money on the UI. return true; //Says that mortgaging has been done successfully. } @@ -805,6 +808,7 @@ public class Player 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 + textHandler.UpdateMoney(money); //Updates the money on the UI. return true; //Says that mortgaging has been done successfully. } @@ -856,12 +860,27 @@ public class Main : MonoBehaviour //Player variables public List players = new List(); //Creates a list for all the players playing in the game. + public GameObject playerParentGameObject; //This is where the parent for the player GameObjects goes. + public List playersGameObjects; //This is the list of player GameObjects + public GameObject playerTemplate; //This is the template for each new player created. + 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].name); //This is just checking if the player has been assigned. + + for (int i = 0; i < GameSettings.players; i++) + { + //Duplicates the player template + //Moves the new player into a parent for players + //Names the game object player and a unique number + Instantiate(playerTemplate, playerTemplate.transform.position, Quaternion.identity, playerParentGameObject.transform).name = $"Player{i}"; + playersGameObjects.Add(GameObject.Find($"/Players/Player{i}")); //Adds to a list of GameObjects by searching for the GameObject. + players.Add(new Player($"Player {i}", i, playersGameObjects[i])); //Creates a unique player class for that specific GameObject + } + + Destroy(playerTemplate); //Deletes the player template GameObject. + + Debug.Log(players[1].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. existingCards = JsonConvert.DeserializeObject>(FileHandler.LoadCards()); //This loads via JSON all the cards from a file which was originally downloaded from a server. board = new Board(players, existingProperties, existingCards); //Creates the board class. diff --git a/Assets/Scripts/monopoly/Movement.cs b/Assets/Scripts/monopoly/Movement.cs index dc76df7..ad1f909 100644 --- a/Assets/Scripts/monopoly/Movement.cs +++ b/Assets/Scripts/monopoly/Movement.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System.Collections.Generic; +using UnityEngine; public class Movement : MonoBehaviour { @@ -6,7 +7,7 @@ public class Movement : MonoBehaviour public bool movement = false; private int position = 0; public GameObject[] waypoints; - public GameObject[] players; + public List players; public int currentPlayer; float rotSpeed; public float speed; @@ -36,6 +37,7 @@ public class Movement : MonoBehaviour void Awake() { main = FindObjectOfType
(); + players = main.playersGameObjects; } void Update()