Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SlimeSky.cs
Go to the documentation of this file.
6
8
9public class SlimeSky : CustomSky
10{
11 private struct Slime
12 {
13 private const int MAX_FRAMES = 4;
14
15 private const int FRAME_RATE = 6;
16
18
20
21 public float Depth;
22
23 public int FrameHeight;
24
25 public int FrameWidth;
26
27 public float Speed;
28
29 public bool Active;
30
31 private int _frame;
32
34 {
35 get
36 {
37 return _texture;
38 }
39 set
40 {
41 _texture = value;
42 FrameWidth = value.Width;
43 FrameHeight = value.Height / 4;
44 }
45 }
46
47 public int Frame
48 {
49 get
50 {
51 return _frame;
52 }
53 set
54 {
55 _frame = value % 24;
56 }
57 }
58
60 {
61 return new Rectangle(0, _frame / 6 * FrameHeight, FrameWidth, FrameHeight);
62 }
63 }
64
66
67 private Slime[] _slimes;
68
70
71 private int _slimesRemaining;
72
73 private bool _isActive;
74
75 private bool _isLeaving;
76
77 public override void OnLoad()
78 {
80 for (int i = 0; i < 4; i++)
81 {
82 _textures[i] = Main.Assets.Request<Texture2D>("Images/Misc/Sky_Slime_" + (i + 1), (AssetRequestMode)1);
83 }
85 }
86
87 private void GenerateSlimes()
88 {
89 _slimes = new Slime[Main.maxTilesY / 6];
90 for (int i = 0; i < _slimes.Length; i++)
91 {
92 int num = (int)((double)Main.screenPosition.Y * 0.7 - (double)Main.screenHeight);
93 int minValue = (int)((double)num - Main.worldSurface * 16.0);
94 _slimes[i].Position = new Vector2(_random.Next(0, Main.maxTilesX) * 16, _random.Next(minValue, num));
95 _slimes[i].Speed = 5f + 3f * (float)_random.NextDouble();
96 _slimes[i].Depth = (float)i / (float)_slimes.Length * 1.75f + 1.6f;
98 if (_random.Next(60) == 0)
99 {
101 _slimes[i].Speed = 6f + 3f * (float)_random.NextDouble();
102 _slimes[i].Depth += 0.5f;
103 }
104 else if (_random.Next(30) == 0)
105 {
107 _slimes[i].Speed = 6f + 2f * (float)_random.NextDouble();
108 }
109 _slimes[i].Active = true;
110 }
111 _slimesRemaining = _slimes.Length;
112 }
113
114 public override void Update(GameTime gameTime)
115 {
117 {
118 return;
119 }
120 for (int i = 0; i < _slimes.Length; i++)
121 {
122 if (!_slimes[i].Active)
123 {
124 continue;
125 }
126 _slimes[i].Frame++;
127 _slimes[i].Position.Y += _slimes[i].Speed;
128 if (!((double)_slimes[i].Position.Y > Main.worldSurface * 16.0))
129 {
130 continue;
131 }
132 if (!_isLeaving)
133 {
134 _slimes[i].Depth = (float)i / (float)_slimes.Length * 1.75f + 1.6f;
135 _slimes[i].Position = new Vector2(_random.Next(0, Main.maxTilesX) * 16, -100f);
137 _slimes[i].Speed = 5f + 3f * (float)_random.NextDouble();
138 if (_random.Next(60) == 0)
139 {
141 _slimes[i].Speed = 6f + 3f * (float)_random.NextDouble();
142 _slimes[i].Depth += 0.5f;
143 }
144 else if (_random.Next(30) == 0)
145 {
147 _slimes[i].Speed = 6f + 2f * (float)_random.NextDouble();
148 }
149 }
150 else
151 {
152 _slimes[i].Active = false;
154 }
155 }
156 if (_slimesRemaining == 0)
157 {
158 _isActive = false;
159 }
160 }
161
162 public override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
163 {
164 if (Main.screenPosition.Y > 10000f || Main.gameMenu)
165 {
166 return;
167 }
168 int num = -1;
169 int num2 = 0;
170 for (int i = 0; i < _slimes.Length; i++)
171 {
172 float depth = _slimes[i].Depth;
173 if (num == -1 && depth < maxDepth)
174 {
175 num = i;
176 }
177 if (depth <= minDepth)
178 {
179 break;
180 }
181 num2 = i;
182 }
183 if (num == -1)
184 {
185 return;
186 }
187 Vector2 vector = Main.screenPosition + new Vector2(Main.screenWidth >> 1, Main.screenHeight >> 1);
188 Rectangle rectangle = new Rectangle(-1000, -1000, 4000, 4000);
189 for (int j = num; j < num2; j++)
190 {
191 if (_slimes[j].Active)
192 {
193 Color color = new Color(Main.ColorOfTheSkies.ToVector4() * 0.9f + new Vector4(0.1f)) * 0.8f;
194 float num3 = 1f;
195 if (_slimes[j].Depth > 3f)
196 {
197 num3 = 0.6f;
198 }
199 else if ((double)_slimes[j].Depth > 2.5)
200 {
201 num3 = 0.7f;
202 }
203 else if (_slimes[j].Depth > 2f)
204 {
205 num3 = 0.8f;
206 }
207 else if ((double)_slimes[j].Depth > 1.5)
208 {
209 num3 = 0.9f;
210 }
211 num3 *= 0.8f;
212 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));
213 Vector2 vector2 = new Vector2(1f / _slimes[j].Depth, 0.9f / _slimes[j].Depth);
214 Vector2 position = _slimes[j].Position;
215 position = (position - vector) * vector2 + vector - Main.screenPosition;
216 position.X = (position.X + 500f) % 4000f;
217 if (position.X < 0f)
218 {
219 position.X += 4000f;
220 }
221 position.X -= 500f;
222 if (rectangle.Contains((int)position.X, (int)position.Y))
223 {
224 spriteBatch.Draw(_slimes[j].Texture, position, _slimes[j].GetSourceRectangle(), color, 0f, Vector2.Zero, vector2.X * 2f, SpriteEffects.None, 0f);
225 }
226 }
227 }
228 }
229
230 public override void Activate(Vector2 position, params object[] args)
231 {
233 _isActive = true;
234 _isLeaving = false;
235 }
236
237 public override void Deactivate(params object[] args)
238 {
239 _isLeaving = true;
240 }
241
242 public override void Reset()
243 {
244 _isActive = false;
245 }
246
247 public override bool IsActive()
248 {
249 return _isActive;
250 }
251}
void Draw(Texture2D texture, Vector2 position, Color color)
override void Draw(SpriteBatch spriteBatch, float minDepth, float maxDepth)
Definition SlimeSky.cs:162
override void Update(GameTime gameTime)
Definition SlimeSky.cs:114
override void Deactivate(params object[] args)
Definition SlimeSky.cs:237
override void Activate(Vector2 position, params object[] args)
Definition SlimeSky.cs:230
Asset< Texture2D >[] _textures
Definition SlimeSky.cs:65
static Microsoft.Xna.Framework.Color ColorOfTheSkies
Definition Main.cs:2577
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 IAssetRepository Assets
Definition Main.cs:209
static bool gameMenu
Definition Main.cs:1926
static bool gamePaused
Definition Main.cs:1072
bool Contains(int x, int y)
Definition Rectangle.cs:92