-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecretUnlockDragon.cs
247 lines (223 loc) · 6.7 KB
/
SecretUnlockDragon.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
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
using System.Collections.Generic;
using Landfall.TABS;
using Landfall.TABS.GameState;
using TFBGames;
using UnityEngine;
using UnityEngine.Events;
using Landfall.TABS.Workshop;
public class SecretUnlockDragon : GameStateListener
{
[SerializeField]
private string m_secret_key = "";
public TABSCampaignAsset m_unlockCampaign;
[SerializeField]
private float m_distanceToUnlock = 5f;
private RotationShake m_rotationShake;
private Rigidbody m_secretObject;
private float m_lookValue;
private float m_unlockValue;
public AudioClip hitClip;
private AudioSource loopSource;
private Transform m_mainCamTransform;
public UnityEvent unlockEvent;
public UnityEvent hideEvent;
private bool done;
public Color glowColor;
public GameObject unlockSparkEffect;
protected override void Awake()
{
base.Awake();
if (m_mainCamTransform == null)
{
OnEnterNewScene();
}
}
private void Update()
{
if (!(m_mainCamTransform != null) || !m_secretObject || done)
{
return;
}
loopSource.volume = Mathf.Pow(m_unlockValue * 0.25f, 1.3f);
var pitch = 1f + 1f * m_unlockValue;
loopSource.pitch = pitch >= 0f ? pitch : 0f;
if (m_unlockValue > 0f || m_lookValue > 10f)
{
SetColor();
}
float num = Vector3.Distance(m_secretObject.worldCenterOfMass, m_mainCamTransform.position);
if (num > m_distanceToUnlock)
{
m_unlockValue -= Time.unscaledDeltaTime * 0.2f;
return;
}
float num2 = Vector3.Angle(m_mainCamTransform.forward, m_secretObject.worldCenterOfMass - m_mainCamTransform.position);
m_lookValue = 1000f / (num * num2);
if (m_lookValue > 8f)
{
float num3 = 0.2f;
m_unlockValue += num3 * Time.unscaledDeltaTime;
UnlockProgressFeedback();
if (m_unlockValue > 1f)
{
UnlockSecret();
}
}
else
{
m_unlockValue -= Time.unscaledDeltaTime * 0.2f;
}
}
private void UnlockProgressFeedback()
{
if ((bool)m_rotationShake)
{
if (m_unlockValue <= 0f)
{
m_rotationShake.AddForce(Random.onUnitSphere * 2f);
m_unlockValue = 0f;
}
m_rotationShake.enabled = true;
m_rotationShake.AddForce(Random.onUnitSphere * m_unlockValue * Time.deltaTime * 50f);
}
}
private void SetColor()
{
m_unlockValue = Mathf.Clamp(m_unlockValue, 0f, float.PositiveInfinity);
Renderer[] componentsInChildren = m_secretObject.GetComponentsInChildren<Renderer>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
Material[] materials = componentsInChildren[i].materials;
for (int j = 0; j < materials.Length; j++)
{
if (materials[j].HasProperty("_EmissionColor"))
{
materials[j].EnableKeyword("_EMISSION");
materials[j].SetColor("_EmissionColor", glowColor * m_unlockValue * 2f);
}
}
componentsInChildren[i].materials = materials;
}
}
private void UnlockSecret()
{
if (!base.enabled)
{
return;
}
if ((bool)ScreenShake.Instance)
{
ScreenShake.Instance.AddForce(Vector3.up * 8f, m_secretObject.transform.position);
}
if ((bool)unlockSparkEffect)
{
GameObject gameObject = Object.Instantiate(unlockSparkEffect, m_secretObject.transform.position, m_secretObject.transform.rotation);
gameObject.AddComponent<RemoveAfterSeconds>().seconds = 5f;
MeshRenderer componentInChildren = m_secretObject.GetComponentInChildren<MeshRenderer>();
if ((bool)componentInChildren)
{
ParticleSystem.ShapeModule shape = gameObject.GetComponent<ParticleSystem>().shape;
shape.meshRenderer = componentInChildren;
}
}
m_secretObject.gameObject.SetActive(false);
unlockEvent?.Invoke();
loopSource.Stop();
loopSource.volume = 1f;
loopSource.PlayOneShot(hitClip);
done = true;
List<Faction> factioniests = new List<Faction>();
foreach (var facts in LandfallUnitDatabase.GetDatabase().FactionList)
{
if (facts.Entity.Name.Contains("Medieval"))
{
factioniests.Add(LandfallUnitDatabase.GetDatabase().GetFactionByGUID(facts.Entity.GUID));
}
if (facts.Entity.Name.Contains("Farmer"))
{
factioniests.Add(LandfallUnitDatabase.GetDatabase().GetFactionByGUID(facts.Entity.GUID));
}
}
m_unlockCampaign.LevelsInCampaign[0].AllowedFactions = factioniests.ToArray();
m_unlockCampaign.LevelsInCampaign[0].MapAsset = LandfallUnitDatabase.GetDatabase().GetMap(new DatabaseID(-1, 835120575));
CampaignPlayerDataHolder.StartedPlayingNewCampaign(m_unlockCampaign, 0);
TABSSceneManager.LoadCampaign();
LandfallUnitDatabase.GetDatabase().AddCampaignWithID(m_unlockCampaign);
if (string.IsNullOrWhiteSpace(m_secret_key))
{
return;
}
ServiceLocator.GetService<ISaveLoaderService>().UnlockSecret(m_secret_key);
CheckAchievements();
//ServiceLocator.GetService<ModalPanel>().OpenUnlockPanel(m_secretDescription, m_secretIcon);
//if (list != null && list.Count > 0)
//{
// foreach (SecretUnlockCondition item in list)
// {
// ServiceLocator.GetService<ModalPanel>().OpenUnlockPanel(item.m_unlockDescription, item.m_unlockImage);
// }
//}
PlacementUI placementUI = Object.FindObjectOfType<PlacementUI>();
if (placementUI != null)
{
placementUI.RedrawUI(m_secret_key);
}
}
public override void OnEnterNewScene()
{
base.OnEnterNewScene();
loopSource = GetComponent<AudioSource>();
if ((bool)loopSource)
{
loopSource.volume = 0f;
}
m_rotationShake = GetComponentInChildren<RotationShake>();
m_secretObject = GetComponentInChildren<Rigidbody>();
if ((bool)m_secretObject)
{
m_secretObject.isKinematic = true;
}
//if (!string.IsNullOrWhiteSpace(m_secret_key) && ServiceLocator.GetService<ISaveLoaderService>().HasUnlockedSecret(m_secret_key))
//{
// if ((bool)m_secretObject)
// {
// m_secretObject.gameObject.SetActive(false);
// }
// base.enabled = false;
// hideEvent?.Invoke();
//}
MainCam mainCam = ServiceLocator.GetService<PlayerCamerasManager>()?.GetMainCam(TFBGames.Player.One);
m_mainCamTransform = ((mainCam != null) ? mainCam.transform : null);
}
public override void OnEnterPlacementState()
{
}
public override void OnEnterBattleState()
{
}
private void CheckAchievements()
{
AchievementService service = ServiceLocator.GetService<AchievementService>();
ISaveLoaderService secretService = ServiceLocator.GetService<ISaveLoaderService>();
if (HasUnlockedFaction(874593522))
{
service.UnlockAchievement("UNLOCKED_ALL_SECRET");
}
if (HasUnlockedFaction(673578412))
{
service.UnlockAchievement("UNLOCKED_ALL_LEGACY");
}
bool HasUnlockedFaction(int factionId)
{
UnitBlueprint[] units = LandfallUnitDatabase.GetDatabase().GetFactionByGUID(new DatabaseID(-1, factionId)).Units;
foreach (UnitBlueprint unitBlueprint in units)
{
if (!string.IsNullOrEmpty(unitBlueprint.Entity.UnlockKey) && !secretService.HasUnlockedSecret(unitBlueprint.Entity.UnlockKey))
{
return false;
}
}
return true;
}
}
}