-
Notifications
You must be signed in to change notification settings - Fork 0
/
timemachine.js
64 lines (51 loc) · 1.9 KB
/
timemachine.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
// Get the play/pause button
const playPauseBtn = document.getElementById('playPauseBtn');
let enginePaused = true;
let timeSpeed = 1; // constant affecting time speed which we control using UI
let gameLoopInterval = setInterval(gameLoop, 400)
let startDate = new Date("2000-01-01");
// Add a click event listener
playPauseBtn.addEventListener('click', function() {
// Get the current image source
const currentSrc = playPauseBtn.querySelector('img').getAttribute('src');
// Check if the current image source is the play image
if (currentSrc === 'images/play.png') {
playPauseBtn.querySelector('img').setAttribute('src', 'images/pause.png');
enginePaused = false;
} else {
playPauseBtn.querySelector('img').setAttribute('src', 'images/play.png');
enginePaused = true;
}
});
const timeSpeedToInterval = { // maps timeSpeed to the time in ms between loop executes
1 : 200,
2 : 25,
3 : 0
}
fastBackwardBtn.addEventListener('click', function() {
if (timeSpeed > 1) {
timeSpeed -= 1;
}
clearInterval(gameLoopInterval)
gameLoopInterval = setInterval(gameLoop, timeSpeedToInterval[timeSpeed])
});
fastForwardBtn.addEventListener('click', function() {
if (timeSpeed < 3) {
timeSpeed += 1;
}
clearInterval(gameLoopInterval)
gameLoopInterval = setInterval(gameLoop, timeSpeedToInterval[timeSpeed])
});
function formatDate(inputDate) {
const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
const date = new Date(inputDate);
const day = date.getDate();
const month = months[date.getMonth()];
const year = date.getFullYear();
return `${day} ${month} ${year}`;
}
function setDateDisplay(date) {
const dateElem = document.getElementById('dateDisplay');
dateElem.textContent = formatDate(date);
}