-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
216 lines (187 loc) · 7.17 KB
/
app.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
import getMicSoundLevel from './audio'
import states from './state'
import nodes from './domNodes'
import createHedges from './hedges'
import './events'
import { RectCircleColliding, setBallPosition, clearContainer, scoreRating, getBrowserName } from './funcs'
let soundLevel = null
let intervalId = null
const deviceHeight = screen.height
const deviceWith = screen.width
const browserName = getBrowserName()
// preventing the game from running
const preventRunning = () => {
states.gameInPlay = null
states.gameOver = null
}
// prevent running the app on edge and safari
if( browserName === "edge" || browserName === 'safari'){
preventRunning()
clearContainer()
// showign a propper message
const h1 = document.createElement('h1')
const h2 = document.createElement('h2')
h1.innerText = "sorry my friend !!!"
h2.innerText = "you need to open this game with chrome or Firefox web browser :)"
nodes.container.appendChild(h1)
nodes.container.appendChild(h2)
}
if(deviceWith < 768){
preventRunning()
clearContainer()
nodes.container.style.display = "flex"
nodes.container.style.flexDirection = "column"
nodes.container.style.justifyContent = "space-between"
const h4 = document.createElement('h1')
const p = document.createElement('p')
const span = document.createElement("span")
h4.innerText = "sorry my friend!"
p.innerText = "for the best game experience, I decided to only let users with a large screen device, such as tablets or laptops can play this game. please come back with a bigger device"
span.innerText = "I will wait for you"
span.style.color = 'rgb(0, 255, 0)'
nodes.container.appendChild(h4)
nodes.container.appendChild(p)
nodes.container.appendChild(span)
}
// set the ball speed based on user device height
const ballSpead = Math.floor(deviceHeight / 200)
states.gameSpeed = ballSpead
// set passages height
states.passageHeight = deviceHeight / 3.5
//always contain the latest value of microphone sound level
getMicSoundLevel((x) => {
if (x) soundLevel = Math.round(x * states.sensitivity)
})
document.addEventListener('keydown', (e) => {
const key = e.keyCode
if (key === 32 && document.activeElement != nodes.feedbackTextarea ) {
startGame()
}
})
nodes.inputSlider.addEventListener('input', (e) => {
let value = e.target.value / 1000
value = value.toFixed(2)
states.sensitivity = value
})
const startGame = () => {
if(states.gameOver) {
reStart()
}
else if (states.gameInPlay === false) {
nodes.startGame.style.display = 'none'
nodes.soundLevelHint.style.display = "none"
nodes.sensitivity.style.display = "none"
nodes.feedback.style.display = "none"
nodes.socials.style.display = "none"
states.gameInPlay = true
if (states.gameOver === false)
intervalId = setInterval(() => {
if (states.gameOver === true) clearInterval(createHedges)
else createHedges()
}, 2000)
requestAnimationFrame(updateScreen)
}
// set ball position for center of the page
setBallPosition(deviceHeight / 2 - 25 )
}
nodes.startBtn.addEventListener('click', startGame)
const updateScreen = () => {
if (!states.gameOver) {
const containerHeight = Math.round(nodes.container.getBoundingClientRect().height)
const characterOffsetTop = nodes.character.offsetTop
if ( soundLevel > 200 && soundLevel < 600 && characterOffsetTop + 50 < containerHeight && !states.lock) {
setBallPosition(characterOffsetTop + states.gameSpeed)
}
else if (soundLevel > 600 && characterOffsetTop > 0){
setBallPosition(characterOffsetTop - states.gameSpeed)
// this is a nasty trick that i can explain, event if i want it
// when player is screaming sound level goes up
// and when the player stop doing that, soundlevel variable start decreasing
// and in the process of decreasing somewhere sound Level gonna be between 200 and 600
// and because of that, when ball goes high after player scream, ball goes down a little
// so i set lock to true
// and in the first if statement i check for lock property in states object
// if lock was true i won't let the ball goes down
states.lock = true
setTimeout(() => {
states.lock = false
}, 2000)
}
moveHedges()
updateScore()
requestAnimationFrame(updateScreen)
}
}
const moveHedges = () => {
if (states.gameInPlay) {
const hedges = document.getElementsByClassName('hedge')
for (let hedge of hedges) {
let hedgeLeft = Math.round(hedge.getBoundingClientRect().left)
hedgeLeft -= states.gameSpeed
if (hedgeLeft <= -50) {
nodes.container.removeChild(hedge)
}
hedge.style.left = hedgeLeft + 'px'
if (RectCircleColliding(nodes.character.getBoundingClientRect(), hedge.getBoundingClientRect())) {
gameOver()
}
}
}
}
const updateScore = () => {
const distances = []
const hedges = document.getElementsByClassName('hedge')
for (let hedge of hedges) {
const hedgeDistanceFromLeft = hedge.offsetLeft + hedge.getBoundingClientRect().width
const characterDistanceFromLeft = nodes.character.offsetLeft
const distance = hedgeDistanceFromLeft - characterDistanceFromLeft
if (distance >= -3) distances.push(hedge)
}
nodes.score.innerHTML = states.playerScore
}
const reStart = () => {
states.playerScore = 0
states.gameInPlay = false
states.gameOver = false
nodes.gameOver.style.display = 'none'
clearInterval(intervalId)
// clear screen
const hedges = document.getElementsByClassName('hedge')
while(hedges.length){
for (let hedge of hedges)
nodes.container.removeChild(hedge)
}
startGame()
}
const gameOver = () => {
states.gameOver = true
states.gameInPlay = false
nodes.sensitivity.style.display = "flex"
nodes.soundLevelHint.style.display = 'block'
nodes.gameOver.style.display = "flex"
nodes.startGame.style.display = "none"
nodes.restartBtn.addEventListener('click', reStart)
nodes.backToMainBtn.addEventListener('click', () => {
nodes.gameOver.style.display = "none"
nodes.startGame.style.display = "flex"
nodes.feedback.style.display = "block"
nodes.socials.style.display = "block"
})
nodes.gameOverScore.innerText = "your score is: " + states.playerScore
nodes.gameOverRate. innerText = scoreRating(states.playerScore)
}
setInterval(() => {
nodes.soundLevelNum.innerText = soundLevel
let className = 'sound-level-number '
if(soundLevel > 200 && soundLevel < 600)
className += "color-yellow"
else if(soundLevel > 600)
className += "color-green"
else
className += 'color-gray'
nodes.soundLevelNum.setAttribute('class', className)
}, 30)
setInterval(() => {
if(states.gameInPlay)
states.playerScore++
}, 30)