Skip to content

Commit d05544c

Browse files
authored
Merge pull request #13 from earthwise01/reskinnable-boom-box
Make Boom Boxes and Boom Box Zips reskinnable
2 parents e1e3106 + eed5044 commit d05544c

5 files changed

Lines changed: 151 additions & 127 deletions

File tree

Code/Entities/BoomBox.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class BoomBox : Solid {
3636
private Coroutine _sequence;
3737
private bool _steamAnger = false;
3838

39-
public BoomBox(Vector2 position, string activationId, float initialDelay, bool startActive)
39+
public BoomBox(Vector2 position, string activationId, float initialDelay, bool startActive, string spriteDir)
4040
: base(position, 24, 24, false) {
4141
Add(Activator = new FactoryActivator());
4242
Activator.StartOn = startActive;
@@ -50,14 +50,16 @@ public BoomBox(Vector2 position, string activationId, float initialDelay, bool s
5050

5151
_initialDelay = initialDelay;
5252

53-
Add(_sprite = new Sprite(GFX.Game, "objects/FactoryHelper/boomBox/"));
53+
string spritePath = string.IsNullOrEmpty(spriteDir) ? "objects/FactoryHelper/boomBox/" : spriteDir + "/";
54+
55+
Add(_sprite = new Sprite(GFX.Game, spritePath));
5456
_sprite.Add("idle", "idle", 0.2f, "idle");
5557
_sprite.Add("activating", "activating", 0.2f, "activating");
5658
_sprite.Add("active", "active", 0.15f, "active");
5759
_sprite.Add("angry", "angry", 0.05f, "angry");
5860
_sprite.Add("resetting", "resetting", 0.15f, "active");
5961

60-
Add(_boomSprite = new Sprite(GFX.Game, "objects/FactoryHelper/boomBox/"));
62+
Add(_boomSprite = new Sprite(GFX.Game, spritePath));
6163
_boomSprite.Add("boom", "boom", 0.04f);
6264
_boomSprite.Color = Color.White * 0.5f;
6365
_boomSprite.Visible = false;
@@ -71,7 +73,7 @@ public BoomBox(Vector2 position, string activationId, float initialDelay, bool s
7173
}
7274

7375
public BoomBox(EntityData data, Vector2 offest)
74-
: this(data.Position + offest, data.Attr("activationId", ""), data.Float("initialDelay", 0f), data.Bool("startActive", false)) {
76+
: this(data.Position + offest, data.Attr("activationId", ""), data.Float("initialDelay", 0f), data.Bool("startActive", false), data.Attr("spriteDir", "")) {
7577
}
7678

7779
public FactoryActivator Activator { get; }

Code/Entities/BoomBoxZip.cs

Lines changed: 108 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,60 @@
88
namespace FactoryHelper.Entities {
99
[CustomEntity("FactoryHelper/BoomBoxZip")]
1010
public class BoomBoxZip : BoomBox {
11-
public float percent;
12-
public Vector2 start, target;
13-
private ZipMoverPathRenderer pathRenderer;
11+
private float _percent;
12+
private Vector2 _start, _target;
13+
private ZipMoverPathRenderer _pathRenderer;
1414

15-
public BoomBoxZip(EntityData data, Vector2 offset)
16-
: this(data.Position + offset, data.Attr("activationId", ""), data.Float("initialDelay", 0f), data.Bool("startActive", false), data.Nodes[0] + offset) {
17-
}
15+
private readonly string _spriteDir;
16+
private readonly Color _ropeColor, _ropeLightColor, _sparkParticleColor;
17+
18+
public BoomBoxZip(EntityData data, Vector2 offset)
19+
: this(data.Position + offset, data.Attr("activationId", ""), data.Float("initialDelay", 0f), data.Bool("startActive", false), data.Nodes[0] + offset,
20+
data.Attr("spriteDir", ""), data.HexColor("ropeColor", Calc.HexToColor("4d3c22")), data.HexColor("ropeLightColor", Calc.HexToColor("766c49")), data.HexColor("sparkParticleColor", Calc.HexToColor("fff538"))) { }
1821

19-
public BoomBoxZip(Vector2 position, string activationId, float initialDelay, bool startActive, Vector2 target)
20-
: base(position, activationId, initialDelay, startActive) {
22+
public BoomBoxZip(Vector2 position, string activationId, float initialDelay, bool startActive, Vector2 target, string spriteDir, Color ropeColor, Color ropeLightColor, Color sparkParticleColor)
23+
: base(position, activationId, initialDelay, startActive, spriteDir) {
2124
Add(new Coroutine(ZipMoverSequence()));
22-
this.start = this.Position;
23-
this.target = target;
25+
_start = position;
26+
_target = target;
27+
28+
_spriteDir = string.IsNullOrEmpty(spriteDir) ? "objects/FactoryHelper/boomBox" : spriteDir;
29+
_ropeColor = ropeColor;
30+
_ropeLightColor = ropeLightColor;
31+
_sparkParticleColor = sparkParticleColor;
2432
}
2533

2634
public override void Added(Scene scene) {
2735
base.Added(scene);
28-
scene.Add(pathRenderer = new ZipMoverPathRenderer(this));
36+
scene.Add(_pathRenderer = new ZipMoverPathRenderer(this));
2937
}
3038

3139
private IEnumerator ZipMoverSequence() {
32-
start = Position;
40+
_start = Position;
3341
while (true) {
3442
if (!HasPlayerRider()) {
3543
yield return null;
3644
continue;
3745
}
46+
3847
Input.Rumble(RumbleStrength.Medium, RumbleLength.Short);
3948
StartShaking(0.1f);
4049
yield return 0.1f;
4150
StopPlayerRunIntoAnimation = false;
42-
float at2 = 0f;
43-
while (at2 < 1f) {
51+
52+
float at = 0f;
53+
while (at < 1f) {
4454
yield return null;
45-
at2 = Calc.Approach(at2, 1f, 2f * Engine.DeltaTime);
46-
percent = Ease.SineIn(at2);
47-
Vector2 vector = Vector2.Lerp(start, target, percent);
48-
ScrapeParticlesCheck(vector);
55+
at = Calc.Approach(at, 1f, 2f * Engine.DeltaTime);
56+
_percent = Ease.SineIn(at);
57+
Vector2 position = Vector2.Lerp(_start, _target, _percent);
58+
ScrapeParticlesCheck(position);
4959
if (Scene.OnInterval(0.1f)) {
50-
pathRenderer.CreateSparks();
60+
_pathRenderer.CreateSparks();
5161
}
5262

53-
MoveTo(vector);
54-
_boomCollider.Position = vector + new Vector2(Width / 2, Height / 2);
63+
MoveTo(position);
64+
_boomCollider.Position = position + new Vector2(Width / 2, Height / 2);
5565
}
5666

5767
StartShaking(0.2f);
@@ -60,12 +70,13 @@ private IEnumerator ZipMoverSequence() {
6070
StopPlayerRunIntoAnimation = true;
6171
yield return 0.5f;
6272
StopPlayerRunIntoAnimation = false;
63-
at2 = 0f;
64-
while (at2 < 1f) {
73+
74+
at = 0f;
75+
while (at < 1f) {
6576
yield return null;
66-
at2 = Calc.Approach(at2, 1f, 0.5f * Engine.DeltaTime);
67-
percent = 1f - Ease.SineIn(at2);
68-
Vector2 position = Vector2.Lerp(target, start, Ease.SineIn(at2));
77+
at = Calc.Approach(at, 1f, 0.5f * Engine.DeltaTime);
78+
_percent = 1f - Ease.SineIn(at);
79+
Vector2 position = Vector2.Lerp(_target, _start, Ease.SineIn(at));
6980
MoveTo(position);
7081
_boomCollider.Position = position + new Vector2(Width / 2, Height / 2);
7182
}
@@ -77,101 +88,95 @@ private IEnumerator ZipMoverSequence() {
7788
}
7889

7990
private void ScrapeParticlesCheck(Vector2 to) {
80-
if (!base.Scene.OnInterval(0.03f)) {
91+
if (!Scene.OnInterval(0.03f)) {
8192
return;
8293
}
8394

84-
bool flag = to.Y != base.ExactPosition.Y;
85-
bool flag2 = to.X != base.ExactPosition.X;
86-
if (flag && !flag2) {
87-
int num = Math.Sign(to.Y - base.ExactPosition.Y);
88-
Vector2 value = (num != 1) ? base.TopLeft : base.BottomLeft;
89-
int num2 = 4;
90-
if (num == 1) {
91-
num2 = Math.Min((int)base.Height - 12, 20);
95+
bool movingY = to.Y != ExactPosition.Y;
96+
bool movingX = to.X != ExactPosition.X;
97+
if (movingY && !movingX) {
98+
int movingDir = Math.Sign(to.Y - ExactPosition.Y);
99+
Vector2 checkFrom = movingDir == 1 ? BottomLeft : TopLeft;
100+
101+
int particlesStart = 4;
102+
if (movingDir == 1) {
103+
particlesStart = Math.Min((int)Height - 12, 20);
92104
}
93105

94-
int num3 = (int)base.Height;
95-
if (num == -1) {
96-
num3 = Math.Max(16, (int)base.Height - 16);
106+
int particlesEnd = (int)Height;
107+
if (movingDir == -1) {
108+
particlesEnd = Math.Max(16, (int)Height - 16);
97109
}
98110

99-
if (base.Scene.CollideCheck<Solid>(value + new Vector2(-2f, num * -2))) {
100-
for (int i = num2; i < num3; i += 8) {
101-
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, base.TopLeft + new Vector2(0f, (float)i + (float)num * 2f), (num == 1) ? (-(float)Math.PI / 4f) : ((float)Math.PI / 4f));
111+
if (Scene.CollideCheck<Solid>(checkFrom + new Vector2(-2f, movingDir * -2))) {
112+
for (int i = particlesStart; i < particlesEnd; i += 8) {
113+
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, TopLeft + new Vector2(0f, i + movingDir * 2f), (movingDir == 1) ? (-MathF.PI / 4f) : (MathF.PI / 4f));
102114
}
103115
}
104116

105-
if (base.Scene.CollideCheck<Solid>(value + new Vector2(base.Width + 2f, num * -2))) {
106-
for (int j = num2; j < num3; j += 8) {
107-
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, base.TopRight + new Vector2(-1f, (float)j + (float)num * 2f), (num == 1) ? ((float)Math.PI * -3f / 4f) : ((float)Math.PI * 3f / 4f));
117+
if (Scene.CollideCheck<Solid>(checkFrom + new Vector2(Width + 2f, movingDir * -2))) {
118+
for (int i = particlesStart; i < particlesEnd; i += 8) {
119+
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, TopRight + new Vector2(-1f, i + movingDir * 2f), (movingDir == 1) ? (MathF.PI * -3f / 4f) : (MathF.PI * 3f / 4f));
108120
}
109121
}
110-
} else {
111-
if (!flag2 || flag) {
112-
return;
113-
}
122+
} else if (movingX && !movingY) {
123+
int movingDir = Math.Sign(to.X - ExactPosition.X);
124+
Vector2 checkFrom = movingDir == 1 ? TopRight : TopLeft;
114125

115-
int num4 = Math.Sign(to.X - base.ExactPosition.X);
116-
Vector2 value2 = (num4 != 1) ? base.TopLeft : base.TopRight;
117-
int num5 = 4;
118-
if (num4 == 1) {
119-
num5 = Math.Min((int)base.Width - 12, 20);
126+
int particlesStart = 4;
127+
if (movingDir == 1) {
128+
particlesStart = Math.Min((int)Width - 12, 20);
120129
}
121130

122-
int num6 = (int)base.Width;
123-
if (num4 == -1) {
124-
num6 = Math.Max(16, (int)base.Width - 16);
131+
int particlesEnd = (int)Width;
132+
if (movingDir == -1) {
133+
particlesEnd = Math.Max(16, (int)Width - 16);
125134
}
126135

127-
if (base.Scene.CollideCheck<Solid>(value2 + new Vector2(num4 * -2, -2f))) {
128-
for (int k = num5; k < num6; k += 8) {
129-
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, base.TopLeft + new Vector2((float)k + (float)num4 * 2f, -1f), (num4 == 1) ? ((float)Math.PI * 3f / 4f) : ((float)Math.PI / 4f));
136+
if (Scene.CollideCheck<Solid>(checkFrom + new Vector2(movingDir * -2, -2f))) {
137+
for (int i = particlesStart; i < particlesEnd; i += 8) {
138+
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, TopLeft + new Vector2(i + movingDir * 2f, -1f), (movingDir == 1) ? (MathF.PI * 3f / 4f) : (MathF.PI / 4f));
130139
}
131140
}
132141

133-
if (base.Scene.CollideCheck<Solid>(value2 + new Vector2(num4 * -2, base.Height + 2f))) {
134-
for (int l = num5; l < num6; l += 8) {
135-
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, base.BottomLeft + new Vector2((float)l + (float)num4 * 2f, 0f), (num4 == 1) ? ((float)Math.PI * -3f / 4f) : (-(float)Math.PI / 4f));
142+
if (Scene.CollideCheck<Solid>(checkFrom + new Vector2(movingDir * -2, Height + 2f))) {
143+
for (int i = particlesStart; i < particlesEnd; i += 8) {
144+
SceneAs<Level>().ParticlesFG.Emit(ZipMover.P_Scrape, BottomLeft + new Vector2(i + movingDir * 2f, 0f), (movingDir == 1) ? (MathF.PI * -3f / 4f) : (-MathF.PI / 4f));
136145
}
137146
}
138147
}
139148
}
140149

141150
private class ZipMoverPathRenderer : Entity {
142-
private static readonly Color ropeColor = Calc.HexToColor("4d3c22");
143-
private static readonly Color ropeLightColor = Calc.HexToColor("766c49");
144-
145-
public BoomBoxZip zipMover;
151+
private readonly BoomBoxZip _zipMover;
146152

147-
private MTexture cog;
148-
private Vector2 from;
149-
private Vector2 to;
150-
private Vector2 sparkAdd;
151-
private float sparkDirFromA;
152-
private float sparkDirFromB;
153-
private float sparkDirToA;
154-
private float sparkDirToB;
153+
private readonly MTexture _cogTexture;
154+
private readonly Vector2 _from, _to;
155+
private readonly Vector2 _sparkAdd;
156+
private readonly float _sparkDirFromA, _sparkDirFromB;
157+
private readonly float _sparkDirToA, _sparkDirToB;
155158

156159
public ZipMoverPathRenderer(BoomBoxZip zipMover) {
157-
base.Depth = 5000;
158-
this.zipMover = zipMover;
159-
from = this.zipMover.start + new Vector2(this.zipMover.Width / 2f, this.zipMover.Height / 2f);
160-
to = this.zipMover.target + new Vector2(this.zipMover.Width / 2f, this.zipMover.Height / 2f);
161-
sparkAdd = (from - to).SafeNormalize(5f).Perpendicular();
162-
float num = (from - to).Angle();
163-
sparkDirFromA = num + (float)Math.PI / 8f;
164-
sparkDirFromB = num - (float)Math.PI / 8f;
165-
sparkDirToA = num + (float)Math.PI - (float)Math.PI / 8f;
166-
sparkDirToB = num + (float)Math.PI + (float)Math.PI / 8f;
167-
cog = GFX.Game["objects/zipmover/cog"];
160+
Depth = 5000;
161+
_zipMover = zipMover;
162+
_from = _zipMover._start + new Vector2(_zipMover.Width / 2f, _zipMover.Height / 2f);
163+
_to = _zipMover._target + new Vector2(_zipMover.Width / 2f, _zipMover.Height / 2f);
164+
_sparkAdd = (_from - _to).SafeNormalize(5f).Perpendicular();
165+
float backwards = (_from - _to).Angle();
166+
_sparkDirFromA = backwards + MathF.PI / 8f;
167+
_sparkDirFromB = backwards - MathF.PI / 8f;
168+
_sparkDirToA = backwards + MathF.PI - MathF.PI / 8f;
169+
_sparkDirToB = backwards + MathF.PI + MathF.PI / 8f;
170+
string customCogSprite = zipMover._spriteDir + "/zipMoverCog";
171+
_cogTexture = GFX.Game[GFX.Game.Has(customCogSprite) ? customCogSprite : "objects/zipmover/cog"];
168172
}
169173

170174
public void CreateSparks() {
171-
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, from + sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkDirFromA);
172-
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, from - sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkDirFromB);
173-
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, to + sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkDirToA);
174-
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, to - sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkDirToB);
175+
Color sparkParticleColor = _zipMover._sparkParticleColor;
176+
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, _from + _sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkParticleColor, _sparkDirFromA);
177+
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, _from - _sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkParticleColor, _sparkDirFromB);
178+
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, _to + _sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkParticleColor, _sparkDirToA);
179+
SceneAs<Level>().ParticlesBG.Emit(ZipMover.P_Sparks, _to - _sparkAdd + Calc.Random.Range(-Vector2.One, Vector2.One), sparkParticleColor, _sparkDirToB);
175180
}
176181

177182
public override void Render() {
@@ -180,21 +185,22 @@ public override void Render() {
180185
}
181186

182187
private void DrawCogs(Vector2 offset, Color? colorOverride = null) {
183-
Vector2 vector = (to - from).SafeNormalize();
184-
Vector2 value = vector.Perpendicular() * 3f;
185-
Vector2 value2 = -vector.Perpendicular() * 4f;
186-
float rotation = zipMover.percent * (float)Math.PI * 2f;
187-
Draw.Line(from + value + offset, to + value + offset, colorOverride.HasValue ? colorOverride.Value : ropeColor);
188-
Draw.Line(from + value2 + offset, to + value2 + offset, colorOverride.HasValue ? colorOverride.Value : ropeColor);
189-
for (float num = 4f - zipMover.percent * (float)Math.PI * 8f % 4f; num < (to - from).Length(); num += 4f) {
190-
Vector2 value3 = from + value + vector.Perpendicular() + vector * num;
191-
Vector2 value4 = to + value2 - vector * num;
192-
Draw.Line(value3 + offset, value3 + vector * 2f + offset, colorOverride.HasValue ? colorOverride.Value : ropeLightColor);
193-
Draw.Line(value4 + offset, value4 - vector * 2f + offset, colorOverride.HasValue ? colorOverride.Value : ropeLightColor);
188+
Vector2 direction = (_to - _from).SafeNormalize();
189+
Vector2 rightRopeOffset = direction.Perpendicular() * 3f;
190+
Vector2 leftRopeOffset = -direction.Perpendicular() * 4f;
191+
192+
Draw.Line(_from + rightRopeOffset + offset, _to + rightRopeOffset + offset, colorOverride ?? _zipMover._ropeColor);
193+
Draw.Line(_from + leftRopeOffset + offset, _to + leftRopeOffset + offset, colorOverride ?? _zipMover._ropeColor);
194+
for (float num = 4f - _zipMover._percent * MathF.PI * 8f % 4f; num < (_to - _from).Length(); num += 4f) {
195+
Vector2 rightSmallRopePos = _from + rightRopeOffset + direction.Perpendicular() + direction * num;
196+
Vector2 leftSmallRopePos = _to + leftRopeOffset - direction * num;
197+
Draw.Line(rightSmallRopePos + offset, rightSmallRopePos + direction * 2f + offset, colorOverride ?? _zipMover._ropeLightColor);
198+
Draw.Line(leftSmallRopePos + offset, leftSmallRopePos - direction * 2f + offset, colorOverride ?? _zipMover._ropeLightColor);
194199
}
195200

196-
cog.DrawCentered(from + offset, colorOverride.HasValue ? colorOverride.Value : Color.White, 1f, rotation);
197-
cog.DrawCentered(to + offset, colorOverride.HasValue ? colorOverride.Value : Color.White, 1f, rotation);
201+
float cogRotation = _zipMover._percent * MathF.PI * 2f;
202+
_cogTexture.DrawCentered(_from + offset, colorOverride ?? Color.White, 1f, cogRotation);
203+
_cogTexture.DrawCentered(_to + offset, colorOverride ?? Color.White, 1f, cogRotation);
198204
}
199205
}
200206
}

Loenn/entities/boom_box.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ boomBox.placements = {
1313
data = {
1414
activationId = "",
1515
initialDelay = 0.0,
16-
startActive = true
16+
startActive = true,
17+
spriteDir = ""
1718
}
1819
}
1920

20-
local inactiveTexture = "objects/FactoryHelper/boomBox/idle00"
21-
local activeTexture = "objects/FactoryHelper/boomBox/active00"
22-
2321
function boomBox.texture(room, entity)
24-
return entity.startActive and activeTexture or inactiveTexture
22+
local spriteDir = (entity.spriteDir or "") == "" and "objects/FactoryHelper/boomBox" or entity.spriteDir
23+
return spriteDir .. (entity.startActive and "/active00" or "/idle00")
2524
end
2625

2726
boomBox.justification = {0.0, 0.0}

0 commit comments

Comments
 (0)