-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock Time.js
358 lines (305 loc) · 10.2 KB
/
Clock Time.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
let factorScale = 1;
let canvas;
let background;
let dimXBackground = 1600;
let dimYBackground = 800;
let cloud;
let dimXCloud = 140;
let dimYCloud = 100;
let posXCloud1 = 1350;
let posYCloud1 = 150;
let posXCloud2 = 820;
let posYCloud2 = 200;
let panel;
let dimXPanel = 690;
let dimYPanel = 270;
let posXPanel = 55;
let posYPanel = 90;
let blockQm;
let dimXBlockQm = 120;
let dimYBlockQm = 120;
let posXBlockQm = 930;
let posYBlockQm = 175;
let block;
let dimXBlock = 120;
let dimYBlock = 120;
let posXBlock1 = 1050;
let posYBlock1 = 175;
let posXBlock2 = 1170;
let posYBlock2 = 175;
let posXBlock3 = 1290;
let posYBlock3 = 175;
let lenJumpBlock3 = 15;
let marioFont;
let textFontSize = 85;
let posXHour = 1114;
let posYHour = 285;
let posXHourShadow = 1110;
let posYHourShadow = 275;
let posXMinute = 1234;
let posYMinute = 285;
let posXMinuteShadow = 1230;
let posYMinuteShadow = 275;
let posXSecond = 1354;
let posYSecond = 285;
let posXSecondShadow = 1350;
let posYSecondShadow = 275;
let marioStand;
let dimXMarioStand = 200;
let dimYMarioStand = 200;
let posXMarioStand = posXBlock3-20;
let posYMarioStand = 389;
let marioJump;
let dimXMarioJump = 200;
let dimYMarioJump = 200;
let posXMarioJump = posXBlock3-20;
let posYMarioJump = posYMarioStand;
let milliseconds;
let jumpScale = 600;
let groundY = 389;
let lastSecond = -1;
let marioJumpSound;
let coin;
let dimXCoin = 120;
let dimYCoin = 120;
let posXCoin = posXBlockQm;
let posYCoin = 175;
let coinSound;
let lastMinute = -1;
let coinBounceScale = 1000;
let blockY = 175;
let goomba;
let dimXGoomba = 140;
let dimYGoomba = 140;
let posXGoombaStart = 400;
let posXGoombaCurrent = 400;
let posYGoombaStart = 449;
let flagDx = 1;
let flagSx = 0;
let speed = 1;
let thesholdDx = 160;
let thesholdSx = 160;
let iframe;
let container;
let valueHIframe = (0.6*dimYPanel);
let valueXIframe = (dimXPanel*0.9);
let posXIframe = (posXPanel*1.6);
let posYIframe = (posYPanel*2);
let textSizeScores = 60;
let totalScore = 0;
let posXTotalScoreText = 850;
let posYTotalScoreText = 70;
let totalCoins = 0;
let posXTotalCoinsText = 1020;
let posYTotalCoinsText = 70;
let posXTotalWorldText = 1180;
let posYTotalWorldText = 70;
let totalTime = 0;
let posXTotalTimeText = 1330;
let posYTotalTimeText = 70;
let posXTotalLivesText = 1480;
let posYTotalLivesText = 70;
let audioIcon;
let dimXAudioIcon = 70;
let dimYAudioIcon = 70;
let posXAudioIcon = 1500;
let posYAudioIcon = 150;
let audioOn = true;
function preload(){
background = loadImage('background.png');
cloud = loadImage('cloud.png');
panel = loadImage('panel.png');
blockQm = loadImage('block_qm.png');
block = loadImage('block.png');
marioStand = loadImage('mario_stand1.png');
marioJump = loadImage('mario_jump1.png');
coin = loadImage('coin.png');
goomba = loadImage('goomba.png');
marioFont = loadFont('Super Plumber Brothers.ttf');
marioJumpSound = loadSound('mario_jump_sound.mp3');
coinSound = loadSound('coin_sound.mp3');
}
function mousePressed() {
userStartAudio();
}
function touchMoved() {
userStartAudio();
}
function setup(){
// Calculate scale factor to adapt image of display dimensions
factorScale = min(windowWidth / dimXBackground, windowHeight / dimYBackground)*1;
canvas = createCanvas(dimXBackground * factorScale, dimYBackground * factorScale);
container = createDiv();
container.style('position','relative');
canvas.parent(container);
// resize images to keep same proportions
background.resize(dimXBackground * factorScale, dimYBackground * factorScale);
cloud.resize(dimXCloud * factorScale, dimYCloud * factorScale);
panel.resize(dimXPanel * factorScale, dimYPanel * factorScale);
blockQm.resize(dimXBlockQm * factorScale, dimYBlockQm * factorScale);
block.resize(dimXBlock * factorScale, dimYBlock * factorScale);
marioStand.resize(dimXMarioStand * factorScale, dimYMarioStand * factorScale);
marioJump.resize(dimXMarioJump * factorScale, dimYMarioJump * factorScale);
coin.resize(dimXCoin * factorScale, dimYCoin * factorScale);
goomba.resize(dimXGoomba * factorScale, dimYGoomba * factorScale);
addIframe();
addAudioIcon();
}
function toggleAudio(){
if(audioOn){
audioIcon.remove();
audioIcon = createImg('noAudio.png');
} else {
audioIcon.remove();
audioIcon = createImg('audio.png');
}
audioIcon.position(posXAudioIcon*factorScale,posYAudioIcon*factorScale);
audioIcon.attribute('onclick','toggleAudio()');
audioIcon.style('width',dimXAudioIcon*factorScale);
audioIcon.style('height',dimYAudioIcon*factorScale);
audioOn = !audioOn;
}
function addAudioIcon(){
audioIcon = createImg('audio.png');
audioIcon.position(posXAudioIcon*factorScale,posYAudioIcon*factorScale);
audioIcon.attribute('onclick','toggleAudio()');
audioIcon.style('width',dimXAudioIcon*factorScale);
audioIcon.style('height',dimYAudioIcon*factorScale);
}
function draw(){
// Draw background on canvas
image(background, 0, 0);
//Draw panel
image(panel, posXPanel * factorScale, posYPanel * factorScale);
//Draw clouds
image(cloud, posXCloud1 * factorScale, posYCloud1 * factorScale);
image(cloud, posXCloud2 * factorScale, posYCloud2 * factorScale);
goombaAnimation();
marioAnimation();
coinAnimation();
//Draw blockQm
image(blockQm, posXBlockQm * factorScale, posYBlockQm * factorScale);
//Draw blocks
image(block,posXBlock1 * factorScale ,posYBlock1 * factorScale);
image(block,posXBlock2 * factorScale ,posYBlock2 * factorScale);
//image(block,posXBlock3 * factorScale ,posYBlock3 * factorScale);
addHoursText();
addScoreText(textSizeScores,factorScale,marioFont,"SCORE",totalTime*totalCoins,posXTotalScoreText,posYTotalScoreText);
addScoreText(textSizeScores,factorScale,marioFont,"COINS",totalCoins,posXTotalCoinsText,posYTotalCoinsText);
addScoreText(textSizeScores,factorScale,marioFont,"WORLD","UNI",posXTotalWorldText,posYTotalWorldText);
addScoreText(textSizeScores,factorScale,marioFont,"TIME",totalTime,posXTotalTimeText,posYTotalTimeText);
addScoreText(textSizeScores,factorScale,marioFont,"LIVES","7",posXTotalLivesText,posYTotalLivesText);
}
function addScoreText(fontSize,factorScale,font,title,value,posX,posY){
fill(255,223,201);
noStroke();
textAlign(CENTER);
textSize(fontSize * factorScale);
textFont(font);
text(title+"\n"+value, posX * factorScale, posY * factorScale);
}
function goombaAnimation(){
image(goomba,posXGoombaCurrent*factorScale,posYGoombaStart*factorScale);
if(flagDx===1){
posXGoombaCurrent += 1;
flagSx = 0;
}else if(flagSx===1){
posXGoombaCurrent = posXGoombaCurrent - speed;
flagDx = 0;
}
if(posXGoombaCurrent>=((thesholdDx*factorScale)+posXGoombaStart)){
flagDx = 0;
flagSx = 1;
}else if(posXGoombaCurrent<=(-(thesholdSx*factorScale)+posXGoombaStart)){
flagDx = 1;
flagSx = 0;
}
}
function marioAnimation(){
const milliseconds = (new Date().getMilliseconds()) / 1000;
posYMarioStand = groundY + min(0, milliseconds*(milliseconds - 0.8))*jumpScale;
let s = second() < 10 ? '0'+second() : second();
if (milliseconds > 0.05 && milliseconds < 0.75){
image(marioJump,posXMarioJump*factorScale,posYMarioStand*factorScale);
image(block,posXBlock3 * factorScale ,(posYBlock3-lenJumpBlock3) * factorScale);
addTimeIntoTheBlock(posXSecond,posYSecond-lenJumpBlock3,posXSecondShadow,posYSecondShadow-lenJumpBlock3,textFontSize,marioFont,s,factorScale);
}else{
image(marioStand,posXMarioStand*factorScale,(posYMarioStand-2)*factorScale);
image(block,posXBlock3 * factorScale ,(posYBlock3) * factorScale);
addTimeIntoTheBlock(posXSecond,posYSecond,posXSecondShadow,posYSecondShadow,textFontSize,marioFont,s,factorScale);
}
if (lastSecond !== second() ) {
if (lastSecond !== -1){
if(audioOn) marioJumpSound.play();
totalTime = totalTime + 1;
}
lastSecond = second();
}
}
function coinAnimation(){
image(coin,posXCoin*factorScale,posYCoin*factorScale);
const milliseconds = (new Date().getMilliseconds()) / 1000;
const seconds = second() + milliseconds;
posYCoin = blockY + min(0, (seconds - 0.35) * (seconds - 1.1))*coinBounceScale;
if (lastMinute !== minute()) {
if (lastMinute !== -1){
coinSound.play();
totalCoins = totalCoins + 1;
}
lastMinute = minute();
}
}
function addHoursText(){
let h = hour() < 10 ? '0'+hour() : hour();
let m = minute() < 10 ? '0'+minute() : minute();
//let s = second() < 10 ? '0'+second() : second();
addTimeIntoTheBlock(posXHour,posYHour,posXHourShadow,posYHourShadow,textFontSize,marioFont,h,factorScale);
addTimeIntoTheBlock(posXMinute,posYMinute,posXMinuteShadow,posYMinuteShadow,textFontSize,marioFont,m,factorScale);
//addTimeIntoTheBlock(posXSecond,posYSecond,posXSecondShadow,posYSecondShadow,textFontSize,marioFont,s,factorScale);
}
function addTimeIntoTheBlock(posX,posY,posXShadow,posYShadow,textFontSize,font,value,factorScale){
//main text hour
fill(0);
noStroke();
textAlign(CENTER);
textSize(textFontSize * factorScale);
textFont(font);
text(value, posX * factorScale , posY * factorScale);
//text shadow hour
fill(255,221,201);
noStroke();
textAlign(CENTER);
textSize(textFontSize * factorScale);
textFont(font);
text(value, posXShadow * factorScale, posYShadow * factorScale);
}
function addIframe() {
// Crea l'iframe
iframe = createElement('iframe');
iframe.attribute('style', 'border-radius:12px');
iframe.attribute('src', 'https://open.spotify.com/embed/playlist/2etD8gdCMHdaCVlfWmdZAS?utm_source=generator&theme=0');
iframe.attribute('width', valueXIframe* factorScale);
iframe.attribute('height', valueHIframe* factorScale);
iframe.attribute('frameBorder', '0');
iframe.attribute('allowfullscreen', '');
iframe.attribute('allow', 'autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture');
iframe.attribute('loading', 'lazy');
iframe.style('position','absolute');
iframe.parent(container);
iframe.position(posXIframe* factorScale,posYIframe* factorScale);
}
window.addEventListener('beforeunload', (event) => {
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
console.log('User switched to another tab');
alert('You have switched to another tab!');
} else {
console.log('User switched back to the tab');
alert('Welcome back to the tab!');
}
});