Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MartianSky.cs
Go to the documentation of this file.
1using System;
6
8
9public class MartianSky : CustomSky
10{
11 private abstract class IUfoController
12 {
13 public abstract void InitializeUfo(ref Ufo ufo);
14
15 public abstract bool Update(ref Ufo ufo);
16 }
17
19 {
20 private Vector2 _speed;
21
22 private int _ticks;
23
24 private int _maxTicks;
25
26 public override void InitializeUfo(ref Ufo ufo)
27 {
28 ufo.Position.X = (float)(Ufo.Random.NextDouble() * (double)(Main.maxTilesX << 4));
29 ufo.Position.Y = (float)(Ufo.Random.NextDouble() * 5000.0);
30 ufo.Opacity = 0f;
31 float num = (float)Ufo.Random.NextDouble() * 5f + 10f;
32 double num2 = Ufo.Random.NextDouble() * 0.6000000238418579 - 0.30000001192092896;
33 ufo.Rotation = (float)num2;
34 if (Ufo.Random.Next(2) == 0)
35 {
36 num2 += 3.1415927410125732;
37 }
38 _speed = new Vector2((float)Math.Cos(num2) * num, (float)Math.Sin(num2) * num);
39 _ticks = 0;
40 _maxTicks = Ufo.Random.Next(400, 500);
41 }
42
43 public override bool Update(ref Ufo ufo)
44 {
45 if (_ticks < 10)
46 {
47 ufo.Opacity += 0.1f;
48 }
49 else if (_ticks > _maxTicks - 10)
50 {
51 ufo.Opacity -= 0.1f;
52 }
53 ufo.Position += _speed;
54 if (_ticks == _maxTicks)
55 {
56 return false;
57 }
58 _ticks++;
59 return true;
60 }
61 }
62
64 {
65 private int _ticks;
66
67 private int _maxTicks;
68
69 public override void InitializeUfo(ref Ufo ufo)
70 {
71 ufo.Position.X = (float)(Ufo.Random.NextDouble() * (double)(Main.maxTilesX << 4));
72 ufo.Position.Y = (float)(Ufo.Random.NextDouble() * 5000.0);
73 ufo.Opacity = 0f;
74 ufo.Rotation = 0f;
75 _ticks = 0;
76 _maxTicks = Ufo.Random.Next(120, 240);
77 }
78
79 public override bool Update(ref Ufo ufo)
80 {
81 if (_ticks < 10)
82 {
83 ufo.Opacity += 0.1f;
84 }
85 else if (_ticks > _maxTicks - 10)
86 {
87 ufo.Opacity -= 0.1f;
88 }
89 if (_ticks == _maxTicks)
90 {
91 return false;
92 }
93 _ticks++;
94 return true;
95 }
96 }
97
98 private struct Ufo
99 {
100 private const int MAX_FRAMES = 3;
101
102 private const int FRAME_RATE = 4;
103
104 public static UnifiedRandom Random = new UnifiedRandom();
105
106 private int _frame;
107
109
111
113
115
116 public int FrameHeight;
117
118 public int FrameWidth;
119
120 public float Depth;
121
122 public float Scale;
123
124 public float Opacity;
125
126 public bool IsActive;
127
128 public float Rotation;
129
130 public int Frame
131 {
132 get
133 {
134 return _frame;
135 }
136 set
137 {
138 _frame = value % 12;
139 }
140 }
141
143 {
144 get
145 {
146 return _texture;
147 }
148 set
149 {
150 _texture = value;
151 FrameWidth = value.Width;
152 FrameHeight = value.Height / 3;
153 }
154 }
155
157 {
158 get
159 {
160 return _controller;
161 }
162 set
163 {
165 value.InitializeUfo(ref this);
166 }
167 }
168
169 public Ufo(Texture2D texture, float depth = 1f)
170 {
171 _frame = 0;
173 _texture = texture;
174 Depth = depth;
175 Scale = 1f;
176 FrameWidth = texture.Width;
177 FrameHeight = texture.Height / 3;
178 GlowTexture = null;
179 Opacity = 0f;
180 Rotation = 0f;
181 IsActive = false;
182 _controller = null;
183 }
184
186 {
187 return new Rectangle(0, _frame / 4 * FrameHeight, FrameWidth, FrameHeight);
188 }
189
190 public bool Update()
191 {
192 return Controller.Update(ref this);
193 }
194
195 public void AssignNewBehavior()
196 {
197 switch (Random.Next(2))
198 {
199 case 0:
200 Controller = new ZipBehavior();
201 break;
202 case 1:
204 break;
205 }
206 }
207 }
208
209 private Ufo[] _ufos;
210
212
213 private int _maxUfos;
214
215 private bool _active;
216
217 private bool _leaving;
218
219 private int _activeUfos;
220
221 public override void Update(GameTime gameTime)
222 {
224 {
225 return;
226 }
227 int num = _activeUfos;
228 for (int i = 0; i < _ufos.Length; i++)
229 {
230 Ufo ufo = _ufos[i];
231 if (ufo.IsActive)
232 {
233 ufo.Frame++;
234 if (!ufo.Update())
235 {
236 if (!_leaving)
237 {
238 ufo.AssignNewBehavior();
239 }
240 else
241 {
242 ufo.IsActive = false;
243 num--;
244 }
245 }
246 }
247 _ufos[i] = ufo;
248 }
249 if (!_leaving && num != _maxUfos)
250 {
251 _ufos[num].IsActive = true;
252 _ufos[num++].AssignNewBehavior();
253 }
254 _active = !_leaving || num != 0;
255 _activeUfos = num;
256 }
257
258 public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
259 {
260 if (Main.screenPosition.Y > 10000f)
261 {
262 return;
263 }
264 int num = -1;
265 int num2 = 0;
266 for (int i = 0; i < _ufos.Length; i++)
267 {
268 float depth = _ufos[i].Depth;
269 if (num == -1 && depth < maxDepth)
270 {
271 num = i;
272 }
273 if (depth <= minDepth)
274 {
275 break;
276 }
277 num2 = i;
278 }
279 if (num == -1)
280 {
281 return;
282 }
283 Color color = new Color(Main.ColorOfTheSkies.ToVector4() * 0.9f + new Vector4(0.1f));
284 Vector2 vector = Main.screenPosition + new Vector2(Main.screenWidth >> 1, Main.screenHeight >> 1);
285 Rectangle rectangle = new Rectangle(-1000, -1000, 4000, 4000);
286 for (int j = num; j < num2; j++)
287 {
288 Vector2 vector2 = new Vector2(1f / _ufos[j].Depth, 0.9f / _ufos[j].Depth);
289 Vector2 position = _ufos[j].Position;
290 position = (position - vector) * vector2 + vector - Main.screenPosition;
291 if (_ufos[j].IsActive && rectangle.Contains((int)position.X, (int)position.Y))
292 {
293 spriteBatch.Draw(_ufos[j].Texture, position, _ufos[j].GetSourceRectangle(), color * _ufos[j].Opacity, _ufos[j].Rotation, Vector2.Zero, vector2.X * 5f * _ufos[j].Scale, SpriteEffects.None, 0f);
294 if (_ufos[j].GlowTexture != null)
295 {
296 spriteBatch.Draw(_ufos[j].GlowTexture, position, _ufos[j].GetSourceRectangle(), Color.White * _ufos[j].Opacity, _ufos[j].Rotation, Vector2.Zero, vector2.X * 5f * _ufos[j].Scale, SpriteEffects.None, 0f);
297 }
298 }
299 }
300 }
301
302 private void GenerateUfos()
303 {
304 float num = (float)Main.maxTilesX / 4200f;
305 _maxUfos = (int)(256f * num);
306 _ufos = new Ufo[_maxUfos];
307 int num2 = _maxUfos >> 4;
308 for (int i = 0; i < num2; i++)
309 {
310 _ = (float)i / (float)num2;
311 _ufos[i] = new Ufo(TextureAssets.Extra[5].Value, (float)Main.rand.NextDouble() * 4f + 6.6f);
313 }
314 for (int j = num2; j < _ufos.Length; j++)
315 {
316 _ = (float)(j - num2) / (float)(_ufos.Length - num2);
317 _ufos[j] = new Ufo(TextureAssets.Extra[6].Value, (float)Main.rand.NextDouble() * 5f + 1.6f);
318 _ufos[j].Scale = 0.5f;
320 }
321 }
322
323 public override void Activate(Vector2 position, params object[] args)
324 {
325 _activeUfos = 0;
326 GenerateUfos();
327 Array.Sort(_ufos, (Ufo ufo1, Ufo ufo2) => ufo2.Depth.CompareTo(ufo1.Depth));
328 _active = true;
329 _leaving = false;
330 }
331
332 public override void Deactivate(params object[] args)
333 {
334 _leaving = true;
335 }
336
337 public override bool IsActive()
338 {
339 return _active;
340 }
341
342 public override void Reset()
343 {
344 _active = false;
345 }
346}
void Draw(Texture2D texture, Vector2 position, Color color)
static void Sort(Array array)
Definition Array.cs:2329
static double Cos(double d)
static double Sin(double a)
virtual int Next()
Definition Random.cs:651
override void Deactivate(params object[] args)
override void Activate(Vector2 position, params object[] args)
override void Update(GameTime gameTime)
override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
static Asset< Texture2D >[] GlowMask
static Asset< Texture2D >[] Extra
static Microsoft.Xna.Framework.Color ColorOfTheSkies
Definition Main.cs:2577
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 UnifiedRandom rand
Definition Main.cs:1387
static int screenWidth
Definition Main.cs:1719
static bool gamePaused
Definition Main.cs:1072
bool Contains(int x, int y)
Definition Rectangle.cs:92
Ufo(Texture2D texture, float depth=1f)