Skip to content

Commit b52d4df

Browse files
committed
use texture for pixel grid
1 parent 354142d commit b52d4df

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed
Loading

Source/Entities/PixelGrid.cs

+47-39
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ private static void PixelGridAroundPlayerUpdate(PixelGrid self) {
2121
self.Collider.Left = player.Collider.Left;
2222
self.Collider.Top = player.Collider.Top;
2323
self.width = TasHelperSettings.PixelGridWidth;
24-
// when use self.Collider = player.Collider, and turn off Celeste TAS's ShowHitboxes,
25-
// if you demodash into wall, then player will stuck in wall
26-
// don't know why
2724
}
2825
}
2926
private static void CreatePixelGridAroundPlayer(On.Celeste.Level.orig_LoadLevel orig, Level self, Player.IntroTypes playerIntro, bool isFromLoader) {
3027
orig(self, playerIntro, isFromLoader);
31-
self.Add(new PixelGrid(() => TasHelperSettings.EnablePixelGrid && player is not null, PixelGridAroundPlayerUpdate, false));
28+
self.Add(new PixelGrid(() => TasHelperSettings.EnablePixelGrid && player is not null, PixelGridAroundPlayerUpdate));
3229
}
3330

3431
}
@@ -41,47 +38,23 @@ public class PixelGrid : Entity {
4138
public Func<bool> visibleGetter;
4239
public int width = 0;
4340
public Action<PixelGrid> UpdateBeforeRender;
44-
public bool fadeOut = false;
4541

46-
public PixelGrid(Func<bool> visibleGetter, Action<PixelGrid> UpdateBeforeRender, bool fadeOut = false) {
42+
private static MTexture texture;
43+
44+
private const int TextureSize = 24 - 2;
45+
46+
public PixelGrid(Func<bool> visibleGetter, Action<PixelGrid> UpdateBeforeRender) {
4747
Depth = 8900;
4848
// lower than BackgroudTiles
4949
Collidable = false;
5050
Collider = new Hitbox(0f, 0f);
5151
this.visibleGetter = visibleGetter;
5252
this.UpdateBeforeRender = UpdateBeforeRender;
53-
this.fadeOut = fadeOut;
5453
}
5554

56-
#pragma warning disable CS8509
57-
public static Color GetGridColor(int index, float alpha = 0.5f) {
58-
return (Math.Abs(index) % 2) switch {
59-
0 => color1 * alpha,
60-
1 => color2 * alpha,
61-
};
62-
}
63-
#pragma warning restore CS8509
64-
65-
public Color FadeOutColor(int RelativeX, int RelativeY, float width) {
66-
return GetGridColor(RelativeX + RelativeY, fadeOut ? (1 - Distance(RelativeX, RelativeY) / width) * TasHelperSettings.PixelGridOpacity * 0.1f : TasHelperSettings.PixelGridOpacity * 0.1f);
67-
}
68-
69-
public float Distance(int RelativeX, int RelativeY) {
70-
float DistX = 0f;
71-
float DistY = 0f;
72-
if (RelativeX < Collider.Left - 1) {
73-
DistX = Collider.Left - 1 - RelativeX;
74-
}
75-
else if (RelativeX > Collider.Right) {
76-
DistX = RelativeX - Collider.Right;
77-
}
78-
if (RelativeY < Collider.Top - 1) {
79-
DistY = Collider.Top - 1 - RelativeY;
80-
}
81-
else if (RelativeY > Collider.Bottom) {
82-
DistY = RelativeY - Collider.Bottom;
83-
}
84-
return (float)Math.Sqrt(DistX * DistX + DistY * DistY);
55+
[Initialize]
56+
public static void Initialize() {
57+
texture = GFX.Game["TASHelper/PixelGrid/grid"];
8558
}
8659

8760
public override void Update() {
@@ -103,11 +76,46 @@ public override void DebugRender(Camera camera) {
10376
}
10477

10578
public void RenderWithoutCondition() {
106-
for (int x = (int)(Collider.Left - width); x < Collider.Right + width; x++) {
107-
for (int y = (int)(Collider.Top - width); y < Collider.Bottom + width; y++) {
108-
Draw.Point(new Vector2(Position.X + x, Position.Y + y), FadeOutColor(x, y, width));
79+
int left = (int)(Collider.Left - width);
80+
int top = (int)(Collider.Top - width);
81+
int w = (int)Collider.Width + 2 * width;
82+
int h = (int)Collider.Height + 2 * width;
83+
Color c1, c2;
84+
float alpha = TasHelperSettings.PixelGridOpacity * 0.1f;
85+
if ((left + top) % 2 == 0){
86+
c1 = color1 * alpha;
87+
c2 = color2 * alpha;
88+
}
89+
else {
90+
c2 = color1 * alpha;
91+
c1 = color2 * alpha;
92+
}
93+
Draw(this.Position + new Vector2(left, top), w, h, c1, c2);
94+
95+
}
96+
97+
private static void Draw(Vector2 Position, int width, int height, Color color1, Color color2) {
98+
if (width <= TextureSize && height <= TextureSize) {
99+
texture.Draw(Position, Vector2.Zero, color1, Vector2.One, 0f, new Rectangle(0, 0, width, height));
100+
texture.Draw(Position, Vector2.Zero, color2, Vector2.One, 0f, new Rectangle(1, 0, width, height));
101+
return;
102+
}
103+
if (height <= TextureSize) {
104+
while (width > 0) {
105+
int w = Math.Min(width, TextureSize);
106+
Draw(Position, w, height, color1, color2);
107+
Position += Vector2.UnitX * w;
108+
width -= w;
109109
}
110+
return;
111+
}
112+
while (height > 0) {
113+
int h = Math.Min(height, TextureSize);
114+
Draw(Position, width, h, color1, color2);
115+
Position += Vector2.UnitY * h;
116+
height -= h;
110117
}
118+
return;
111119
}
112120
}
113121

0 commit comments

Comments
 (0)