Skip to content

Commit 82c9db9

Browse files
committed
Add reskinnability to Switch Blocks and Color Switches; add Switch Block Color Controller
1 parent b37b67b commit 82c9db9

13 files changed

Lines changed: 177 additions & 43 deletions

File tree

Code/Entities/ColorSwitch.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public class ColorSwitch : Solid
1515

1616
private readonly MTexture[,] edges = new MTexture[3, 3];
1717

18-
private static readonly Color defaultBackgroundColor = Calc.HexToColor("191919");
19-
private static readonly Color defaultEdgeColor = Calc.HexToColor("646464");
18+
public static readonly Color DefaultBackgroundColor = Calc.HexToColor("191919");
19+
public static readonly Color DefaultEdgeColor = Calc.HexToColor("646464");
20+
private static Color RoomDefaultBackgroundColor(Level level) => level.Tracker.GetEntity<SwitchBlockColorController>()?.SwitchBackgroundColor ?? DefaultBackgroundColor;
21+
private static Color RoomDefaultEdgeColor(Level level) => level.Tracker.GetEntity<SwitchBlockColorController>()?.SwitchEdgeColor ?? DefaultEdgeColor;
2022

21-
private Color BackgroundColor = defaultBackgroundColor;
22-
private Color EdgeColor = defaultEdgeColor;
23+
private Color BackgroundColor;
24+
private Color EdgeColor;
2325
private Color currentEdgeColor, currentBackgroundColor;
2426

2527
private Vector2 scale = Vector2.One;
@@ -31,10 +33,10 @@ public class ColorSwitch : Solid
3133

3234
public ColorSwitch(EntityData data, Vector2 offset)
3335
: this(data.Position + offset, data.Width, data.Height,
34-
data.Bool("blue"), data.Bool("rose"), data.Bool("orange"), data.Bool("lime"), data.Bool("random"))
36+
data.Bool("blue"), data.Bool("rose"), data.Bool("orange"), data.Bool("lime"), data.Bool("random"), data.Attr("spriteDir", "").Trim().TrimEnd('/'))
3537
{ }
3638

