-
Notifications
You must be signed in to change notification settings - Fork 2
/
tilemap.ts
325 lines (292 loc) · 9.66 KB
/
tilemap.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/// <reference path="utils.ts" />
/// <reference path="geom.ts" />
/// <reference path="sprite.ts" />
// TileMap
//
class TileMap {
tilesize: number;
width: number;
height: number;
bounds: Rect;
map: Int32Array[];
private _rangemap: { [index:string]: RangeMap } = {};
constructor(tilesize: number, width: number, height: number,
map: Int32Array[]=null) {
this.tilesize = tilesize;
this.width = width;
this.height = height;
if (map === null) {
map = range(height).map(() => { return new Int32Array(width); });
}
this.map = map;
this.bounds = new Rect(0, 0,
this.width*this.tilesize,
this.height*this.tilesize);
}
toString() {
return '<TileMap: '+this.width+','+this.height+'>';
}
get(x: number, y: number): number {
if (0 <= x && 0 <= y && x < this.width && y < this.height) {
return this.map[y][x];
} else {
return -1;
}
}
set(x: number, y: number, c: number) {
if (0 <= x && 0 <= y && x < this.width && y < this.height) {
this.map[y][x] = c;
this._rangemap = {};
}
}
fill(c: number, rect: Rect=null) {
if (rect === null) {
rect = new Rect(0, 0, this.width, this.height);
}
for (let dy = 0; dy < rect.height; dy++) {
let y = rect.y+dy;
for (let dx = 0; dx < rect.width; dx++) {
let x = rect.x+dx;
this.map[y][x] = c;
}
}
this._rangemap = {};
}
copy(): TileMap {
let map:Int32Array[] = [];
for (let a of this.map) {
map.push(a.slice());
}
return new TileMap(this.tilesize, this.width, this.height, map);
}
coord2map(rect: Vec2|Rect): Rect {
let ts = this.tilesize;
if (rect instanceof Rect) {
let x0 = Math.floor(rect.x/ts);
let y0 = Math.floor(rect.y/ts);
let x1 = Math.ceil((rect.x+rect.width)/ts);
let y1 = Math.ceil((rect.y+rect.height)/ts);
return new Rect(x0, y0, x1-x0, y1-y0);
} else {
let x = Math.floor(rect.x/ts);
let y = Math.floor(rect.y/ts);
return new Rect(x, y, 1, 1);
}
}
map2coord(rect: Vec2|Rect): Rect {
let ts = this.tilesize;
if (rect instanceof Vec2) {
return new Rect(rect.x*ts, rect.y*ts, ts, ts);
} else if (rect instanceof Rect) {
return new Rect(rect.x*ts, rect.y*ts,
rect.width*ts, rect.height*ts);
} else {
return null;
}
}
apply(f: (x:number,y:number,c:number)=>boolean, rect: Rect=null): Vec2 {
if (rect === null) {
rect = new Rect(0, 0, this.width, this.height);
}
for (let dy = 0; dy < rect.height; dy++) {
let y = rect.y+dy;
for (let dx = 0; dx < rect.width; dx++) {
let x = rect.x+dx;
let c = this.get(x, y);
if (f(x, y, c)) {
return new Vec2(x,y);
}
}
}
return null;
}
shift(vx: number, vy: number, rect: Rect=null) {
if (rect === null) {
rect = new Rect(0, 0, this.width, this.height);
}
let src:Int32Array[] = [];
for (let dy = 0; dy < rect.height; dy++) {
let a = new Int32Array(rect.width);
for (let dx = 0; dx < rect.width; dx++) {
a[dx] = this.map[rect.y+dy][rect.x+dx];
}
src.push(a);
}
for (let dy = 0; dy < rect.height; dy++) {
for (let dx = 0; dx < rect.width; dx++) {
let x = (dx+vx + rect.width) % rect.width;
let y = (dy+vy + rect.height) % rect.height;
this.map[rect.y+y][rect.x+x] = src[dy][dx];
}
}
}
findTile(f0: (c:number)=>boolean, rect: Rect=null): Vec2 {
return this.apply((x,y,c)=>{return f0(c);}, rect);
}
findTileByCoord(f0: (c:number)=>boolean, range: Rect): Rect {
let p = this.apply((x,y,c)=>{return f0(c);}, this.coord2map(range));
return (p === null)? null : this.map2coord(p);
}
getTileRects(f0: (c:number)=>boolean, range:Rect): Rect[] {
let ts = this.tilesize;
let rects = [] as Rect[];
let f = (x:number, y:number, c:number) => {
if (f0(c)) {
rects.push(new Rect(x*ts, y*ts, ts, ts));
}
return false;
}
this.apply(f, this.coord2map(range));
return rects;
}
getRangeMap(key:string, f: (c:number)=>boolean): RangeMap {
let map = this._rangemap[key];
if (map === undefined) {
map = new RangeMap(this, f);
this._rangemap[key] = map;
}
return map;
}
render(ctx: CanvasRenderingContext2D, sprites: SpriteSheet) {
this.renderFromBottomLeft(
ctx, (x,y,c) => { return sprites.get(c); });
}
renderFromBottomLeft(
ctx: CanvasRenderingContext2D,
ft: (x:number,y:number,c:number)=>Sprite,
x0=0, y0=0, w=0, h=0) {
// Align the pos to the bottom left corner.
let ts = this.tilesize;
w = (w != 0)? w : this.width;
h = (h != 0)? h : this.height;
// Draw tiles from the bottom-left first.
for (let dy = h-1; 0 <= dy; dy--) {
let y = y0+dy;
for (let dx = 0; dx < w; dx++) {
let x = x0+dx;
let c = this.get(x, y);
let sprite = ft(x, y, c);
if (sprite !== null) {
ctx.save();
ctx.translate(ts*dx, ts*dy);
sprite.render(ctx);
ctx.restore();
}
}
}
}
renderFromTopRight(
ctx: CanvasRenderingContext2D,
ft: (x:number,y:number,c:number)=>Sprite,
x0=0, y0=0, w=0, h=0) {
// Align the pos to the bottom left corner.
let ts = this.tilesize;
w = (w != 0)? w : this.width;
h = (h != 0)? h : this.height;
// Draw tiles from the top-right first.
for (let dy = 0; dy < h; dy++) {
let y = y0+dy;
for (let dx = w-1; 0 <= dx; dx--) {
let x = x0+dx;
let c = this.get(x, y);
let sprite = ft(x, y, c);
if (sprite !== null) {
ctx.save();
ctx.translate(ts*dx, ts*dy);
sprite.render(ctx);
ctx.restore();
}
}
}
}
renderWindow(
ctx: CanvasRenderingContext2D,
window:Rect, sprites: SpriteSheet) {
this.renderWindowFromBottomLeft(
ctx, window, (x,y,c) => { return sprites.get(c); });
}
renderWindowFromBottomLeft(
ctx: CanvasRenderingContext2D,
window: Rect,
ft: (x:number,y:number,c:number)=>Sprite) {
let ts = this.tilesize;
let x0 = Math.floor(window.x/ts);
let y0 = Math.floor(window.y/ts);
let x1 = Math.ceil((window.x+window.width)/ts);
let y1 = Math.ceil((window.y+window.height)/ts);
ctx.save();
ctx.translate(x0*ts-window.x, y0*ts-window.y);
this.renderFromBottomLeft(
ctx, ft,
x0, y0, x1-x0+1, y1-y0+1);
ctx.restore();
}
renderWindowFromTopRight(
ctx: CanvasRenderingContext2D,
window: Rect,
ft: (x:number,y:number,c:number)=>Sprite) {
let ts = this.tilesize;
let x0 = Math.floor(window.x/ts);
let y0 = Math.floor(window.y/ts);
let x1 = Math.ceil((window.x+window.width)/ts);
let y1 = Math.ceil((window.y+window.height)/ts);
ctx.save();
ctx.translate(x0*ts-window.x, y0*ts-window.y);
this.renderFromTopRight(
ctx, ft,
x0, y0, x1-x0+1, y1-y0+1);
ctx.restore();
}
}
// RangeMap
//
class RangeMap {
width: number;
height: number;
private _data: Int32Array[];
constructor(tilemap: TileMap, f: (c:number)=>boolean) {
let data = new Array(tilemap.height+1);
let row0 = new Int32Array(tilemap.width+1);
for (let x = 0; x < tilemap.width; x++) {
row0[x+1] = 0;
}
data[0] = row0;
for (let y = 0; y < tilemap.height; y++) {
let row1 = new Int32Array(tilemap.width+1);
let n = 0;
for (let x = 0; x < tilemap.width; x++) {
if (f(tilemap.get(x, y))) {
n++;
}
row1[x+1] = row0[x+1] + n;
}
data[y+1] = row1;
row0 = row1;
}
this.width = tilemap.width;
this.height = tilemap.height;
this._data = data;
}
get(x0: number, y0: number, x1: number, y1: number): number {
let t: number;
if (x1 < x0) {
t = x0; x0 = x1; x1 = t;
// assert(x0 <= x1);
}
if (y1 < y0) {
t = y0; y0 = y1; y1 = t;
// assert(y0 <= y1);
}
x0 = clamp(0, x0, this.width);
y0 = clamp(0, y0, this.height);
x1 = clamp(0, x1, this.width);
y1 = clamp(0, y1, this.height);
return (this._data[y1][x1] - this._data[y1][x0] -
this._data[y0][x1] + this._data[y0][x0]);
}
exists(rect: Rect): boolean {
return (this.get(rect.x, rect.y,
rect.x+rect.width,
rect.y+rect.height) !== 0);
}
}