Refactor BombUtil and couple with player

This commit is contained in:
Denis-Cosmin Nutiu 2019-08-26 21:36:26 +03:00
parent 01bddcc821
commit 369a86c9d8
7 changed files with 19 additions and 23 deletions

View file

@ -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}

View file

@ -15,7 +15,7 @@ namespace src.Managers
public float explosionDuration = 0.55f;
private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
private static readonly HashSet<Vector3> UsedPosition = new HashSet<Vector3>();
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;
}
}
}

View file

@ -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<BombsUtilManager>();
_gameStateManager = gameObject.AddComponent<GameStateManager>();
_levelManager = gameObject.AddComponent<LevelManager>();
_upgradeManager = gameObject.AddComponent<UpgradeManager>();
@ -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.

View file

@ -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<PlayerUpgrade>();
_bombsUtilManager = gameObject.AddComponent<BombsUtilManager>();
}
protected void Start()
{
{
_gameStateManager = GameStateManager.instance;
_bombsUtil = BombsUtilManager.instance;
_rigidbody2d = GetComponent<Rigidbody2D>();
_collider2D = GetComponent<Collider2D>();
@ -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()

View file

@ -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<BombsUtilManager>();
bombManager.IncreasePower();
}
}

View file

@ -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<BombsUtilManager>();
bombManager.IncreaseAllowedBombs();
}
}

View file

@ -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<BombsUtilManager>();
bombManager.IncreaseAllowedBombs();
bombManager.IncreasePower();
}