Repository navigation
Expand file tree
/
Copy pathJavaScripting.js
More file actions
324 lines (286 loc) · 9.27 KB
/
Copy pathJavaScripting.js
File metadata and controls
324 lines (286 loc) · 9.27 KB
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
window.MovingL = false
window.MovingR = false
window.MovingU = false
window.MovingD = false
let Speed = 5
let positionX = 60
let positionY = 500
let schroum = new Audio("schroum.m4a")
let dim_piou = { width: 536/5*1.5, height: 305/5*1.5 };
function init(){
let porte1 = document.getElementById("porte1")
porte1.style.left = "1835px"
porte1.style.top = "500px"
let porte2 = document.getElementById("porte2")
porte2.style.left = "1718px"
porte2.style.top = "530px"
let piou = document.getElementById("piou")
piou.style.left = "60px"
piou.style.top = "500px"
let crumb1 = document.getElementById("crumb1")
crumb1.style.left = "1400px"
crumb1.style.top = "550px"
let crumb2 = document.getElementById("crumb2")
crumb2.style.left = "1100px"
crumb2.style.top = "850px"
let crumb3 = document.getElementById("crumb3")
crumb3.style.left = "200px"
crumb3.style.top = "850px"
let crumb4 = document.getElementById("crumb4")
crumb4.style.left = "1300px"
crumb4.style.top = "300px"
let poubelle = document.getElementById("poubelle")
poubelle.style.left = "1400px"
poubelle.style.top = "650px"
let chaise = document.getElementById("chaise")
chaise.style.left = "1250px"
chaise.style.top = "520px"
let peluche = document.getElementById("peluche")
peluche.style.left = "5px"
peluche.style.top = "750px"
let bureau = document.getElementById("bureau")
bureau.style.left = "1400px"
bureau.style.top = "300px"
let lit = document.getElementById("lit")
lit.style.left = "500px"
lit.style.top = "300px"
let commode1 = document.getElementById("commode1")
commode1.style.left = "900px"
commode1.style.top = "760px"
let commode2 = document.getElementById("commode2")
commode2.style.left = "900px"
commode2.style.top = "600px"
let Mur = document.getElementById("Mur")
Mur.style.left = "1865px"
Mur.style.top = "300px"
let Mur2 = document.getElementById("Mur2")
Mur2.style.left = "10px"
Mur2.style.top = "920px"
}
function update() {
let piou = document.getElementById("piou")
let piouDim = piou.getBoundingClientRect();
let NouvPiouDim = { left: piouDim.left, top:piouDim.top, right:piouDim.right, bottom:piouDim.bottom, width: piouDim.width, height: piouDim.height };
if (document.getElementById("porte1")) {
let porte1Dim = porte1.getBoundingClientRect();
if (isIntersecting(porte1Dim, NouvPiouDim)) {
console.log("porte1");
Level_2()
}
}
if (document.getElementById("porte2")) {
let porte2Dim = porte2.getBoundingClientRect();
if (isIntersecting(porte2Dim, NouvPiouDim)) {
console.log("porte2");
schroum.play();
removeImage(porte2)
}
}
if (document.getElementById("crumb1")) {
let crumb1Dim = crumb1.getBoundingClientRect();
if (isIntersecting(crumb1Dim, NouvPiouDim)) {
console.log("boo");
removeImage(crumb1);
schroum.play();
mangeMiette()
}
}
if (document.getElementById("crumb2")) {
let crumb2Dim = crumb2.getBoundingClientRect();
if (isIntersecting(crumb2Dim, NouvPiouDim)) {
console.log("boo");
removeImage(crumb2);
schroum.play();
mangeMiette()
}
}
if (document.getElementById("crumb3")) {
let crumb3Dim = crumb3.getBoundingClientRect();
if (isIntersecting(crumb3Dim, NouvPiouDim)) {
console.log("boo");
removeImage(crumb3);
schroum.play();
mangeMiette()
}
}
if (document.getElementById("crumb4")) {
let crumb4Dim = crumb4.getBoundingClientRect();
if (isIntersecting(crumb4Dim, NouvPiouDim)) {
console.log("boo");
removeImage(crumb4);
schroum.play();
mangeMiette()
}
}
if (manageIntersect("L"))
MovingL = false;
if (manageIntersect("R"))
MovingR = false;
if (manageIntersect("U"))
MovingU = false;
if (manageIntersect("D"))
MovingD = false;
if (piouDim.left - Speed < 0) MovingL = false;
if (piouDim.right + Speed > window.innerWidth) MovingR = false;
if (piouDim.top - Speed < 216) MovingU = false;
if (piouDim.bottom + Speed > window.innerHeight) MovingD = false;
if (MovingL) {
positionX -= Speed;
}
if (MovingR) {
positionX += Speed;
}
if (MovingU) {
positionY -= Speed;
}
if (MovingD) {
positionY += Speed;
}
piou.style.left = positionX + "px";
piou.style.top = positionY + "px";
requestAnimationFrame(update);
}
function manageIntersect(direction) {
let piou = document.getElementById("piou")
let piouDim = piou.getBoundingClientRect();
let NouvPiouDim = { left: piouDim.left, top:piouDim.top, right:piouDim.right, bottom:piouDim.bottom, width: piouDim.width, height: piouDim.height };
if (direction === "R")
NouvPiouDim.left += Speed
if (direction === "L")
NouvPiouDim.left -= Speed
if (direction === "U")
NouvPiouDim.top -= Speed
if (direction === "D"){
NouvPiouDim.top += Speed
}
NouvPiouDim.right = NouvPiouDim.left + piouDim.width
NouvPiouDim.bottom = NouvPiouDim.top + piouDim.height
for (const otherElement of document.querySelectorAll('img.image')) {
if (piou != otherElement) {
let mobilierDim = otherElement.getBoundingClientRect();
if (isIntersecting(NouvPiouDim, mobilierDim)){
return true;
}
}
}
return false;
}
document.addEventListener("keydown", (event) => {
const piou = document.getElementById("piou");
if (event.key === "ArrowLeft") {
MovingL = true;
piou.src = "piou haut gauche.png";
setPiouDimensions(piou, "horizontal");
}
if (event.key === "ArrowRight") {
MovingR = true;
piou.src = "piou haut droit.png";
setPiouDimensions(piou, "horizontal");
}
if (event.key === "ArrowUp") {
MovingU = true;
piou.src = "piou haut nord.png";
setPiouDimensions(piou, "vertical");
}
if (event.key === "ArrowDown") {
MovingD = true;
piou.src = "piou haut sud.png";
setPiouDimensions(piou, "vertical");
}
if (event.key === "Shift") {
Speed = 10;
}
});
document.addEventListener("keyup", (event) => {
if (event.key === "ArrowLeft") {
MovingL = false
}
if (event.key === "ArrowRight") {
MovingR = false
}
if (event.key === "ArrowUp") {
MovingU = false
}
if (event.key === "ArrowDown") {
MovingD = false
}
if (event.key === "Shift") {
Speed = 5
}
}
)
document.querySelectorAll('.dir-btn').forEach(btn => {
const dir = btn.dataset.direction;
btn.addEventListener('touchstart', () => {
if (dir === 'left') MovingL = true;
if (dir === 'right') MovingR = true;
if (dir === 'up') MovingU = true;
if (dir === 'down') MovingD = true;
if (dir === 'shift') Speed = 10;
});
btn.addEventListener('touchend', () => {
if (dir === 'left') MovingL = false;
if (dir === 'right') MovingR = false;
if (dir === 'up') MovingU = false;
if (dir === 'down') MovingD = false;
if (dir === 'shift') Speed = 5;
});
});
function setPiouDimensions(piou, orientation) {
// Sauvegarder les dimensions actuelles
const currentWidth = piou.offsetWidth;
const currentHeight = piou.offsetHeight;
// Définir les nouvelles dimensions en fonction de l'orientation
let newWidth, newHeight;
if (orientation === "vertical") {
newWidth = dim_piou.height; // Largeur pour l'orientation verticale
newHeight = dim_piou.width; // Hauteur pour l'orientation verticale
} else if (orientation === "horizontal") {
newWidth = dim_piou.width; // Largeur pour l'orientation horizontale
newHeight = dim_piou.height; // Hauteur pour l'orientation horizontale
}
// Appliquer temporairement les nouvelles dimensions pour tester les collisions
piou.style.width = `${newWidth}px`;
piou.style.height = `${newHeight}px`;
// Vérifier si le personnage entre en collision avec un obstacle
const piouDim = piou.getBoundingClientRect();
for (const obstacle of document.querySelectorAll('img.image')) {
const obstacleDim = obstacle.getBoundingClientRect();
if (isIntersecting(piouDim, obstacleDim)) {
// Collision détectée, annuler le changement de dimensions
piou.style.width = `${currentWidth}px`;
piou.style.height = `${currentHeight}px`;
console.log("Collision détectée, dimensions annulées.");
return;
}
}
// Si aucune collision, conserver les nouvelles dimensions
//console.log("Dimensions changées avec succès.");
}
document.getElementById("counter").textContent = "Crumbs eaten : " + getCounter();
function removeImage(img) {
img.remove();
}
init();
update();
function isIntersecting(rect1, rect2) {
return (rect1.left < rect2.right && rect1.right > rect2.left && rect1.top < rect2.bottom && rect1.bottom > rect2.top);
}
function Home() {
location.replace("Home.html")
}
function Level_2() {
location.replace("Level 2.html")
}
function Level_3() {
location.replace("Level 3.html")
}
function Credits() {
location.replace("Credits.html")
}
function mangeMiette() {
let currentCount = getCounter();
currentCount++;
setCounter(currentCount);
document.getElementById("counter").textContent = "Crumbs eaten : " + currentCount;
}