Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StardustSky.cs
Go to the documentation of this file.
1using System;
7
9
10public class StardustSky : CustomSky
11{
12 private struct Star
13 {
15
16 public float Depth;
17
18 public int TextureIndex;
19
20 public float SinOffset;
21
22 public float AlphaFrequency;
23
24 public float AlphaAmplitude;
25 }
26
28
30
32
34
35 private bool _isActive;
36
37 private Star[] _stars;
38
39 private float _fadeOpacity;
40
41 public override void OnLoad()
42 {
43 _planetTexture = Main.Assets.Request<Texture2D>("Images/Misc/StarDustSky/Planet", (AssetRequestMode)1);
44 _bgTexture = Main.Assets.Request<Texture2D>("Images/Misc/StarDustSky/Background", (AssetRequestMode)1);
46 for (int i = 0; i < _starTextures.Length; i++)
47 {
48 _starTextures[i] = Main.Assets.Request<Texture2D>("Images/Misc/StarDustSky/Star " + i, (AssetRequestMode)1);
49 }
50 }
51
52 public override void Update(GameTime gameTime)
53 {
54 if (_isActive)
55 {
56 _fadeOpacity = Math.Min(1f, 0.01f + _fadeOpacity);
57 }
58 else
59 {
60 _fadeOpacity = Math.Max(0f, _fadeOpacity - 0.01f);
61 }
62 }
63
64 public override Color OnTileColor(Color inColor)
65 {
66 return new Color(Vector4.Lerp(inColor.ToVector4(), Vector4.One, _fadeOpacity * 0.5f));
67 }
68
69 public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
70 {
71 if (maxDepth >= float.MaxValue && minDepth < float.MaxValue)
72 {
74 spriteBatch.Draw(_bgTexture.Value, new Rectangle(0, Math.Max(0, (int)((Main.worldSurface * 16.0 - (double)Main.screenPosition.Y - 2400.0) * 0.10000000149011612)), Main.screenWidth, Main.screenHeight), Color.White * Math.Min(1f, (Main.screenPosition.Y - 800f) / 1000f * _fadeOpacity));
75 Vector2 vector = new Vector2(Main.screenWidth >> 1, Main.screenHeight >> 1);
76 Vector2 vector2 = 0.01f * (new Vector2((float)Main.maxTilesX * 8f, (float)Main.worldSurface / 2f) - Main.screenPosition);
77 spriteBatch.Draw(_planetTexture.Value, vector + new Vector2(-200f, -200f) + vector2, null, Color.White * 0.9f * _fadeOpacity, 0f, new Vector2(_planetTexture.Width() >> 1, _planetTexture.Height() >> 1), 1f, SpriteEffects.None, 1f);
78 }
79 int num = -1;
80 int num2 = 0;
81 for (int i = 0; i < _stars.Length; i++)
82 {
83 float depth = _stars[i].Depth;
84 if (num == -1 && depth < maxDepth)
85 {
86 num = i;
87 }
88 if (depth <= minDepth)
89 {
90 break;
91 }
92 num2 = i;
93 }
94 if (num == -1)
95 {
96 return;
97 }
98 float num3 = Math.Min(1f, (Main.screenPosition.Y - 1000f) / 1000f);
99 Vector2 vector3 = Main.screenPosition + new Vector2(Main.screenWidth >> 1, Main.screenHeight >> 1);
100 Rectangle rectangle = new Rectangle(-1000, -1000, 4000, 4000);
101 for (int j = num; j < num2; j++)
102 {
103 Vector2 vector4 = new Vector2(1f / _stars[j].Depth, 1.1f / _stars[j].Depth);
104 Vector2 position = (_stars[j].Position - vector3) * vector4 + vector3 - Main.screenPosition;
105 if (rectangle.Contains((int)position.X, (int)position.Y))
106 {
108 float num4 = (float)Math.Sin(_stars[j].AlphaFrequency * Main.GlobalTimeWrappedHourly * 5f + _stars[j].SinOffset) * 0.1f - 0.1f;
109 value = MathHelper.Clamp(value, 0f, 1f);
111 spriteBatch.Draw(value2, position, null, Color.White * num3 * value * 0.8f * (1f - num4) * _fadeOpacity, 0f, new Vector2(value2.Width >> 1, value2.Height >> 1), (vector4.X * 0.5f + 0.5f) * (value * 0.3f + 0.7f), SpriteEffects.None, 0f);
112 }
113 }
114 }
115
116 public override float GetCloudAlpha()
117 {
118 return (1f - _fadeOpacity) * 0.3f + 0.7f;
119 }
120
121 public override void Activate(Vector2 position, params object[] args)
122 {
123 _fadeOpacity = 0.002f;
124 _isActive = true;
125 int num = 200;
126 int num2 = 10;
127 _stars = new Star[num * num2];
128 int num3 = 0;
129 for (int i = 0; i < num; i++)
130 {
131 float num4 = (float)i / (float)num;
132 for (int j = 0; j < num2; j++)
133 {
134 float num5 = (float)j / (float)num2;
135 _stars[num3].Position.X = num4 * (float)Main.maxTilesX * 16f;
136 _stars[num3].Position.Y = num5 * ((float)Main.worldSurface * 16f + 2000f) - 1000f;
137 _stars[num3].Depth = _random.NextFloat() * 8f + 1.5f;
139 _stars[num3].SinOffset = _random.NextFloat() * 6.28f;
140 _stars[num3].AlphaAmplitude = _random.NextFloat() * 5f;
141 _stars[num3].AlphaFrequency = _random.NextFloat() + 1f;
142 num3++;
143 }
144 }
146 }
147
148 private int SortMethod(Star meteor1, Star meteor2)
149 {
150 return meteor2.Depth.CompareTo(meteor1.Depth);
151 }
152
153 public override void Deactivate(params object[] args)
154 {
155 _isActive = false;
156 }
157
158 public override void Reset()
159 {
160 _isActive = false;
161 }
162
163 public override bool IsActive()
164 {
165 if (!_isActive)
166 {
167 return _fadeOpacity > 0.001f;
168 }
169 return true;
170 }
171}
void Draw(Texture2D texture, Vector2 position, Color color)
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
static void Sort(Array array)
Definition Array.cs:2329
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static double Sin(double a)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
override void Deactivate(params object[] args)
override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
override Color OnTileColor(Color inColor)
int SortMethod(Star meteor1, Star meteor2)
override void Update(GameTime gameTime)
override void Activate(Vector2 position, params object[] args)
static Asset< Texture2D > BlackTile
static double worldSurface
Definition Main.cs:1272
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 IAssetRepository Assets
Definition Main.cs:209
static float GlobalTimeWrappedHourly
Definition Main.cs:405
bool Contains(int x, int y)
Definition Rectangle.cs:92
static Vector4 Lerp(Vector4 value1, Vector4 value2, float amount)
Definition Vector4.cs:277