From 369a86c9d8b55c896a304b620415bfb2a9c624e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Mon, 26 Aug 2019 21:36:26 +0300 Subject: [PATCH 1/2] Refactor BombUtil and couple with player --- Assets/Resources/Items/GoldenBombUpgrade.prefab | 2 +- Assets/Scripts/src/Managers/BombsUtilManager.cs | 10 +++++----- Assets/Scripts/src/Managers/GameManager.cs | 9 +-------- Assets/Scripts/src/Player/PlayerController.cs | 12 ++++++------ Assets/Scripts/src/Upgrade/BombFlamesUpgrade.cs | 3 ++- Assets/Scripts/src/Upgrade/BombPlacementUpgrade.cs | 3 ++- Assets/Scripts/src/Upgrade/GoldenBombUpgrade.cs | 3 ++- 7 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Assets/Resources/Items/GoldenBombUpgrade.prefab b/Assets/Resources/Items/GoldenBombUpgrade.prefab index 0ec599f..b643fa0 100644 --- a/Assets/Resources/Items/GoldenBombUpgrade.prefab +++ b/Assets/Resources/Items/GoldenBombUpgrade.prefab @@ -25,7 +25,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1820949686450855695} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 0.5, y: 0.5, z: 0} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 5103883358052461890} diff --git a/Assets/Scripts/src/Managers/BombsUtilManager.cs b/Assets/Scripts/src/Managers/BombsUtilManager.cs index 4f45c86..ed254c9 100644 --- a/Assets/Scripts/src/Managers/BombsUtilManager.cs +++ b/Assets/Scripts/src/Managers/BombsUtilManager.cs @@ -15,7 +15,7 @@ namespace src.Managers public float explosionDuration = 0.55f; - private readonly HashSet _usedPosition = new HashSet(); + private static readonly HashSet UsedPosition = new HashSet(); private const int MaxPower = 7; private const int MaxAllowedBombs = 10; @@ -51,19 +51,19 @@ namespace src.Managers { if (!CanPlaceBomb(position)) return; placedBombs++; - _usedPosition.Add(position); + UsedPosition.Add(position); } public void UnregisterBomb(Vector3 position) { - if (!_usedPosition.Contains(position)) return; + if (!UsedPosition.Contains(position)) return; placedBombs--; - _usedPosition.Remove(position); + UsedPosition.Remove(position); } public bool CanPlaceBomb(Vector3 position) { - return !_usedPosition.Contains(position) && placedBombs < allowedBombs; + return !UsedPosition.Contains(position) && placedBombs < allowedBombs; } } } \ No newline at end of file diff --git a/Assets/Scripts/src/Managers/GameManager.cs b/Assets/Scripts/src/Managers/GameManager.cs index 6243c7c..defa782 100644 --- a/Assets/Scripts/src/Managers/GameManager.cs +++ b/Assets/Scripts/src/Managers/GameManager.cs @@ -16,7 +16,6 @@ namespace src.Managers private GameStateManager _gameStateManager; private LevelManager _levelManager; private UpgradeManager _upgradeManager; - private BombsUtilManager _bombsUtilManager; public void Awake() { @@ -33,7 +32,6 @@ namespace src.Managers DontDestroyOnLoad(gameObject); // Load singletons - _bombsUtilManager = gameObject.AddComponent(); _gameStateManager = gameObject.AddComponent(); _levelManager = gameObject.AddComponent(); _upgradeManager = gameObject.AddComponent(); @@ -63,12 +61,7 @@ namespace src.Managers { return _upgradeManager; } - - public BombsUtilManager GetBombsUtilManager() - { - return _bombsUtilManager; - } - + private IEnumerator PreInitGame() { var preStageUi = Instantiate(PrefabAtlas.PreStageUi); // Will destroy itself. diff --git a/Assets/Scripts/src/Player/PlayerController.cs b/Assets/Scripts/src/Player/PlayerController.cs index e1c5429..943f824 100644 --- a/Assets/Scripts/src/Player/PlayerController.cs +++ b/Assets/Scripts/src/Player/PlayerController.cs @@ -18,10 +18,10 @@ namespace src.Player private Rigidbody2D _rigidbody2d; private Collider2D _collider2D; private Transform _respawnPosition; - private BombsUtilManager _bombsUtil; + private BombsUtilManager _bombsUtilManager; private Animator _animator; private PlayerUpgrade _playerUpgrade; - + /* Variables */ private bool _isDead; @@ -34,12 +34,12 @@ namespace src.Player protected void Awake() { _playerUpgrade = gameObject.AddComponent(); + _bombsUtilManager = gameObject.AddComponent(); } protected void Start() - { + { _gameStateManager = GameStateManager.instance; - _bombsUtil = BombsUtilManager.instance; _rigidbody2d = GetComponent(); _collider2D = GetComponent(); @@ -152,10 +152,10 @@ namespace src.Player var absX = Mathf.RoundToInt(position.x); var absY = Mathf.RoundToInt(position.y); var newPosition = new Vector2(absX, absY); - if (!_bombsUtil.CanPlaceBomb(newPosition)) return; + if (!_bombsUtilManager.CanPlaceBomb(newPosition)) return; Instantiate(PrefabAtlas.Bomb, newPosition, Quaternion.identity); - _bombsUtil.RegisterBomb(newPosition); + _bombsUtilManager.RegisterBomb(newPosition); } public void Respawn() diff --git a/Assets/Scripts/src/Upgrade/BombFlamesUpgrade.cs b/Assets/Scripts/src/Upgrade/BombFlamesUpgrade.cs index b46e361..f1329ad 100644 --- a/Assets/Scripts/src/Upgrade/BombFlamesUpgrade.cs +++ b/Assets/Scripts/src/Upgrade/BombFlamesUpgrade.cs @@ -1,4 +1,5 @@ using src.Base; +using src.Managers; namespace src.Upgrade { @@ -6,7 +7,7 @@ namespace src.Upgrade { public override void PerformUpgrade() { - var bombManager = gameManager.GetBombsUtilManager(); + var bombManager = _playerToUpgrade.GetComponent(); bombManager.IncreasePower(); } } diff --git a/Assets/Scripts/src/Upgrade/BombPlacementUpgrade.cs b/Assets/Scripts/src/Upgrade/BombPlacementUpgrade.cs index 39b16ad..dc48aa8 100644 --- a/Assets/Scripts/src/Upgrade/BombPlacementUpgrade.cs +++ b/Assets/Scripts/src/Upgrade/BombPlacementUpgrade.cs @@ -1,4 +1,5 @@ using src.Base; +using src.Managers; namespace src.Upgrade { @@ -6,7 +7,7 @@ namespace src.Upgrade { public override void PerformUpgrade() { - var bombManager = gameManager.GetBombsUtilManager(); + var bombManager = _playerToUpgrade.GetComponent(); bombManager.IncreaseAllowedBombs(); } } diff --git a/Assets/Scripts/src/Upgrade/GoldenBombUpgrade.cs b/Assets/Scripts/src/Upgrade/GoldenBombUpgrade.cs index acf0bff..104a318 100644 --- a/Assets/Scripts/src/Upgrade/GoldenBombUpgrade.cs +++ b/Assets/Scripts/src/Upgrade/GoldenBombUpgrade.cs @@ -1,4 +1,5 @@ using src.Base; +using src.Managers; namespace src.Upgrade { @@ -7,7 +8,7 @@ namespace src.Upgrade { public override void PerformUpgrade() { - var bombManager = gameManager.GetBombsUtilManager(); + var bombManager = _playerToUpgrade.GetComponent(); bombManager.IncreaseAllowedBombs(); bombManager.IncreasePower(); } From e56982568bc134b420e86eb9ded5ccf6abef053c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Mon, 26 Aug 2019 21:41:58 +0300 Subject: [PATCH 2/2] Improve exitDoor script --- Assets/Scripts/src/Wall/ExitDoor.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Assets/Scripts/src/Wall/ExitDoor.cs b/Assets/Scripts/src/Wall/ExitDoor.cs index b0d0444..db16603 100644 --- a/Assets/Scripts/src/Wall/ExitDoor.cs +++ b/Assets/Scripts/src/Wall/ExitDoor.cs @@ -1,5 +1,6 @@ using System; using src.Base; +using src.Helpers; using src.Managers; using UnityEngine; @@ -8,12 +9,10 @@ namespace src.Wall public class ExitDoor : GameplayComponent { private GameManager _gameManager; - private Collider2D _collider2D; private void Start() { _gameManager = GameManager.instance; - _collider2D = GetComponent(); } /* Trigger the next level and destroy itself. */