37-
public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose, bool orange, bool lime, bool random)
39+
public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose, bool orange, bool lime, bool random, string spriteDir)
3840
: base(position, width, height, true)
3941
{
4042
this.SurfaceSoundIndex = SurfaceIndex.ZipMover;
@@ -43,7 +45,7 @@ public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose
4345
if (!blue && !rose && !orange && !lime)
4446
blue = rose = orange = lime = true;
4547

46-
string block = "objects/VortexHelper/onoff/switch";
48+
string block = string.IsNullOrEmpty(spriteDir) ? "objects/VortexHelper/onoff/switch" : spriteDir + "/switch";
4749
for (int i = 0; i < 3; i++)
4850
for (int j = 0; j < 3; j++)
4951
this.edges[i, j] = GFX.Game[block].GetSubtexture(i * 8, j * 8, 8, 8);
@@ -61,8 +63,6 @@ public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose
6163
if (colorBools[i])
6264
this.colors[arrIdx++] = (VortexHelperSession.SwitchBlockColor) i;
6365

64-
NextColor(this.colors[this.nextColorIndex], true);
65-
6666
Add(new LightOcclude());
6767
Add(new SoundSource(SFX.game_01_console_static_loop)
6868
{
@@ -74,13 +74,23 @@ public ColorSwitch(Vector2 position, int width, int height, bool blue, bool rose
7474
if (height > 32)
7575
this.scaleStrength.Y = height / 32f;
7676

77-
Color col = this.colors[this.nextColorIndex].IsActive() ? defaultBackgroundColor : this.colors[this.nextColorIndex].GetColor();
78-
SetEdgeColor(this.EdgeColor, this.EdgeColor);
79-
SetBackgroundColor(col, col);
80-
8177
this.OnDashCollide = Dashed;
8278
}
8379

80+
public override void Awake(Scene scene)
81+
{
82+
base.Awake(scene);
83+
84+
Level level = SceneAs<Level>();
85+
86+
Color bgCol = this.colors[this.nextColorIndex].IsActive() ? RoomDefaultBackgroundColor(level) : this.colors[this.nextColorIndex].GetColor(level);
87+
Color edgeCol = RoomDefaultEdgeColor(level);
88+
SetBackgroundColor(bgCol, bgCol);
89+
SetEdgeColor(edgeCol, edgeCol);
90+
91+
NextColor(this.colors[this.nextColorIndex], true);
92+
}
93+
8494
public override void Render()
8595
{
8696
Vector2 position = this.Position;
@@ -92,9 +102,10 @@ public override void Render()
92102
int rectH = (int) ((this.Height - 2) * this.scale.Y);
93103
var rect = new Rectangle(x, y, rectW, rectH);
94104

105+
Color defaultCol = RoomDefaultBackgroundColor(SceneAs<Level>());
95106
Color col = this.random
96-
? Color.Lerp(defaultBackgroundColor, Color.White, (float) (0.05f * Math.Sin(this.Scene.TimeActive * 5f)) + 0.05f)
97-
: this.BackgroundColor != defaultBackgroundColor
107+
? Color.Lerp(defaultCol, Color.White, (float) (0.05f * Math.Sin(this.Scene.TimeActive * 5f)) + 0.05f)
108+
: this.BackgroundColor != defaultCol
98109
? Color.Lerp(this.currentBackgroundColor, Color.Black, 0.2f)
99110
: this.currentBackgroundColor;
100111

@@ -176,6 +187,8 @@ private DashCollisionResults Dashed(Player player, Vector2 direction)
176187

177188
public void Switch(Vector2 direction)
178189
{
190+
Level level = SceneAs<Level>();
191+
179192
this.scale = new Vector2(
180193
1f + (Math.Abs(direction.Y) * 0.5f - Math.Abs(direction.X) * 0.5f) / this.scaleStrength.X,
181194
1f + (Math.Abs(direction.X) * 0.5f - Math.Abs(direction.Y) * 0.5f) / this.scaleStrength.Y
@@ -185,18 +198,18 @@ public void Switch(Vector2 direction)
185198
this.nextColorIndex = Calc.Random.Next(0, this.colors.Length);
186199

187200
VortexHelperModule.SessionProperties.SessionSwitchBlockColor = this.colors[this.nextColorIndex];
188-
Color col = VortexHelperModule.SessionProperties.SessionSwitchBlockColor.GetColor();
201+
Color col = VortexHelperModule.SessionProperties.SessionSwitchBlockColor.GetColor(level);
189202

190203
UpdateColorSwitches(this.Scene, this.colors[this.nextColorIndex]);
191-
SetEdgeColor(defaultEdgeColor, col);
204+
SetEdgeColor(RoomDefaultEdgeColor(level), col);
192205
this.currentBackgroundColor = Color.White;
193206

194207
Audio.Play(CustomSFX.game_colorSwitch_hit, this.Center);
195208
if (SwitchBlock.RoomHasSwitchBlock(this.Scene, VortexHelperModule.SessionProperties.SessionSwitchBlockColor))
196209
Audio.Play(CustomSFX.game_switchBlock_switch, "tone", VortexHelperModule.SessionProperties.SessionSwitchBlockColor.GetSoundParam());
197210

198211
Input.Rumble(RumbleStrength.Strong, RumbleLength.Long);
199-
SceneAs<Level>().DirectionalShake(direction, 0.25f);
212+
level.DirectionalShake(direction, 0.25f);
200213
StartShaking(0.25f);
201214

202215
ParticleType p = LightningBreakerBox.P_Smash;
@@ -213,6 +226,8 @@ public static void UpdateColorSwitches(Scene scene, VortexHelperSession.SwitchBl
213226

214227
private void NextColor(VortexHelperSession.SwitchBlockColor colorNext, bool start)
215228
{
229+
Level level = SceneAs<Level>();
230+
216231
if (colorNext == this.colors[this.nextColorIndex] && !this.singleColor)
217232
{
218233
if (!start)
@@ -224,8 +239,8 @@ private void NextColor(VortexHelperSession.SwitchBlockColor colorNext, bool star
224239
if (this.colors[this.nextColorIndex].IsActive())
225240
this.nextColorIndex++;
226241
}
227-
228-
this.BackgroundColor = this.colors[this.nextColorIndex].IsActive() ? defaultBackgroundColor : this.colors[this.nextColorIndex].GetColor();
242+
243+
this.BackgroundColor = this.colors[this.nextColorIndex].IsActive() ? RoomDefaultBackgroundColor(level) : this.colors[this.nextColorIndex].GetColor(level);
229244
}
230245

231246
private void SmashParticles(Vector2 dir, ParticleType smashParticle)

Code/Entities/SwitchBlock.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Celeste.Mod.VortexHelper.Misc.Extensions;
33
using Microsoft.Xna.Framework;
44
using Monocle;
5+
using System;
56
using System.Collections.Generic;
67

78
namespace Celeste.Mod.VortexHelper.Entities;
@@ -33,7 +34,8 @@ public override void Render()
3334
private bool Activated;
3435
private readonly VortexHelperSession.SwitchBlockColor switchBlockColor;
3536
private readonly int index;
36-
private readonly Color color;
37+
private readonly string spriteDir;
38+
private Color color;
3739

3840
private readonly LightOcclude occluder;
3941
private Wiggler wiggler;
@@ -50,13 +52,14 @@ public override void Render()
5052
private readonly int blockHeight = 2;
5153

5254
public SwitchBlock(EntityData data, Vector2 offset)
53-
: this(data.Position + offset, data.Width, data.Height, data.Int("index", 0)) { }
55+
: this(data.Position + offset, data.Width, data.Height, data.Int("index", 0), data.Attr("spriteDir", "").Trim().TrimEnd('/')) { }
5456

55-
public SwitchBlock(Vector2 position, int width, int height, int index)
57+
public SwitchBlock(Vector2 position, int width, int height, int index, string spriteDir)
5658
: base(position, width, height, true)
5759
{
5860
this.SurfaceSoundIndex = SurfaceIndex.CassetteBlock;
5961
this.index = index;
62+
this.spriteDir = string.IsNullOrEmpty(spriteDir) ? "objects/VortexHelper/onoff" : spriteDir;
6063

6164
this.switchBlockColor = this.index switch
6265
{
@@ -66,15 +69,16 @@ public SwitchBlock(Vector2 position, int width, int height, int index)
6669
_ => VortexHelperSession.SwitchBlockColor.Blue,
6770
};
6871

69-
this.color = this.switchBlockColor.GetColor();
70-
this.Activated = this.Collidable = this.switchBlockColor.IsActive();
71-
7272
Add(this.occluder = new LightOcclude());
7373
}
7474

7575
public override void Awake(Scene scene)
7676
{
7777
base.Awake(scene);
78+
79+
this.color = this.switchBlockColor.GetColor(SceneAs<Level>());
80+
this.Activated = this.Collidable = this.switchBlockColor.IsActive();
81+
7882
Color color = Calc.HexToColor("667da5");
7983
var disabledColor = new Color(color.R / 255f * (this.color.R / 255f), color.G / 255f * (this.color.G / 255f), color.B / 255f * (this.color.B / 255f), 1f);
8084

@@ -151,10 +155,11 @@ public override void Awake(Scene scene)
151155

152156
string idx = this.index switch
153157
{
158+
0 => "blue",
154159
1 => "red",
155160
2 => "orange",
156161
3 => "green",
157-
_ => "blue",
162+
_ => throw new ArgumentOutOfRangeException()
158163
};
159164

160165
// cassette block autotiling
@@ -231,8 +236,8 @@ private bool CheckForSame(float x, float y)
231236

232237
private void SetImage(float x, float y, int tx, int ty, string idx)
233238
{
234-
this.pressed.Add(CreateImage(x, y, tx, ty, GFX.Game["objects/VortexHelper/onoff/outline_" + idx]));
235-
this.solid.Add(CreateImage(x, y, tx, ty, GFX.Game["objects/VortexHelper/onoff/solid"]));
239+
this.pressed.Add(CreateImage(x, y, tx, ty, GFX.Game[this.spriteDir + "/outline_" + idx]));
240+
this.solid.Add(CreateImage(x, y, tx, ty, GFX.Game[this.spriteDir + "/solid"]));
236241
}
237242

238243
private Image CreateImage(float x, float y, int tx, int ty, MTexture tex)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Celeste.Mod.Entities;
2+
using Celeste.Mod.VortexHelper.Misc.Extensions;
3+
using Microsoft.Xna.Framework;
4+
using Monocle;
5+
6+
namespace Celeste.Mod.VortexHelper.Entities;
7+
8+
[CustomEntity("VortexHelper/SwitchBlockColorController")]
9+
[Tracked]
10+
public class SwitchBlockColorController : Entity
11+
{
12+
public readonly Color BlueColor, RoseColor, OrangeColor, LimeColor;
13+
public readonly Color SwitchBackgroundColor, SwitchEdgeColor;
14+
15+
public SwitchBlockColorController(EntityData data, Vector2 offset) : base(data.Position + offset)
16+
{
17+
BlueColor = data.HexColor("blueColor", EnumExt.SwitchBlockBlue);
18+
RoseColor = data.HexColor("roseColor", EnumExt.SwitchBlockRose);
19+
OrangeColor = data.HexColor("orangeColor", EnumExt.SwitchBlockOrange);
20+
LimeColor = data.HexColor("limeColor", EnumExt.SwitchBlockLime);
21+
22+
SwitchBackgroundColor = data.HexColor("switchBackgroundColor", ColorSwitch.DefaultBackgroundColor);
23+
SwitchEdgeColor = data.HexColor("switchEdgeColor", ColorSwitch.DefaultEdgeColor);
24+
}
25+
}

Code/Misc/Extensions/EnumExt.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.Xna.Framework;
1+
using Celeste.Mod.VortexHelper.Entities;
2+
using Microsoft.Xna.Framework;
23
using Monocle;
4+
using System;
35

46
namespace Celeste.Mod.VortexHelper.Misc.Extensions;
57

@@ -10,20 +12,26 @@ public static class EnumExt
1012
public static readonly Color SwitchBlockOrange = Calc.HexToColor("ff9532");
1113
public static readonly Color SwitchBlockLime = Calc.HexToColor("9cff32");
1214

13-
public static Color GetColor(this VortexHelperSession.SwitchBlockColor color) => color switch
15+
public static Color GetColor(this VortexHelperSession.SwitchBlockColor color, Level level)
1416
{
15-
VortexHelperSession.SwitchBlockColor.Rose => SwitchBlockRose,
16-
VortexHelperSession.SwitchBlockColor.Orange => SwitchBlockOrange,
17-
VortexHelperSession.SwitchBlockColor.Lime => SwitchBlockLime,
18-
_ => SwitchBlockBlue,
19-
};
17+
SwitchBlockColorController controller = level.Tracker.GetEntity<SwitchBlockColorController>();
18+
return color switch
19+
{
20+
VortexHelperSession.SwitchBlockColor.Blue => controller?.BlueColor ?? SwitchBlockBlue,
21+
VortexHelperSession.SwitchBlockColor.Rose => controller?.RoseColor ?? SwitchBlockRose,
22+
VortexHelperSession.SwitchBlockColor.Orange => controller?.OrangeColor ?? SwitchBlockOrange,
23+
VortexHelperSession.SwitchBlockColor.Lime => controller?.LimeColor ?? SwitchBlockLime,
24+
_ => throw new ArgumentOutOfRangeException()
25+
};
26+
}
2027

2128
public static int GetSoundParam(this VortexHelperSession.SwitchBlockColor color) => color switch
2229
{
30+
VortexHelperSession.SwitchBlockColor.Blue => 0,
2331
VortexHelperSession.SwitchBlockColor.Rose => 1,
2432
VortexHelperSession.SwitchBlockColor.Orange => 2,
2533
VortexHelperSession.SwitchBlockColor.Lime => 3,
26-
_ => 0,
34+
_ => throw new ArgumentOutOfRangeException()
2735
};
2836

2937
public static bool IsActive(this VortexHelperSession.SwitchBlockColor color) => color == VortexHelperModule.SessionProperties.SessionSwitchBlockColor;
227 Bytes
Loading
218 Bytes
Loading
223 Bytes
Loading
172 Bytes
Loading
229 Bytes
Loading

Loenn/entities/color_switch.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ colorSwitch.placements = {
1717
rose = true,
1818
orange = true,
1919
lime = true,
20-
random = false
20+
random = false,
21+
spriteDir = ""
2122
}
2223
},
2324
{
@@ -29,12 +30,12 @@ colorSwitch.placements = {
2930
rose = true,
3031
orange = true,
3132
lime = true,
32-
random = true
33+
random = true,
34+
spriteDir = ""
3335
}
3436
}
3537
}
3638

37-
local frame = "objects/VortexHelper/onoff/switch"
3839
local nine_patch_options = {
3940
mode = "border",
4041
borderMode = "repeat",
@@ -45,6 +46,7 @@ local bgColor = {40 / 255, 40 / 255, 40 / 255, 1.0}
4546
function colorSwitch.sprite(room, entity)
4647
local x, y = entity.x or 0, entity.y or 0
4748
local width, height = entity.width or 16, entity.height or 16
49+
local frame = (entity.spriteDir or "") ~= "" and (entity.spriteDir .. "/switch") or "objects/VortexHelper/onoff/switch"
4850

4951
return {
5052
drawableRectangle.fromRectangle("fill", x + 1, y + 1, width - 2, height - 2, bgColor):getDrawableSprite(),

0 commit comments

Comments
 (0)