ExtinguishableFire.cs
2.05 KB
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
87
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This simulate an extinguishable fire,
/// </summary>
public class ExtinguishableFire : MonoBehaviour
{
public ParticleSystem fireParticleSystem;
public ParticleSystem smokeParticleSystem;
protected bool m_isExtinguished;
const float m_FireStartingTime = 2.0f;
private void Start()
{
m_isExtinguished = true;
smokeParticleSystem.Stop();
fireParticleSystem.Stop();
StartCoroutine(StartingFire());
}
public void Extinguish()
{
if (m_isExtinguished)
return;
m_isExtinguished = true;
StartCoroutine(Extinguishing());
}
IEnumerator Extinguishing()
{
fireParticleSystem.Stop();
smokeParticleSystem.time = 0;
smokeParticleSystem.Play();
float elapsedTime = 0.0f;
while (elapsedTime < m_FireStartingTime)
{
float ratio = Mathf.Max(0.0f, 1.0f - (elapsedTime / m_FireStartingTime));
fireParticleSystem.transform.localScale = Vector3.one * ratio;
yield return null;
elapsedTime += Time.deltaTime;
}
yield return new WaitForSeconds(2.0f);
smokeParticleSystem.Stop();
fireParticleSystem.transform.localScale = Vector3.one;
yield return new WaitForSeconds(4.0f);
StartCoroutine(StartingFire());
}
IEnumerator StartingFire()
{
smokeParticleSystem.Stop();
fireParticleSystem.time = 0;
fireParticleSystem.Play();
float elapsedTime = 0.0f;
while (elapsedTime < m_FireStartingTime)
{
float ratio = Mathf.Min(1.0f, (elapsedTime / m_FireStartingTime));
fireParticleSystem.transform.localScale = Vector3.one * ratio;
yield return null;
elapsedTime += Time.deltaTime;
}
fireParticleSystem.transform.localScale = Vector3.one;
m_isExtinguished = false;
}
}