-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.js
213 lines (179 loc) · 5.92 KB
/
sprites.js
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
"use strict";
class Picture {
#imagem;
#size;
#load;
#ready;
constructor(src) {
if (typeof src !== "string") throw new TypeError();
this.#imagem = new Image();
this.#size = undefined;
this.#ready = false;
this.#load = new Promise((resolve, reject) => {
this.#imagem.onload = () => {
this.#size = new Size(this.#imagem.width, this.#imagem.height);
resolve(this);
this.#ready = true;
};
});
this.#imagem.src = src;
}
get size() {
return this.#size;
}
get image() {
return this.#imagem;
}
get ready() {
return this.#ready;
}
async whenReady() {
return await this.#load;
}
}
class SpriteSheet {
#picture;
#board;
#spriteSize;
static async create(rows, columns, src) {
if (!isNum(rows) || !isNum(columns) || typeof src !== "string") throw new TypeError();
const img = new Picture(src);
await img.whenReady();
return new SpriteSheet(rows, columns, img);
}
constructor(rows, columns, picture) {
if (!isNum(rows) || !isNum(columns) || !(picture instanceof Picture)) throw new TypeError();
if (!picture.ready) throw new RangeError();
this.#picture = picture;
this.#board = new Board(rows, columns);
this.#spriteSize = new Size(this.#picture.size.width / columns, this.#picture.size.height / rows);
console.log(this);
}
sprite(position) {
if (!(position instanceof BoardPosition)) throw new TypeError();
return new Sprite(this.#picture, position, this.spriteSize, this);
}
at(row, column) {
if (!isNum(row) || !isNum(column)) throw new TypeError();
if (row < 0 || column < 0 || row >= this.board.rows || column >= this.board.columns) throw new RangeError();
return this.sprite(this.board.at(row, column));
}
get board() {
return this.#board;
}
get spriteSize() {
return this.#spriteSize;
}
}
class Sprite {
#picture;
#position;
#size;
#sheet;
constructor(picture, position, size, sheet) {
if (!(picture instanceof Picture)) throw new TypeError();
if (!(position instanceof BoardPosition)) throw new TypeError();
if (!(size instanceof Size)) throw new TypeError();
if (!(sheet instanceof SpriteSheet)) throw new TypeError();
this.#picture = picture;
this.#position = position;
this.#size = size;
this.#sheet = sheet;
}
draw(ctx, point) {
if (!(ctx instanceof CanvasRenderingContext2D)) throw new TypeError();
if (!(point instanceof Point)) throw new TypeError();
if (!this.#picture.ready) throw new RangeError();
ctx.drawImage(
this.#picture.image,
this.position.column * this.size.width,
this.position.row * this.size.height,
this.size.width,
this.size.height,
point.x,
point.y,
this.size.width,
this.size.height
);
}
get position() {
return this.#position;
}
get size() {
return this.#size;
}
drawTile(ctx, position) {
if (!(ctx instanceof CanvasRenderingContext2D)) throw new TypeError();
if (!(position instanceof BoardPosition)) throw new TypeError();
this.draw(ctx, position.toPoint(this.size));
}
}
class Cell {
#position;
#layers;
constructor(position, layerCount) {
if (!(position instanceof BoardPosition)) throw new TypeError();
if (!isNum(layerCount)) throw new TypeError();
if (layerCount <= 0) throw new RangeError();
this.#position = position;
this.#layers = [];
for (let i = 0; i < layerCount; i++) {
this.#layers.push(null);
}
}
get position() {
return this.#position;
}
get layerCount() {
return this.#layers.length;
}
at(layer) {
if (!isNum(layer)) throw new TypeError();
if (layer < 0 || layer >= this.layerCount) throw new RangeError();
return this.#layers[layer];
}
put(elem) {
if (!(elem instanceof Element)) throw new TypeError();
if (elem.layer < 0 || elem.layer >= this.layerCount) throw new RangeError();
this.#layers[elem.layer] = elem;
}
remove(layer) {
if (!isNum(layer)) throw new TypeError();
if (layer < 0 || layer >= this.layerCount) throw new RangeError();
this.#layers[layer] = null;
}
}
class Element {
#cell;
#spriteColumnMaker;
#layer;
constructor(cell, spriteColumnMaker, layer) {
if (!(cell instanceof Cell)) throw new TypeError();
if (!(spriteColumnMaker instanceof Function)) throw new TypeError();
if (!isNum(layer)) throw new TypeError();
if (layer < 0 || layer >= cell.layerCount) throw new RangeError();
this.#cell = cell;
this.#spriteColumnMaker = spriteColumnMaker;
this.#layer = layer;
}
get position() {
return this.#cell.position;
}
get cell() {
return this.#cell;
}
set cell(newValue) {
if (!(newValue instanceof Cell)) throw new TypeError();
this.#cell = newValue;
}
get layer() {
return this.#layer;
}
draw(quadro, sheet, ctx) {
if (!isNum(quadro)) throw new TypeError();
if (!(sheet instanceof SpriteSheet)) throw new TypeError();
if (!(ctx instanceof CanvasRenderingContext2D)) throw new TypeError();
if (quadro < 0 || quadro >= sheet.rows) throw new RangeError();
sheet.at(quadro, this.#spriteColumnMaker()).drawTile(ctx, this.position);
}
}