-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilesExt.ts
121 lines (104 loc) · 3.58 KB
/
tilesExt.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
//% blockNamespace=scene
namespace tilesExt {
import TL = tiles.Location;
export enum LineType {
Diagonal = 0,
Covering = 1
}
/**
* Calculates the locations between two given locations.
* @param lineType width of maze
* @param l1 start
* @param l2 end
* @param exclusive exclude the endpoints
* @returns The locations intersected by the line
*/
//% blickId="tilesExt_line"
//% block="locations on $lineType line from $l1 to $l2 || exclusive $exclusive"
//% exclusive.defl=false
//% inlineInputMode=inline
//% l1.shadow=mapgettile
//% l2.shadow=mapgettile
export function line(lineType: LineType, l1: TL, l2: TL, exclusive?: boolean): TL[] {
return lineType === LineType.Diagonal ? diagonalLine(l1,l2,exclusive) :
lineType === LineType.Covering ? coveringLine(l1,l2,exclusive) :
undefined;
}
function lerp(start: number, end: number, t: number) {
return start * (1.0 - t) + t * end;
}
function diagonalLine(l1: TL, l2: TL, exclusive: boolean): TL[] {
const dx = l2.col-l1.col, dy = l2.row-l1.row;
const N = Math.max(Math.abs(dx), Math.abs(dy));
if (N <= 0) {
return exclusive ? [] : [l1];
}
const res = [];
const lastN = N - (exclusive ? 1 : 0);
for (let step = (exclusive ? 1 : 0); step <= lastN; step++) {
let t = step / N;
res.push(
new TL(
Math.round(lerp(l1.col, l2.col, t)),
Math.round(lerp(l1.row, l2.row, t)),
l1.tileMap
));
}
return res;
}
export function coveringLine(l1: TL, l2: TL, exclusive: boolean): TL[] {
// https://www.redblobgames.com/grids/line-drawing/#stepping
const dx = l2.col-l1.col, dy = l2.row-l1.row;
const absx = Math.abs(dx), absy = Math.abs(dy);
const stepx = Math.sign(dx), stepy = Math.sign(dy);
let x = l1.col, y = l1.row;
let ix = 0, iy = 0;
if (exclusive) {
if ((1+ix<<1)*absy < (1+iy<<1)*absx) {
x += stepx;
ix++;
} else {
y += stepy;
iy++;
}
}
const res = [];
for (; ix < absx || iy < absy;) {
res.push(new TL(x, y, l1.tileMap));
if ((1+ix<<1)*absy < (1+iy<<1)*absx) {
x += stepx;
ix++;
} else {
y += stepy;
iy++;
}
}
if (!exclusive) {
res.push(new TL(x, y, l1.tileMap));
}
return res;
}
//% block="is $loc wall"
//% blockId=tilesExt_isWall
//% inlineInputMode=inline
//% loc.shadow=mapgettile
export function isWall(loc: TL, tileMap?: tiles.TileMap): boolean {
tileMap = tileMap || game.currentScene().tileMap;
return tileMap.isObstacle(loc.col, loc.row);
}
/**
* Get a random tile of the given type
* @param tile the type of tile to get a random selection of
*/
//% blockId=tilesExt_getRandomTilesByType
//% group=Locations
//% block="array of max $maxCount $tile locations"
//% tile.shadow=tileset_tile_picker
export function getRandomTilesByType(tile: Image, maxCount: number): TL[] {
const scene = game.currentScene();
if (!tile || !scene.tileMap)
return undefined;
const index = scene.tileMap.getImageType(tile);
return scene.tileMap.sampleTilesByType(index, maxCount);
}
}