Reformat Bomb Code

This commit is contained in:
Denis-Cosmin Nutiu 2019-06-09 23:33:48 +03:00
parent c7eff9ff61
commit 00e2abf8e3
3 changed files with 83 additions and 87 deletions

View file

@ -1,18 +1,20 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using src.Managers;
using UnityEngine; using UnityEngine;
namespace src.Ammo
{
public class BombController : MonoBehaviour, IExplosable public class BombController : MonoBehaviour, IExplosable
{ {
public GameObject explosionPrefab; public GameObject explosionPrefab;
BombStatsManager bombStatsUtil = BombStatsManager.Instance; private readonly BombStatsManager _bombStatsUtil = BombStatsManager.Instance;
bool exploded = false; private bool _exploded;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
Invoke("Explode", bombStatsUtil.Timer); Invoke(nameof(Explode), _bombStatsUtil.Timer);
} }
void Explode() void Explode()
@ -27,33 +29,33 @@ public class BombController : MonoBehaviour, IExplosable
StartCoroutine(CreateExplosions(Vector3.up)); StartCoroutine(CreateExplosions(Vector3.up));
StartCoroutine(CreateExplosions(Vector3.right)); StartCoroutine(CreateExplosions(Vector3.right));
exploded = true; _exploded = true;
Destroy(gameObject, 0.3f); Destroy(gameObject, 0.3f);
} }
private IEnumerator CreateExplosions(Vector3 direction) private IEnumerator CreateExplosions(Vector3 direction)
{ {
for (int i = 1; i < bombStatsUtil.Power; i++) for (int i = 1; i < _bombStatsUtil.Power; i++)
{ {
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), direction, i, 1 << 8); RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, i,
1 << 8);
if (!hit.collider) if (!hit.collider)
{ {
Instantiate(explosionPrefab, transform.position + (i * direction), explosionPrefab.transform.rotation); Instantiate(explosionPrefab, transform.position + i * direction,
explosionPrefab.transform.rotation);
} }
else else
{ {
break; break;
} }
} }
yield return new WaitForSeconds(0.05f); yield return new WaitForSeconds(0.05f);
} }
public void OnTriggerEnter2D(Collider2D other) public void OnTriggerEnter2D(Collider2D other)
{ {
if (!exploded && other.CompareTag("Explosion")) if (!_exploded && other.CompareTag("Explosion"))
{ {
onExplosion(); onExplosion();
} }
@ -61,8 +63,10 @@ public class BombController : MonoBehaviour, IExplosable
public void onExplosion() public void onExplosion()
{ {
//In caz ca o bomba loveste bomba, dam cancel la explozie sa nu explodeze twice si o explodam automagic // In caz ca o bomba loveste bomba, dam cancel la explozie
CancelInvoke("Explode"); // sa nu explodeze twice si o explodam automagic
CancelInvoke(nameof(Explode));
Explode(); Explode();
} }
} }
}

View file

@ -1,13 +1,14 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using src.Managers;
using UnityEngine; using UnityEngine;
public class Explosion : MonoBehaviour public class Explosion : MonoBehaviour
{ {
BombStatsManager bombUtil = BombStatsManager.Instance; private readonly BombStatsManager _bombUtil = BombStatsManager.Instance;
public void Start() public void Start()
{ {
Destroy(gameObject, bombUtil.ExplosionDuration); Destroy(gameObject, _bombUtil.ExplosionDuration);
} }
} }

View file

@ -1,11 +1,8 @@
using System.Collections; namespace src.Managers
using System.Collections.Generic; {
public sealed class BombStatsManager public sealed class BombStatsManager
{ {
private const int MaxPower = 7;
private static readonly BombStatsManager instance = new BombStatsManager();
const int MAX_POWER = 7;
public int Power { get; private set; } = 3; public int Power { get; private set; } = 3;
@ -17,20 +14,14 @@ public sealed class BombStatsManager
{ {
} }
public static BombStatsManager Instance public static BombStatsManager Instance { get; } = new BombStatsManager();
{
get
{
return instance;
}
}
public void increasePower() public void IncreasePower()
{ {
if(Power <= MAX_POWER) if (Power <= MaxPower)
{ {
Power++; Power++;
} }
} }
}
} }