-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeathMessage.cs
190 lines (150 loc) · 6.77 KB
/
DeathMessage.cs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Tamana
{
public enum DeathPhase { Waiting, AppearingMessage, AppearingBlackScreen, VanishingBlackScreen }
public class DeathMessage : MonoBehaviour
{
private static GameObject messagePrefab;
public static GameObject blackScreenPrefab { private set; get; }
public static GameObject resurrectionLightPrefab { private set; get; }
public static int deathNumber { get; set; }
private bool messageStartToAppear;
private DeathPhase phase;
[HideInInspector]
public Image img;
[HideInInspector]
public Image txt;
[HideInInspector]
public Image blackScreen;
private Vector2 txtSize;
private float resizeSpeed;
private float waitingTime;
public static DeathMessage mInstance { private set; get; }
private void Awake()
{
messagePrefab = GM.LoadResources("Death Message");
blackScreenPrefab = GM.LoadResources("BlackScreen");
resurrectionLightPrefab = GM.LoadResources("ResurrectionLight");
mInstance = this;
enabled = false;
Message.RepositionMessageUI();
}
void Start()
{
messageStartToAppear = false;
txtSize = txt.rectTransform.sizeDelta;
txt.rectTransform.sizeDelta = txt.rectTransform.sizeDelta * 0.7f;
resizeSpeed = Mathf.Sqrt((txtSize - txt.rectTransform.sizeDelta).sqrMagnitude / 2.0f);
StartCoroutine(WaitBeforeTextAppear());
phase = DeathPhase.AppearingMessage;
}
void Update()
{
switch(phase)
{
case DeathPhase.AppearingMessage : AppearingMessage(); break;
case DeathPhase.AppearingBlackScreen : AppearingBlackScreen(); break;
case DeathPhase.VanishingBlackScreen : VanishingBlackScreen(); break;
}
}
public static void InstantiateMessage()
{
var obj = Instantiate(messagePrefab);
mInstance.img = obj.GetComponent<UnityEngine.UI.Image>();
mInstance.txt = obj.transform.GetChild(0).GetComponent<UnityEngine.UI.Image>();
if(Screen.width > 1920)
{
mInstance.img.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.width / 8);
mInstance.txt.rectTransform.sizeDelta = new Vector2(mInstance.img.rectTransform.sizeDelta.y * 2, mInstance.img.rectTransform.sizeDelta.y);
}
mInstance.img.color = Color.clear;
mInstance.txt.color = Color.clear;
obj.transform.SetParent(GM.canvas.transform);
mInstance.img.rectTransform.localPosition = Vector3.zero;
mInstance.txt.rectTransform.localPosition = Vector3.zero;
}
//=========================================================================================================================
// P H A S E S
//=========================================================================================================================
private void AppearingMessage()
{
img.color = Color.Lerp(img.color, Color.white, Time.deltaTime);
if (messageStartToAppear)
{
txt.color = Color.Lerp(txt.color, Color.white, Time.deltaTime);
txt.rectTransform.sizeDelta = Vector2.MoveTowards(txt.rectTransform.sizeDelta, txtSize, resizeSpeed * Time.deltaTime);
}
if (((Vector4)(txt.color - Color.white)).sqrMagnitude < 1.0f && txt.rectTransform.sizeDelta == txtSize)
{
StartCoroutine(WaitBeforeBlackScreenAppear());
phase = DeathPhase.AppearingBlackScreen;
}
}
private void AppearingBlackScreen()
{
if (blackScreen == null)
return;
blackScreen.color = Color.Lerp(blackScreen.color, Color.black, 2 * Time.deltaTime);
if (Mathf.Abs(blackScreen.color.a - 1.0f) <= 0.0001f)
{
Destroy(img.gameObject);
Destroy(txt.gameObject);
ResurrectionTombstone.ResurrectPlayer();
waitingTime = Time.time + 1.5f;
phase = DeathPhase.VanishingBlackScreen;
}
}
private void VanishingBlackScreen()
{
if (Time.time < waitingTime)
return;
blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, 2 * Time.deltaTime);
if (Mathf.Abs(blackScreen.color.a - 1.0f) >= 0.99f)
{
Destroy(blackScreen.gameObject);
StartCoroutine(WaitAndPlayReviveAnimation());
Instantiate(resurrectionLightPrefab).transform.position = GM.playerPosition;
enabled = false;
}
}
//=========================================================================================================================
// E N U M E R A T O R S
//=========================================================================================================================
public IEnumerator WaitUntilDeath()
{
yield return new WaitUntil(() => PlayerControl.Attack.mInstance.isDeath == true);
StartCoroutine(WaitBeforeInstantiate());
}
public IEnumerator WaitBeforeInstantiate()
{
yield return new WaitForSeconds(2.0f);
this.enabled = true;
InstantiateMessage();
Start();
}
private IEnumerator WaitBeforeTextAppear()
{
yield return new WaitForSeconds(1.0f);
messageStartToAppear = true;
}
private IEnumerator WaitBeforeBlackScreenAppear()
{
yield return new WaitForSeconds(1.5f);
var obj = Instantiate(blackScreenPrefab);
blackScreen = obj.GetComponent<Image>();
blackScreen.rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
blackScreen.color = Color.clear;
blackScreen.transform.SetParent(GM.canvas.transform);
blackScreen.rectTransform.localPosition = Vector3.zero;
}
private IEnumerator WaitAndPlayReviveAnimation()
{
yield return new WaitForSeconds(1.0f);
GM.playerAnimator.Play("Revive");
StartCoroutine(WaitUntilDeath());
}
}
}