Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LanternSky.cs
Go to the documentation of this file.
1using System;
8
10
11public class LanternSky : CustomSky
12{
13 private struct Lantern
14 {
15 private const int MAX_FRAMES_X = 3;
16
17 public int Variant;
18
19 public int TimeUntilFloat;
20
22
24
26
27 public float Depth;
28
29 public float Rotation;
30
31 public int FrameHeight;
32
33 public int FrameWidth;
34
35 public float Speed;
36
37 public bool Active;
38
40 {
41 get
42 {
43 return _texture;
44 }
45 set
46 {
48 FrameWidth = value.Width / 3;
49 FrameHeight = value.Height;
50 }
51 }
52
53 public float FloatAdjustedSpeed => Speed * ((float)TimeUntilFloat / (float)TimeUntilFloatMax);
54
59 }
60
61 private bool _active;
62
63 private bool _leaving;
64
65 private float _opacity;
66
68
69 private Lantern[] _lanterns;
70
72
73 private int _lanternsDrawing;
74
75 private const float slowDown = 0.5f;
76
77 public override void OnLoad()
78 {
80 GenerateLanterns(onlyMissing: false);
81 }
82
83 private void GenerateLanterns(bool onlyMissing)
84 {
85 if (!onlyMissing)
86 {
87 _lanterns = new Lantern[Main.maxTilesY / 4];
88 }
89 for (int i = 0; i < _lanterns.Length; i++)
90 {
91 if (!onlyMissing || !_lanterns[i].Active)
92 {
93 int num = (int)((double)Main.screenPosition.Y * 0.7 - (double)Main.screenHeight);
94 int minValue = (int)((double)num - Main.worldSurface * 16.0);
95 _lanterns[i].Position = new Vector2(_random.Next(0, Main.maxTilesX) * 16, _random.Next(minValue, num));
96 ResetLantern(i);
97 _lanterns[i].Active = true;
98 }
99 }
101 }
102
103 public void ResetLantern(int i)
104 {
105 _lanterns[i].Depth = (1f - (float)i / (float)_lanterns.Length) * 4.4f + 1.6f;
106 _lanterns[i].Speed = -1.5f - 2.5f * (float)_random.NextDouble();
109 _lanterns[i].TimeUntilFloat = (int)((float)(2000 + _random.Next(1200)) * 2f);
111 }
112
113 public override void Update(GameTime gameTime)
114 {
116 {
117 return;
118 }
119 _opacity = Utils.Clamp(_opacity + (float)LanternNight.LanternsUp.ToDirectionInt() * 0.01f, 0f, 1f);
120 for (int i = 0; i < _lanterns.Length; i++)
121 {
122 if (!_lanterns[i].Active)
123 {
124 continue;
125 }
126 float num = Main.windSpeedCurrent;
127 if (num == 0f)
128 {
129 num = 0.1f;
130 }
131 float num2 = (float)Math.Sin(_lanterns[i].Position.X / 120f) * 0.5f;
132 _lanterns[i].Position.Y += num2 * 0.5f;
134 _lanterns[i].Position.X += (0.1f + num) * (3f - _lanterns[i].Speed) * 0.5f * ((float)i / (float)_lanterns.Length + 1.5f) / 2.5f;
135 _lanterns[i].Rotation = num2 * (float)((!(num < 0f)) ? 1 : (-1)) * 0.5f;
136 _lanterns[i].TimeUntilFloat = Math.Max(0, _lanterns[i].TimeUntilFloat - 1);
137 if (_lanterns[i].Position.Y < 300f)
138 {
139 if (!_leaving)
140 {
141 ResetLantern(i);
142 _lanterns[i].Position = new Vector2(_random.Next(0, Main.maxTilesX) * 16, (float)Main.worldSurface * 16f + 1600f);
143 }
144 else
145 {
146 _lanterns[i].Active = false;
148 }
149 }
150 }
151 _active = true;
152 }
153
154 public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
155 {
156 if (Main.gameMenu && _active)
157 {
158 _active = false;
159 _leaving = false;
160 for (int i = 0; i < _lanterns.Length; i++)
161 {
162 _lanterns[i].Active = false;
163 }
164 }
165 if ((double)Main.screenPosition.Y > Main.worldSurface * 16.0 || Main.gameMenu || _opacity <= 0f)
166 {
167 return;
168 }
169 int num = -1;
170 int num2 = 0;
171 for (int j = 0; j < _lanterns.Length; j++)
172 {
173 float depth = _lanterns[j].Depth;
174 if (num == -1 && depth < maxDepth)
175 {
176 num = j;
177 }
178 if (depth <= minDepth)
179 {
180 break;
181 }
182 num2 = j;
183 }
184 if (num == -1)
185 {
186 return;
187 }
188 Vector2 vector = Main.screenPosition + new Vector2(Main.screenWidth >> 1, Main.screenHeight >> 1);
189 Rectangle rectangle = new Rectangle(-1000, -1000, 4000, 4000);
190 for (int k = num; k < num2; k++)
191 {
192 if (_lanterns[k].Active)
193 {
194 Color color = new Color(250, 120, 60, 120);
195 float num3 = 1f;
196 if (_lanterns[k].Depth > 5f)
197 {
198 num3 = 0.3f;
199 }
200 else if ((double)_lanterns[k].Depth > 4.5)
201 {
202 num3 = 0.4f;
203 }
204 else if (_lanterns[k].Depth > 4f)
205 {
206 num3 = 0.5f;
207 }
208 else if ((double)_lanterns[k].Depth > 3.5)
209 {
210 num3 = 0.6f;
211 }
212 else if (_lanterns[k].Depth > 3f)
213 {
214 num3 = 0.7f;
215 }
216 else if ((double)_lanterns[k].Depth > 2.5)
217 {
218 num3 = 0.8f;
219 }
220 else if (_lanterns[k].Depth > 2f)
221 {
222 num3 = 0.9f;
223 }
224 color = new Color((int)((float)(int)color.R * num3), (int)((float)(int)color.G * num3), (int)((float)(int)color.B * num3), (int)((float)(int)color.A * num3));
225 Vector2 vector2 = new Vector2(1f / _lanterns[k].Depth, 0.9f / _lanterns[k].Depth);
226 vector2 *= 1.2f;
227 Vector2 position = _lanterns[k].Position;
228 position = (position - vector) * vector2 + vector - Main.screenPosition;
229 position.X = (position.X + 500f) % 4000f;
230 if (position.X < 0f)
231 {
232 position.X += 4000f;
233 }
234 position.X -= 500f;
235 if (rectangle.Contains((int)position.X, (int)position.Y))
236 {
237 DrawLantern(spriteBatch, _lanterns[k], color, vector2, position, num3);
238 }
239 }
240 }
241 }
242
243 private void DrawLantern(SpriteBatch spriteBatch, Lantern lantern, Color opacity, Vector2 depthScale, Vector2 position, float alpha)
244 {
245 float y = (Main.GlobalTimeWrappedHourly % 6f / 6f * ((float)Math.PI * 2f)).ToRotationVector2().Y;
246 float num = y * 0.2f + 0.8f;
247 Color color = new Color(255, 255, 255, 0) * _opacity * alpha * num * 0.4f;
248 for (float num2 = 0f; num2 < 1f; num2 += 1f / 3f)
249 {
250 Vector2 vector = new Vector2(0f, 2f).RotatedBy((float)Math.PI * 2f * num2 + lantern.Rotation) * y;
251 spriteBatch.Draw(lantern.Texture, position + vector, lantern.GetSourceRectangle(), color, lantern.Rotation, lantern.GetSourceRectangle().Size() / 2f, depthScale.X * 2f, SpriteEffects.None, 0f);
252 }
253 spriteBatch.Draw(lantern.Texture, position, lantern.GetSourceRectangle(), opacity * _opacity, lantern.Rotation, lantern.GetSourceRectangle().Size() / 2f, depthScale.X * 2f, SpriteEffects.None, 0f);
254 }
255
256 public override void Activate(Vector2 position, params object[] args)
257 {
258 if (_active)
259 {
260 _leaving = false;
261 GenerateLanterns(onlyMissing: true);
262 }
263 else
264 {
265 GenerateLanterns(onlyMissing: false);
266 _active = true;
267 _leaving = false;
268 }
269 }
270
271 public override void Deactivate(params object[] args)
272 {
273 _leaving = true;
274 }
275
276 public override bool IsActive()
277 {
278 return _active;
279 }
280
281 public override void Reset()
282 {
283 _active = false;
284 }
285}
void Draw(Texture2D texture, Vector2 position, Color color)
static double Sin(double a)
const double PI
Definition Math.cs:16
static byte Max(byte val1, byte val2)
Definition Math.cs:738
override void Activate(Vector2 position, params object[] args)
override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
override void Deactivate(params object[] args)
void GenerateLanterns(bool onlyMissing)
Definition LanternSky.cs:83
override void Update(GameTime gameTime)
void DrawLantern(SpriteBatch spriteBatch, Lantern lantern, Color opacity, Vector2 depthScale, Vector2 position, float alpha)
static Asset< Texture2D >[] Extra
static float windSpeedCurrent
Definition Main.cs:1360
static double worldSurface
Definition Main.cs:1272
static bool hasFocus
Definition Main.cs:1781
static int screenHeight
Definition Main.cs:1721
static Vector2 screenPosition
Definition Main.cs:1715
static int maxTilesX
Definition Main.cs:1114
static int screenWidth
Definition Main.cs:1719
static bool gameMenu
Definition Main.cs:1926
static bool gamePaused
Definition Main.cs:1072
bool Contains(int x, int y)
Definition Rectangle.cs:92