-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
370 lines (317 loc) · 8.72 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"use strict";
function setGlobals() {
// window.ext is set by cocoonjs
window.isMobile = !!window.ext
window.devPixelRatio = (window.devicePixelRatio > 2 ? 2 : window.devicePixelRatio) || 1
window.$canv = document.getElementById('mainCanv')
$canv.width = window.innerWidth * devPixelRatio
$canv.height = window.innerHeight * devPixelRatio
window.ctx = $canv.getContext('2d')
ctx.lineJoin = 'round'
window.debug = false // true
// this probably shouldnt be a global...
window.usingSmallLogo = false
// color pallet // blue l blue l green orange d orange
window.pallet = [[105,210,231], [167,219,216], [224,228,204], [243,134,48], [250,105,0]]
window.lastColor = new Color()
window.GAME = {
MENU: {
opacity: 1
},
state: 'menu',
firstLoop : true,
bufferLoop: true
}
window.ASSETS = {loaded: false}
if(debug){
window.stats = new Stats()
document.body.appendChild(stats.domElement)
}
// game loop
window.MS_PER_UPDATE = 32
window.previousTime = 0.0
window.lag = 0.0
window.quality = 10
}
setGlobals()
loadAssets(fadeInMenu)
function init() {
GAME.state = 'playing'
GAME.player = new Fish(false)
GAME.fishes = [GAME.player]
GAME.spawner = new Spawner($canv.width, $canv.height, GAME.player, GAME.fishes)
GAME.levelParticles = []
GAME.levelBar = new LevelBar($canv.width)
GAME.levelBalls = new LevelBalls($canv.width, $canv.height)
GAME.levelBallParticles = []
GAME.endGameParticles = []
GAME.firstLoop = true
//previousTime = Date.now() - previousTime
requestAnimFrame(draw)
}
function lowerQuality() {
if(!isMobile) return
if(quality >= 7) {
quality -= 1
resizeWindow()
}
}
// main game loop
function draw(time) {
var i, l, j, dist, nextStage, fish, fish2
lag += time - previousTime
previousTime = time
if(GAME.state === 'playing'){
requestAnimFrame(function(t){
draw(t)
})
} else {
return fadeInMenu()
}
var player = GAME.player
var fishes = GAME.fishes
var spawner = GAME.spawner
var levelParticles = GAME.levelParticles
var levelBar = GAME.levelBar
var levelBalls = GAME.levelBalls
var levelBallParticles = GAME.levelBallParticles
var endGameParticles = GAME.endGameParticles
if(debug) stats.begin()
var MAX_CYCLES = 17
while(lag >= MS_PER_UPDATE && MAX_CYCLES) {
physics()
lag -= MS_PER_UPDATE
MAX_CYCLES--
}
if(MAX_CYCLES === 0 && GAME.firstLoop) {
GAME.firstLoop = false
} else if( MAX_CYCLES === 0 && GAME.bufferLoop) {
GAME.bufferLoop = false
} else if (MAX_CYCLES === 0) {
// adaptive quality
lowerQuality()
GAME.firstLoop = true
GAME.bufferLoop = true
}
// if 5 frames behind, jump
if(lag/MS_PER_UPDATE > 75) {
lag = 0.0
}
paint()
if(debug) stats.end()
function physics() {
levelBarPhysics()
levelBallPhysics()
endGameParticlePhysics()
// enemy spawner
spawner.update()
// enemy spawner debug
fishPhysics()
playerScore()
}
function paint() {
// clear and draw background
ctx.fillStyle = '#111'
ctx.fillRect(0, 0, $canv.width, $canv.height)
// static position objects
levelBar.draw(ctx)
paintLevelParticles()
paintLevelBallParticles()
levelBalls.draw(ctx)
// dynamic position objects
ctx.save()
ctx.translate(-player.x + $canv.width/2, -player.y + $canv.height/2)
paintEndGameParticles()
paintFish()
if(debug) spawner.debug()
ctx.restore()
drawSoundControl()
}
function levelBarPhysics() {
// levelBar level up
nextStage = levelBar.physics()
if(nextStage) {
GAME.levelBallParticles = levelBallParticles.concat(levelBar.toParticles(levelBalls))
levelBalls.nextColors = levelBar.colors.slice(0, 2)
// reset levelBar
GAME.levelBar = new LevelBar($canv.width)
}
// levelBar Particles physics
i = levelParticles.length
while(i-- > 0) {
dist = levelParticles[i].physics()
if (dist < 10) {
levelParticles.splice(i, 1)
if(!levelBar.updating) {
levelBar.updating = true
levelBar.addColor()
}
if(levelParticles.length === 0) {
levelBar.updating = false
}
}
}
}
function levelBallPhysics() {
// levelBalls level up
nextStage = levelBalls.physics()
if(nextStage) {
GAME.endGameParticles = levelBalls.toParticles(player)
// un-static position
for(i=0;i<GAME.endGameParticles.length;i++) {
GAME.endGameParticles[i].x += player.x - $canv.width/2
GAME.endGameParticles[i].y += player.y - $canv.height/2
}
GAME.levelBalls = new LevelBalls($canv.width, $canv.height)
}
i = levelBallParticles.length
while(i-- > 0) {
dist = levelBallParticles[i].physics()
if (dist < 10) {
levelBallParticles.splice(i, 1)
if(!levelBalls.updating) {
levelBalls.updating = true
levelBalls.addBall()
}
if(levelBallParticles.length === 0) {
levelBalls.updating = false
levelBalls.shift()
}
}
}
}
function endGameParticlePhysics() {
for(i = -1, l = endGameParticles.length; ++i < l;) {
endGameParticles[i].physics()
}
}
function fishPhysics() {
// physics and drawing
i = fishes.length
while(i-- > 0) {
// cleanup dead fish - in here for performance
if(fishes[i].dead) {
if(fishes[i] === player) {
setTimeout(function(){
GAME.state = 'menu'
}, 4000)
}
fishes.splice(i, 1)
continue
}
fish = fishes[i]
if(Math.abs(fish.x - player.x) < $canv.width && Math.abs(fish.y - player.y) < $canv.height) {
fish.physics()
// collision - in here for performance
j=i
while (j-- > 0) {
fish2 = fishes[j]
if(Math.abs(fish2.x - player.x) < $canv.width && Math.abs(fish2.y - player.y) < $canv.height) {
if(fish.collide(fish2)) {
if(fish.size >= fish2.size){
fish2.killedBy(fish)
} else {
fish.killedBy(fish2)
}
}
}
}
}
// if far enough away from player, remove
if(distance(fish, player) > Math.max($canv.width, $canv.height) * 1.5) {
fish.dead = true
}
}
}
function playerScore() {
i=player.colors.length
if (i <=4 ) return
while(i-->0) {
if (player.colors[i].loaded < 1) {
return
}
}
// player score
// steal colors from player
player.drawColors()
var newParticles = player.toParticles(levelBar)
// staticly position
for(i=0;i<newParticles.length;i++) {
newParticles[i].x += -player.x + $canv.width/2
newParticles[i].y += -player.y + $canv.height/2
}
GAME.levelParticles = levelParticles.concat(newParticles)
player.colors.splice(0, 4)
}
function paintLevelParticles() {
for(i = -1, l = levelParticles.length; ++i < l;) {
levelParticles[i].draw(ctx) // iterate levelParticles
}
}
function paintLevelBallParticles() {
for(i = -1, l = levelBallParticles.length; ++i < l;) {
levelBallParticles[i].draw(ctx)
}
}
function paintEndGameParticles() {
for(i = -1, l = endGameParticles.length; ++i < l;) {
endGameParticles[i].draw(ctx)
}
}
function paintFish() {
// draw fish
for(i = -1, l = fishes.length; ++i < l;) {
fish = fishes[i]
if(Math.abs(fish.x - player.x) < $canv.width/2 + 100 && Math.abs(fish.y - player.y) < $canv.height/2 + 100) {
fish.draw(ctx)
}
}
}
}
function loadAssets(cb) {
var imgs = [
{ name: 'logo', src: 'assets/logo.png' },
{ name: 'logoSmall', src: 'assets/logo-small.png' },
{ name: 'soundOn', src: 'assets/sound-on.png' },
{ name: 'soundOff', src: 'assets/sound-off.png' },
{name: 'enter', src: 'assets/enter.png'}
]
function process() {
var next = imgs.pop()
if(next) {
var img = new Image()
img.onload = function() {
ASSETS[next.name] = this
process()
}
img.src = next.src
} else {
ASSETS.loaded = true
cb()
}
}
process()
}
// level debug
function levelUp(){
var levelBar = GAME.levelBar
for(var i=0;i<9;i++)
levelBar.addColor()
levelBar.colors.forEach(function(col){
col.loaded = 1
})
levelBar.x = levelBar.targetX
levelBar.addColor()
}
function levelUp2(){
var levelBalls = GAME.levelBalls
for(var i=0;i<8;i++){
levelBalls.addBall()
levelBalls.shift()
}
levelBalls.balls.forEach(function(b){b.size = b.targetSize})
levelBalls.addBall()
}
//setTimeout(levelUp, 3000)
//setTimeout(function(){GAME.levelBar.addColor()}, 10000)
//setTimeout(levelUp2, 3000)