-
Notifications
You must be signed in to change notification settings - Fork 3
/
bucky_rewrite.html
532 lines (428 loc) · 14.9 KB
/
bucky_rewrite.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<!DOCTYPE html>
<head>
<script src="control.js"></script>
<script src="enemy.js"></script>
<script src="interactives.js"></script>
<script src="player.js"></script>
<script src="utility.js"></script>
<script src="loadmap.js"></script>
<script src="assets.js"></script>
<script src="maps.js"></script>
<script type = "text/javascript">
/* BugList
-- Timer's automatic removal can cause another single timer to not get updated that tick - low impact
*/
// debugging
var editor_mode = true;
var debugging = false;
var debugging_text = true;
var debugging_camera = false;
var music = false;
levelMusic = new Audio('music/canopy.ogg');
levelMusic.volume = 0.5;
levelMusic.addEventListener('ended', function() {
this.currentTime = 0;
if(music) this.play();
}, false);
if(music) levelMusic.play();
// Global Variables
var IN_ACTION = 10;
var DONE = 11;
var IMAGE_SHEET = 12;
var MOVEMENT = 13;
var DEAD = 1;
var ALIVE = 2;
var HIT = 3;
var UNHIT = 4;
var WALKING = 5;
var ATTACKING = 3;
var JUMPING = 4;
var STILL = 6;
var LEFT = -1;
var RIGHT = 1;
var ctx;
var blocksize = 25;
var imageMap;
var GRAVITY = 0.2;
var UpdateManager = new Array();
var clickLastFrame = false;
var statsBox = null;
var framecount = 0;
var physicsTimer = null;
var physExecuteMs = 7;
var drawExecuteMs = 16.667;
var physicsDelta = physExecuteMs;
var drawTimer = null;
var graphics_drawTimer= null;
var drawDelta = null;
var drawFps = 60;
var statsDiv;
var itemTypes;
var collisionChecks = 0;
var assets_loaded = 0;
var image_preload_array = new Array();
function Game(x_boundary, y_boundary){
this.boundary = {};
this.boundary.x = x_boundary; // x-coord boundary
this.boundary.y = y_boundary; // y-coord boundary
this.clearColor = "#7ADAE1"; // initial clear color is sky blue
this.state = null;
this.drawOffset = 0;
this.drunkTime = 0;
this.drunkPeriod = 5;
this.drunkTimeIncrement = 0.05;
this.drunkStrength = 0;
this.drunkStrengthIncrement = 1;
this.drunkStrengthLimit = 50;
this.physCorrect = 1;
this.drawCorrect = 1;
this.initialized = null;
this.camera = {};
this.camera.offset = 0;
this.camera.boundary = {};
this.camera.boundary.left = 250;
this.camera.boundary.right = 400;
this.camera.offset = 0;
this.camera.lag = 0.1;
this.camera.maxSpeed = 7;
this.editor = {};
this.editor.selected = null;
this.wonLevel = false;
this.resetGame = function() {
window.cancelAnimationFrame(animId);
begin_game();
}
this.clearScreen = function() {
ctx.fillStyle = this.clearColor;
ctx.fillRect(0, 0, this.boundary.x, this.boundary.y);
}
this.draw = function() {
if(this.wonLevel){
ctx.font = "100pt Helvetica";
ctx.lineWidth = 10;
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#FFFFFF";
ctx.strokeText("A WINRAR,", 125+Math.random()*20, 265+Math.random()*20);
ctx.strokeText(" IS YOU!", 125+Math.random()*20, 390+Math.random()*20);
ctx.fillText("A WINRAR,", 125+Math.random()*20, 265+Math.random()*20);
ctx.fillText(" IS YOU!", 125+Math.random()*20, 390+Math.random()*20);
}
}
}
function page_load() {
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
if (document.attachEvent){
document.attachEvent("on"+mousewheelevt, function(e) {mouse_scroll(e)}, false);
}else if (document.addEventListener){
document.addEventListener(mousewheelevt, function(e) {mouse_scroll(e)}, false);
}
preload(); // preloader will call begin game when ready
//begin_game();
}
function begin_game() {
// play muzak
UpdateManager = new Array();
statsDiv = document.getElementById("fps");
// reset timing variables to prevent odd behavior upon reset
physicsDelta = physExecuteMs;
drawTimer = null;
graphics_drawTimer= null;
drawDelta = null;
drawFps = 60;
// Define individual tilesets
GrassTileset = new TileSet(
new Array(
"images/grass_tileset/grass_top_left.png",
"images/grass_tileset/grass_top_middle.png",
"images/grass_tileset/grass_top_right.png",
"images/grass_tileset/grass_middle_left.png",
"images/grass_tileset/ground_tile.png",
"images/grass_tileset/grass_middle_right.png",
"images/grass_tileset/grass_bottom_left.png",
"images/grass_tileset/grass_bottom_middle.png",
"images/grass_tileset/grass_bottom_right.png",
"images/grass_tileset/grass_top_single.png",
"images/grass_tileset/grass_left_single.png",
"images/grass_tileset/grass_right_single.png",
"images/grass_tileset/grass_bottom_single.png",
"images/grass_tileset/grass_middle_single_horizontal.png",
"images/grass_tileset/grass_middle_single_vertical.png",
"images/grass_tileset/grass_single.png"
),
1,
"full"
);
RockTileset = new TileSet(
new Array(
"images/rock_tileset/platform.png",
"images/rock_tileset/platform_underneath.png"
),
2,
"dual"
);
SpikeTileset = new TileSet(
new Array(
"images/spike_tileset/spike_pit.png",
"images/spike_tileset/spike_lower.png"
),
4,
"dual"
);
BeerTileset = new TileSet(
new Array(
"images/items/beer_mug.png"
),
3,
"single"
);
ItemBlockTileset = new TileSet(
new Array(
"images/items/item_block.png",
"images/items/item_block_hit.png"
),
6,
"single"
);
// define different tile collections
collideTypes = new Array(GrassTileset, RockTileset);
itemTypes = new Array(BeerTileset);
TilesetArrays = new Array(GrassTileset, RockTileset, SpikeTileset);
EditorTilesetArray = new Array(
(new TileSet(new Array("images/blank.png"), 0, "single")),
GrassTileset,
RockTileset,
SpikeTileset,
BeerTileset,
ItemBlockTileset,
(new TileSet(new Array("images/enemy/generic_enemy_placeholder.gif"), 5, "single"))
);
RightClickMenu = new TileMenu();
// get a reference to the canvas
var canvas = document.getElementById("draw_canvas");
ctx = canvas.getContext("2d");
// declare initial objects (Controller, Game, Player, etc)
Controller = new Control();
BuckyGame = new Game(1000, 600);
// CALL MAPGEN BEFORE ANYTHING ENTERS THE UPDATE MANAGER (or it will be cleared)
mapGen();
Buckingham = new Player(300, 0, BuckyGame);
TestInfo = new InfoBox(500, 100, "Test Info Box:", "Bacon ipsum dolor sit amet brisket pork belly short loin, sausage ham hock kielbasa swine pork.");
UpdateManager.push(TestInfo);
PlayButton = new Button(500, 450, 100, 30, "#000000", "#CCCCCC", "Play Game");
ResetButton = new Button(10, 10, 120, 30, "#000000", "#CCCCCC", "Reset Game",
function(){
clearInterval(statsInterval);
clearInterval(physInterval);
BuckyGame.resetGame();
}
);
UpdateManager.push(ResetButton);
// PLACEABLE ANIMATION TEST
// TestPlaceableAnimation = new PlaceableAnimation(
// 250,
// -15,
// new Array("images/animations/explosion/explosion1.gif", "images/animations/explosion/explosion2.gif", "images/animations/explosion/explosion3.gif", "images/animations/explosion/explosion4.gif", "images/animations/explosion/explosion5.gif", "images/animations/explosion/explosion6.gif", "images/animations/explosion/explosion7.gif", "images/animations/explosion/explosion8.gif", "images/animations/explosion/explosion9.gif", "images/animations/explosion/explosion10.gif", "images/animations/explosion/explosion11.gif", "images/animations/explosion/explosion12.gif", "images/animations/explosion/explosion13.gif", "images/animations/explosion/explosion14.gif", "images/animations/explosion/explosion15.gif", "images/animations/explosion/explosion16.gif", "images/animations/explosion/explosion17.gif", "images/animations/explosion/explosion18.gif", "images/animations/explosion/explosion19.gif", "images/animations/explosion/explosion20.gif", "images/animations/explosion/explosion21.gif", "images/animations/explosion/explosion22.gif", "images/animations/explosion/explosion23.gif", "images/animations/explosion/explosion24.gif"
// ),
// 60,
// true,
// BuckyGame
// );
//UpdateManager.push(TestPlaceableAnimation);
// define parallax background
Background = new Parallax("images/backgrounds/test_background.jpg", 0.3, BuckyGame);
caller(); // start the physics and drawing loops to start game
}
function caller(){
physInterval = setInterval(physics, physExecuteMs);
if(debugging || debugging_text){
statsInterval = setInterval(stats, 200);
}
draw_world();
}
function physics(){
collisionChecks = 0;
/******************************************
PHYSICS TIMER, USED FOR ACCURATE MOVEMENT
******************************************/
if(physicsTimer == null){
physicsTimer = performance.now();
} else {
physicsDelta = performance.now()-physicsTimer;
physicsTimer = performance.now();
BuckyGame.physCorrect = BuckyGame.physCorrect * (4/5) + physicsDelta/physExecuteMs * (1/5);
if(BuckyGame.physCorrect > 3) BuckyGame.physCorrect = 3;
}
if(Controller.control){
var output_string = "map = [\n";
for(i = 0; i<map.length; i++){
for(k = 0; k<map[0].length; k++){
if(k == 0){
output_string += "[" + map[i][k] + ", ";
} else if(k==map[0].length-1 && i != map.length-1){
output_string += map[i][k] + "],\n";
} else if(i == map.length-1 && k == map[0].length-1){
output_string += map[i][k] + "]\n];";
} else {
output_string += map[i][k] + ", ";
}
}
}
document.getElementById('MapOutput').innerHTML=output_string;
}
// perform player's physics
Buckingham.physics();
// camera physics
if( !(Buckingham.position.x + Buckingham.width <= BuckyGame.camera.boundary.right && Buckingham.position.x >= BuckyGame.camera.boundary.left) ) {
// drop it like it's hot.
if(Buckingham.position.x + Buckingham.width > BuckyGame.camera.boundary.right){
BuckyGame.camera.offset = -(Buckingham.position.x + Buckingham.width - BuckyGame.camera.boundary.right);
} else {
BuckyGame.camera.offset = BuckyGame.camera.boundary.left - Buckingham.position.x;
}
} else {
BuckyGame.camera.offset = 0;
}
BuckyGame.drawOffset += BuckyGame.camera.offset;
for(var i = 0; i<UpdateManager.length; i++){
UpdateManager[i].physics(BuckyGame.camera.offset);
}
if(Controller.mouse.click.left && editor_mode && BuckyGame.editor.selected){
clickLastFrame = true;
y_tile = Math.floor(Controller.mouse.move.y/blocksize);
x_tile = Math.floor( (Controller.mouse.move.x - BuckyGame.drawOffset)/blocksize );
if(BuckyGame.editor.selected != null){
map[y_tile][x_tile] = BuckyGame.editor.selected.num;
imageMap[y_tile][x_tile] = BuckyGame.editor.selected.tileArray[0];
}
}
if(!Controller.mouse.click.left && clickLastFrame){
if(BuckyGame.editor.selected != null){
mapGen();
}
clickLastFrame = false;
}
}
function stats() {
/******************************************
GENERATE SOME STATISTICS FOR SHOWING ONSCREEN
******************************************/
// currently unused, replaced by canvas infobox.
}
function draw_world() {
/******************************************
GRAPHICS TIMER, USED FOR STATS DISPLAY
******************************************/
if(Controller.minus){
physExecuteMs += (4/60);
console.log("physExecuteMs is now " + physExecuteMs);
} else if (Controller.plus){
physExecuteMs -= (4/60);
if(physExecuteMs <= 0){
physExecuteMs = (4/60);
}
console.log("physExecuteMs is now " + physExecuteMs);
}
if(graphics_drawTimer == null){
graphics_drawTimer = performance.now();
} else {
drawDelta = performance.now()-graphics_drawTimer;
graphics_drawTimer = performance.now();
drawFps = drawFps * (49/50) + 1000/drawDelta * 1/50;
if(drawFps > 1000) drawFps = 60; // sanity check (check for infinity)
BuckyGame.drawCorrect = BuckyGame.drawCorrect * (4/5) + drawDelta/drawExecuteMs * (1/5);
if(BuckyGame.drawCorrect > 2) BuckyGame.drawCorrect = 2;
}
BuckyGame.clearScreen();
Background.draw();
BuckyGame.drunkTime += BuckyGame.drunkTimeIncrement;
// draw tile image map
for(var i = 0; i<map.length; i++){
for(var k = 0; k<map[i].length; k++){
if( (k+1)*blocksize+BuckyGame.drawOffset > 0 && (k-1)*blocksize+BuckyGame.drawOffset<BuckyGame.boundary.x){
if(imageMap[i][k] != null) ctx.drawImage(imageMap[i][k], Math.floor(k*blocksize+BuckyGame.drawOffset), Math.floor((i*blocksize)+Math.sin(BuckyGame.drunkTime+(k*BuckyGame.drunkPeriod)/blocksize)*BuckyGame.drunkStrength), blocksize, blocksize);
}
}
}
/********************************************
RUN THROUGH UPDATEMANAGER ITEMS
DRAW DRAWABLE ITEMS, UPDATE OTHERS
*******************************************/
for(var i = 0; i<UpdateManager.length; i++){
temp = UpdateManager[i];
if(temp.draws){
temp.draw();
}
}
// for(var i = 0; i<UpdateManager.length; i++){
// temp = UpdateManager[i];
// if (temp instanceof Timer){
// temp.physics(BuckyGame.camera.offset);
// }
// }
Buckingham.draw();
BuckyGame.draw();
ResetButton.draw();
if(editor_mode && BuckyGame.editor.selected != null){
ctx.fillStyle = "#000000";
ctx.lineWidth = 0.3;
for(var i = 0; i < Math.floor(BuckyGame.boundary.y/blocksize); i++){
ctx.beginPath();
ctx.moveTo(0, i*blocksize);
ctx.lineTo(BuckyGame.boundary.x, i * blocksize);
ctx.stroke();
}
for(var i = 0; i < Math.floor(BuckyGame.boundary.x/blocksize)+1; i++){
ctx.beginPath();
ctx.moveTo(i * blocksize+BuckyGame.drawOffset%blocksize, 0);
ctx.lineTo(i * blocksize+BuckyGame.drawOffset%blocksize, BuckyGame.boundary.y);
ctx.stroke();
}
}
// draw out stats container
if(debugging_text || debugging){
if(framecount == 0){
statsBox = new InfoBox(BuckyGame.boundary.x-260, 10,
"Debugging Panel: ",
"PhysDelta: "
+ physicsDelta.toFixed(3)
+ "\n PhysCorrect: "
+ BuckyGame.physCorrect.toFixed(2)
+ "\n DrawCorrect: "
+ BuckyGame.drawCorrect.toFixed(2)
+ "\n drawFPS: "
+ drawFps.toFixed(2)
+ "\n BuckyVelX : "
+ Buckingham.vel.x.toFixed(2)
+ "\n UpdateManager: "
+ UpdateManager.length
+ "\n Collisions: "
+ collisionChecks);
framecount = 6;
}
framecount--;
statsBox.draw();
ctx.fillStyle = "#DDFFDD";
RightClickMenu.draw();
}
if(debugging_camera){
ctx.strokeStyle = "#FF2222";
ctx.lineWidth = 2;
ctx.strokeRect(BuckyGame.camera.boundary.left, 0, BuckyGame.camera.boundary.right-BuckyGame.camera.boundary.left, 600);
}
animId = window.requestAnimationFrame(draw_world);
}
window.addEventListener('keydown',key_event,true);
window.addEventListener('keyup',key_event_up,true);
window.addEventListener('mouseup', mouse_up, false);
window.addEventListener('mousedown', mouse_down, false);
</script>
</head>
<body style="overflow: hidden;" onload="page_load()" oncontextmenu="return false;">
<canvas onmousemove="mouse_move(event)" onmousedown="mouse_press(event)" id="draw_canvas" width="1000" height="600" tabindex="1"></canvas>
<textarea id="MapOutput" style="width: 400px; height: 150px;">Map Output</textarea>
<script type="text/javascript">
gameCanvas = document.getElementById("draw_canvas");
document.getElementById("draw_canvas").focus();
</script>
</body>
</html>