Refactor Ammo code
This commit is contained in:
parent
2c746e252f
commit
001eca70c5
6 changed files with 38 additions and 35 deletions
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 class BombsSpawner : GameplayComponent
|
||||||
|
{
|
||||||
public GameObject bombPrefab;
|
public GameObject bombPrefab;
|
||||||
|
|
||||||
private BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
|
private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
|
||||||
|
|
||||||
public void PlaceBomb(Transform transform)
|
public void PlaceBomb(Transform transform)
|
||||||
{
|
{
|
||||||
var absX = Mathf.RoundToInt(transform.position.x);
|
var absX = Mathf.RoundToInt(transform.position.x);
|
||||||
var absY = Mathf.RoundToInt(transform.position.y);
|
var absY = Mathf.RoundToInt(transform.position.y);
|
||||||
Vector3 position = new Vector3(absX, absY, 0);
|
var position = new Vector2(absX, absY);
|
||||||
if (_bombsUtil.CanPlaceBomb(position))
|
if (_bombsUtil.CanPlaceBomb(position))
|
||||||
{
|
{
|
||||||
Instantiate(bombPrefab, position, Quaternion.identity);
|
Instantiate(bombPrefab, position, Quaternion.identity);
|
||||||
_bombsUtil.PlaceBomb(position);
|
_bombsUtil.PlaceBomb(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
{
|
{
|
||||||
|
public class Explosion : GameplayComponent
|
||||||
|
{
|
||||||
private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance;
|
private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance;
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
Destroy(gameObject, _bombUtil.ExplosionDuration);
|
Destroy(gameObject, _bombUtil.ExplosionDuration);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue