From fe80191716a13bdc2bb84cefdb441a20cd60c341 Mon Sep 17 00:00:00 2001 From: Ken Schaefer Date: Sun, 16 Mar 2025 09:37:53 -0500 Subject: [PATCH] game manager --- Assets/Scenes/Main.unity | 325 ++++++++++++++++++++++++++++- Assets/Scripts/GameManager.cs | 51 +++++ Assets/Scripts/GameManager.cs.meta | 2 + Assets/Scripts/Store.cs | 48 ++--- 4 files changed, 401 insertions(+), 25 deletions(-) create mode 100644 Assets/Scripts/GameManager.cs create mode 100644 Assets/Scripts/GameManager.cs.meta diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 60215f3..6ddf916 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -119,6 +119,140 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &318397170 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 318397171} + - component: {fileID: 318397172} + m_Layer: 5 + m_Name: ProgressSlider + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &318397171 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 318397170} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 1633529898} + - {fileID: 596541424} + m_Father: {fileID: 1308558881} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: -0.1908, y: -6.7747} + m_SizeDelta: {x: 563.871, y: 56.09} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &318397172 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 318397170} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 67db9e8f0e2ae9c40bc1e2b64352a6b4, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_WrapAround: 0 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_SelectedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_SelectedTrigger: Selected + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 0} + m_FillRect: {fileID: 744141870} + m_HandleRect: {fileID: 0} + m_Direction: 0 + m_MinValue: 0 + m_MaxValue: 1 + m_WholeNumbers: 0 + m_Value: 0 + m_OnValueChanged: + m_PersistentCalls: + m_Calls: [] +--- !u!1 &366304913 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 366304915} + - component: {fileID: 366304914} + m_Layer: 0 + m_Name: GameManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &366304914 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 366304913} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a32c22d03f0117a478c3adfd3644f41f, type: 3} + m_Name: + m_EditorClassIdentifier: + BalanceText: {fileID: 856092499} +--- !u!4 &366304915 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 366304913} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 440.69376, y: 788.0375, z: -2.1456857} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &423320530 GameObject: m_ObjectHideFlags: 0 @@ -394,6 +528,42 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &596541423 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 596541424} + m_Layer: 5 + m_Name: Fill Area + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &596541424 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 596541423} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 744141870} + m_Father: {fileID: 318397171} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0.25} + m_AnchorMax: {x: 1, y: 0.75} + m_AnchoredPosition: {x: -5, y: 0} + m_SizeDelta: {x: -20, y: 0} + m_Pivot: {x: 0.5, y: 0.5} --- !u!1 &619394800 GameObject: m_ObjectHideFlags: 0 @@ -581,6 +751,81 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0, y: 0} +--- !u!1 &744141869 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 744141870} + - component: {fileID: 744141872} + - component: {fileID: 744141871} + m_Layer: 5 + m_Name: Fill + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &744141870 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 744141869} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 596541424} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 10, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &744141871 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 744141869} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.14378417, g: 0.5566038, b: 0.123398006, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &744141872 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 744141869} + m_CullTransparentMesh: 1 --- !u!1 &856092497 GameObject: m_ObjectHideFlags: 0 @@ -1154,6 +1399,7 @@ RectTransform: - {fileID: 1143714943} - {fileID: 1130218633} - {fileID: 2115714079} + - {fileID: 318397171} m_Father: {fileID: 663531050} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} @@ -1211,8 +1457,84 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b9f44b3ea485b3c42a008ebee4240951, type: 3} m_Name: m_EditorClassIdentifier: - BalanceText: {fileID: 856092499} + gameManager: {fileID: 366304914} StoreCountText: {fileID: 2115714080} + StoreSlider: {fileID: 318397172} +--- !u!1 &1633529897 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1633529898} + - component: {fileID: 1633529900} + - component: {fileID: 1633529899} + m_Layer: 5 + m_Name: Background + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1633529898 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1633529897} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 318397171} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0.25} + m_AnchorMax: {x: 1, y: 0.75} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1633529899 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1633529897} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10907, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &1633529900 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1633529897} + m_CullTransparentMesh: 1 --- !u!1 &1811417435 GameObject: m_ObjectHideFlags: 0 @@ -1572,3 +1894,4 @@ SceneRoots: - {fileID: 619394802} - {fileID: 663531050} - {fileID: 2125081262} + - {fileID: 366304915} diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs new file mode 100644 index 0000000..be24feb --- /dev/null +++ b/Assets/Scripts/GameManager.cs @@ -0,0 +1,51 @@ +using System; +using TMPro; + +using UnityEngine; + +public class GameManager : MonoBehaviour +{ + #region GameManager + private decimal _currentBalance = 1.00M; + public decimal CurrentBalance { get => _currentBalance; set => _currentBalance = value; } + + [SerializeField] + private TextMeshProUGUI BalanceText; + + + #endregion + + // Start is called once before the first execution of Update after the MonoBehaviour is created + void Start() + { + BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); + } + + // Update is called once per frame + void Update() + { + + } + + public void AddToBalance(decimal amount) + { + CurrentBalance += amount; + BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); + } + + public void SubtractFromBalance(decimal amount) + { + if(CurrentBalance < amount) + { + Debug.Log("Not enough money"); + return; + } + CurrentBalance -= amount; + BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); + } + + public bool CanAfford(decimal amount) + { + return CurrentBalance >= amount; + } +} diff --git a/Assets/Scripts/GameManager.cs.meta b/Assets/Scripts/GameManager.cs.meta new file mode 100644 index 0000000..b3e5040 --- /dev/null +++ b/Assets/Scripts/GameManager.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: a32c22d03f0117a478c3adfd3644f41f \ No newline at end of file diff --git a/Assets/Scripts/Store.cs b/Assets/Scripts/Store.cs index d88cbb6..4a41bb7 100644 --- a/Assets/Scripts/Store.cs +++ b/Assets/Scripts/Store.cs @@ -1,24 +1,21 @@ -using System; -using UnityEngine; using TMPro; +using UnityEngine; +using UnityEngine.UI; public class Store : MonoBehaviour { - #region GameManager - Decimal CurrentBalance = 1.00M; - Decimal BaseStorePrice = 1.00M; - [SerializeField] - private TextMeshProUGUI BalanceText; - #endregion - - - + private GameManager gameManager; [SerializeField] private TextMeshProUGUI StoreCountText; + [SerializeField] + private Button BuyButton; + [SerializeField] + private Slider StoreSlider; private int StoreCount = 0; - private Decimal StoreProfit = 0.50M; + private decimal BaseStorePrice = 1.00M; + private decimal StoreProfit = 0.50M; private float StoreReset = 4.0f; private float StoreTime = 0.0f; @@ -29,7 +26,7 @@ public class Store : MonoBehaviour void Start() { StoreCountText.text = StoreCount.ToString(); - BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); + } // Update is called once per frame @@ -40,31 +37,34 @@ public class Store : MonoBehaviour StoreTime += Time.deltaTime; if (StoreTime >= StoreReset) { + StoreOpen = false; StoreTime = 0.0f; - CurrentBalance += StoreProfit * StoreCount; - BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); + gameManager.AddToBalance(StoreProfit * StoreCount); } + + } + StoreSlider.value = StoreTime / StoreReset; + if(gameManager.CanAfford(BaseStorePrice)) + { + BuyButton.interactable = true; } } public void Buy() { - if(BaseStorePrice > CurrentBalance) + gameManager.SubtractFromBalance(BaseStorePrice); + StoreCount++; + StoreCountText.text = StoreCount.ToString(); + if(!gameManager.CanAfford(BaseStorePrice)) { - Debug.Log("Not enough money"); - return; + BuyButton.interactable = false; } - - CurrentBalance -= BaseStorePrice; - StoreCount++; - StoreCountText.text = StoreCount.ToString(); - BalanceText.text = String.Format("$ {0:C2}", CurrentBalance.ToString()); } public void OpenStore() { StoreOpen = true; - Debug.Log("Store Opened"); + } }