Skip to content

Commit a479c8b

Browse files
authored
Merge pull request #91 from EverestAPI/custom_speed_move_block
Move Block with customizable speed
2 parents f4fb8f7 + 58ac13f commit a479c8b

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
module SpringCollab2020MoveBlockCustomSpeed
2+
3+
using ..Ahorn, Maple
4+
5+
@mapdef Entity "SpringCollab2020/MoveBlockCustomSpeed" MoveBlockCustomSpeed(x::Integer, y::Integer, width::Integer=Maple.defaultBlockWidth, height::Integer=Maple.defaultBlockHeight,
6+
direction::String="Up", canSteer::Bool=false, moveSpeed::Number=60)
7+
8+
const placements = Ahorn.PlacementDict()
9+
10+
buttonColor = (71, 64, 112, 255) ./ 255
11+
12+
directions = Maple.move_block_directions
13+
for direction in directions, steerable in false:true
14+
key = "Move Block ($(uppercasefirst(direction)), Custom Speed$(steerable ? ", Steerable" : "")) (Spring Collab 2020)"
15+
placements[key] = Ahorn.EntityPlacement(
16+
MoveBlockCustomSpeed,
17+
"rectangle",
18+
Dict{String, Any}(
19+
"canSteer" => steerable,
20+
"direction" => direction
21+
)
22+
)
23+
end
24+
25+
Ahorn.editingOptions(entity::MoveBlockCustomSpeed) = Dict{String, Any}(
26+
"direction" => Maple.move_block_directions
27+
)
28+
Ahorn.minimumSize(entity::MoveBlockCustomSpeed) = 16, 16
29+
Ahorn.resizable(entity::MoveBlockCustomSpeed) = true, true
30+
31+
Ahorn.selection(entity::MoveBlockCustomSpeed) = Ahorn.getEntityRectangle(entity)
32+
33+
midColor = (4, 3, 23) ./ 255
34+
highlightColor = (59, 50, 101) ./ 255
35+
36+
arrows = Dict{String, String}(
37+
"up" => "objects/moveBlock/arrow02",
38+
"left" => "objects/moveBlock/arrow04",
39+
"right" => "objects/moveBlock/arrow00",
40+
"down" => "objects/moveBlock/arrow06",
41+
)
42+
43+
button = "objects/moveBlock/button"
44+
45+
function Ahorn.render(ctx::Ahorn.Cairo.CairoContext, entity::MoveBlockCustomSpeed, room::Maple.Room)
46+
x = Int(get(entity.data, "x", 0))
47+
y = Int(get(entity.data, "y", 0))
48+
49+
50+
width = Int(get(entity.data, "width", 32))
51+
height = Int(get(entity.data, "height", 32))
52+
53+
tilesWidth = div(width, 8)
54+
tilesHeight = div(height, 8)
55+
56+
canSteer = get(entity.data, "canSteer", false)
57+
direction = lowercase(get(entity.data, "direction", "up"))
58+
arrowSprite = Ahorn.getSprite(arrows[lowercase(direction)], "Gameplay")
59+
60+
frame = "objects/moveBlock/base"
61+
if canSteer
62+
if direction == "up" || direction == "down"
63+
frame = "objects/moveBlock/base_v"
64+
65+
else
66+
frame = "objects/moveBlock/base_h"
67+
end
68+
end
69+
70+
Ahorn.drawRectangle(ctx, 2, 2, width - 4, height - 4, highlightColor, highlightColor)
71+
Ahorn.drawRectangle(ctx, 8, 8, width - 16, height - 16, midColor)
72+
73+
for i in 2:tilesWidth - 1
74+
Ahorn.drawImage(ctx, frame, (i - 1) * 8, 0, 8, 0, 8, 8)
75+
Ahorn.drawImage(ctx, frame, (i - 1) * 8, height - 8, 8, 16, 8, 8)
76+
77+
if canSteer && (direction != "up" && direction != "down")
78+
Ahorn.drawImage(ctx, button, (i - 1) * 8, -2, 6, 0, 8, 6, tint=buttonColor)
79+
end
80+
end
81+
82+
for i in 2:tilesHeight - 1
83+
Ahorn.drawImage(ctx, frame, 0, (i - 1) * 8, 0, 8, 8, 8)
84+
Ahorn.drawImage(ctx, frame, width - 8, (i - 1) * 8, 16, 8, 8, 8)
85+
86+
if canSteer && (direction == "up" || direction == "down")
87+
Ahorn.Cairo.save(ctx)
88+
89+
Ahorn.rotate(ctx, -pi / 2)
90+
Ahorn.drawImage(ctx, button, i * 8 - height - 8, -2, 6, 0, 8, 6, tint=buttonColor)
91+
Ahorn.scale(ctx, 1, -1)
92+
Ahorn.drawImage(ctx, button, i * 8 - height - 8, -2 - width, 6, 0, 8, 6, tint=buttonColor)
93+
94+
Ahorn.Cairo.restore(ctx)
95+
end
96+
end
97+
98+
Ahorn.drawImage(ctx, frame, 0, 0, 0, 0, 8, 8)
99+
Ahorn.drawImage(ctx, frame, width - 8, 0, 16, 0, 8, 8)
100+
Ahorn.drawImage(ctx, frame, 0, height - 8, 0, 16, 8, 8)
101+
Ahorn.drawImage(ctx, frame, width - 8, height - 8, 16, 16, 8, 8)
102+
103+
if canSteer && (direction != "up" && direction != "down")
104+
Ahorn.Cairo.save(ctx)
105+
106+
Ahorn.drawImage(ctx, button, 2, -2, 0, 0, 6, 6, tint=buttonColor)
107+
Ahorn.scale(ctx, -1, 1)
108+
Ahorn.drawImage(ctx, button, 2 - width, -2, 0, 0, 6, 6, tint=buttonColor)
109+
110+
Ahorn.Cairo.restore(ctx)
111+
end
112+
113+
if canSteer && (direction == "up" || direction == "down")
114+
Ahorn.Cairo.save(ctx)
115+
116+
Ahorn.rotate(ctx, -pi / 2)
117+
Ahorn.drawImage(ctx, button, -height + 2, -2, 0, 0, 8, 6, tint=buttonColor)
118+
Ahorn.drawImage(ctx, button, -10, -2, 14, 0, 8, 6, tint=buttonColor)
119+
Ahorn.scale(ctx, 1, -1)
120+
Ahorn.drawImage(ctx, button, -height + 2, -2 -width, 0, 0, 8, 6, tint=buttonColor)
121+
Ahorn.drawImage(ctx, button, -10, -2 -width, 14, 0, 8, 6, tint=buttonColor)
122+
123+
Ahorn.Cairo.restore(ctx)
124+
end
125+
126+
Ahorn.drawRectangle(ctx, div(width - arrowSprite.width, 2) + 1, div(height - arrowSprite.height, 2) + 1, 8, 8, highlightColor, highlightColor)
127+
Ahorn.drawImage(ctx, arrowSprite, div(width - arrowSprite.width, 2), div(height - arrowSprite.height, 2))
128+
end
129+
130+
end

