using Dependencies ValakhP Scripts using Explosions using System Colle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Dependencies.ValakhP.Scripts;
using Explosions;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BombController : SSingleton<BombController>
{
private bool hasChainReaction = true;
private float delayChainReaction = 0.6f;
private float detectDistance = 1000000;
private List<Bomb> bombs = new List<Bomb>();
public void Subscribe(Bomb bomb)
{
if (!bombs.Contains(bomb))
{
bombs.Add(bomb);
}
}
public void UnsubscribeAll()
{
bombs = new List<Bomb>();
}
public void ExplodeBomb(Bomb bomb)
{
if (bomb.Exploded && bomb.IsTicking)
{
return;
}
bomb.Exploded = true;
ExplosionUtils.ExplodeWithEffect<Bomb>(bomb.transform.position, bomb.ExplosionForce, bomb.ExplosionRadius);
bomb.gameObject.SetActive(false);
if (hasChainReaction)
{
Bomb nextBomb = null;
var minDistance = float.MaxValue;
foreach (var b in bombs)
{
if (b != bomb)
{
var distance = Vector3.Distance(bomb.transform.position, b.transform.position);
if (distance < detectDistance && distance < minDistance)
{
minDistance = distance;
nextBomb = b;
}
}
}
if (nextBomb != null)
{
StartCoroutine(ExplodeWithDelay(nextBomb));
}
}
}
public IEnumerator ExplodeWithDelay(Bomb bomb)
{
bomb.IsTicking = true;
yield return new WaitForSeconds(delayChainReaction);
ExplodeBomb(bomb);
}
public void OnCollisionBomb(Bomb bomb, Collision collision)
{
if (bomb.Exploded)
{
return;
}
var rb = collision.gameObject.GetComponent<Rigidbody>();
if (rb != null)
{
if (rb.velocity.magnitude > bomb.Sensitivity)
{
ExplodeBomb(bomb);
}
}
}
}