-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
213 lines (191 loc) · 7.36 KB
/
main.ts
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
/**
* Fun with castles!
* Build your castle walls and towers.
* Functions are mapped to blocks using various macros
* in comments starting with %. The most important macro
* is "block", and it specifies that a block should be
* generated for an **exported** function.
*/
//% color="#AA278D" weight=100
namespace castles {
function drawRectangle(length: number, width: number, blockType: number) {
for (let index = 0; index < 2; index++) {
builder.mark()
builder.move(FORWARD, length - 1)
builder.tracePath(blockType)
builder.turn(LEFT_TURN)
builder.mark()
builder.move(FORWARD, width - 1)
builder.tracePath(blockType)
builder.turn(LEFT_TURN)
}
}
function drawAlternatingRectangle(length: number, width: number, blockType: number) {
function drawAlternatingLine(l: number, placeAtStart: boolean) {
let putBlock = placeAtStart;
for (let index2 = 0; index2 < l - 1; index2++) {
builder.move(FORWARD, 1)
if (putBlock) {
builder.place(blockType)
}
putBlock = !(putBlock)
}
return putBlock
}
builder.place(blockType)
for (let index3 = 0; index3 < 2; index3++) {
let putBlock2 = drawAlternatingLine(length, false)
builder.turn(LEFT_TURN)
drawAlternatingLine(width, putBlock2)
builder.turn(LEFT_TURN)
}
}
/**
* Build a wall
*/
//% block="build castle wall || made of $blockType width $width length $length height $height"
//% width.defl=3
//% length.defl=27
//% height.defl=6
//% blockType.defl=Block.Cobblestone
//% expandableArgumentMode="toggle"
//% blockType.fieldEditor="gridpicker"
//% blockType.fieldOptions.width=340 blockType.fieldOptions.columns=8 blockType.fieldOptions.tooltips=true
//% blockType.fieldOptions.tooltipsXOffset="20" blockType.fieldOptions.tooltipsYOffset="-20"
//% blockType.fieldOptions.maxRows="8"
//% blockType.fieldOptions.hasSearchBar=true
//% blockType.fieldOptions.hideRect=true
//% width.min=1 width.max=1000
//% length.min=1 length.max=1000
//% height.min=1 height.max=1000
//% inlineInputMode="external"
//% weight=340
export function buildCastleWall(width: number = 3, length: number = 27, height: number = 6, blockType: Block = Block.Cobblestone) {
builder.place(CYAN_STAINED_GLASS)
builder.shift(-Math.floor(width / 2), 0, -Math.floor(width / 2))
// build 2 walls from bottom to top (up to WallHeight)
for (let index4 = 0; index4 < height - 1; index4++) {
drawRectangle(length, width, blockType)
builder.move(UP, 1)
}
drawAlternatingRectangle(length, width, blockType)
// mov builder to center on other wall's end ( added -1 as hotfix :/ )
builder.shift(length - Math.floor(width / 2) - 1, 1 - height, Math.floor(width / 2))
// verify it's ok
builder.place(CYAN_WOOL)
}
/**
* Build a square tower
*/
//% block="build castle tower || shape square|made of $blockType width $width height $height"
//% width.defl=5
//% height.defl=8
//% blockType.defl=Block.Cobblestone
//% expandableArgumentMode="toggle"
//% blockType.fieldEditor="gridpicker"
//% blockType.fieldOptions.width=340 blockType.fieldOptions.columns=8 blockType.fieldOptions.tooltips=true
//% blockType.fieldOptions.tooltipsXOffset="20" blockType.fieldOptions.tooltipsYOffset="-20"
//% blockType.fieldOptions.maxRows="8"
//% blockType.fieldOptions.hasSearchBar=true
//% blockType.fieldOptions.hideRect=true
//% width.min=1 width.max=1000
//% height.min=1 height.max=1000
//% inlineInputMode="external"
//% weight=350
export function buildCastleTower(width: number = 5, height: number = 8, blockType: Block = Block.Cobblestone) {
// builder is at the center of the tower - shift it to a corner
builder.shift(- Math.floor(width / 2), 0, - Math.floor(width / 2))
// build 4 walls from bottom to top (up to TowerHeight)
for (let index = 0; index <= height; index++) {
// add 4 walls
drawRectangle(width, width, blockType);
builder.move(UP, 1)
}
// build the upper part, larger by 1
// shift in diagonal by 1 block
builder.shift(-1, 0, -1)
for (let builderHeight = 0; builderHeight < 2; builderHeight++) {
// add 4 walls
drawRectangle(width + 2, width + 2, blockType)
builder.move(UP, 1)
}
// put crenelation on the last level
drawAlternatingRectangle(width + 2, width + 2, blockType)
// mov builder back to center
builder.shift(Math.floor(width / 2) + 1, 0 - (height + 3), Math.floor(width / 2) + 1)
}
/**
* Build a simple castle with four towers and four walls around the player
*/
//% block="build a simple castle."
//% weight=250
export function buildBasicCastle() {
let originalPlayerPosition = player.position()
// move builder back to player position to allow repeat usage
builder.teleportTo(originalPlayerPosition)
// start off facing left of the player
builder.face(getDirectionLeftOfPlayer())
// shift away from player
builder.shift(-13, 0, -13)
for (let index = 0; index < 4; index++) {
buildCastleTower(5, 8)
buildCastleWall(3, 27, 6)
builder.turn(LEFT_TURN)
}
}
/**
* Build a castle in the sky.
*/
//% block="Build a castle in the sky."
//% weight=150
export function buildCastleInTheSky() {
let originalPlayerPosition = player.position()
builder.teleportTo(originalPlayerPosition)
builder.face(getDirectionLeftOfPlayer())
builder.turn(RIGHT_TURN)
builder.shift(24, 20, 0)
shapes.circle(Block.Wool, builder.position(), 23, Axis.Y, ShapeOperation.Replace)
builder.shift(-13, 1, -13)
for (let index7 = 0; index7 < 4; index7++) {
buildCastleTower()
buildCastleWall()
builder.turn(LEFT_TURN)
}
}
/**
* Grow a bean stalk to reach a castle in the sky.
*/
//% block="Grow a bean stalk| to reach a castle in the sky."
//% inlineInputMode="external"
//% weight=145
export function growBeanStalk() {
blocks.fill(
GREEN_WOOL,
player.position(),
pos(0, 20, 0),
FillOperation.Replace
)
blocks.fill(
LADDER,
player.position(),
positions.add(
player.position(),
pos(0, 40, 0)
),
FillOperation.Keep
)
}
function getDirectionLeftOfPlayer(): CompassDirection {
let playerOrientation = player.getOrientation(); // between -180 && 180
if (playerOrientation >= -45 && playerOrientation < 45) {
return CompassDirection.East
}
if (playerOrientation >= 45 && playerOrientation < 135) {
return CompassDirection.South
}
if (playerOrientation >= -135 && playerOrientation < -45) {
return CompassDirection.West
}
return CompassDirection.North
}
}