-
Notifications
You must be signed in to change notification settings - Fork 3
/
utility.js
427 lines (360 loc) · 13.9 KB
/
utility.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/////////// requestAnimationFrame shim
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
/////////// performance.now() shim
window.performance = window.performance || {};
performance.now = (function() {
return performance.now ||
performance.mozNow ||
performance.msNow ||
performance.oNow ||
performance.webkitNow ||
function() { return new Date().getTime(); };
})();
function Position(x_pos, y_pos){
this.x = x_pos; // x pos
this.y = y_pos; // y pos
}
function Timer(end_time, repeat, curr_game) {
IN_ACTION = 10;
DONE = 11;
this.loop = repeat;
this.game = BuckyGame;
this.time = {};
this.time.current = 0;
this.time.end = end_time;
this.status = IN_ACTION;
this.actionFlag = false;
UpdateManager.push(this);
this.physics = function() {
if(this.status == IN_ACTION){
this.time.current += curr_game.physCorrect * physExecuteMs / 16.667;
}
if(this.time.current >= this.time.end){
this.actionFlag = true;
if(!this.loop){
this.status = DONE;
}
// if the timer is done, remove it from the UpdateManager
if(this.loop){
this.time.current = 0;
} else {
for(i = 0; i<UpdateManager.length; i++){
if(this == UpdateManager[i]){
UpdateManager.splice(i, 1);
}
}
}
}
}
this.resetFlag = function(){
this.actionFlag = false;
}
}
function Animation(imageUrlArray, duration, repeat, curr_game){
IN_ACTION = 10;
DONE = 11;
this.duration = duration;
this.repeat = repeat;
this.game = curr_game;
this.status = IN_ACTION;
this.timer = null;
this.imageArray = new Array(imageUrlArray.length-1);
parent = this;
for(var i = 0; i<imageUrlArray.length; i++){
this.imageArray[i] = new Image();
this.imageArray[i].src = imageUrlArray[i];
}
this.frame = {};
this.current = function(){
// check if timer has been created yet, if not, create it.
// this prevents 'one-time' animations from starting prematurely
if(this.timer == null){
this.timer = new Timer(this.duration, this.repeat, BuckyGame);
}
this.status = this.timer.status;
return this.imageArray[ Math.round( (this.timer.time.current / this.timer.time.end) * (this.imageArray.length-1) ) ];
}
}
function PlaceableAnimation(x_pos, y_pos, imageUrlArray, duration, repeat, curr_game){
IN_ACTION = 10;
DONE = 11;
this.animation = new Animation(imageUrlArray, duration, repeat, curr_game);
this.position = new Position(x_pos, y_pos);
this.physics = function(x_change){
this.position.x += x_change;
// if the timer's status is DONE, remove this animation from the update manager.
// (never happens if the animation loops)
if(this.animation.timer.status == DONE){
for(i = 0; i<UpdateManager.length; i++){
if(this == UpdateManager[i]){
UpdateManager.splice(i, 1);
}
}
}
}
this.current = function(){
return this.animation.current();
}
this.draw = function(){
ctx.drawImage(this.current(), Math.floor(this.position.x), Math.floor(this.position.y)+Math.sin(BuckyGame.drunkTime+(this.position.x-BuckyGame.drawOffset)/Math.pow(blocksize, 2)*BuckyGame.drunkPeriod)*BuckyGame.drunkStrength);
}
}
function MotionTween(startx, starty, endx, endy, duration, game){
this.begin_x = startx;
this.begin_y = starty;
this.end_x = endx;
this.end_y = endy;
this.duration = duration;
this.current = {};
this.game = game;
this.timer = new Timer(this.duration, false, this.game);
this.x = function(){
return Math.round( (this.timer.time.current / this.timer.time.end) * (this.end_x - this.begin_x) + this.begin_x );
}
this.y = function(){
return Math.round( (this.timer.time.current / this.timer.time.end) * (this.end_y - this.begin_y) + this.begin_y );
}
}
function collisionAction(movable, stationary){
var movableCenterX = 0;
var movableCenterY = 0;
var stationaryCenterX = 0;
var stationaryCenterY = 0;
var distanceX = 0;
var distanceY = 0;
var minDistanceX = 0;
var minDistanceY = 0;
var depthX = 0;
var depthY = 0;
movableCenterX = movable.position.x + movable.width/2;
movableCenterY = movable.position.y + movable.height/2;
stationaryCenterX = stationary.position.x + stationary.width/2;
stationaryCenterY = stationary.position.y + stationary.height/2;
distanceX = movableCenterX - stationaryCenterX;
distanceY = movableCenterY - stationaryCenterY;
minDistanceX = (movable.width-movable.collision.width_offset)/2 + (stationary.width-stationary.collision.width_offset)/2;
minDistanceY = (movable.height-movable.collision.height_offset)/2 + (stationary.height-stationary.collision.height_offset)/2;
if(Math.abs(distanceX) >= minDistanceX || Math.abs(distanceY) >= minDistanceY){
return false;
depthX = 0;
depthY = 0;
} else {
depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
}
collisionChecks ++;
if( ( (movable instanceof Player || movable instanceof Enemy) && (stationary instanceof Block) ) ||
(movable instanceof Enemy && stationary instanceof Enemy) || (movable instanceof Enemy && stationary instanceof ItemBlock) ){
if(Math.abs(depthY) < Math.abs(depthX)){ // resolve y first if true
movable.position.y += depthY;
if(depthY<0){
movable.jump.toggle = true;
movable.jump.release = false;
movable.vel.y = 0;
} else {
movable.vel.y = 0;
}
} else { // resolve x first if the first statement was false
movable.position.x += depthX;
if(movable instanceof Enemy && Math.abs(depthX) != 0){
movable.collided = true;
}
//this.x_speed = 0;
}
return true;
} else if (movable instanceof Player && stationary instanceof Item){
if(depthY != 0 || depthX != 0){
if(BuckyGame.drunkStrength <= BuckyGame.drunkStrengthLimit && stationary.visible){
BuckyGame.drunkStrength += BuckyGame.drunkStrengthIncrement;
}
stationary.visible = false;
return true;
} else {
return false;
}
} else if (movable instanceof Player && stationary instanceof ItemBlock){
if(Math.abs(depthY) < Math.abs(depthX)){ // resolve y first if true
movable.position.y += depthY;
if(depthY<0){
movable.jump.toggle = true;
movable.jump.release = false;
movable.vel.y = 0;
} else {
movable.vel.y = 0;
if(stationary.state != HIT){
for(i = 0; i<itemTypes.length; i++){
if(itemTypes[i].num == stationary.contains){
UpdateManager.push( new Item(stationary.position.x, stationary.position.y-blocksize, itemTypes[i].getTile()));
}
}
}
stationary.state = HIT;
}
} else { // resolve x first if the first statement was false
movable.position.x += depthX;
//this.x_speed = 0;
}
return true;
} else if (movable instanceof Player && stationary instanceof Enemy){
if(Math.abs(depthY)<25 && movable.vel.y > stationary.vel.y && movable.position.y < stationary.position.y){
movable.position.y -= Math.abs(movable.position.y + movable.height - stationary.position.y) * 1.1;
if(Controller.space){
movable.vel.y = -8;
} else {
movable.vel.y = -4;
}
stationary.state = DEAD;
movable.sounds.splat.currentTime = 0;
movable.sounds.splat.play();
} else {
movable.state = DEAD;
movable.sounds.boom.currentTime = 0;
movable.sounds.boom.play();
}
} else if (movable instanceof Player && stationary instanceof HurtBlock){
if(Math.abs(depthX) < Math.abs(depthY)){
movable.state = DEAD;
movable.sounds.boom.currentTime = 0;
movable.sounds.boom.play();
} else {
}
return true;
} else if (movable instanceof Enemy && stationary instanceof ItemBlock){
if(Math.abs(depthY) < Math.abs(depthX)){ // resolve y first if true
movable.position.y += depthY;
if(depthY<0){
movable.jump.toggle = true;
movable.jump.release = false;
movable.vel.y = 0;
} else {
movable.vel.y = 0;
if(stationary.state != HIT){
for(i = 0; i<itemTypes.length; i++){
if(itemTypes[i].num == stationary.contains){
UpdateManager.push( new Item(stationary.position.x, stationary.position.y-blocksize, itemTypes[i].getTile()));
}
}
}
}
} else { // resolve x first if the first statement was false
movable.position.x += depthX;
//this.x_speed = 0;
}
return true;
} else if (movable instanceof Player && stationary instanceof WinBlock){
if(depthY != 0 || depthX != 0){ // resolve y first if true
BuckyGame.wonLevel = true;
}
return true;
}
}
// tile collection, used for sets of random-use tiles
function TileCollection(imageUrlArray) {
this.tileArray = new Array(imageUrlArray.length-1);
for(i = 0; i<imageUrlArray.length; i++){
this.tileArray[i] = new Image();
this.tileArray[i].src = imageUrlArray[i];
}
this.getRandomTile = function() {
return(this.tileArray[Math.floor(Math.random()*this.tileArray.length)])
}
}
// used to implement autotiling, making map creating much easier
function TileSet(imageUrlArray, typeNum, tileType) {
// starts off with 9 set, ends with 4 set
// topleft, topmiddle, topright, middleleft, middlemiddle, middleright, bottomleft, bottommiddle, bottomright
// topsingle, leftsingle, rightsingle, bottomsingle
this.tileArray = new Array(imageUrlArray.length-1);
this.type = tileType;
this.num = typeNum;
for(i = 0; i<imageUrlArray.length; i++){
this.tileArray[i] = new Image();
this.tileArray[i].src = imageUrlArray[i];
}
this.aryComp = function(array1, array2){
for(i = 0; i<array1.length; i++){
for(k = 0; k<array1[i].length; k++){
if( ( array1[i][k] != array2[i][k] ) && ( array2[i][k] != 9 ) ) return false;
}
}
return true;
}
this.getTile = function(sur){
// get sent a 3x3 array of the tiles around current spot (this one being the middle)
if(sur != null){
for(i = 0; i<sur.length; i++){
for(k = 0; k<sur[1].length; k++){
if(sur[i][k] != this.num){
sur[i][k] = 0;
}
}
}
}
if(sur != null && this.num != sur[1][1]) return false;
if(this.type == "full"){
if( this.aryComp(sur, [[9,0,9],[0,this.num,this.num],[9,this.num,9]] ) ){ return this.tileArray[0];
} else if( this.aryComp(sur, [[9,0,9],[this.num,this.num,this.num],[9,this.num,9]] ) ){ return this.tileArray[1];
} else if( this.aryComp(sur, [[9,0,9],[this.num,this.num,0],[9,this.num,9]] ) ){ return this.tileArray[2];
} else if( this.aryComp(sur, [[9,this.num,9],[0,this.num,this.num],[9,this.num,9]] ) ){ return this.tileArray[3];
} else if( this.aryComp(sur, [[9,this.num,9],[this.num,this.num,this.num],[9,this.num,9]] ) ){ return this.tileArray[4];
} else if( this.aryComp(sur, [[9,this.num,9],[this.num,this.num,0],[9,this.num,9]] ) ){ return this.tileArray[5];
} else if( this.aryComp(sur, [[9,this.num,9],[0,this.num,this.num],[9,0,9]] ) ){ return this.tileArray[6];
} else if( this.aryComp(sur, [[9,this.num,9],[this.num,this.num,this.num],[9,0,9]] ) ){ return this.tileArray[7];
} else if( this.aryComp(sur, [[9,this.num,9],[this.num,this.num,0],[9,0,9]] ) ){ return this.tileArray[8];
} else if( this.aryComp(sur, [[9,0,9],[0,this.num,0],[9,this.num,9]] ) ){ return this.tileArray[9];
} else if( this.aryComp(sur, [[9,0,9],[0,this.num,this.num],[9,0,9]] ) ){ return this.tileArray[10];
} else if( this.aryComp(sur, [[9,0,9],[this.num,this.num,0],[9,0,9]] ) ){ return this.tileArray[11];
} else if( this.aryComp(sur, [[9,this.num,9],[0,this.num,0],[9,0,9]] ) ){ return this.tileArray[12];
} else if( this.aryComp(sur, [[9,0,9],[this.num,this.num,this.num],[9,0,9]] ) ){ return this.tileArray[13];
} else if( this.aryComp(sur, [[9,this.num,9],[0,this.num,0],[9,this.num,9]] ) ){ return this.tileArray[14];
} else { return this.tileArray[15]; }
} else if(this.type == "dual"){
if( this.aryComp(sur, [[9,0,9],[9,this.num,9],[9,this.num,9]])){ return this.tileArray[0]; // top
} else if( this.aryComp(sur, [[9,0,9],[9,this.num,9],[9,0,9]])){ return this.tileArray[0]; // bottom
} else { return this.tileArray[1]; }
} else if(this.type == "single"){ return this.tileArray[0]; }
}
}
function Parallax(imageSrc, percentMovementSpeed, currGame){
this.image = new Image();
this.image.src = imageSrc;
this.width = this.image.width;
this.height = this.image.height;
this.multiplier = percentMovementSpeed;
this.game = currGame;
this.draw = function(){
x_draw1 = Math.round(this.game.drawOffset * this.multiplier%this.width-this.width);
x_draw2 = Math.round(this.game.drawOffset * this.multiplier%this.width);
x_draw3 = Math.round(this.game.drawOffset * this.multiplier%this.width+this.width);
if(x_draw1 < this.game.boundary.x && x_draw1+this.width >= 0){
ctx.drawImage(this.image, x_draw1, 0);
}
if(x_draw2 < this.game.boundary.x && x_draw2+this.width >= 0){
ctx.drawImage(this.image, x_draw2, 0);
}
if(x_draw3 < this.game.boundary.x && x_draw3+this.width >= 0){
ctx.drawImage(this.image, x_draw3, 0);
}
}
}