-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCustomWindSnow.cs
More file actions
177 lines (160 loc) · 5.87 KB
/
Copy pathCustomWindSnow.cs
File metadata and controls
177 lines (160 loc) · 5.87 KB
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
using Celeste;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace vitmod
{
public class CustomWindSnow : Backdrop
{
public CustomWindSnow(string colors, string alphas, int amount, float speedX, float speedY, bool ignoreWind) : base()
{
this.colors = new List<Color>();
string[] colorStrings = colors.Split(',');
foreach(string color in colorStrings)
{
this.colors.Add(Calc.HexToColor(color));
}
this.alphas = new List<float>();
string[] alphaStrings = alphas.Split(',');
foreach(string alpha in alphaStrings)
{
this.alphas.Add(float.Parse(alpha));
}
this.amount = amount;
addSpeed = new Vector2(speedX * 100f, speedY * 100f);
this.ignoreWind = ignoreWind;
Color = Color.White;
CameraOffset = Vector2.Zero;
scale = Vector2.One;
rotation = 0f;
visibleFade = 1f;
positions = new Vector2[amount];
particleColors = new int[amount];
Random rng = new Random();
for (int i = 0; i < positions.Length; i++)
{
positions[i] = Calc.Random.Range(new Vector2(0f, 0f), new Vector2(640f, 360f));
particleColors[i] = rng.Next(0, this.colors.Count);
}
sines = new SineWave[amount / 15];
for (int i = 0; i < sines.Length; i++)
{
sines[i] = new SineWave(Calc.Random.Range(0.8f, 1.2f), 0f);
sines[i].Randomize();
}
}
public override void Update(Scene scene)
{
base.Update(scene);
Level level = scene as Level;
visibleFade = Calc.Approach(visibleFade, IsVisible(level) ? 1f : 0f, Engine.DeltaTime * 2f);
foreach(SineWave sine in sines)
{
sine.Update();
}
windVector = ignoreWind ? Vector2.Zero : level.Wind;
windVector += addSpeed;
scale.X = Math.Max(1f, Math.Abs(windVector.Length()) / 100f);
float vertical = (float)Math.Abs(Math.Sin(Math.Atan2(windVector.Y, windVector.X)));
scale.X /= Math.Max(1f - vertical, 0.4f);
scale.Y = 1f / Math.Max(1f, scale.X * 0.25f);
rotation %= (float)Math.PI * 2;
float target_rot = (float)Math.Atan2(windVector.Y, windVector.X);
if (Math.Abs(rotation - target_rot) == Math.PI)
{
rotation = target_rot;
}
else
{
if (rotation - target_rot > Math.PI)
{
target_rot += (float)Math.PI * 2;
}
else if (target_rot - rotation > Math.PI)
{
target_rot -= (float)Math.PI * 2;
}
}
rotation = Calc.Approach(rotation, target_rot, Engine.DeltaTime * 8f);
if (windVector.X == 0f)
{
windVector.Y *= 3f;
}
for (int i = 0; i < positions.Length; i++)
{
float value = sines[i % sines.Length].Value;
Vector2 zero = new Vector2(windVector.X + value * 10f, windVector.Y + value * 10f);
if (windVector.Length() == 0f)
{
zero.Y += 20f;
}
positions[i] += zero * Engine.DeltaTime;
}
}
public override void Render(Scene scene)
{
base.Render(scene);
int index = 0;
float limit = (float)Math.Abs(Math.Sin(Math.Atan2(windVector.Y, windVector.X)));
if (windVector == Vector2.Zero)
{
limit = 0f;
}
limit = 0.6f + (1 - limit) * 0.4f;
foreach(Vector2 init in positions)
{
Color color = colors[particleColors[index]];
if (alphas.Count == colors.Count)
{
color *= visibleFade * alphas[particleColors[index]];
}
else
{
color *= visibleFade * alphas[0];
}
Vector2 position = init;
position.Y -= (scene as Level).Camera.Y + CameraOffset.Y;
position.Y %= 360f;
if (position.Y < 0f)
{
position.Y += 360f;
}
position.X -= (scene as Level).Camera.X + CameraOffset.X;
position.X %= 640f;
if (position.X < 0f)
{
position.X += 640f;
}
if (index < amount * limit)
{
for (int draw_y = 0; draw_y < (scene as Level).Camera.Viewport.Height; draw_y += 360)
{
for (int draw_x = 0; draw_x < (scene as Level).Camera.Viewport.Width; draw_x += 640)
{
GFX.Game["particles/snow"].DrawCentered(position + new Vector2(draw_x, draw_y), color, scale, rotation);
}
}
}
index++;
}
}
private List<Color> colors;
private List<float> alphas;
private int amount;
private Vector2 addSpeed;
private Vector2 windVector;
private bool ignoreWind;
public Vector2 CameraOffset;
private Vector2[] positions;
private int[] particleColors;
private SineWave[] sines;
private Vector2 scale;
private float rotation;
private float visibleFade;
}
}