summaryrefslogtreecommitdiff
path: root/Valheim_v202102/Valheim/assembly_valheim/MessageHud.cs
blob: 26ed8afc45a4ad0aed269bb67249942fd0d2cbe6 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MessageHud : MonoBehaviour
{
	public enum MessageType
	{
		TopLeft = 1,
		Center
	}

	private class UnlockMsg
	{
		public Sprite m_icon;

		public string m_topic;

		public string m_description;
	}

	private class MsgData
	{
		public Sprite m_icon;

		public string m_text;

		public int m_amount;
	}

	private class BiomeMessage
	{
		public string m_text;

		public bool m_playStinger;
	}

	private MsgData currentMsg = new MsgData();

	private static MessageHud m_instance;

	public Text m_messageText;

	public Image m_messageIcon;

	public Text m_messageCenterText;

	public GameObject m_unlockMsgPrefab;

	public int m_maxUnlockMsgSpace = 110;

	public int m_maxUnlockMessages = 4;

	public int m_maxLogMessages = 50;

	public GameObject m_biomeFoundPrefab;

	public GameObject m_biomeFoundStinger;

	private Queue<BiomeMessage> m_biomeFoundQueue = new Queue<BiomeMessage>();

	private List<string> m_messageLog = new List<string>();

	private List<GameObject> m_unlockMessages = new List<GameObject>();

	private Queue<UnlockMsg> m_unlockMsgQueue = new Queue<UnlockMsg>();

	private Queue<MsgData> m_msgQeue = new Queue<MsgData>();

	private float m_msgQueueTimer = -1f;

	private GameObject m_biomeMsgInstance;

	public static MessageHud instance => m_instance;

	private void Awake()
	{
		m_instance = this;
	}

	private void OnDestroy()
	{
		m_instance = null;
	}

	private void Start()
	{
		m_messageText.canvasRenderer.SetAlpha(0f);
		m_messageIcon.canvasRenderer.SetAlpha(0f);
		m_messageCenterText.canvasRenderer.SetAlpha(0f);
		for (int i = 0; i < m_maxUnlockMessages; i++)
		{
			m_unlockMessages.Add(null);
		}
		ZRoutedRpc.instance.Register<int, string>("ShowMessage", RPC_ShowMessage);
	}

	private void Update()
	{
		if (Hud.IsUserHidden())
		{
			HideAll();
			return;
		}
		UpdateUnlockMsg(Time.deltaTime);
		UpdateMessage(Time.deltaTime);
		UpdateBiomeFound(Time.deltaTime);
	}

	private void HideAll()
	{
		for (int i = 0; i < m_maxUnlockMessages; i++)
		{
			if (m_unlockMessages[i] != null)
			{
				Object.Destroy(m_unlockMessages[i]);
				m_unlockMessages[i] = null;
			}
		}
		m_messageText.canvasRenderer.SetAlpha(0f);
		m_messageIcon.canvasRenderer.SetAlpha(0f);
		m_messageCenterText.canvasRenderer.SetAlpha(0f);
		if ((bool)m_biomeMsgInstance)
		{
			Object.Destroy(m_biomeMsgInstance);
			m_biomeMsgInstance = null;
		}
	}

	public void MessageAll(MessageType type, string text)
	{
		ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "ShowMessage", (int)type, text);
	}

	private void RPC_ShowMessage(long sender, int type, string text)
	{
		ShowMessage((MessageType)type, text);
	}

	public void ShowMessage(MessageType type, string text, int amount = 0, Sprite icon = null)
	{
		if (!Hud.IsUserHidden())
		{
			text = Localization.instance.Localize(text);
			switch (type)
			{
			case MessageType.TopLeft:
			{
				MsgData msgData = new MsgData();
				msgData.m_icon = icon;
				msgData.m_text = text;
				msgData.m_amount = amount;
				m_msgQeue.Enqueue(msgData);
				AddLog(text);
				break;
			}
			case MessageType.Center:
				m_messageCenterText.text = text;
				m_messageCenterText.canvasRenderer.SetAlpha(1f);
				m_messageCenterText.CrossFadeAlpha(0f, 4f, ignoreTimeScale: true);
				break;
			}
		}
	}

	private void UpdateMessage(float dt)
	{
		m_msgQueueTimer += dt;
		if (m_msgQeue.Count <= 0)
		{
			return;
		}
		MsgData msgData = m_msgQeue.Peek();
		bool flag = m_msgQueueTimer < 4f && msgData.m_text == currentMsg.m_text && msgData.m_icon == currentMsg.m_icon;
		if (m_msgQueueTimer >= 1f || flag)
		{
			MsgData msgData2 = m_msgQeue.Dequeue();
			m_messageText.text = msgData2.m_text;
			if (flag)
			{
				msgData2.m_amount += currentMsg.m_amount;
			}
			if (msgData2.m_amount > 1)
			{
				Text messageText = m_messageText;
				messageText.text = messageText.text + " x" + msgData2.m_amount;
			}
			m_messageText.canvasRenderer.SetAlpha(1f);
			m_messageText.CrossFadeAlpha(0f, 4f, ignoreTimeScale: true);
			if (msgData2.m_icon != null)
			{
				m_messageIcon.sprite = msgData2.m_icon;
				m_messageIcon.canvasRenderer.SetAlpha(1f);
				m_messageIcon.CrossFadeAlpha(0f, 4f, ignoreTimeScale: true);
			}
			else
			{
				m_messageIcon.canvasRenderer.SetAlpha(0f);
			}
			currentMsg = msgData2;
			m_msgQueueTimer = 0f;
		}
	}

	private void UpdateBiomeFound(float dt)
	{
		if (m_biomeMsgInstance != null && m_biomeMsgInstance.GetComponentInChildren<Animator>().GetCurrentAnimatorStateInfo(0).IsTag("done"))
		{
			Object.Destroy(m_biomeMsgInstance);
			m_biomeMsgInstance = null;
		}
		if (m_biomeFoundQueue.Count > 0 && m_biomeMsgInstance == null && m_msgQeue.Count == 0 && m_msgQueueTimer > 2f)
		{
			BiomeMessage biomeMessage = m_biomeFoundQueue.Dequeue();
			m_biomeMsgInstance = Object.Instantiate(m_biomeFoundPrefab, base.transform);
			Text component = Utils.FindChild(m_biomeMsgInstance.transform, "Title").GetComponent<Text>();
			string text = Localization.instance.Localize(biomeMessage.m_text);
			component.text = text;
			if (biomeMessage.m_playStinger && (bool)m_biomeFoundStinger)
			{
				Object.Instantiate(m_biomeFoundStinger);
			}
		}
	}

	public void ShowBiomeFoundMsg(string text, bool playStinger)
	{
		BiomeMessage biomeMessage = new BiomeMessage();
		biomeMessage.m_text = text;
		biomeMessage.m_playStinger = playStinger;
		m_biomeFoundQueue.Enqueue(biomeMessage);
	}

	public void QueueUnlockMsg(Sprite icon, string topic, string description)
	{
		UnlockMsg unlockMsg = new UnlockMsg();
		unlockMsg.m_icon = icon;
		unlockMsg.m_topic = Localization.instance.Localize(topic);
		unlockMsg.m_description = Localization.instance.Localize(description);
		m_unlockMsgQueue.Enqueue(unlockMsg);
		AddLog(topic + ":" + description);
		ZLog.Log("Queue unlock msg:" + topic + ":" + description);
	}

	private int GetFreeUnlockMsgSlot()
	{
		for (int i = 0; i < m_unlockMessages.Count; i++)
		{
			if (m_unlockMessages[i] == null)
			{
				return i;
			}
		}
		return -1;
	}

	private void UpdateUnlockMsg(float dt)
	{
		for (int i = 0; i < m_unlockMessages.Count; i++)
		{
			GameObject gameObject = m_unlockMessages[i];
			if (!(gameObject == null) && gameObject.GetComponentInChildren<Animator>().GetCurrentAnimatorStateInfo(0).IsTag("done"))
			{
				Object.Destroy(gameObject);
				m_unlockMessages[i] = null;
				break;
			}
		}
		if (m_unlockMsgQueue.Count > 0)
		{
			int freeUnlockMsgSlot = GetFreeUnlockMsgSlot();
			if (freeUnlockMsgSlot != -1)
			{
				Transform parent = base.transform;
				GameObject gameObject2 = Object.Instantiate(m_unlockMsgPrefab, parent);
				m_unlockMessages[freeUnlockMsgSlot] = gameObject2;
				RectTransform obj = gameObject2.transform as RectTransform;
				Vector3 vector = obj.anchoredPosition;
				vector.y -= m_maxUnlockMsgSpace * freeUnlockMsgSlot;
				obj.anchoredPosition = vector;
				UnlockMsg unlockMsg = m_unlockMsgQueue.Dequeue();
				Image component = obj.Find("UnlockMessage/icon_bkg/UnlockIcon").GetComponent<Image>();
				Text component2 = obj.Find("UnlockMessage/UnlockTitle").GetComponent<Text>();
				Text component3 = obj.Find("UnlockMessage/UnlockDescription").GetComponent<Text>();
				component.sprite = unlockMsg.m_icon;
				component2.text = unlockMsg.m_topic;
				component3.text = unlockMsg.m_description;
			}
		}
	}

	private void AddLog(string logText)
	{
		m_messageLog.Add(logText);
		while (m_messageLog.Count > m_maxLogMessages)
		{
			m_messageLog.RemoveAt(0);
		}
	}

	public List<string> GetLog()
	{
		return m_messageLog;
	}
}