From 001eca70c58f79c86dbe80fd2bd90dc99be5164c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Sat, 15 Jun 2019 13:48:25 +0300 Subject: [PATCH] Refactor Ammo code --- Assets/Scripts/src/Ammo/BombController.cs | 17 +++++----- Assets/Scripts/src/Ammo/BombsSpawner.cs | 32 ++++++++++--------- Assets/Scripts/src/Ammo/Explosion.cs | 17 +++++----- .../Scripts/src/Managers/BombsUtilManager.cs | 2 +- Assets/Scripts/src/Managers/GameManager.cs | 2 +- Assets/Scripts/src/Player/PlayerController.cs | 3 +- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/Assets/Scripts/src/Ammo/BombController.cs b/Assets/Scripts/src/Ammo/BombController.cs index f133622..47fd009 100644 --- a/Assets/Scripts/src/Ammo/BombController.cs +++ b/Assets/Scripts/src/Ammo/BombController.cs @@ -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(); - if (key != null) - { - key.onExplosion(); - } + key?.onExplosion(); + break; } } + yield return new WaitForSeconds(0.05f); } diff --git a/Assets/Scripts/src/Ammo/BombsSpawner.cs b/Assets/Scripts/src/Ammo/BombsSpawner.cs index 13ccb0d..a0a4c72 100644 --- a/Assets/Scripts/src/Ammo/BombsSpawner.cs +++ b/Assets/Scripts/src/Ammo/BombsSpawner.cs @@ -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 GameObject bombPrefab; - - private BombsUtilManager _bombsUtil = BombsUtilManager.Instance; - - public void PlaceBomb(Transform transform) + public class BombsSpawner : GameplayComponent { - var absX = Mathf.RoundToInt(transform.position.x); - var absY = Mathf.RoundToInt(transform.position.y); - Vector3 position = new Vector3(absX, absY, 0); - if (_bombsUtil.CanPlaceBomb(position)) + public GameObject bombPrefab; + + private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance; + + public void PlaceBomb(Transform transform) { - Instantiate(bombPrefab, position, Quaternion.identity); - _bombsUtil.PlaceBomb(position); + var absX = Mathf.RoundToInt(transform.position.x); + 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); + } } } } diff --git a/Assets/Scripts/src/Ammo/Explosion.cs b/Assets/Scripts/src/Ammo/Explosion.cs index 431eec7..ec30d55 100644 --- a/Assets/Scripts/src/Ammo/Explosion.cs +++ b/Assets/Scripts/src/Ammo/Explosion.cs @@ -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 { - private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance; - - public void Start() + public class Explosion : GameplayComponent { - Destroy(gameObject, _bombUtil.ExplosionDuration); + private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance; + + public void Start() + { + Destroy(gameObject, _bombUtil.ExplosionDuration); + } } } diff --git a/Assets/Scripts/src/Managers/BombsUtilManager.cs b/Assets/Scripts/src/Managers/BombsUtilManager.cs index 5ef931b..2091465 100644 --- a/Assets/Scripts/src/Managers/BombsUtilManager.cs +++ b/Assets/Scripts/src/Managers/BombsUtilManager.cs @@ -5,7 +5,7 @@ namespace src.Managers { public sealed class BombsUtilManager { - private HashSet _usedPosition = new HashSet(); + private readonly HashSet _usedPosition = new HashSet(); private const int MaxPower = 7; private const int MaxAllowedBombs = 10; diff --git a/Assets/Scripts/src/Managers/GameManager.cs b/Assets/Scripts/src/Managers/GameManager.cs index 2d50961..adc3f65 100644 --- a/Assets/Scripts/src/Managers/GameManager.cs +++ b/Assets/Scripts/src/Managers/GameManager.cs @@ -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() diff --git a/Assets/Scripts/src/Player/PlayerController.cs b/Assets/Scripts/src/Player/PlayerController.cs index 98022a3..4120425 100644 --- a/Assets/Scripts/src/Player/PlayerController.cs +++ b/Assets/Scripts/src/Player/PlayerController.cs @@ -1,7 +1,6 @@ -using System; +using src.Ammo; using src.Base; using UnityEngine; -using UnityEngine.Tilemaps; namespace src.Player {