Refactor Ammo code

This commit is contained in:
Denis-Cosmin Nutiu 2019-06-15 13:48:25 +03:00
parent 2c746e252f
commit 001eca70c5
6 changed files with 38 additions and 35 deletions

View file

@ -1,10 +1,11 @@
using System.Collections; using System.Collections;
using src.Base;
using src.Managers; using src.Managers;
using UnityEngine; using UnityEngine;
namespace src.Ammo namespace src.Ammo
{ {
public class BombController : MonoBehaviour, IExplosable public class BombController : GameplayComponent, IExplosable
{ {
public GameObject explosionPrefab; public GameObject explosionPrefab;
@ -36,10 +37,11 @@ namespace src.Ammo
private IEnumerator CreateExplosions(Vector3 direction) private IEnumerator CreateExplosions(Vector3 direction)
{ {
for (int i = 1; i < _bombsUtil.Power; i++) var currentPosition = transform.position;
for (var i = 1; i < _bombsUtil.Power; i++)
{ {
RaycastHit2D hit = Physics2D.Raycast(new Vector3(transform.position.x + 0.5f, transform.position.y + 0.5f, 0) , direction, i, var hit = Physics2D.Raycast(new Vector2(currentPosition.x + 0.5f,
1 << 8); currentPosition.y + 0.5f), direction, i, 1 << 8);
if (!hit.collider) if (!hit.collider)
{ {
@ -49,13 +51,12 @@ namespace src.Ammo
else else
{ {
var key = hit.collider.GetComponent<IExplosable>(); var key = hit.collider.GetComponent<IExplosable>();
if (key != null) key?.onExplosion();
{
key.onExplosion();
}
break; break;
} }
} }
yield return new WaitForSeconds(0.05f); yield return new WaitForSeconds(0.05f);
} }

View file

@ -1,23 +1,25 @@
using src.Managers; using src.Base;
using System.Collections; using src.Managers;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class BombsSpawner : MonoBehaviour namespace src.Ammo
{ {
public GameObject bombPrefab; public class BombsSpawner : GameplayComponent
private BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
public void PlaceBomb(Transform transform)
{ {
var absX = Mathf.RoundToInt(transform.position.x); public GameObject bombPrefab;
var absY = Mathf.RoundToInt(transform.position.y);
Vector3 position = new Vector3(absX, absY, 0); private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
if (_bombsUtil.CanPlaceBomb(position))
public void PlaceBomb(Transform transform)
{ {
Instantiate(bombPrefab, position, Quaternion.identity); var absX = Mathf.RoundToInt(transform.position.x);
_bombsUtil.PlaceBomb(position); var absY = Mathf.RoundToInt(transform.position.y);
var position = new Vector2(absX, absY);
if (_bombsUtil.CanPlaceBomb(position))
{
Instantiate(bombPrefab, position, Quaternion.identity);
_bombsUtil.PlaceBomb(position);
}
} }
} }
} }

View file

@ -1,14 +1,15 @@
using System.Collections; using src.Base;
using System.Collections.Generic;
using src.Managers; using src.Managers;
using UnityEngine;
public class Explosion : MonoBehaviour namespace src.Ammo
{ {
private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance; public class Explosion : GameplayComponent
public void Start()
{ {
Destroy(gameObject, _bombUtil.ExplosionDuration); private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance;
public void Start()
{
Destroy(gameObject, _bombUtil.ExplosionDuration);
}
} }
} }

View file

@ -5,7 +5,7 @@ namespace src.Managers
{ {
public sealed class BombsUtilManager public sealed class BombsUtilManager
{ {
private HashSet<Vector3> _usedPosition = new HashSet<Vector3>(); private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
private const int MaxPower = 7; private const int MaxPower = 7;
private const int MaxAllowedBombs = 10; private const int MaxAllowedBombs = 10;

View file

@ -4,7 +4,7 @@ namespace src.Managers
{ {
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
public static GameManager Instance = null; public static GameManager Instance;
private LevelManager _levelManager; private LevelManager _levelManager;
public void Awake() public void Awake()

View file

@ -1,7 +1,6 @@
using System; using src.Ammo;
using src.Base; using src.Base;
using UnityEngine; using UnityEngine;
using UnityEngine.Tilemaps;
namespace src.Player namespace src.Player
{ {