-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
470 lines (372 loc) · 14.8 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
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
//make the upgrades, add them to an upgradeList
upgradeList = intitalizeUpgradeList();//creates the list of upgrades
function init() {//called when this is loaded
//gravity is initialized in initial
world = new b2World(gravity, true);
hello=0;
//canvas initialized in the initial
context = canvas.getContext("2d");
dtRemaining = 0;
stepAmount = 1/60;
debug=0;
//debugDraw
debugDraw = new CanvasDebugDraw(context);
debugDraw.scale=SCALE;
debugDraw.SetFlags(b2Draw.e_shapeBit | b2Draw.e_jointBit);
world.SetDebugDraw(debugDraw);
$("#debugDraw").click(function () {
if ($("#debugDraw:checked").val()) {
debug = true;
} else {
debug = false;
canvas.width = canvas.width;
}
});
createWorld();
//Create DOB OBjects
createDOMObjects();
window.addEventListener("keydown",doKeyDown);
window.addEventListener("keyup",doKeyUp);
requestAnimationFrame(gameLoop);
};
function update(dt) {
dtRemaining += dt;
while(dtRemaining > stepAmount) {
dtRemaining -= stepAmount;
world.Step(stepAmount,
10, // velocity iterations
10);// position iterations
}
if(debug) {
world.DrawDebugData();
//drawDOMObjects();//#########################FOR THE DIV MOVING
} else {
context.clearRect(0,0,canvas.width,canvas.height);
context.save();
context.scale(SCALE,SCALE);
giraffe.update();//eat and blink
updateUpgrades();//add new upgrades if any exist
if (waterSpoutIsActive){
waterSpout.activate();
};
if (snow){
snow.update();
};
//reset all of the body Arrays, which help us draw in order
regularBodies.length=0;//the list of normal bodies. this is what the majority of things should be in
rainbowParticleBodies.length=0;
potionParticleBodies.length=0;
tailBodies.length=0;
giraffeBodies.length=0;
giraffeAdornmentBodies.length=0;
wallPieceBodies.length=0;
debugPointsBodies.length=0;
//Each body has a bodywrapper. This bodywrapper has the draw function. It also has a
//details node which stores EVERYTHING we need to know about the body that we set.
for (var body = world.GetBodyList() ; body!=null; body = body.GetNext()) {
var bodyWrap = body.GetUserData();//gets the bodycontaininer from a b2d definition
if ((bodyWrap === undefined || bodyWrap === null)){continue;}//don't like that one null body
//UpdateDanger
if (inArray(bodyWrap.classID,dangerousObjects)){//if it is in dangerous objects
updateDanger(bodyWrap);
}
//Destroy a body
if ((body.GetPosition().x<-10)||(body.GetPosition().x>80)||(body.GetPosition().y>60)){//need to update to say if it's above or to the left?
destroyBodyList.push(body);
continue;
}
//if the parent has an update function, do that. This updates the actual object.
if ( inArray(bodyWrap.desc,bodiesWithParentUpdate)){
body.parent.update();
}
//if the body has an actual function that should run and update, do that (for example each wall piece must be checked)
if (bodyWrap.updateFunction){
bodyWrap.updateFunction();
}
//if they are to be drawn explicitly from an array later, add them to their respective Array
if (bodyWrap.desc in bodyArrayMapping){
bodyArrayMapping[bodyWrap.desc].push(body);
continue;
}
if (inArray(bodyWrap.desc,bodiesExplicitlyDrawn)){//if we are a body to ignore, skip
continue;
}
if(bodyWrap) {
//bodyWrap.draw(context);
regularBodies.push(body);//add this body to the regularBodies
}
}
drawDOMObjects();//Enable this to get the test div to move
trash.addObjectBodiesToDestroyList();//adds all bodies associated with objects that have been deleted to destroy list
trash.takeOutTheTrash();//destroys all the bodies on that list
environmentBodies.length=0;
allBodiesToDraw.length=0;
//always behind
//allBodiesToDraw.push(backgroundSkyBody);
allBodiesToDraw.push(cloud.cloud);
allBodiesToDraw.push(grassBack);
giraffeBodies.push(giraffe.leg1);
giraffeBodies.push(giraffe.leg2);
giraffeBodies.push(giraffe.leg3);
giraffeBodies.push(giraffe.leg4);
giraffeBodies.push(giraffe.neckLow);
giraffeBodies.push(giraffe.neckHigh);
giraffeBodies.push(giraffe.head);
giraffeBodies.push(giraffe.torso);
giraffeBodies=giraffeBodies.concat(tailBodies);
//draw the environment background
allBodiesToDraw=allBodiesToDraw.concat(regularBodies);//add in the regular bodies
potionParticleBodies.sort(function(a,b){return b.particleIndex-a.particleIndex;});
allBodiesToDraw=allBodiesToDraw.concat(potionParticleBodies);//add potion particles
allBodiesToDraw=allBodiesToDraw.concat(rainbowParticleBodies);//add unicorn trails
//draw giraffe here.
allBodiesToDraw=allBodiesToDraw.concat(giraffeBodies);
allBodiesToDraw=allBodiesToDraw.concat(giraffeAdornmentBodies);
//draw the other things
allBodiesToDraw.push(grassFront);
allBodiesToDraw=allBodiesToDraw.concat(wallPieceBodies);
allBodiesToDraw.push(bottomHidden);
allBodiesToDraw=allBodiesToDraw.concat(debugPointsBodies);
for (var i=0;i<allBodiesToDraw.length;i++){
var bodyToDraw=allBodiesToDraw[i];
if (bodyToDraw.GetUserData().desc=='fireParticle'){
if (bodyToDraw.parent.remaining_life>15)
{
context.globalCompositeOperation = "lighter";
bodyToDraw.GetUserData().draw(context);
context.globalCompositeOperation = "source-over";
}
else
{
bodyToDraw.GetUserData().draw(context);
}
continue;
}
bodyToDraw.GetUserData().draw(context);
}
//add the debug numbers
if (showDebugExtras){
context.font="2px Arial";
context.fillText("x "+calcX,1,2);
context.fillText("y "+calcY,1,3.8);
}
context.restore();
}
};
function click(callback) {
function handleClick(e) {
e.preventDefault();
var point = {
x: (e.offsetX || e.layerX) / SCALE,
y: (e.offsetY || e.layerY) / SCALE
};
world.QueryPoint(function(fixture) {
callback(fixture.GetBody(),
fixture,
point);
},point);
}
canvas.addEventListener("mousedown",handleClick);
canvas.addEventListener("touchstart",handleClick);
};
lastFrame = new Date().getTime();
window.gameLoop = function() {
var tm = new Date().getTime();
requestAnimationFrame(gameLoop);
var dt = (tm - lastFrame) / 1000;
if(dt > 1/15) { dt = 1/15; }
update(dt);
lastFrame = tm;
};
function createWorld() {
//create World elements
//backgroundSkyBody = createABody(backgroundSkyProps);
bottomHidden = createABody(bottomHiddenProps);
grassBack = createABody(grassBackProps);
grassFront = createABody(grassFrontProps);
cloud = new Cloud();
trash = new Trash();
// Create some walls
var thickness=3;
var offset=thickness/2;
heightBottomBox=10;
new bodyWrapper( { color: "black", type: "static", desc: "wall",x:canvas.width/SCALE+offset, y: 0, height: 60, width: thickness,categoryBits:CATEGORY_WALL});//right
new bodyWrapper( { color: "black", type: "static", desc: "wall",x: 0, y: -offset, height: thickness, width: 120,categoryBits:CATEGORY_WALL });//top widths are super long so nothing falls through
leftWall = new LeftWall();
//bottomWall=createABody(bottomWallProps);
//fireball=new Fireball();
//Create DOB OBjects
//createDOMObjects(); ;#########################FOR THE DIV MOVING;#########################FOR THE DIV MOVING
giraffe = new Giraffe();
bloob= new Bloob(2,new b2Vec2(20,20),"blue");
bloob= new Bloob(2,new b2Vec2(20,20),"green");
bloob= new Bloob(2,new b2Vec2(20,20),"orange");
dragNDrop();
world.SetContactListener(listener);//Add listener here so that everything is initialized
}
function doKeyDown(e){
//if they hit q, move rocket
if (e.keyCode==81){
if (!rocketsAreMoving){
rocketsAreMoving=1;
}
}
if (e.keyCode==16){
showDebugExtras=!showDebugExtras;
}
}
//SHOWS DEBUG MOUSE COORDINATES
function doKeyUp(e){
if (e.keyCode==81){
rocketsAreMoving=0;
}
if (e.keyCode==90){//hit z to go into debug mode
debug=!debug;
}
}
window.addEventListener("load",init);
//Lastly, add in the `requestAnimationFrame` shim, if necessary. Does nothing
//if `requestAnimationFrame` is already on the `window` object.
(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);
};
}
}());
//used if you are dragging and dropping
function dragNDrop() {
var obj = null;
var joint = null;
function calculateWorldPosition(e) {//gets where the event happened
return point = {
x: (e.offsetX || e.layerX) / SCALE,
y: (e.offsetY || e.layerY) / SCALE
};
}
//TODO Delete this tag if not used anymore. Don't think it is.
//Finds a fixture that we should be able to click and drag. Anything that is dynamic should be clickable and draggable.
// function queryFixture(fixture){
// var body=fixture.GetBody(); //get the body
// obj = body.GetUserData(); //used later down the line
// //print(obj.details.desc); //for debug purposes.
// if (body.GetType()==b2Body.b2_dynamicBody){
// print(obj.details.desc);
// return false;//return false to stop iterating, we have found a body that is dynamic (which is what we are looking for)
//
// }
// return true;//return true to keep checking for fixtures.
// };
function QueryCallback(point)
{
this.m_point = point;
this.m_fixture = null;
}
QueryCallback.prototype =
{
ReportFixture: function(fixture)
{
var body = fixture.GetBody();
obj = body.GetUserData(); //used later down the line
if (body.GetType() == b2Body.b2_dynamicBody)
{
var inside = fixture.TestPoint(this.m_point);
if (inside)
{
this.m_fixture = fixture;
// We are done, terminate the query.
return false;
}
}
// Continue the query.
return true;
}
};
canvas.addEventListener("mousedown",function(e) {
e.preventDefault();
var point = calculateWorldPosition(e);
// Make a small box.
var aabb = new b2AABB();
var d = new b2Vec2();
d.Set(0.001, 0.001);
aabb.lowerBound = b2Vec2.Subtract(point, d);
aabb.upperBound = b2Vec2.Add(point, d);
// Query the world for overlapping shapes.
var callback = new QueryCallback(point);
//this.m_world.QueryAABB(callback, aabb);
world.QueryAABB(callback,aabb);
//So that we have the mouse position
//If clicking on giraffe, change his eyes
if (inArray(obj.desc,giraffeParts)){
giraffe.beingDragged=1;
}
if(trash.isActive){
//TODO If you can trash it, you should be able to do this.
new TrashHand(point,obj);
}
print(obj.desc);//print out which object we are clicking on
});
canvas.addEventListener("mousemove",function(e) {
if(!obj) { return; }
var point = calculateWorldPosition(e);
if(!joint) {
var jointDefinition = new b2MouseJointDef();
jointDefinition.bodyA = world.GetGroundBody();
jointDefinition.bodyB = obj.body;
jointDefinition.target.Set(point.x,point.y);
jointDefinition.maxForce = 9999999;
jointDefinition.timeStep = stepAmount;
joint = world.CreateJoint(jointDefinition);
}
joint.SetTarget(new b2Vec2(point.x,point.y));
});
canvas.addEventListener("mouseup",function(e) {
if(!obj) { return; }
//If we are clicking on giraffe, reset mouth
if (inArray(obj.desc,giraffeParts)){
giraffe.mouth.reset=1;
}
obj = null;
if(joint) {
world.DestroyJoint(joint);
joint = null;
}
});
};
function updateDanger(wrapper){
var change;
var body=wrapper.body;
change=Point.distance(new Point(body.GetPosition().x,body.GetPosition().y),new Point(wrapper.lastX,wrapper.lastY));
var addition=(wrapper.danger*change)/10;
dangerFaced+=addition;
totalDangerFaced+=addition;
dangerFaced=Math.round(dangerFaced * 100) / 100;
totalDangerFaced=Math.round(totalDangerFaced * 100) / 100;
$("#dangerFacedValue").html(dangerFaced.toFixed(1));
$("#totalDangerValue").html(totalDangerFaced.toFixed(1));
wrapper.lastX=body.GetPosition().x;;
wrapper.lastY=body.GetPosition().y;
}
function increaseDangerFaced(value){
dangerFaced+=value;
}
function print(string){
console.log(string);
}