-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmytilemap.ts
279 lines (232 loc) · 9.36 KB
/
mytilemap.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
module scene {
export function setTileMap(map: Image, scale = TileScale.Sixteen): void {
const scene = game.currentScene();
(scene.tileMap as tiles.legacy.LegacyTilemap).setMap(map);
scene.tileMap.scale = scale;
}
export function setTile(index: number, img: Image, wall?: boolean): void {
const scene = game.currentScene();
(scene.tileMap as tiles.legacy.LegacyTilemap).setTile(index, img, !!wall);
}
}
module tiles.legacy {
class TileSet {
obstacle: boolean;
private map: TileMap;
private originalImage: Image;
private cachedImage: Image;
constructor(image: Image, collisions: boolean, map: tiles.TileMap) {
this.originalImage = image;
this.obstacle = collisions;
this.map = map;
}
get image(): Image {
const size = 1 << this.map.scale;
if (!this.cachedImage || this.cachedImage.width != size || this.cachedImage.height != size) {
if (this.originalImage.width == size && this.originalImage.height == size) {
this.cachedImage = this.originalImage;
} else {
this.cachedImage = image.create(size, size);
this.cachedImage.drawImage(this.originalImage, 0, 0);
}
}
return this.cachedImage;
}
}
export class LegacyTilemap extends tiles.TileMap {
private _mapImage: Image;
private _tileSets: TileSet[];
private _screenX: number;
public isLegacy: boolean;
constructor(scale: TileScale = TileScale.Sixteen, left = 0) {
super(scale);
this._screenX = left;
this._tileSets = [];
this.isLegacy = true;
}
get data(): TileMapData {
return null;
}
get image(): Image {
return this._mapImage;
}
myLeft(): number {
return this._screenX << this.scale;
}
myWidth(): number {
return screen.width - this.myLeft();
}
offsetX(value: number): number {
return Math.clamp(0,
Math.max(this.areaWidth() - this.myWidth(), 0),
value);
}
offsetY(value: number): number {
return Math.clamp(0, Math.max(this.areaHeight() - screen.height, 0), value);
}
areaWidth(): number {
return this._mapImage ? (this._mapImage.width << this.scale) : 0;
}
areaHeight(): number {
return this._mapImage ? (this._mapImage.height << this.scale) : 0;
}
get layer(): number {
return this._layer;
}
set layer(value: number) {
if (this._layer != value) {
this._layer = value;
}
}
get enabled(): boolean {
return !!this._mapImage;
}
setTile(index: number, img: Image, collisions?: boolean): void {
if (this.isInvalidIndex(index)) return;
this._tileSets[index] = new TileSet(img, collisions, this);
}
setMap(map: Image): void {
this._mapImage = map;
}
public getTileLegacy(col: number, row: number): Tile {
return new Tile(col, row, this);
}
public getTile(col: number, row: number): Location {
return new Location(col, row, this);
}
public setTileAt(col: number, row: number, index: number): void {
if (!this.isOutsideMap(col, row) && !this.isInvalidIndex(index))
this._mapImage.setPixel(col, row, index);
}
public getTilesByType(index: number): Location[] {
if (this.isInvalidIndex(index) || !this.enabled) return [];
const output: Location[] = [];
for (let col = 0; col < this._mapImage.width; ++col) {
for (let row = 0; row < this._mapImage.height; ++row) {
const currTile = this._mapImage.getPixel(col, row);
if (currTile === index) {
output.push(new Location(col, row, this));
}
}
}
return output;
}
public getTilesByTypeLegacy(index: number): Tile[] {
if (this.isInvalidIndex(index) || !this.enabled) return [];
const output: Tile[] = [];
for (let col = 0; col < this._mapImage.width; ++col) {
for (let row = 0; row < this._mapImage.height; ++row) {
const currTile = this._mapImage.getPixel(col, row);
if (currTile === index) {
output.push(new Tile(col, row, this));
}
}
}
return output;
}
private generateTile(index: number): TileSet {
const size = 1 << this.scale
const i = image.create(size, size);
i.fill(index);
return this._tileSets[index] = new TileSet(i, false, this);
}
private isOutsideMap(col: number, row: number): boolean {
return !this.enabled || col < 0 || col >= this._mapImage.width
|| row < 0 || row >= this._mapImage.height;
}
protected isInvalidIndex(index: number): boolean {
return index < 0 || index > 0xf;
}
// TODO: proper clipping on the left side
protected draw(target: Image, camera: scene.Camera): void {
if (!this.enabled) return;
// render tile map
const bitmask = (0x1 << this.scale) - 1;
const offsetX = camera.drawOffsetX & bitmask;
const offsetY = camera.drawOffsetY & bitmask;
const x0 = Math.max(0, camera.drawOffsetX >> this.scale);
const xn = Math.min(this._mapImage.width, ((camera.drawOffsetX + this.myWidth()) >> this.scale) + 1);
const y0 = Math.max(0, camera.drawOffsetY >> this.scale);
const yn = Math.min(this._mapImage.height, ((camera.drawOffsetY + target.height) >> this.scale) + 1);
for (let x = x0; x <= xn; ++x) {
for (let y = y0; y <= yn; ++y) {
const index = this._mapImage.getPixel(x, y);
const tile = this._tileSets[index] || this.generateTile(index);
if (tile) {
target.drawTransparentImage(
tile.image,
this.myLeft() + ((x - x0) << this.scale) - offsetX,
((y - y0) << this.scale) - offsetY
);
}
}
}
if (game.debug) {
// render debug grid overlay
for (let x = x0; x <= xn; ++x) {
const xLine = ((x - x0) << this.scale) - offsetX;
if (xLine >= 0 && xLine <= screen.width) {
target.drawLine(
xLine,
0,
xLine,
target.height,
1
);
}
}
for (let y = y0; y <= yn; ++y) {
const yLine = ((y - y0) << this.scale) - offsetY;
if (yLine >= 0 && yLine <= screen.height) {
target.drawLine(
0,
yLine,
target.width,
yLine,
1
);
}
}
}
}
public isObstacle(col: number, row: number): boolean {
if (!this.enabled) return false;
if (this.isOutsideMap(col, row)) return true;
const t = this._tileSets[this._mapImage.getPixel(col, row)];
return t && t.obstacle;
}
public getObstacle(col: number, row: number): sprites.StaticObstacle {
const index = this.isOutsideMap(col, row) ? 0 : this._mapImage.getPixel(col, row);
const tile = this._tileSets[index] || this.generateTile(index);
return new sprites.StaticObstacle(
tile.image,
row << this.scale,
col << this.scale,
this.layer,
index
);
}
public isOnWall(s: Sprite): boolean {
const hbox = s._hitbox
const left = Fx.toIntShifted(hbox.left, this.scale);
const right = Fx.toIntShifted(hbox.right, this.scale);
const top = Fx.toIntShifted(hbox.top, this.scale);
const bottom = Fx.toIntShifted(hbox.bottom, this.scale);
for (let col = left; col <= right; ++col) {
for (let row = top; row <= bottom; ++row) {
if (this.isObstacle(col, row)) {
return true;
}
}
}
return false;
}
public getTileIndex(col: number, row: number): number {
return this._mapImage.getPixel(col, row);
}
public getTileImage(index: number): Image {
if (!this._tileSets[index]) this.generateTile(index);
return this._tileSets[index].image;
}
}
}