-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
129 lines (109 loc) · 3.6 KB
/
script.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
import { updateGround, setupGround } from "./ground.js"
import { updatePlayer, setupPlayer, getPlayerRect, setPlayerLose } from "./player.js"
import { updateObstacle, setupObstacle, getObstacleRects } from "./obstacle.js"
import { updateNft, setupNft, getNftRects } from "./nft.js"
const GAME_WIDTH = 100
const GAME_HEIGHT = 30
const SPEED_SCALE_INCREASE = 0.00001
const gameElem = document.querySelector("[data-game]")
const scoreElem = document.querySelector("[data-score]")
const startScreenElem = document.querySelector("[data-start-screen]")
const gweiTotalScoreEleme = document.querySelector("[data-wei-total-score]")
const nftTotalScoreElem = document.querySelector("[data-nft-total-score]")
const nftScoreElem = document.querySelector("[data-nft-score ]")
setPixelToGameScale()
window.addEventListener("resize", setPixelToGameScale)
document.addEventListener("keydown", handleStart, { once: true })
let lastTime
let speedScale
let score
let nftScore = 0
function update(time) {
if (lastTime == null) {
lastTime = time
window.requestAnimationFrame(update)
return
}
const delta = time - lastTime
updateGround(delta, speedScale)
updatePlayer(delta, speedScale)
updateObstacle(delta, speedScale)
updateNft(delta, speedScale)
updateSpeedScale(delta)
updateScore(delta)
checkIfWeGotNft()
// if there is a collision lose the game
if (checkLose()) return handleLose()
lastTime = time
window.requestAnimationFrame(update)
}
function checkLose() {
const playerRect = getPlayerRect()
return getObstacleRects().some(rect => isCollision(rect, playerRect))
}
function isCollision(rect1, rect2) {
return (
rect1.left < rect2.right &&
rect1.top < rect2.bottom &&
rect1.right > rect2.left &&
rect1.bottom > rect2.top
)
}
function checkIfWeGotNft() {
const playerRect = getPlayerRect()
if(getNftRects().some(rect => isCollision(rect, playerRect))) {
const nftToRemove = document.querySelectorAll("[data-nft]")[0]
nftToRemove.remove()
nftScore += 1
nftScoreElem.textContent = `nft score: ${nftScore}`
}
return getNftRects().some(rect => isCollision(rect, playerRect))
}
function updateSpeedScale(delta) {
speedScale += delta * SPEED_SCALE_INCREASE
}
function updateScore(delta) {
// kolku score dobivas tuka se presmetue
// console.log("delta u update score function = ", delta)
score += delta * 0.01
scoreElem.textContent = `Wei score: ${Math.floor(score)}`
}
// kaa kje pozne
function handleStart() {
lastTime = null
speedScale = 1
score = 0
setupGround()
setupPlayer()
setupObstacle()
setupNft()
startScreenElem.classList.add("hide")
// call this onnly wehne screen refershes
window.requestAnimationFrame(update)
}
window.totalNFTScore = 0
window.totalGweiScore = 0
function handleLose() {
window.totalGweiScore += Math.floor(score)
window.totalNFTScore += nftScore
nftTotalScoreElem.textContent = `NFT total score: ${window.totalNFTScore}`
gweiTotalScoreEleme.textContent = `Wei total score ${window.totalGweiScore}`
nftScore = 0
nftScoreElem.textContent = `nft score: ${nftScore}`
setPlayerLose()
// save
setTimeout(() => {
document.addEventListener("keydown", handleStart, { once: true })
startScreenElem.classList.remove("hide")
}, 100)
}
function setPixelToGameScale() {
let gameToPixelScale
if (window.innerWidth / window.innerHeight < GAME_WIDTH / GAME_HEIGHT) {
gameToPixelScale = window.innerWidth / GAME_WIDTH
} else {
gameToPixelScale = window.innerHeight / GAME_HEIGHT
}
gameElem.style.width = `${GAME_WIDTH * gameToPixelScale}px`
gameElem.style.height = `${GAME_HEIGHT * gameToPixelScale}px`
}