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.Generic;
using src.Managers;
using UnityEngine;
namespace src.Ammo
{
public class BombController : MonoBehaviour, IExplosable
{
public GameObject explosionPrefab;
BombStatsManager bombStatsUtil = BombStatsManager.Instance;
bool exploded = false;
private readonly BombStatsManager _bombStatsUtil = BombStatsManager.Instance;
private bool _exploded;
// Start is called before the first frame update
void Start()
{
Invoke("Explode", bombStatsUtil.Timer);
Invoke(nameof(Explode), _bombStatsUtil.Timer);
}
void Explode()
@ -27,33 +29,33 @@ public class BombController : MonoBehaviour, IExplosable
StartCoroutine(CreateExplosions(Vector3.up));
StartCoroutine(CreateExplosions(Vector3.right));
exploded = true;
_exploded = true;
Destroy(gameObject, 0.3f);
}
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)
{
Instantiate(explosionPrefab, transform.position + (i * direction), explosionPrefab.transform.rotation);
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"))
if (!_exploded && other.CompareTag("Explosion"))
{
onExplosion();
}
@ -61,8 +63,10 @@ public class BombController : MonoBehaviour, IExplosable
public void onExplosion()
{
//In caz ca o bomba loveste bomba, dam cancel la explozie sa nu explodeze twice si o explodam automagic
CancelInvoke("Explode");
// In caz ca o bomba loveste bomba, dam cancel la explozie
// sa nu explodeze twice si o explodam automagic
CancelInvoke(nameof(Explode));
Explode();
}
}
}

View file

@ -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);
}
}

View file

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