-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
222 lines (201 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
214
215
216
217
218
219
220
221
222
const locationDataDict: { [locationKey: string]: { [key: string]: any } } = {};
namespace tiles {
function getLocationKey(location: tiles.Location): string {
return `${location.col},${location.row}`;
}
function getDataForLocation(location: tiles.Location, key: string): any {
const locationKey = getLocationKey(location);
const data = locationDataDict[locationKey];
return data ? data[key] : undefined;
}
function setDataForLocation(location: tiles.Location, key: string, value: any): void {
const locationKey = getLocationKey(location);
if (!locationDataDict[locationKey]) {
locationDataDict[locationKey] = {};
}
locationDataDict[locationKey][key] = value;
}
function getSpritesAtLocation(location: tiles.Location): Sprite[] {
const locationKey = getLocationKey(location);
const data = locationDataDict[locationKey];
const spritesAtLocation: Sprite[] = [];
if (data) {
Object.keys(data).forEach(key => {
const value = data[key];
if (value instanceof Sprite) {
spritesAtLocation.push(value);
}
});
}
return spritesAtLocation;
}
/**
* Sets a sprite in the data of a tile
*/
//% blockId=tileDataSetSprite block="set $location $key to sprite $value=variables_get(mySprite)"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=13
//% blockGap=8
export function setDataSprite(location: tiles.Location, key: string, value: Sprite): void {
setDataForLocation(location, key, value);
}
/**
* Gets a sprite in the data of a tile
*/
//% blockId=tileDataGetSprite block="$location data $key as sprite"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=12
//% blockGap=8
export function readDataSprite(location: tiles.Location, key: string): Sprite | undefined {
return getDataForLocation(location, key) as Sprite;
}
/**
* Sets a boolean in the data of a tile
*/
//% blockId=tileDataSetBoolean block="set $location $key to boolean $value"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=11
//% blockGap=8
export function setDataBoolean(location: tiles.Location, key: string, value: boolean): void {
setDataForLocation(location, key, value);
}
/**
* Gets a boolean in the data of a tile
*/
//% blockId=tileDataGetBoolean block="$location data $key as boolean"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=10
//% blockGap=8
export function readDataBoolean(location: tiles.Location, key: string): boolean {
return getDataForLocation(location, key) as boolean || false;
}
/**
* Sets a string in the data of a tile
*/
//% blockId=tileDataSetString block="set $location $key to string $value"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=9
//% blockGap=8
export function setDataString(location: tiles.Location, key: string, value: string): void {
setDataForLocation(location, key, value);
}
/**
* Gets a string in the data of a tile
*/
//% blockId=tileDataGetString block="$location data $key as string"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=8
//% blockGap=8
export function readDataString(location: tiles.Location, key: string): string {
return getDataForLocation(location, key) as string || "";
}
/**
* Sets a number in the data of a tile
*/
//% blockId=tileDataSetNumber block="set $location $key to number $value"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=7
//% blockGap=8
export function setDataNumber(location: tiles.Location, key: string, value: number): void {
setDataForLocation(location, key, value);
}
/**
* Changes a number in the data of a tile
*/
//% blockId=tileDataChangeNumber block="change $location $key data by $value"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=6
//% blockGap=8
export function changeDataNumber(location: tiles.Location, key: string, value: number): void {
let currentValue = readDataNumber(location, key);
setDataForLocation(location, key, currentValue + value);
}
/**
* Gets a number in the data of a tile
*/
//% blockId=tileDataGetNumber block="$location data $key as number"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=5
//% blockGap=8
export function readDataNumber(location: tiles.Location, key: string): number {
return getDataForLocation(location, key) as number || 0;
}
/**
* Sets an image in the data of a tile
*/
//% blockId=tileDataSetImage block="set $location $key to image $value"
//% location.shadow=mapgettile
//% value.shadow=screen_image_picker
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=4
//% blockGap=8
export function setDataImage(location: tiles.Location, key: string, value: Image): void {
setDataForLocation(location, key, value);
}
/**
* Gets an image in the data of a tile
*/
//% blockId=tileDataGetImage block="$location data $key as image"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=3
//% blockGap=8
export function readDataImage(location: tiles.Location, key: string): Image | undefined {
return getDataForLocation(location, key) as Image;
}
/**
* Move stored data from one tile to another
*/
//% blockId=tileDataMove block="move data from $originalLocation to $newLocation || moving attached sprites $moveSprites"
//% originalLocation.shadow=mapgettile
//% newLocation.shadow=mapgettile
//% expandableArgumentMode="toggle"
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=2
//% blockGap=8
export function moveData(originalLocation: tiles.Location, newLocation: tiles.Location, moveStoredSprites: boolean = false): void {
const originalKey = getLocationKey(originalLocation);
const newKey = getLocationKey(newLocation);
locationDataDict[newKey] = locationDataDict[originalKey];
delete locationDataDict[originalKey];
if (moveStoredSprites) {
getSpritesAtLocation(newLocation).forEach((sprite: Sprite) => {
tiles.placeOnTile(sprite, newLocation)
})
}
}
/**
* Cleanse the data of a tile
*/
//% blockId=tileDataCleanse block="cleanse $location data"
//% location.shadow=mapgettile
//% blockNamespace=scene color="#401255"
//% group="Data"
//% weight=1
//% blockGap=8
export function cleanseData(location: tiles.Location): void {
const locationKey = getLocationKey(location);
delete locationDataDict[locationKey];
}
}