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 src.Base;
using src.Managers;
using UnityEngine;
namespace src.Ammo
{
public class BombController : MonoBehaviour, IExplosable
public class BombController : GameplayComponent, IExplosable
{
public GameObject explosionPrefab;
@ -36,10 +37,11 @@ namespace src.Ammo
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,
1 << 8);
var hit = Physics2D.Raycast(new Vector2(currentPosition.x + 0.5f,
currentPosition.y + 0.5f), direction, i, 1 << 8);
if (!hit.collider)
{
@ -49,13 +51,12 @@ namespace src.Ammo
else
{
var key = hit.collider.GetComponent<IExplosable>();
if (key != null)
{
key.onExplosion();
}
key?.onExplosion();
break;
}
}
yield return new WaitForSeconds(0.05f);
}

View file

@ -1,23 +1,25 @@
using src.Managers;
using System.Collections;
using System.Collections.Generic;
using src.Base;
using src.Managers;
using UnityEngine;
public class BombsSpawner : MonoBehaviour
namespace src.Ammo
{
public class BombsSpawner : GameplayComponent
{
public GameObject bombPrefab;
private BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
public void PlaceBomb(Transform transform)
{
var absX = Mathf.RoundToInt(transform.position.x);
var absY = Mathf.RoundToInt(transform.position.y);
Vector3 position = new Vector3(absX, absY, 0);
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 System.Collections.Generic;
using src.Base;
using src.Managers;
using UnityEngine;
public class Explosion : MonoBehaviour
namespace src.Ammo
{
public class Explosion : GameplayComponent
{
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
{
private HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
private const int MaxPower = 7;
private const int MaxAllowedBombs = 10;

View file

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

View file

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