-
Notifications
You must be signed in to change notification settings - Fork 1
/
no_mouse_cam.js
350 lines (303 loc) · 10.3 KB
/
no_mouse_cam.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
var lifeEl = byId('life')
var scrapEl = byId('scrap')
var buildingCageEl = byId('buildingCage')
var pressAnyKeyEl = byId('pressAnyKey')
var levelMsgEl = byId('levelMsg')
var boardHeaderEl = byId('boardHeader')
var boardEl = byId('board')
var scrapCountEl = byId('scrapCount')
var introEl = byId('intro')
var statsEl = byId('stats')
var tinfoilHatEnergyEl = byId('tinfoilHatEnergy')
var canvas = document.createElement('canvas');
canvas.id = 'canvas';
document.body.appendChild(canvas);
var vw
var vh
var setCanvasSize = function() {
vw = Math.min(window.innerWidth, 640)
vh = Math.min(window.innerHeight, 480)
canvas.width = vw
canvas.height = vh
}
setCanvasSize()
window.onresize = function(event) {
setCanvasSize()
}
var CAMERA
var ctx = canvas.getContext('2d', { alpha: false })
var TYPES = THINGS_PAINT_ORDER.reverse()
var TIME_FACTOR
var WasCloseToBuildCage = null
function update (dt) {
if (WORLD.count === 0) {
lifeEl.innerHTML = player.life
levelMsgEl.style.display = 'block'
levelMsgEl.innerHTML = 'Level ' + LEVEL
aa.play('newLevel')
window.setTimeout(function() {
levelMsgEl.style.display = 'none'
}, 1500)
}
// TODO: using dt cause collision handling errors. Fix it
TIME_FACTOR = 0.01 * 1//dt/(1/60)
var thingsOfType
, things
, thing
, collisions
, detail;
if (WORLD.scrapCount !== scrapCountPrev) {
scrapCountEl.innerHTML = WORLD.scrapCount
scrapCountPrev = WORLD.scrapCount
}
if (!WORLD.lost) {
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = '#000000'
ctx.fill()
ctx.closePath()
}
if (WORLD.recoredInput) {
CAMERA = {
xoffset: player.w/2+player.x-vw/2,
yoffset: player.h/2+player.y-vh/2
}
if (WORLD.recoredInput[WORLD.count]) {
INPUT = WORLD.recoredInput[WORLD.count]
} else {
WORLD.test()
}
} else {
CAMERA =
{xoffset: ~~(player.w/2+player.x-vw/2)
,yoffset: ~~(player.h/2+player.y-vh/2)
}
}
// input record
if (RECORD_INPUT) {
RECORDED_INPUT.push(cloneInput())
}
// Iterate object types
for (var i = TYPES.length - 1; i >= 0; i--) {
thingsOfType = WORLD.things[TYPES[i]]
// Iterate things of certain type
for (var j = thingsOfType.length - 1; j >= 0; j--) {
thing = thingsOfType[j]
if (thing._toRecycle) continue
var spriteControl
if (
((thing.x+thing.w) > CAMERA.xoffset) &&
((thing.x) < CAMERA.xoffset+vw) &&
((thing.y+thing.h) > CAMERA.yoffset) &&
((thing.y) < CAMERA.yoffset+vh) &&
thing.sprites
) {
var spriteSet = null
if (thing.movex || thing.movey) {
spriteSet = thing.toSide === -1 ? 'moveLeft' : 'moveRight'
spriteControl = 'move'
} else {
if (thing.spriteSet !== '') { // forced sprite set
spriteSet = thing.spriteSet
} else if (thing.toSide) { // moving sprite set
spriteSet = thing.toSide === -1 ? 'idleLeft' : 'idleRight'
} else {
spriteSet = 'idle'
}
spriteControl = 'idle'
}
var toPaint
if (spriteSet) {
var spriteIndex = thing[spriteControl].spriteI
toPaint = thing.sprites[spriteSet][spriteIndex]
if (!(thing[spriteControl].timeline % thing[spriteControl].spriteCicle)) {
if (thing[spriteControl].spriteI < thing.sprites[spriteSet].length-1) {
thing[spriteControl].spriteI++
} else {
thing[spriteControl].spriteI = 0
}
}
ctx.drawImage(toPaint, ~~(thing.x-CAMERA.xoffset), ~~(thing.y-CAMERA.yoffset))
thing[spriteControl].timeline++
}
} else {
if (
((thing.x+thing.w) > CAMERA.xoffset) &&
((thing.x) < CAMERA.xoffset+vw) &&
((thing.y+thing.h) > CAMERA.yoffset) &&
((thing.y) < CAMERA.yoffset+vh)
) {
ctx.beginPath()
ctx.rect(~~(thing.x-CAMERA.xoffset), ~~(thing.y-CAMERA.yoffset), ~~thing.w, ~~thing.h)
ctx.fillStyle = thing.color || '#00ff00'
ctx.fill()
ctx.closePath()
}
}
if (WORLD.lost) continue
// store prev position, useful for
// collision handling and other stuff
thing._prevx = thing.x
thing._prevy = thing.y
if (TYPES[i] === 'explosions') {
if (thing.idle.timeline === 40) {
thing._toRecycle = true
}
}
if (TYPES[i] === 'playerBullets' || TYPES[i] === 'enemyBullets') {
if (
(thing.x > WORLD.w) ||
((thing.x + thing.w) < 0) ||
(thing.y > WORLD.h) ||
((thing.y + thing.h) < 0)
) {
thing._toRecycle = true
continue
}
collisions = checkCollision(thing, WORLD.things.walls, undefined, true)
handleBulletWallColl(collisions)
if (TYPES[i] === 'playerBullets') {
collisions = checkCollision(thing, WORLD.things.enemies)
handleBulletThingColl(collisions)
}
if (TYPES[i] === 'enemyBullets') {
collisions = checkCollision(thing, WORLD.things.players)
handleBulletThingColl(collisions)
}
}
// objects with property `movex` or `movey` have
// position increased by speed
if (thing.movex) {thing.x += thing.movex * TIME_FACTOR * thing.speed}
if (thing.movey) {thing.y += thing.movey * TIME_FACTOR * thing.speed}
if (thing.ai) {thing.ai()}
if (TYPES[i] === 'enemies' || TYPES[i] === 'players') {
collisions = checkCollision(thing, WORLD.things.walls.concat(WORLD.things.antennas))
handlePlayerColl(collisions)
tinfoilHatEnergyEl.innerHTML = ~~WORLD.energy
}
if (TYPES[i] === 'enemies' && !WORLD.cagesToBuild) {
thing.explode()
//thing._toRecycle = true
//thing.followers[0]._toRecycle = true
}
if (TYPES[i] === 'players') {
handleInput(player)
collisions = checkCollision(thing, WORLD.things.scraps)
handlePlayerScrapColl(collisions)
}
if (TYPES[i] === 'antennas') {
if (!thing.caged) {
let xd = thing.x - player.x + 15
let yd = thing.y - player.y
let sumRadius = thing.w/2 + player.w/2
let sqrRadius = sumRadius * sumRadius
let distSqr = (xd * xd) + (yd * yd)
if (WORLD.energy < 100) {
WORLD.energy += (1/distSqr)*200
}
//WORLD.energy += distSqr
// TODO: Capture energy from antenna signal
var isClose = (distSqr <= sqrRadius*1.5)
if (isClose) {
thing.closeToBuildCage = true
// Dont have scraps to build
if (WORLD.scrapCount < scrapsToBuildCage) {
//...
} else {
if (!thing.buildingCage) {
thing.buildingCage = true
}
// Increment building count if stay near antenna
thing.buildingCagePct += 1
buildingCageEl.innerHTML = ~~((thing.buildingCagePct/buildingCagePctTarget)*100)
if (thing.buildingCagePct >= buildingCagePctTarget) {
// build complete
thing.buildingCagePct = 0
WORLD.cagesToBuild--
thing.caged = true
aa.play( 'caged' )
thing.closeToBuildCage = false
thing.spriteSet = 'caged'
BOARD.cage += 1
WORLD.scrapCount -= scrapsToBuildCage
}
}
} else {
thing.closeToBuildCage = false
// Reset building if you leave near antenna
if (thing.buildingCage) {
thing.buildingCage = false
thing.buildingCagePct = 0
}
}
}
}
if (thing.followers) {
for (var fi = thing.followers.length - 1; fi >= 0; fi--) {
thing.followers[fi].x += thing.x-thing._prevx
thing.followers[fi].y += thing.y-thing._prevy
}
}
}
}
if (WORLD.lost) return
var closeToBuildCage = false
for (var _i = WORLD.things.antennas.length - 1; _i >= 0; _i--) {
closeToBuildCage = closeToBuildCage || WORLD.things.antennas[_i].closeToBuildCage
}
if (WasCloseToBuildCage !== closeToBuildCage) {
if (closeToBuildCage && WORLD.scrapCount < scrapsToBuildCage) {
buildingCageEl.innerHTML = 'You need 10 scraps to build a cage'
aa.play( 'unable' )
}
if (!closeToBuildCage) {
buildingCageEl.innerHTML = ''
}
}
WasCloseToBuildCage = closeToBuildCage
if (WORLD.cagesToBuild === 0) {
WORLD.cagesToBuild = null
levelMsgEl.style.display = 'block'
levelMsgEl.innerHTML = 'Level Disrupted!'
pressAnyKeyEl.style.display = 'none'
window.setTimeout(function() {
levelMsgEl.style.display = 'none'
window.setTimeout(function() {
boardHeaderEl.style.display = 'block'
boardHeaderEl.innerHTML = ''
boardEl.style.display = 'block'
fillBoard()
window.setTimeout(function() {
acceptKeyForNewGame = true
pressAnyKeyEl.innerHTML = 'PRESS ANY KEY TO NEXT LEVEL'
pressAnyKeyEl.style.display = 'block'
}, 2000)
}, 2000)
}, 2000)
}
var noEnemies = (WORLD.enemyCount <= 0 && !((WORLD.count+1) % 30))
if (!!WORLD.cagesToBuild && (noEnemies || !((WORLD.count+1) % 100) && (WORLD.enemyCount < WORLD.maxEnemies))) {
// generate enemies
var pathToBornI = randomMinMax(0, WORLD.enemySpawnArea.length-1)
var pathToBorn = WORLD.enemySpawnArea[pathToBornI]
// they are born not to close to player
var safeDist = Math.abs(pathToBorn.x - WORLD.things.players[0].x) > ORT_BORN_DIST ||
Math.abs(pathToBorn.x - WORLD.things.players[0].x) > ORT_BORN_DIST
;
if (safeDist) {
var newEnemy = Drone()
WORLD.enemyCount += 1
newEnemy.x = pathToBorn.x
newEnemy.y = pathToBorn.y
var SHADOW = entityFactory({
idle: [ShadowSprite3]
},
recycle(WORLD.things.shadows)
)
SHADOW.x = newEnemy.x+2
SHADOW.y = newEnemy.y+30
newEnemy.followers.push(SHADOW)
}
}
WORLD.count += 1
}