forked from CommunalHelper/VortexHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorSwitch.cs
More file actions
310 lines (248 loc) · 11.9 KB
/
ColorSwitch.cs
File metadata and controls
310 lines (248 loc) · 11.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using Celeste.Mod.Entities;
using Celeste.Mod.VortexHelper.Misc.Extensions;
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Linq;
namespace Celeste.Mod.VortexHelper.Entities;
[CustomEntity("VortexHelper/ColorSwitch")]
[Tracked(false)]
public class ColorSwitch : Solid
{
private uint seed;
private readonly MTexture[,] edges = new MTexture[3, 3];
private static readonly Color defaultBackgroundColor = Calc.HexToColor("191919");
private static readonly Color defaultEdgeColor = Calc.HexToColor("646464");
private Color BackgroundColor = defaultBackgroundColor;
private Color EdgeColor = defaultEdgeColor;
private Color currentEdgeColor, currentBackgroundColor;
private Vector2 scale = Vector2.One;
private Vector2 scaleStrength = Vector2.One;
private readonly VortexHelperSession.SwitchBlockColor[] colors;
private int nextColorIndex = 0;
private readonly bool singleColor, random;
private readonly bool holdableActivated;
public ColorSwitch(EntityData data, Vector2 offset)
: this(data.Position + offset, data.Width, data.Height,
data.Bool("blue"), data.Bool("rose"), data.Bool("orange"), data.Bool("lime"), data.Bool("random"), data.Bool("holdableActivated"))
{ }
public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose, bool orange, bool lime, bool random, bool holdableActivated)
: base(position, width, height, true)
{
this.SurfaceSoundIndex = SurfaceIndex.ZipMover;
// if all are false, then block is useless, so make it not useless.
if (!blue && !rose && !orange && !lime)
blue = rose = orange = lime = true;
string block = "objects/VortexHelper/onoff/switch";
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
this.edges[i, j] = GFX.Game[block].GetSubtexture(i * 8, j * 8, 8, 8);
this.random = random;
bool[] colorBools = new bool[] { blue, rose, orange, lime };
int colorArraySize = colorBools.Count(b => b);
this.colors = new VortexHelperSession.SwitchBlockColor[colorArraySize];
this.singleColor = colorArraySize == 1;
int arrIdx = 0;
for (int i = 0; i < colorBools.Length; i++)
if (colorBools[i])
this.colors[arrIdx++] = (VortexHelperSession.SwitchBlockColor) i;
NextColor(this.colors[this.nextColorIndex], true);
Add(new LightOcclude());
Add(new SoundSource(SFX.game_01_console_static_loop)
{
Position = new Vector2(this.Width, this.Height) / 2f,
});
if (width > 32)
this.scaleStrength.X = width / 32f;
if (height > 32)
this.scaleStrength.Y = height / 32f;
Color col = this.colors[this.nextColorIndex].IsActive() ? defaultBackgroundColor : this.colors[this.nextColorIndex].GetColor();
SetEdgeColor(this.EdgeColor, this.EdgeColor);
SetBackgroundColor(col, col);
this.holdableActivated = holdableActivated;
this.OnDashCollide = Dashed;
}
public override void Render()
{
Vector2 position = this.Position;
this.Position += this.Shake;
int x = (int) (this.Center.X + (this.X + 1 - this.Center.X) * this.scale.X);
int y = (int) (this.Center.Y + (this.Y + 1 - this.Center.Y) * this.scale.Y);
int rectW = (int) ((this.Width - 2) * this.scale.X);
int rectH = (int) ((this.Height - 2) * this.scale.Y);
var rect = new Rectangle(x, y, rectW, rectH);
Color col = this.random
? Color.Lerp(defaultBackgroundColor, Color.White, (float) (0.05f * Math.Sin(this.Scene.TimeActive * 5f)) + 0.05f)
: this.BackgroundColor != defaultBackgroundColor
? Color.Lerp(this.currentBackgroundColor, Color.Black, 0.2f)
: this.currentBackgroundColor;
Draw.Rect(rect, col);
for (int i = rect.Y; i < rect.Bottom; i += 2)
{
float scale = 0.05f + (1f + (float) Math.Sin(i / 16f + this.Scene.TimeActive * 2f)) / 2f * 0.2f;
Draw.Line(rect.X, i, rect.X + rect.Width, i, Color.White * 0.55f * scale);
}
uint tempseed = this.seed;
PlaybackBillboard.DrawNoise(rect, ref tempseed, Color.White * 0.05f);
int w = (int) (this.Width / 8f);
int h = (int) (this.Height / 8f);
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
int tx = (i != 0) ? ((i != w - 1f) ? 1 : 2) : 0;
int ty = (j != 0) ? ((j != h - 1f) ? 1 : 2) : 0;
if (tx == 1 && ty == 1)
continue;
Vector2 renderPos = new Vector2(i, j) * 8f + Vector2.One * 4f + this.Position;
renderPos = this.Center + (renderPos - Center) * scale;
this.edges[tx, ty].DrawCentered(renderPos, this.currentEdgeColor, this.scale);
}
}
base.Render();
this.Position = position;
}
public override void Update()
{
base.Update();
if (this.Scene.OnInterval(0.1f))
this.seed++;
float t = Calc.Min(1f, 4f * Engine.DeltaTime);
this.currentEdgeColor = Color.Lerp(this.currentEdgeColor, this.EdgeColor, t);
this.currentBackgroundColor = Color.Lerp(this.currentBackgroundColor, this.BackgroundColor, t);
this.scale = Calc.Approach(this.scale, Vector2.One, Engine.DeltaTime * 4f);
}
private void SetEdgeColor(Color targetColor, Color currentColor)
{
this.EdgeColor = targetColor;
this.currentEdgeColor = currentColor;
}
private void SetBackgroundColor(Color targetColor, Color currentColor)
{
this.BackgroundColor = targetColor;
this.currentBackgroundColor = currentColor;
}
private DashCollisionResults Dashed(Player player, Vector2 direction)
{
if (!SaveData.Instance.Assists.Invincible && player.CollideCheck<Spikes>())
return DashCollisionResults.NormalCollision;
if (this.colors[this.nextColorIndex].IsActive())
return DashCollisionResults.NormalCollision;
// no switch, normal collision
if (player.StateMachine.State == Player.StRedDash)
player.StateMachine.State = Player.StNormal;
Switch(direction);
return DashCollisionResults.Rebound;
}
public void Switch(Vector2 direction)
{
this.scale = new Vector2(
1f + (Math.Abs(direction.Y) * 0.5f - Math.Abs(direction.X) * 0.5f) / this.scaleStrength.X,
1f + (Math.Abs(direction.X) * 0.5f - Math.Abs(direction.Y) * 0.5f) / this.scaleStrength.Y
);
if (this.random)
this.nextColorIndex = Calc.Random.Next(0, this.colors.Length);
VortexHelperModule.SessionProperties.SessionSwitchBlockColor = this.colors[this.nextColorIndex];
Color col = VortexHelperModule.SessionProperties.SessionSwitchBlockColor.GetColor();
UpdateColorSwitches(this.Scene, this.colors[this.nextColorIndex]);
SetEdgeColor(defaultEdgeColor, col);
this.currentBackgroundColor = Color.White;
Audio.Play(CustomSFX.game_colorSwitch_hit, this.Center);
if (SwitchBlock.RoomHasSwitchBlock(this.Scene, VortexHelperModule.SessionProperties.SessionSwitchBlockColor))
Audio.Play(CustomSFX.game_switchBlock_switch, "tone", VortexHelperModule.SessionProperties.SessionSwitchBlockColor.GetSoundParam());
Input.Rumble(RumbleStrength.Strong, RumbleLength.Long);
SceneAs<Level>().DirectionalShake(direction, 0.25f);
StartShaking(0.25f);
ParticleType p = LightningBreakerBox.P_Smash;
p.Color = col; p.Color2 = Color.Lerp(col, Color.White, 0.5f);
SmashParticles(direction.Perpendicular(), p);
SmashParticles(-direction.Perpendicular(), p);
}
public static void UpdateColorSwitches(Scene scene, VortexHelperSession.SwitchBlockColor color)
{
foreach (ColorSwitch colorSwitch in scene.Tracker.GetEntities<ColorSwitch>())
colorSwitch.NextColor(color, false);
}
private void NextColor(VortexHelperSession.SwitchBlockColor colorNext, bool start)
{
if (colorNext == this.colors[this.nextColorIndex] && !this.singleColor)
{
if (!start)
this.nextColorIndex++;
if (this.nextColorIndex > this.colors.Length - 1)
this.nextColorIndex = 0;
if (this.colors[this.nextColorIndex].IsActive())
this.nextColorIndex++;
}
this.BackgroundColor = this.colors[this.nextColorIndex].IsActive() ? defaultBackgroundColor : this.colors[this.nextColorIndex].GetColor();
}
private void SmashParticles(Vector2 dir, ParticleType smashParticle)
{
float direction;
Vector2 position;
Vector2 positionRange;
int num;
if (dir == Vector2.UnitX)
{
direction = 0f;
position = this.CenterRight - Vector2.UnitX * 12f;
positionRange = Vector2.UnitY * (this.Height - 6f) * 0.5f;
num = (int) (this.Height / 8f) * 4;
}
else if (dir == -Vector2.UnitX)
{
direction = (float) Math.PI;
position = this.CenterLeft + Vector2.UnitX * 12f;
positionRange = Vector2.UnitY * (this.Height - 6f) * 0.5f;
num = (int) (this.Height / 8f) * 4;
}
else if (dir == Vector2.UnitY)
{
direction = (float) Math.PI / 2f;
position = this.BottomCenter - Vector2.UnitY * 12f;
positionRange = Vector2.UnitX * (this.Width - 6f) * 0.5f;
num = (int) (this.Width / 8f) * 4;
}
else
{
direction = -(float) Math.PI / 2f;
position = this.TopCenter + Vector2.UnitY * 12f;
positionRange = Vector2.UnitX * (this.Width - 6f) * 0.5f;
num = (int) (this.Width / 8f) * 4;
}
num += 2;
SceneAs<Level>().Particles.Emit(smashParticle, num, position, positionRange, direction);
}
internal static class Hooks
{
public static void Hook()
{
On.Celeste.TheoCrystal.OnCollideH += TheoCrystal_OnCollideH;
On.Celeste.TheoCrystal.OnCollideV += TheoCrystal_OnCollideV;
On.Celeste.Glider.OnCollideH += Glider_OnCollideH;
On.Celeste.Glider.OnCollideV += Glider_OnCollideV;
}
public static void Unhook()
{
On.Celeste.TheoCrystal.OnCollideH -= TheoCrystal_OnCollideH;
On.Celeste.TheoCrystal.OnCollideV -= TheoCrystal_OnCollideV;
On.Celeste.Glider.OnCollideH -= Glider_OnCollideH;
On.Celeste.Glider.OnCollideV -= Glider_OnCollideV;
}
private static void ActivateSwitch(Action callOrig, CollisionData data, Func<bool> speedChecker)
{
if (data.Hit is ColorSwitch colorSwitch
&& colorSwitch.holdableActivated
&& !colorSwitch.colors[colorSwitch.nextColorIndex].IsActive()
&& speedChecker())
{
colorSwitch.Switch(data.Direction);
}
callOrig();
}
private static void TheoCrystal_OnCollideH(On.Celeste.TheoCrystal.orig_OnCollideH orig, TheoCrystal self, CollisionData data) => ActivateSwitch(() => orig(self, data), data, () => true);
private static void TheoCrystal_OnCollideV(On.Celeste.TheoCrystal.orig_OnCollideV orig, TheoCrystal self, CollisionData data) => ActivateSwitch(() => orig(self, data), data, () => self.Speed.Y > 160f || self.Speed.Y < 0f);
private static void Glider_OnCollideH(On.Celeste.Glider.orig_OnCollideH orig, Glider self, CollisionData data) => ActivateSwitch(() => orig(self, data), data, () => true);
private static void Glider_OnCollideV(On.Celeste.Glider.orig_OnCollideV orig, Glider self, CollisionData data) => ActivateSwitch(() => orig(self, data), data, () => self.Speed.Y < 0f);
}
}