2019-06-02 10:00:00 +00:00
|
|
|
|
using System.Collections;
|
2019-06-15 10:48:25 +00:00
|
|
|
|
using src.Base;
|
2019-06-09 20:33:48 +00:00
|
|
|
|
using src.Managers;
|
2019-06-02 10:00:00 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
namespace src.Ammo
|
2019-06-02 10:00:00 +00:00
|
|
|
|
{
|
2019-06-15 10:48:25 +00:00
|
|
|
|
public class BombController : GameplayComponent, IExplosable
|
2019-06-09 20:33:48 +00:00
|
|
|
|
{
|
|
|
|
|
public GameObject explosionPrefab;
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-10 14:28:33 +00:00
|
|
|
|
private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance;
|
2019-06-09 20:33:48 +00:00
|
|
|
|
private bool _exploded;
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2019-06-10 14:28:33 +00:00
|
|
|
|
Invoke(nameof(Explode), _bombsUtil.Timer);
|
2019-06-09 20:33:48 +00:00
|
|
|
|
}
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
void Explode()
|
|
|
|
|
{
|
|
|
|
|
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
GetComponent<SpriteRenderer>().enabled = false;
|
|
|
|
|
transform.Find("2DCollider").gameObject.SetActive(false);
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
StartCoroutine(CreateExplosions(Vector3.down));
|
|
|
|
|
StartCoroutine(CreateExplosions(Vector3.left));
|
|
|
|
|
StartCoroutine(CreateExplosions(Vector3.up));
|
|
|
|
|
StartCoroutine(CreateExplosions(Vector3.right));
|
2019-06-02 10:00:00 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
_exploded = true;
|
|
|
|
|
Destroy(gameObject, 0.3f);
|
2019-06-10 14:28:33 +00:00
|
|
|
|
_bombsUtil.RemoveBomb(transform.position);
|
2019-06-09 20:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator CreateExplosions(Vector3 direction)
|
2019-06-02 10:00:00 +00:00
|
|
|
|
{
|
2019-06-15 10:48:25 +00:00
|
|
|
|
var currentPosition = transform.position;
|
|
|
|
|
for (var i = 1; i < _bombsUtil.Power; i++)
|
2019-06-02 10:00:00 +00:00
|
|
|
|
{
|
2019-06-15 10:48:25 +00:00
|
|
|
|
var hit = Physics2D.Raycast(new Vector2(currentPosition.x + 0.5f,
|
|
|
|
|
currentPosition.y + 0.5f), direction, i, 1 << 8);
|
2019-06-11 19:17:23 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
if (!hit.collider)
|
|
|
|
|
{
|
2019-06-11 19:17:23 +00:00
|
|
|
|
Instantiate(explosionPrefab, transform.position + i * direction,
|
2019-06-09 20:33:48 +00:00
|
|
|
|
explosionPrefab.transform.rotation);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-10 14:28:33 +00:00
|
|
|
|
var key = hit.collider.GetComponent<IExplosable>();
|
2019-06-15 10:48:25 +00:00
|
|
|
|
key?.onExplosion();
|
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-06-02 10:00:00 +00:00
|
|
|
|
}
|
2019-06-15 10:48:25 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
yield return new WaitForSeconds(0.05f);
|
2019-06-02 10:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
public void OnTriggerEnter2D(Collider2D other)
|
2019-06-02 10:00:00 +00:00
|
|
|
|
{
|
2019-06-09 20:33:48 +00:00
|
|
|
|
if (!_exploded && other.CompareTag("Explosion"))
|
|
|
|
|
{
|
|
|
|
|
onExplosion();
|
|
|
|
|
}
|
2019-06-02 10:00:00 +00:00
|
|
|
|
}
|
2019-06-08 12:25:09 +00:00
|
|
|
|
|
2019-06-09 20:33:48 +00:00
|
|
|
|
public void onExplosion()
|
|
|
|
|
{
|
|
|
|
|
CancelInvoke(nameof(Explode));
|
|
|
|
|
Explode();
|
|
|
|
|
}
|
2019-06-08 12:25:09 +00:00
|
|
|
|
}
|
2019-06-09 20:33:48 +00:00
|
|
|
|
}
|