From 72a3d7092d552840f58c957de4411451b9f4811d Mon Sep 17 00:00:00 2001 From: ferindyaa <13521161@std.stei.itb.ac.id> Date: Fri, 10 May 2024 02:32:38 +0700 Subject: [PATCH] add: shopkeeper manager --- Assets/Scripts/Global/CurrentStateData.cs | 29 ++++++ Assets/Scripts/Managers/PauseManager.cs | 97 +++++++++---------- Assets/Scripts/Managers/ShopKeeperManager.cs | 74 ++++++++++++++ .../Managers/ShopKeeperManager.cs.meta | 11 +++ Assets/Scripts/Utils/GameData.cs | 1 + 5 files changed, 160 insertions(+), 52 deletions(-) create mode 100644 Assets/Scripts/Managers/ShopKeeperManager.cs create mode 100644 Assets/Scripts/Managers/ShopKeeperManager.cs.meta diff --git a/Assets/Scripts/Global/CurrentStateData.cs b/Assets/Scripts/Global/CurrentStateData.cs index 63c906e..e21aa4e 100644 --- a/Assets/Scripts/Global/CurrentStateData.cs +++ b/Assets/Scripts/Global/CurrentStateData.cs @@ -146,6 +146,30 @@ public class CurrentStateData return true; } + public static int GetCurrentCoin() + { + return _currentGameData.coin; + } + + public static void SetCurrentCoin(int coin) + { + _currentGameData.coin = coin; + } + + public static void AddCoin(int coin) + { + _currentGameData.coin += coin; + } + + public static bool SubtractCoin(int coin) + { + if (_currentGameData.coin < coin) + return false; + + _currentGameData.coin -= coin; + return true; + } + public static float GetCurrentPlayTime() { return _currentGameData.playTime; @@ -226,6 +250,11 @@ public class CurrentStateData _currentGameData.currentWeapon = weapon; } + public static void AddWeapon(int weapon) + { + _currentGameData.weapons[weapon] = true; + } + public static float GetMultiplier() { return _currentGameData.dmgMultiplier; diff --git a/Assets/Scripts/Managers/PauseManager.cs b/Assets/Scripts/Managers/PauseManager.cs index efd5ff0..d3a966b 100644 --- a/Assets/Scripts/Managers/PauseManager.cs +++ b/Assets/Scripts/Managers/PauseManager.cs @@ -1,59 +1,52 @@ using UnityEngine; -using System.Collections; -using UnityEngine.UI; using UnityEngine.Audio; -#if UNITY_EDITOR -using UnityEditor; -#endif -public class PauseManager : MonoBehaviour { - public AudioMixerSnapshot paused; - public AudioMixerSnapshot unpaused; - public GameObject pauseCanvas; - private bool isOpen = false; +public class PauseManager : MonoBehaviour +{ + private static bool _isPaused; - void Start() - { - pauseCanvas.SetActive(isOpen); - } + public AudioMixerSnapshot paused; + public AudioMixerSnapshot unpaused; - void Update() - { - if (Input.GetKeyDown(KeyCode.Escape)) - { - Pause(); - } - } - - public void Pause() - { - pauseCanvas.SetActive(!isOpen); + private void Start() + { + Time.timeScale = 1; + _isPaused = false; + } + + private void Lowpass() + { + if (Time.timeScale == 0) + { + if (paused != null) + { + paused.TransitionTo(.01f); + } + } + else + { + if (unpaused != null) + { + unpaused?.TransitionTo(.01f); + } + } + } + + public void Pause() + { Time.timeScale = Time.timeScale == 0 ? 1 : 0; - Lowpass (); - - } - - void Lowpass() - { - if (Time.timeScale == 0) - { - isOpen = true; - paused.TransitionTo(.01f); - } - - else - { - isOpen = false; - unpaused.TransitionTo(.01f); - } - } - - public void Quit() - { - #if UNITY_EDITOR - EditorApplication.isPlaying = false; - #else - Application.Quit(); - #endif - } + _isPaused = Time.timeScale == 0; + Lowpass(); + } + + public static void StaticPauseOrUnPause() + { + Time.timeScale = Time.timeScale == 0 ? 1 : 0; + _isPaused = Time.timeScale == 0; + } + + public static bool CheckIfPaused() + { + return _isPaused; + } } diff --git a/Assets/Scripts/Managers/ShopKeeperManager.cs b/Assets/Scripts/Managers/ShopKeeperManager.cs new file mode 100644 index 0000000..0fd4d77 --- /dev/null +++ b/Assets/Scripts/Managers/ShopKeeperManager.cs @@ -0,0 +1,74 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ShopKeeperManager : MonoBehaviour +{ + private static readonly int BuyClick = Animator.StringToHash("BuyClick"); + private static readonly int BuyErrorClick = Animator.StringToHash("BuyErrorClick"); + + public GameObject shopKeeper; + private Animator _anim; + private ShopKeeperEffect _shopKeeperEffect; + public GameObject shopKeeperCanvas; + private float _startTime; + public float timeLimit; + public float additionalTime = 5f; + public GameObject enemyManager; + + void Start() + { + _anim = GetComponent<Animator>(); + _shopKeeperEffect = shopKeeper.GetComponent<ShopKeeperEffect>(); + shopKeeperCanvas.SetActive(false); + _startTime = Time.time; + PauseManager.StaticPauseOrUnPause(); + UpdateShopKeeperState(CurrentStateData.GetCurrentScene() == "level_01"); + } + + void Update() + { + if (!shopKeeper.activeSelf) return; + + float elapsed = Time.time - _startTime; + if (elapsed > timeLimit + additionalTime) + { + UpdateShopKeeperState(false); + return; + } + + if (elapsed > timeLimit) + { + SetShopKeeperFlight(true, 15, false); + return; + } + + _anim.SetBool("ShopKeeperFlying", _shopKeeperEffect.isShopKeeperFlying); + + if (Input.GetKey(KeyCode.B) && !PauseManager.CheckIfPaused()) + { + if (!_shopKeeperEffect.isShopKeeperFlying) + { + _anim.SetTrigger(BuyErrorClick); + } + else + { + PauseManager.StaticPauseOrUnPause(); + shopKeeperCanvas.SetActive(true); + } + } + } + + private void SetShopKeeperFlight(bool flying, float height, bool alive) + { + _shopKeeperEffect.isShopKeeperFlying = flying; + _shopKeeperEffect.floatHeight = height; + _anim.SetBool("ShopKeeperAlive", alive); + } + + private void UpdateShopKeeperState(bool isActive) + { + shopKeeper.SetActive(isActive); + enemyManager.SetActive(!isActive); + } +} diff --git a/Assets/Scripts/Managers/ShopKeeperManager.cs.meta b/Assets/Scripts/Managers/ShopKeeperManager.cs.meta new file mode 100644 index 0000000..58eff35 --- /dev/null +++ b/Assets/Scripts/Managers/ShopKeeperManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ae61ffaabdfc3c543bd1b111c78cd222 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Utils/GameData.cs b/Assets/Scripts/Utils/GameData.cs index 03bad9b..c767a6a 100644 --- a/Assets/Scripts/Utils/GameData.cs +++ b/Assets/Scripts/Utils/GameData.cs @@ -4,6 +4,7 @@ public struct GameData { public string playerName; public string difficulty; + public int coin; public int star; public float playTime; public string scene; -- GitLab