-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
148 lines (133 loc) · 4.34 KB
/
Main.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
let debug = false;
// Selección automática de idioma
const navLang = navigator.language.split("-")[0]; // Queremos "en", no "en-US"
if (availableLangs.includes(navLang))
{
lang = navLang;
}
// TODO pensar en una alternativa a canvas, para ver si mejora el rendimiento
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d", { alpha: false, desynchronized: true });
ctx.imageSmoothingEnabled = false;
const inputs = new Map();
GameScreen.imgScale = 1;
GameScreen.inputs = inputs;
GameScreen.ctx = ctx;
GameScreen.previousScreen; // Useful when resuming from pause menu
GameScreen.currentScreen = new LoadingScreen();
GameScreen.width = canvas.width;
GameScreen.height = canvas.height;
GameScreen.drawDistance = Math.sqrt(GameScreen.width * GameScreen.width + GameScreen.height * GameScreen.height);
window.onbeforeunload = function() {
return "MOLI";
};
// Desaparición del cursor cuando está inactivo sobre el canvas
{
let mouseTimer = null;
let cursorVisible = true;
function disappearCursor() {
mouseTimer = null;
canvas.style.cursor = "none";
cursorVisible = false;
}
canvas.onmousemove = function() {
if (mouseTimer) {
clearTimeout(mouseTimer);
}
if (!cursorVisible) {
canvas.style.cursor = "default";
cursorVisible = true;
}
mouseTimer = setTimeout(disappearCursor, 2500);
};
}
window.addEventListener("keydown", (event) => {
if (event.key === "F5") {
return;
}
if (event.key === "Tab") {
event.preventDefault();
event.stopPropagation();
return;
}
const key = event.key.toLowerCase();
if (inputs.has(key)) {
const input = inputs.get(key);
if (input.isResetted()) {
input.activate();
}
} else {
const newInput = new Input();
inputs.set(key, newInput);
newInput.activate();
}
if (inputs.has("AnyKey")) {
if (Input.anyKey.isResetted()) {
Input.anyKey.activate();
}
} else {
inputs.set("AnyKey", Input.anyKey);
Input.anyKey.activate();
}
});
window.addEventListener("keyup", (event) => {
const key = event.key.toLowerCase();
inputs.get(key)?.reset();
inputs.get("AnyKey")?.reset();
});
// if (navigator.maxTouchPoints)
// {
// window.addEventListener("touchstart", (event) => {
// if (event.touches.length === 0) {
// return;
// }
// // NOTE: Tal vez convenga ubicar el criterio de si es enter, left o right en las distintas pantallas de juego
// // Por ahora se añade la escucha a ratón para probar el soporte en pantallas táctiles
// // WARNING: Puede haber bug si se utiliza el teclado al mismo tiempo
// // NOTE: Tal vez convenga que las opciones en los menús se alineen horizontalmente
// let inputToAdd = "Enter";
// let x = 0.5;
// if (event.touches.length) {
// x = event.touches[0].clientX / document.body.clientWidth;
// }
// if (x < 0.4)
// {
// inputToAdd = "ArrowLeft";
// }
// else if (x > 0.6)
// {
// inputToAdd = "ArrowRight";
// }
// if (inputs.has(inputToAdd)) {
// const input = inputs.get(inputToAdd);
// if (input.isResetted()) {
// input.activate();
// }
// } else {
// inputs.set(inputToAdd, new Input());
// }
// if (inputs.has("AnyKey")) {
// if (Input.anyKey.isResetted()) {
// Input.anyKey.activate();
// }
// } else {
// inputs.set("AnyKey", Input.anyKey);
// }
// });
// window.addEventListener("touchend", (event) => {
// inputs.get("Enter")?.reset();
// inputs.get("ArrowLeft")?.reset();
// inputs.get("ArrowRight")?.reset();
// inputs.get("AnyKey")?.reset();
// });
// }
function loop() {
GameScreen.currentScreen.runIteration();
for (const input of GameScreen.inputs.values()) {
input.consumeIfActivated(); // Si la iteración actual no consumió el input, hay que consumirlo para que no se malinterprete en la próxima iteración
}
}
function init() {
setInterval(() => requestAnimationFrame(loop), 1000 / 60);
}
init();