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,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<SpriteRenderer>().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<SpriteRenderer>().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();
}
}
}

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,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++;
}
}
}
}