-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
286 lines (249 loc) · 7.96 KB
/
index.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<html>
<head>
<meta charset="utf-8">
<title>Threejs and Blender Game Design</title>
<link rel="stylesheet" type="text/css" href="styles/index.css">
<script src="https://cdn.bootcss.com/three.js/90/three.min.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r17/Stats.min.js"></script>
<script src="src/lib/FlyControls.js"></script>
<script src="src/lib/OrbitControls.js"></script>
<script src="src/constants.js"></script>
<script src="src/utils.js"></script>
<script src="src/skybox.js"></script>
<script src="src/object.js"></script>
<script src="src/hitDetect.js"></script>
</head>
<body>
<div class="front-sight center"></div>
<a class="source" href="https://github.com/zhoudaxia2016/threejs-game">source</a>
<div class="hints">
<h1> Hints </h1>
<div class="main">
<div class="control">
<div class="play-btn" id="play">Play</div>
<div class="play-btn" id="replay">Replay</div>
</div>
<div class="item">
<h2><a href="#game-intro">How to play?</a></h2>
<ul class="target" id="game-intro">
<li>
<h3>Name</h3>
<div class="content">Zombie Girl</div>
</li>
<li>
<h3>Game background</h3>
<div class="content">The grils in this forest has turned into zombies. Your task is to kill all the zombie girls.</div>
</li>
<li>
<h3>Game keys</h3>
<div class="content rule">
<p><span class="key">W.</span> Move forward</p>
<p><span class="key">A.</span> Turn left</p>
<p><span class="key">D.</span> Turn right</p>
<p><span class="key">Shift.</span> Speed up</p>
<p><span class="key">Q.</span> Lift up the gun</p>
<p><span class="key">E.</span> Aiming</p>
<p><span class="key">Space.</span> Shooting</p>
<p><span class="key">Mouse.</span> Move gun star when aiming</p>
</div>
</li>
<li>
<h3>Game goal</h3>
<div class="content">Find the zombie girls and kill them and prevent being killed.</div>
</li>
</ul>
</div>
<div class="item">
<h2><a href="#watch-scene">Want to watch the whole scene?</a></h2>
<ul class="target" id="watch-scene">
<li id="oribit-controls">
<h3>Oribit Controls</h3>
<div class="content">
<span class="key">Left Mouse</span> for rotating view, <span class="key">Middle Mouse</span> for zooming view, <span class="key">Right Mouse</span> for moving view
</div>
</li>
<li id="fly-controls">
<h3>Fly Controls</h3>
<div class="content">
<span class="key">W and Left Mouse</span> for moving forward, <span class="key">A.</span> for moving left, <span class="key">D.</span> for moving right, <span class="key">S and Right Mouse</span> for moving backward, <span class="key">R.</span> for moving up, <span class="key">F.</span> for moving down
</div>
</li>
</li>
</ul>
</div>
</div>
<script>
let scene, renderer, camera, container
let qtree
let bullets = []
let controls, clock = new THREE.Clock()
let state = {
animating: false,
playing: true
}
// Show fps
let stats = new Stats()
document.body.appendChild(stats.dom)
let hints = document.getElementsByClassName('hints')[0]
let canvas = document.getElementsByTagName('canvas')[0]
hints.addEventListener('click', function (e) {
state.animating = false
growHint()
})
document.getElementById('play').addEventListener('click', function (e) {
state.animating = true
state.playing = true
shrinkHint()
e.stopPropagation()
})
document.getElementById('replay').addEventListener('click', function (e) {
state.animating = true
state.playing = true
replay()
shrinkHint()
e.stopPropagation()
})
let controls_types = [
{
name: 'oribit-controls',
controls: THREE.OrbitControls
}, {
name: 'fly-controls',
controls: THREE.FlyControls
}
]
for (let item of controls_types) {
document.getElementById(item.name).addEventListener('click', function (e) {
if (renderer && camera) {
if (item.name === 'fly-controls') {
controls = new item.controls(camera)
} else {
controls = new item.controls(camera, renderer.domElement)
}
state.animating = true
state.playing = false
shrinkHint()
}
e.stopPropagation()
})
}
function growHint () {
hints.style.width = '500px'
hints.style.height = '80%'
hints.style.right = '50%'
hints.style.top = '10%'
hints.style.transform = 'translateX(50%)'
hints.style.overflow = 'scroll'
hints.scrollTop = 0
}
function shrinkHint () {
hints.style.width = '100px'
hints.style.height = '30px'
hints.style.right = '0'
hints.style.top = '0'
hints.style.transform = 'translateX(0)'
hints.style.overflow = 'hidden'
hints.scrollTop = 0
}
init()
// 导入role
let role = new Person(FILES.ROLE, ROLE.INITIAL_SPEED, ROLE.MOVE_DURATION, ROLE.FAST_SPEED)
let roleLoadPromise = role.load()
let zombies = [], zombies_all = []
let zombieLoadPromises = []
for (let i = 0; i < 1; i ++) {
let zombie = new Zombie(FILES.ZOMBIE)
let promise = zombie.load(role.listener, FILES.ZOMBIE_SOUND)
zombies.push(zombie)
zombies_all.push(zombie)
zombieLoadPromises.push(promise)
}
// 导入land和surrounddings
let surrounddings = [], land = {}, cloneLand = {}
let surrounddingLoadPromise = Surroundding.load(FILES.SURROUNDDING)
let loadPromise = Promise.all([roleLoadPromise, surrounddingLoadPromise])
loadPromise.then(function () {
role.setCamera(camera)
qtree = createQuadTree()
let { left, right, top, bottom } = land.rect
for (let i = 0; i < zombies.length; i ++) {
let x = right + (left - right) * Math.random()
let z = bottom + (top - bottom) * Math.random()
zombies[i].model.position.set(x, 0, z)
}
let model = land.model
land.vertices = model.geometry.vertices.map(item => item.clone().applyMatrix4(model.matrixWorld))
render()
fall(land, surrounddings)
fall(land, zombies)
render()
animate()
})
function init () {
container = document.createElement('div')
document.body.appendChild(container)
// scene
scene = new THREE.Scene()
// camera
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
// light
scene.add( new THREE.AmbientLight( 0xffffff ) );
directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.set( 0, 100, 0 );
directionalLight.castShadow = true
scene.add( directionalLight );
createSkyBox(scene)
// renderer
renderer = new THREE.WebGLRenderer()
renderer.shadowMap.enabled = true
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setClearColor(0xaaaaaa)
container.appendChild(renderer.domElement)
window.addEventListener( 'resize', onWindowResize, false)
}
function animate () {
requestAnimationFrame(animate)
// 不需要渲染,直接return
if (!state.animating) return
// 正在游戏
if (state.playing) {
update()
// 正在查看scene
} else {
controls.update(clock.getDelta())
}
render()
stats.update()
}
function render () {
renderer.render(scene, camera)
}
// 刷新游戏状态
function update () {
role.update()
for (let zombie of zombies) {
zombie.update(role)
}
for (let bullet of bullets) {
bullet.update()
}
hitDetect(qtree)
}
function onWindowResize () {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize( window.innerWidth, window.innerHeight )
render()
}
function replay () {
role.revive()
zombies = []
for (let zombie of zombies_all) {
zombie.revive()
zombies.push(zombie)
}
}
</script>
</body>
</html>