From ceacacd3eea6dd71eb9ed9d4b96d57d9876ed987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Szczerrzuja=20Rud=C5=BA?= <57622414+szczerrzuja@users.noreply.github.com> Date: Wed, 22 Nov 2023 20:24:57 +0100 Subject: [PATCH] Economy (#36) * Resources #1 * Resource production --- .vscode/settings.json | 3 + ProjectTowerDefence/.vscode/settings.json | 3 + .../Assets/Prefabs/InGameUI.prefab | 4 + .../ResourceController.prefab | 62 +++++++++ .../ResourceController.prefab.meta | 7 ++ .../Assets/Scenes/Level_3.unity | 118 ++++++++++++------ .../Scripts/Buildings/ResourcesController.cs | 63 +++++++--- .../Scripts/Buildings/ResourcesProduction.cs | 5 +- .../Assets/Scripts/UiController.cs | 40 ++++++ 9 files changed, 246 insertions(+), 59 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 ProjectTowerDefence/.vscode/settings.json create mode 100644 ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab create mode 100644 ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab.meta diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..013007b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.preferCSharpExtension": true +} \ No newline at end of file diff --git a/ProjectTowerDefence/.vscode/settings.json b/ProjectTowerDefence/.vscode/settings.json new file mode 100644 index 0000000..91ef337 --- /dev/null +++ b/ProjectTowerDefence/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "ProjectTowerDefence.sln" +} \ No newline at end of file diff --git a/ProjectTowerDefence/Assets/Prefabs/InGameUI.prefab b/ProjectTowerDefence/Assets/Prefabs/InGameUI.prefab index 735f1af..dbeca73 100644 --- a/ProjectTowerDefence/Assets/Prefabs/InGameUI.prefab +++ b/ProjectTowerDefence/Assets/Prefabs/InGameUI.prefab @@ -3913,6 +3913,10 @@ MonoBehaviour: m_EditorClassIdentifier: upgradeButton: {fileID: 6807917380029169322} destroyButton: {fileID: 6807917381556369733} + Gold: {fileID: 6807917380352134223} + Wood: {fileID: 6807917380677331075} + Supply: {fileID: 6807917381460935671} + Mana: {fileID: 6807917381179926136} --- !u!1 &6807917381641104908 GameObject: m_ObjectHideFlags: 0 diff --git a/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab b/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab new file mode 100644 index 0000000..f2a8b57 --- /dev/null +++ b/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab @@ -0,0 +1,62 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &5158832432637384124 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 5158832432637384122} + - component: {fileID: 5158832432637384123} + m_Layer: 0 + m_Name: ResourceController + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &5158832432637384122 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5158832432637384124} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -55.978725, y: 45.228745, z: -7.8830934} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &5158832432637384123 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5158832432637384124} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 857dc9da19c3cfa4ca36b1851817dffb, type: 3} + m_Name: + m_EditorClassIdentifier: + resources: + - name: Gold + ResourceID: 0 + amount: 20 + MaxAmmount: 0 + - name: Wood + ResourceID: 1 + amount: 10 + MaxAmmount: 0 + - name: Supply + ResourceID: 2 + amount: 0 + MaxAmmount: 100 + - name: Mana + ResourceID: 3 + amount: 10 + MaxAmmount: 300 diff --git a/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab.meta b/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab.meta new file mode 100644 index 0000000..2830c11 --- /dev/null +++ b/ProjectTowerDefence/Assets/Prefabs/StartingMapContent/ResourceController.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a0447336b0a5db94b91f74e3465bf34e +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectTowerDefence/Assets/Scenes/Level_3.unity b/ProjectTowerDefence/Assets/Scenes/Level_3.unity index 411bb54..cb8fc8b 100644 --- a/ProjectTowerDefence/Assets/Scenes/Level_3.unity +++ b/ProjectTowerDefence/Assets/Scenes/Level_3.unity @@ -38661,6 +38661,14 @@ PrefabInstance: propertyPath: levelController value: objectReference: {fileID: 522010061} + - target: {fileID: 6807917381595984780, guid: f51816d589bc4bc499ac32056d6e0ea5, type: 3} + propertyPath: ResourceBar + value: + objectReference: {fileID: 1980655408} + - target: {fileID: 6807917381595984780, guid: f51816d589bc4bc499ac32056d6e0ea5, type: 3} + propertyPath: ownerResources + value: + objectReference: {fileID: 710165580} - target: {fileID: 6807917381595984781, guid: f51816d589bc4bc499ac32056d6e0ea5, type: 3} propertyPath: m_Name value: InGameUI @@ -42319,49 +42327,17 @@ Transform: m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 9414ce5147538c14d9577e2862009f48, type: 3} m_PrefabInstance: {fileID: 710124205} m_PrefabAsset: {fileID: 0} ---- !u!1 &710165579 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 710165581} - - component: {fileID: 710165580} - m_Layer: 0 - m_Name: ResourceController - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &710165580 +--- !u!114 &710165580 stripped MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} + m_CorrespondingSourceObject: {fileID: 5158832432637384123, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + m_PrefabInstance: {fileID: 5158832433009381879} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 710165579} + m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 857dc9da19c3cfa4ca36b1851817dffb, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!4 &710165581 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 710165579} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -55.978725, y: 45.228745, z: -7.8830934} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 8 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &710168714 PrefabInstance: m_ObjectHideFlags: 0 @@ -62587,10 +62563,14 @@ MonoBehaviour: m_EditorClassIdentifier: timeToProduction: 2 produced_Resources: - - ResourceID: 0 + - name: + ResourceID: 0 amount: 10 - - ResourceID: 2 + MaxAmmount: 0 + - name: + ResourceID: 2 amount: 2 + MaxAmmount: 0 owner: {fileID: 710165580} --- !u!136 &1043941204 CapsuleCollider: @@ -118145,6 +118125,11 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 9c1e58677d1f47945a8389d0390172b7, type: 3} +--- !u!1 &1980655408 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 6807917381872425784, guid: f51816d589bc4bc499ac32056d6e0ea5, type: 3} + m_PrefabInstance: {fileID: 644626409} + m_PrefabAsset: {fileID: 0} --- !u!1001 &1981525064 PrefabInstance: m_ObjectHideFlags: 0 @@ -129071,6 +129056,63 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 98ba2af8312fabc43ab5a4b0dd2ac153, type: 3} +--- !u!1001 &5158832433009381879 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalPosition.x + value: -55.978725 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalPosition.y + value: 45.228745 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalPosition.z + value: -7.8830934 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_RootOrder + value: 8 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384122, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5158832432637384124, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} + propertyPath: m_Name + value: ResourceController + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: a0447336b0a5db94b91f74e3465bf34e, type: 3} --- !u!1001 &6322297430338665782 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesController.cs b/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesController.cs index 1ae172e..cc6ecc4 100644 --- a/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesController.cs +++ b/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesController.cs @@ -1,6 +1,8 @@ using System.Collections; using System.Collections.Generic; +using TMPro.Examples; using UnityEngine; +using System; /* TODO: update surowców w gui, update surowców w programie, lista budynków do "ściągania" surowców, zarządzanie listą budynków do surowca @@ -8,11 +10,11 @@ public class ResourcesController : MonoBehaviour { - List resources = new List(); + [SerializeField] private List resources = new List(); void Start() { - for(int i=0; i< (int)Resource.ResourcesTypes.EndNumber; i++) + for(int i=0; i< (int)Resource.ResourcesTypes.End; i++) { resources.Add(new Resource((uint)i)); } @@ -24,24 +26,29 @@ public void AddResourcesAmount(List res) { for(int i=0; i GetResourcesRef() { + return ref resources; + } } [System.Serializable] @@ -49,26 +56,44 @@ public void addSingleResource(Resource res) Helper class to contain resource in other class */ public class Resource{ - - /* - Tutaj wrzucasz wszystkie rzeczy które mają być w grze. Ostatni musi być EndNumber - */ - public enum ResourcesTypes { + public enum ResourcesTypes { Gold = 0, Wood, + Supply, Mana, - EndNumber + End } + public string name; + /* + Tutaj wrzucasz wszystkie rzeczy które mają być w grze. Ostatni musi być End + */ public uint ResourceID; public float amount; + //jezeli ta wartość jest równa 0, to maksymalna wartośc jest równa nieskończoność + public int MaxAmmount; + + /* + Konsxtruktor, gdzie wartość startowa będzie równa 0 + */ public Resource(uint resourceID) { ResourceID = resourceID; amount = 0.0f; + MaxAmmount = 0; } + /* + Konsturktor z wartością startową + */ public Resource(uint resourceID, float startAmount) { ResourceID = resourceID; amount = startAmount; + MaxAmmount = 0; + } + public Resource(uint resourceID, float startAmount, int maxAmmount) + { + ResourceID = resourceID; + amount = startAmount; + MaxAmmount = maxAmmount; } } diff --git a/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesProduction.cs b/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesProduction.cs index 58e3b8b..154dba2 100644 --- a/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesProduction.cs +++ b/ProjectTowerDefence/Assets/Scripts/Buildings/ResourcesProduction.cs @@ -8,11 +8,12 @@ public class ResourcesProduction : MonoBehaviour { [SerializeField] float timeToProduction; [SerializeField] List produced_Resources; [SerializeField] ResourcesController owner; + + void Start() { if(owner!=null) { - Debug.Log("Mam wadce"); StartCoroutine(ProduceResource()); } } @@ -24,7 +25,7 @@ IEnumerator ProduceResource(){ yield return new WaitForSecondsRealtime(timeToProduction); if(owner != null) { - Debug.Log("Here i am"); + StartCoroutine(ProduceResource()); owner.AddResourcesAmount(produced_Resources); } diff --git a/ProjectTowerDefence/Assets/Scripts/UiController.cs b/ProjectTowerDefence/Assets/Scripts/UiController.cs index ea46e62..187cae3 100644 --- a/ProjectTowerDefence/Assets/Scripts/UiController.cs +++ b/ProjectTowerDefence/Assets/Scripts/UiController.cs @@ -1,6 +1,8 @@ using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; +using System.Collections; +using System.Collections.Generic; public class UiController : MonoBehaviour { @@ -22,6 +24,16 @@ public class UiController : MonoBehaviour protected Image upgradeButtonImage; protected Image destroyButtonImage; + [SerializeField] protected ResourcesController ownerResources; + [SerializeField] protected Text Gold; + [SerializeField] protected Text Wood; + [SerializeField] protected Text Supply; + [SerializeField] protected Text Mana; + + protected List CurrentResources = new List(); + protected int MaxSupply; + + public void LoadMenu() { Time.timeScale = 1; @@ -66,6 +78,11 @@ private void Start() upgradeButtonImage.color = redColor; destroyButtonImage.color = redColor; + for(int i = 0; i < (int)Resource.ResourcesTypes.End; i++) + { + CurrentResources.Add(0); + } + } private void Update() @@ -74,6 +91,18 @@ private void Update() { }*/ + /* + Jeżeli to czytasz to znaczy że stwierdziłeś że linie kodu poniżej są głupie. Problem jest taki, ze nie są + Wartości będą zmieniane bardzo często więc nie ma sensu wrzucać tego w funkcje i zmieniać co zmianę wartości pojedyńczego pola + Dlatego łatwiej i lepiej jest zrobić to tym sposobem + Jezeli coś zmienisz, to prosze zrób to tak, żeby to działało + Powodzenia + */ + UpdateResources(); + Gold.text = "Gold:" + CurrentResources[(int)Resource.ResourcesTypes.Gold].ToString(); + Wood.text = "Wood:" + CurrentResources[(int)Resource.ResourcesTypes.Wood].ToString(); + Supply.text = "Supply:" + CurrentResources[(int)Resource.ResourcesTypes.Supply].ToString() + "/" + MaxSupply.ToString(); + Mana.text = "Mana:" + CurrentResources[(int)Resource.ResourcesTypes.Mana].ToString(); } public void UpgradeButtonControl() @@ -133,4 +162,15 @@ public void SetTowerId(int towerId) { placingBuildings.buildingId = towerId; } + private void UpdateResources() + { + List resources = ownerResources.GetResourcesRef(); + for(int i = 0; i < (int)Resource.ResourcesTypes.End; i++) + { + CurrentResources[i] = (int)resources[i].amount; + } + MaxSupply= (int)resources[(int)Resource.ResourcesTypes.Supply].MaxAmmount; + + } + }