Ahorn/lang/en_gb.lang

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,8 @@ placements.entities.SpringCollab2020/GroupedTriggerSpikesUp.tooltips.type=Change
101101
placements.entities.SpringCollab2020/GroupedTriggerSpikesDown.tooltips.type=Changes the visual appearance of the spikes.
102102
placements.entities.SpringCollab2020/GroupedTriggerSpikesLeft.tooltips.type=Changes the visual appearance of the spikes.
103103
placements.entities.SpringCollab2020/GroupedTriggerSpikesRight.tooltips.type=Changes the visual appearance of the spikes.
104+
105+
# Move Block with Customizable Speed
106+
placements.entities.SpringCollab2020/MoveBlockCustomSpeed.tooltips.canSteer=Determines whether the move block can be moved by the player.
107+
placements.entities.SpringCollab2020/MoveBlockCustomSpeed.tooltips.direction=Determines the direction the move block moves in upon activation.
108+
placements.entities.SpringCollab2020/MoveBlockCustomSpeed.tooltips.moveSpeed=The block speed, in pixels per second. Vanilla speeds are 60 for "slow", 75 for "fast".

Entities/MoveBlockCustomSpeed.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Celeste.Mod.Entities;
2+
using Microsoft.Xna.Framework;
3+
using Monocle;
4+
using System.Collections;
5+
using System.Reflection;
6+
7+
namespace Celeste.Mod.SpringCollab2020.Entities {
8+
[CustomEntity("SpringCollab2020/MoveBlockCustomSpeed")]
9+
class MoveBlockCustomSpeed : MoveBlock {
10+
private static MethodInfo moveBlockController = typeof(MoveBlock).GetMethod("Controller", BindingFlags.NonPublic | BindingFlags.Instance);
11+
private static FieldInfo moveBlockTargetSpeed = typeof(MoveBlock).GetField("targetSpeed", BindingFlags.NonPublic | BindingFlags.Instance);
12+
13+
private float moveSpeed;
14+
15+
public MoveBlockCustomSpeed(EntityData data, Vector2 offset) : base(data, offset) {
16+
moveSpeed = data.Float("moveSpeed");
17+
18+
// remove the Controller coroutine.
19+
foreach (Component component in this) {
20+
if (component.GetType() == typeof(Coroutine)) {
21+
Remove(component);
22+
break;
23+
}
24+
}
25+
26+
// replace it with our own "wrapped" coroutine.
27+
Add(new Coroutine(controllerWrapper()));
28+
}
29+
30+
private IEnumerator controllerWrapper() {
31+
IEnumerator controller = (IEnumerator) moveBlockController.Invoke(this, new object[0]);
32+
33+
bool checkNext = false;
34+
35+
while (controller.MoveNext()) {
36+
// the target speed is set just AFTER a "yield return 0.2f".
37+
// if we encounter it, we should check its value and change it if required on the next frame.
38+
if (controller.Current != null && (float) controller.Current == 0.2f) {
39+
checkNext = true;
40+
} else if (checkNext) {
41+
checkNext = false;
42+
43+
// if the speed is 60 (block is moving), replace it with our custom speed.
44+
if ((float) moveBlockTargetSpeed.GetValue(this) == 60f) {
45+
moveBlockTargetSpeed.SetValue(this, moveSpeed);
46+
}
47+
}
48+
49+
yield return controller.Current;
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)