-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodoma.js
378 lines (320 loc) · 9.08 KB
/
kodoma.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* kodoma
* @version 0.0.1
* @author Haruki-Hotarou-2021
* @license MIT
*/
// Classe para criar um canvas
class Canvas {
constructor(id, width, height, backgroundColor = "#000000") {
this.id = id;
this.width = width;
this.height = height;
this.backgroundColor = backgroundColor;
this.gridColor = '#FFFFFF';
this.getWidth();
this.getHeight();
this._getCtx();
// Cria o elemento canvas no DOM
this.element = document.createElement('canvas');
this.element.id = this.id;
this.element.width = this.width;
this.element.height = this.height;
this.element.style.backgroundColor = this.backgroundColor;
// Obtém o contexto do canvas
this.ctx = this.element.getContext('2d');
//this.centralize();
this.grid();
// Define a margin e o padding do body para 0
document.body.style.margin = 0;
document.body.style.padding = 0;
document.body.style.background = '#000000';
// Adiciona o elemento canvas ao body
document.body.appendChild(this.element);
}
_getCtx() {
const ctxt = this.ctx;
}
// Obtém a largura do canvas
getWidth() {
return this.width;
}
// Obtém a altura do canvas
getHeight() {
return this.height;
}
/*
// Define a origem das coordenadas para o centro da tela
centralize() {
this.ctx.translate(this.width / 2, this.height / 2);
// Invete o eixo Y
this.ctx.scale(1, -1);
}
*/
grid() {
// Desenha o grid
this.ctx.beginPath();
for (var x = -this.width; x <= this.width; x += 20) {
this.ctx.moveTo(x, -this.height);
this.ctx.lineTo(x, this.height);
}
for (var y = -this.height; y <= this.height; y += 20) {
this.ctx.moveTo(-this.width, y);
this.ctx.lineTo(this.width, y);
}
this.ctx.strokeStyle = this.gridColor;
this.ctx.stroke();
}
}
// Define o Título da página
function setTitle(title) {
document.title = title;
}
// Define o favicon da página
function setFavicon(url) {
const link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = url;
document.head.appendChild(link);
}
/*
// Exemplo de uso
setFavicon('player.png');
*/
// Cria um canvas
let canvas = new Canvas('canvas', window.innerWidth, window.innerHeight);
setCanvas('canvas');
function setCanvas(id) {
canvasID = document.getElementById(id);
ctx = canvasID.getContext('2d');
}
// Função que limpa o canvas
function cls() {
/*let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');*/
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Função para carregar os recursos do jogo
window.onload = function() {
onLoad();
// Função para atualizar e desenhar os recursos do jogo
function update() {
let lastTime = 0;
function loop(timestamp) {
const dt = timestamp - lastTime;
cls();
onGame(dt);
lastTime = timestamp;
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
}
update();
}
// Aguardar segundos
function wait(seconds) {
if (isNaN(seconds) || seconds < 0) {
console.error("O parâmetro 'seconds' deve ser um número não negativo");
}
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
// Classe que cria um sprite
class spr {
constructor(sprite, width, height, X, Y, canvas) {
this.sprite = new Image();
this.sprite.src = sprite;
this.width = width;
this.height = height;
this.x = X - this.width / 2;
this.y = Y - this.height / 2;
this.canvas = canvas;
this.ctxt = canvas.ctx;
this.display();
}
display() {
this.ctxt.drawImage(this.sprite, this.x, this.y, this.width, this.height);
}
}
// Classe que cria um botão
class btn {
constructor(type, width, height, x, y, color, fill, canvas) {
this.type = type;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.color = color;
this.fill = fill;
this.canvas = canvas;
this.ctxt = canvas.ctx;
this.display();
/*this.onTouch();*/
}
display() {
switch (this.type) {
case "circ":
this._desenharCirculo(this.ctxt);
break;
case "quad":
this._desenharQuadradoArredondado(this.ctxt);
break;
}
}
/*
// Função para lidar com o evento de touch no botão
onTouch(callback) {
this.ontouchstart = () => {
callback(); // Chama a função de callback passada como parâmetro
}
}
*/
_desenharCirculo(ctx) {
ctx.beginPath();
ctx.arc(this.x + this.width / 2, this.y + this.height / 2, this.width / 2, 0, 2 * Math.PI);
if (this.fill) {
ctx.fillStyle = this.color;
ctx.fill();
} else {
ctx.strokeStyle = this.color;
ctx.stroke();
}
}
_desenharQuadradoArredondado(ctx) {
const radius = 5; // Raio de arredondamento das bordas
ctx.beginPath();
ctx.moveTo(this.x + radius, this.y);
ctx.lineTo(this.x + this.width - radius, this.y);
ctx.quadraticCurveTo(this.x + this.width, this.y, this.x + this.width, this.y + radius);
ctx.lineTo(this.x + this.width, this.y + this.height - radius);
ctx.quadraticCurveTo(this.x + this.width - radius, this.y + this.height, this.x + this.x - radius, this.y + this.height);
ctx.lineTo(this.x + radius, this.y + this.height);
ctx.quadraticCurveTo(this.x, this.y + this.height - radius, this.x, this.y + this.y - radius);
ctx.lineTo(this.x, this.y + radius);
ctx.quadraticCurveTo(this.x + radius, this.y, this.x + this.x + radius, this.y);
if (this.fill) {
ctx.fillStyle = this.color;
ctx.fill();
} else {
ctx.strokeStyle = this.color;
ctx.stroke();
}
}
}
/*
function setCanvas() {
this.canvas = document.getElementById('canvas');
}
*/
// Classe que cria um círculo
class circ {
constructor(radius, x, y, fill, color, canvas) {
this.radius = radius;
this.x = x;
this.y = y;
this.fill = fill;
this.color = color;
this.canvas = canvas;
this.ctxt = canvas.ctx;
this.display();
/*this.onTouch();*/
}
display() {
this.ctxt.beginPath();
this.ctxt.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
if (this.fill) {
this.ctxt.fillStyle = this.color;
this.ctxt.fill();
} else {
this.ctxt.strokeStyle = this.color;
this.ctxt.stroke();
}
}
isInside(x, y) {
const distance = Math.sqrt((x - this.x) ** 2 + (y - this.y) ** 2);
return distance <= this.radius;
}
/*setCanvas()*/
/*
let canvas = this.canvas;
// Adiciona evento touchstart ao canvas
canvas.addEventListener('touchstart', (event) => {
const touchX = event.changedTouches[0].clientX - canvas.offsetLeft;
const touchY = event.changedTouches[0].clientY - canvas.offsetTop;
if (this.isInside(touchX, touchY)) {
// Chame sua função de callback aqui
onTouch()
console.log('Toque no círculo!');
}
});
*/
/*
setCanvas(canvas) {
this.canvas = canvas;
this._setupTouchEvents();
}
*/
/*
onTouch(callback) {
this.ontouchstart = () => {
if (this.isInside(mouseX, mouseY)) {
callback(); // Chama a função de callback passada como parâmetro
}
}
}
*/
/*
onTouch(callback) {
// No listener attached directly to the canvas
// Leverage the update loop for touch detection
let isTouching = false;
let lastTouchX;
let lastTouchY;
function handleTouchStart(event) {
if (event.type === 'touchstart') {
const touchX = event.touches[0].clientX - canvas.offsetLeft;
const touchY = event.touches[0].clientY - canvas.offsetTop;
isTouching = this.isInside(touchX, touchY);
lastTouchX = touchX;
lastTouchY = touchY;
console.log(isTouching)
}
}
function handleTouchMove(event) {
if (event.type === 'touchmove' && isTouching) {
const touchX = event.touches[0].clientX - canvas.offsetLeft;
const touchY = event.touches[0].clientY - canvas.offsetTop;
// Check if touch moved outside the circle during move
if (!this.isInside(touchX, touchY)) {
console.log(isTouching);
isTouching = false;
}
}
}
function handleTouchEnd(event) {
if (event.type === 'touchend' && isTouching) {
// Trigger callback only if touch started and ended inside the circle
callback();
console.log(isTouching);
isTouching = false;
}
}
window.addEventListener('touchstart', handleTouchStart.bind(this));
window.addEventListener('touchmove', handleTouchMove.bind(this));
window.addEventListener('touchend', handleTouchEnd.bind(this));
}
*/
}
// Função que exibe um texto na tela
function print(text, x, y, size, font, color = "white") {
/*let myCanvas = document.getElementById('canvas');
let ctx = myCanvas.getContext('2d');*/
if (!text) {
console.error("O parâmetro 'text' não pode ser vazio");
}
ctx.font = `${size}px ${font}`;
ctx.fillStyle = color;
ctx.fillText(text, x, y);
}