-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.js
233 lines (201 loc) · 6.28 KB
/
main.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
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 )
const renderer = new THREE.WebGLRenderer()
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
const light = new THREE.AmbientLight( 0xffffff )
scene.add( light )
const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
directionalLight.castShadow = true
scene.add( directionalLight )
directionalLight.position.set( 0, 1, 1 )
camera.position.z = 5
renderer.setClearColor( 0xB7C3F3, 1 )
const loader = new THREE.GLTFLoader()
let doll
const start_position = 6
const end_position = -start_position
const text = document.querySelector('.text')
let DEAD_PLAYERS = 0
let SAFE_PLAYERS = 0
const startBtn = document.querySelector('.start-btn')
//musics
const bgMusic = new Audio('./music/bg.mp3')
bgMusic.loop = true
const winMusic = new Audio('./music/win.mp3')
const loseMusic = new Audio('./music/lose.mp3')
loader.load( './model/scene.gltf', function ( gltf ){
scene.add( gltf.scene )
doll = gltf.scene
gltf.scene.position.set(0,-1, 0)
gltf.scene.scale.set(0.4, 0.4, 0.4)
startBtn.innerText = "start"
})
function lookBackward(){
gsap.to(doll.rotation, {duration: .45, y: -3.15})
setTimeout(() => dallFacingBack = true, 150)
}
function lookForward(){
gsap.to(doll.rotation, {duration: .45, y: 0})
setTimeout(() => dallFacingBack = false, 450)
}
function createCube(size, posX, rotY = 0, color = 0xfbc851){
const geometry = new THREE.BoxGeometry( size.w, size.h, size.d )
const material = new THREE.MeshBasicMaterial( { color } )
const cube = new THREE.Mesh( geometry, material )
cube.position.set(posX, 0, 0)
cube.rotation.y = rotY
scene.add( cube )
return cube
}
//Creating runway
createCube({w: start_position * 2 + .21, h: 1.5, d: 1}, 0, 0, 0xe5a716).position.z = -1
createCube({w: .2, h: 1.5, d: 1}, start_position, -.4)
createCube({w: .2, h: 1.5, d: 1}, end_position, .4)
class Player {
constructor(name = "Player", radius = .25, posY = 0, color = 0xffffff){
const geometry = new THREE.SphereGeometry( radius, 100, 100 )
const material = new THREE.MeshBasicMaterial( { color } )
const player = new THREE.Mesh( geometry, material )
scene.add( player )
player.position.x = start_position - .4
player.position.z = 1
player.position.y = posY
this.player = player
this.playerInfo = {
positionX: start_position - .4,
velocity: 0,
name,
isDead: false
}
}
run(){
if(this.playerInfo.isDead) return
this.playerInfo.velocity = .03
}
stop(){
gsap.to(this.playerInfo, { duration: .1, velocity: 0 })
}
check(){
if(this.playerInfo.isDead) return
if(!dallFacingBack && this.playerInfo.velocity > 0){
text.innerText = this.playerInfo.name + " lost!!!"
this.playerInfo.isDead = true
this.stop()
DEAD_PLAYERS++
loseMusic.play()
if(DEAD_PLAYERS == players.length){
text.innerText = "Everyone lost!!!"
gameStat = "ended"
}
if(DEAD_PLAYERS + SAFE_PLAYERS == players.length){
gameStat = "ended"
}
}
if(this.playerInfo.positionX < end_position + .7){
text.innerText = this.playerInfo.name + " is safe!!!"
this.playerInfo.isDead = true
this.stop()
SAFE_PLAYERS++
winMusic.play()
if(SAFE_PLAYERS == players.length){
text.innerText = "Everyone is safe!!!"
gameStat = "ended"
}
if(DEAD_PLAYERS + SAFE_PLAYERS == players.length){
gameStat = "ended"
}
}
}
update(){
this.check()
this.playerInfo.positionX -= this.playerInfo.velocity
this.player.position.x = this.playerInfo.positionX
}
}
async function delay(ms){
return new Promise(resolve => setTimeout(resolve, ms))
}
const player1 = new Player("Player 1", .25, .3, 0xD1FFC6)
const player2 = new Player("Player 2", .25, -.3, 0xFFCFD2)
const players = [
{
player: player1,
key: "ArrowUp",
name: "Player 1"
},
{
player: player2,
key: "w",
name: "Player 2"
}
]
const TIME_LIMIT = 15
async function init(){
await delay(500)
text.innerText = "Starting in 3"
await delay(500)
text.innerText = "Starting in 2"
await delay(500)
text.innerText = "Starting in 1"
lookBackward()
await delay(500)
text.innerText = "Gooo!!!"
bgMusic.play()
start()
}
let gameStat = "loading"
function start(){
gameStat = "started"
const progressBar = createCube({w: 8, h: .1, d: 1}, 0, 0, 0xebaa12)
progressBar.position.y = 3.35
gsap.to(progressBar.scale, {duration: TIME_LIMIT, x: 0, ease: "none"})
setTimeout(() => {
if(gameStat != "ended"){
text.innerText = "Time Out!!!"
loseMusic.play()
gameStat = "ended"
}
}, TIME_LIMIT * 1000)
startDall()
}
let dallFacingBack = true
async function startDall(){
lookBackward()
await delay((Math.random() * 1500) + 1500)
lookForward()
await delay((Math.random() * 750) + 750)
startDall()
}
startBtn.addEventListener('click', () => {
if(startBtn.innerText == "START"){
init()
document.querySelector('.modal').style.display = "none"
}
})
function animate(){
renderer.render( scene, camera )
players.map(player => player.player.update())
if(gameStat == "ended") return
requestAnimationFrame( animate )
}
animate()
window.addEventListener( "keydown", function(e){
if(gameStat != "started") return
let p = players.find(player => player.key == e.key)
if(p){
p.player.run()
}
})
window.addEventListener( "keyup", function(e){
let p = players.find(player => player.key == e.key)
if(p){
p.player.stop()
}
})
window.addEventListener( 'resize', onWindowResize, false )
function onWindowResize(){
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize( window.innerWidth, window.innerHeight )
}