-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasterEggs.js
53 lines (42 loc) · 1.74 KB
/
easterEggs.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
import { setCustomProp, incrementCustomProp, getCustomProp } from "./UpdateCustomerProp.js"
const SPEED = 0.05 // same speed as ground moving
// these are intervals for how long between summoning an egg on the screen
const EGGS_INTERVAL_MIN = 500 // min = 500ms
const EGGS_INTERVAL_MAX = 2000 // max = 2s
const container = document.querySelector('[data-container]')
let nextEggTime
export function setupEasterEggs() {
nextEggTime = EGGS_INTERVAL_MIN
document.querySelectorAll('.easter-egg').forEach(egg => {
egg.remove() //removes all the eggs when restart
})
}
export function updateEasterEggs(diff, speedScale) {
let eggs = document.querySelectorAll('.easter-egg')
console.log(eggs);
eggs.forEach(egg => {
incrementCustomProp(egg, "--left", diff * speedScale * SPEED * -1)
if (getCustomProp(egg, "--left") <= -100) {
egg.remove()
}
})
if (nextEggTime <= 0) {
createEgg()
nextEggTime = randomIntBetween(EGGS_INTERVAL_MIN, EGGS_INTERVAL_MAX) / speedScale // this is done to make the eggs spawn faster and faster, like the ground
}
nextEggTime -= diff // this num decreases and decreases until it's below 0 and then it creates an easter egg
}
export function getEggsRects() {
return [...document.querySelectorAll('.easter-egg')].map(eggs => {return eggs.getBoundingClientRect()})
}
function createEgg() {
const egg = document.createElement("img")
egg.dataset.easterEgg = true
egg.src = "images/easter-egg.png"
egg.classList.add("easter-egg")
setCustomProp(egg, "--left", 100)
container.appendChild(egg)
}
function randomIntBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}