From 00e2abf8e3d7061d84b7ea6d08d52db3fb68c0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Sun, 9 Jun 2019 23:33:48 +0300 Subject: [PATCH] Reformat Bomb Code --- Assets/Scripts/src/Ammo/BombController.cs | 114 +++++++++--------- Assets/Scripts/src/Ammo/Explosion.cs | 5 +- .../Scripts/src/Managers/BombStatsManager.cs | 51 ++++---- 3 files changed, 83 insertions(+), 87 deletions(-) diff --git a/Assets/Scripts/src/Ammo/BombController.cs b/Assets/Scripts/src/Ammo/BombController.cs index 0c516be..b59cb9e 100644 --- a/Assets/Scripts/src/Ammo/BombController.cs +++ b/Assets/Scripts/src/Ammo/BombController.cs @@ -1,68 +1,72 @@ using System.Collections; -using System.Collections.Generic; +using src.Managers; using UnityEngine; -public class BombController : MonoBehaviour, IExplosable +namespace src.Ammo { - public GameObject explosionPrefab; - - BombStatsManager bombStatsUtil = BombStatsManager.Instance; - bool exploded = false; - - // Start is called before the first frame update - void Start() + public class BombController : MonoBehaviour, IExplosable { - Invoke("Explode", bombStatsUtil.Timer); - } + public GameObject explosionPrefab; - void Explode() - { - Instantiate(explosionPrefab, transform.position, Quaternion.identity); + private readonly BombStatsManager _bombStatsUtil = BombStatsManager.Instance; + private bool _exploded; - GetComponent().enabled = false; - transform.Find("2DCollider").gameObject.SetActive(false); - - StartCoroutine(CreateExplosions(Vector3.down)); - StartCoroutine(CreateExplosions(Vector3.left)); - StartCoroutine(CreateExplosions(Vector3.up)); - StartCoroutine(CreateExplosions(Vector3.right)); - - exploded = true; - Destroy(gameObject, 0.3f); - } - - private IEnumerator CreateExplosions(Vector3 direction) - { - for (int i = 1; i < bombStatsUtil.Power; i++) + // Start is called before the first frame update + void Start() { - RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), direction, i, 1 << 8); - if (!hit.collider) - { - Instantiate(explosionPrefab, transform.position + (i * direction), explosionPrefab.transform.rotation); - } - else - { - break; - } - + Invoke(nameof(Explode), _bombStatsUtil.Timer); } - yield return new WaitForSeconds(0.05f); - - } - - public void OnTriggerEnter2D(Collider2D other) - { - if (!exploded && other.CompareTag("Explosion")) + void Explode() { - onExplosion(); + Instantiate(explosionPrefab, transform.position, Quaternion.identity); + + GetComponent().enabled = false; + transform.Find("2DCollider").gameObject.SetActive(false); + + StartCoroutine(CreateExplosions(Vector3.down)); + StartCoroutine(CreateExplosions(Vector3.left)); + StartCoroutine(CreateExplosions(Vector3.up)); + StartCoroutine(CreateExplosions(Vector3.right)); + + _exploded = true; + Destroy(gameObject, 0.3f); + } + + private IEnumerator CreateExplosions(Vector3 direction) + { + for (int i = 1; i < _bombStatsUtil.Power; i++) + { + RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, i, + 1 << 8); + if (!hit.collider) + { + Instantiate(explosionPrefab, transform.position + i * direction, + explosionPrefab.transform.rotation); + } + else + { + break; + } + } + + yield return new WaitForSeconds(0.05f); + } + + public void OnTriggerEnter2D(Collider2D other) + { + if (!_exploded && other.CompareTag("Explosion")) + { + onExplosion(); + } + } + + public void onExplosion() + { + // In caz ca o bomba loveste bomba, dam cancel la explozie + // sa nu explodeze twice si o explodam automagic + CancelInvoke(nameof(Explode)); + Explode(); } } - - public void onExplosion() - { - //In caz ca o bomba loveste bomba, dam cancel la explozie sa nu explodeze twice si o explodam automagic - CancelInvoke("Explode"); - Explode(); - } -} +} \ No newline at end of file diff --git a/Assets/Scripts/src/Ammo/Explosion.cs b/Assets/Scripts/src/Ammo/Explosion.cs index ca22366..b453fd1 100644 --- a/Assets/Scripts/src/Ammo/Explosion.cs +++ b/Assets/Scripts/src/Ammo/Explosion.cs @@ -1,13 +1,14 @@ using System.Collections; using System.Collections.Generic; +using src.Managers; using UnityEngine; public class Explosion : MonoBehaviour { - BombStatsManager bombUtil = BombStatsManager.Instance; + private readonly BombStatsManager _bombUtil = BombStatsManager.Instance; public void Start() { - Destroy(gameObject, bombUtil.ExplosionDuration); + Destroy(gameObject, _bombUtil.ExplosionDuration); } } diff --git a/Assets/Scripts/src/Managers/BombStatsManager.cs b/Assets/Scripts/src/Managers/BombStatsManager.cs index b6af6d1..d30f1ee 100644 --- a/Assets/Scripts/src/Managers/BombStatsManager.cs +++ b/Assets/Scripts/src/Managers/BombStatsManager.cs @@ -1,36 +1,27 @@ -using System.Collections; -using System.Collections.Generic; - -public sealed class BombStatsManager +namespace src.Managers { - - private static readonly BombStatsManager instance = new BombStatsManager(); - const int MAX_POWER = 7; - - public int Power { get; private set; } = 3; - - public float Timer { get; } = 3.0f; - - public float ExplosionDuration { get; } = 0.55f; - - private BombStatsManager() + public sealed class BombStatsManager { - } + private const int MaxPower = 7; - public static BombStatsManager Instance - { - get + public int Power { get; private set; } = 3; + + public float Timer { get; } = 3.0f; + + public float ExplosionDuration { get; } = 0.55f; + + private BombStatsManager() { - return instance; + } + + public static BombStatsManager Instance { get; } = new BombStatsManager(); + + public void IncreasePower() + { + if (Power <= MaxPower) + { + Power++; + } } } - - public void increasePower() - { - if(Power <= MAX_POWER) - { - Power++; - } - } - -} +} \ No newline at end of file