-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
443 lines (332 loc) · 13 KB
/
sketch.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
//THIS IS A TEMPLATE FOR CREATING A P5 GALAGO GAME CANVAS
var gameEngine;
var instructions;
var boxImage;
var boxSprite;
//Preload data such as images and sound
function preload() {
boxImage = loadImage("images/crate.png");
}
//Setup the canvas and all preloaded canvas detail
function setup() {
createCanvas(800, 600);
instructions = createP("Click mouse to set the box as a sensor and release to disable sensor.");
instructions.position(width/2-260, 1);
instructions.style("color: white; font-size: 18px;");
gameEngine = new GameEngine();
gameEngine.debugEnabled = true;
gameEngine.load();
//gameEngine.setTimeScale(0.2);
addBox(width/2, 100, 50, 50);
addPlatform(width/2, 500, 500, 10);
gameEngine.start();
}
//Draw loop when rendering the sprites and world
function draw() {
background(80);
//Update the gameEngine
gameEngine.update();
}
function addBox(xPos, yPos, w, h) {
sprite = new Sprite(boxImage, xPos, yPos, w, h, gameEngine.BOX, {isStatic: false, collisionFilter: {group: -1}});
sprite.setUserData("tag", "box");
sprite.setPhysicsScale(1, 0.5);
sprite.setPhysicsOffset(0, -h*0.25);
gameEngine.addSprite(sprite);
sprite.setCollisionCallback(function(sprite, collider) {
// var tag = collider.getUserData("tag");
// if (tag == 'box') {
// console.log("******************************************************************************");
// console.log(collider);
// collider.body.isSensor = true;
// // collider.setSensor(true);
// } else {
// collider.body.isSensor = false;
// }
});
}
function addPlatform(xPos, yPos, w, h) {
sprite = new Sprite(null, xPos, yPos, w, h, gameEngine.BOX, {isStatic: true});
gameEngine.addSprite(sprite);
}
function mousePressed() {
addBox(mouseX, mouseY, random(30, 60), random(30, 60));
}
function mouseReleased() {
}
// var gameEngine;
// var lastItem;
// var crateImage;
// var ballImage;
// var playerImage;
// var smokeImage;
// var ringImage;
// var bladeImage;
// var platformImage;
// var platformStoneImage;
// var groundLeftImage;
// var groundRightImage;
// var groundCenterImage;
// var sideLeftImage;
// var sideRightImage;
// var spider1Image;
// var spider2Image;
// var spider3Image;
// var spider4Image;
// var spider5Image;
// var spider6Image;
// var spider7Image;
// var spider;
// var pickedSprite;
// var movePlatformSprite;
// var platformMoveSpeed = 0.6;
// var platformMoveDistance = 80;
// var bladeSprite;
// var player;
// var moveSpeed = 4;
// var smokeParticleSystem;
// var onGround = false;
// var bombInterval = 100;
// function preload() {
// crateImage = loadImage("images/crate.png");
// ballImage = loadImage("images/spikeball.png");
// playerImage = loadImage("images/player.png");
// smokeImage = loadImage("images/smoke.png");
// platformImage = loadImage("images/platform.png");
// platformStoneImage = loadImage("images/platform-stone.png");
// ringImage = loadImage("images/ring.png");
// bladeImage = loadImage("images/blade.png");
// groundLeftImage = loadImage("images/ground-left.png");
// groundRightImage = loadImage("images/ground-right.png");
// groundCenterImage = loadImage("images/ground-center.png");
// sideLeftImage = loadImage("images/side-left.png");
// sideRightImage = loadImage("images/side-right.png");
// spider1Image = loadImage("images/spider-sprite1.png");
// spider2Image = loadImage("images/spider-sprite2.png");
// spider3Image = loadImage("images/spider-sprite3.png");
// spider4Image = loadImage("images/spider-sprite4.png");
// spider5Image = loadImage("images/spider-sprite5.png");
// spider6Image = loadImage("images/spider-sprite6.png");
// spider7Image = loadImage("images/spider-sprite7.png");
// }
// function setup() {
// createCanvas(1024, 768);
// gameEngine = new GameEngine();
// gameEngine.load();
// ////This is the collision listener
// //gameEngine.addCollisionListener(function(collisionEvent) {
// // // console.log("collisionEvent: " + collisionEvent.colliderB);
// // if (collisionEvent.colliderA.getUserData("player") != null ||
// // collisionEvent.colliderB.getUserData("player") != null) {
// // onGround = true;
// // }
// // if (collisionEvent.colliderA.getUserData("bomb") != null) {
// // collisionEvent.colliderA.destroy();
// // }
// // if (collisionEvent.colliderB.getUserData("bomb") != null) {
// // collisionEvent.colliderB.destroy();
// // }
// //});
// //gameEngine.setGravity(0, 1.5);
// groundHeight = height-30;
// groundSize = 64;
// //Load the terrain sprites and game objects
// for (var y=-64; y<height+64; y+=64) {
// gameEngine.addSprite(new Sprite(sideRightImage, 0, y, groundSize, groundSize, gameEngine.BOX));
// gameEngine.addSprite(new Sprite(sideLeftImage, width, y, groundSize, groundSize, gameEngine.BOX));
// }
// for (var x=-64; x<width+64; x+=64) {
// gameEngine.addSprite(new Sprite(groundCenterImage, x, groundHeight, groundSize, groundSize, gameEngine.BOX));
// }
// //Add crates
// gameEngine.addSprite(new Sprite(crateImage, width/2 - 200, height-128, groundSize, groundSize, gameEngine.BOX, {isStatic: false, restitution: 0, friction: 0}))
// spider = new Sprite(spider1Image, width/2, height/2, 60, 60)
// spider.addAnimation("walk", [spider1Image, spider2Image, spider3Image, spider4Image, spider5Image, spider6Image, spider7Image]);
// spider.playAnimation("walk", 5);
// gameEngine.addSprite(spider);
// //gameEngine.addSprite(new Sprite(groundRightImage, width/2 - 200, height-groundHeight, 100, 100, gameEngine.BOX));
// //gameEngine.addSprite(new Sprite(groundCenterImage, width/2 - 300, height-groundHeight, 100, 100, gameEngine.BOX));
// //gameEngine.addSprite(new Sprite(groundCenterImage, width/2 - 400, height-groundHeight, 100, 100, gameEngine.BOX));
// //gameEngine.addSprite(new Sprite(groundLeftImage, width/2 + 200, height-groundHeight, 100, 100, gameEngine.BOX));
// //gameEngine.addSprite(new Sprite(groundCenterImage, width/2 + 300, height-groundHeight, 100, 100, gameEngine.BOX));
// //gameEngine.addSprite(new Sprite(groundCenterImage, width/2 + 400, height-groundHeight, 100, 100, gameEngine.BOX));
// //Load the platform
// movePlatformSprite = new Sprite(platformStoneImage, width/2, height/2+50, 100, 25, gameEngine.BOX);
// gameEngine.addSprite(movePlatformSprite);
// //Load the blade sprites
// bladeSprite = new Sprite(bladeImage, 150, height/2, 80, 80, gameEngine.CIRCLE);
// gameEngine.addSprite(bladeSprite);
// //gameEngine.addSprite(new BoxSprite(crateImage, 200, 400, 0, 70, 70, {isStatic: false, restitution: 0.1, friction: 1}));
// player = new Sprite(playerImage, width/2-300, 340, 50, 50, gameEngine.BOX, {isStatic: false, restitution: 0.2, friction: 0.1});
// player.setUserData("player", true);
// //player.setRenderer(function(obj) {
// // console.log("render");
// // fill(23);
// // rect(0, 0, 30, 30);
// //});
// gameEngine.addSprite(player);
// player.setController(callBackPlayer);
// player.setCollisionCallback(function(current, collider) {
// console.log("collision: " + current.getUserData("player"));
// // collider.destroy();
// // current.clearForces();
// // current.applyForce(0, -200);
// // player.setScale(1, 1);
// });
// playerTail = new ParticleSystem(createVector(100, 100), smokeImage);
// playerTail.setKillSpeed(5);
// playerTail.setStartSize(50);
// playerTail.setEndSize(0);
// playerTail.setEmitterWidth(0);
// playerTail.setEmitterHeight(0);
// playerTail.setStartColor(color(255, 255, 0));
// playerTail.setEndColor(color(255, 255, 222, 0));
// playerTail.setInitialVelocity(createVector(0, 0));
// playerTail.setVelocityVariance(0.6);
// playerTail.setGravity(createVector(0, 0));
// playerTail.setParticlesPerSecond(1);
// playerTail.setEmitRate(3);
// gameEngine.addParticleSystem(playerTail);
// player.attachChild(playerTail);
// platformSprite = new Sprite(platformStoneImage, width-100, 100, 100, 25, gameEngine.BOX);
// gameEngine.addSprite(platformSprite);
// platformSprite = new Sprite(platformStoneImage, width-300, 200, 100, 25, gameEngine.BOX);
// gameEngine.addSprite(platformSprite);
// //Waterfall
// waterfallParticleSystem = new ParticleSystem(createVector(width-100, 100));
// waterfallParticleSystem.setKillSpeed(3.2);
// waterfallParticleSystem.setStartSize(20);
// waterfallParticleSystem.setEndSize(50);
// waterfallParticleSystem.setEmitterWidth(60);
// waterfallParticleSystem.setEmitterHeight(2);
// waterfallParticleSystem.setStartColor(color(255, 255, 255, 100));
// waterfallParticleSystem.setEndColor(color(42, 107, 162, 20));
// waterfallParticleSystem.setInitialVelocity(createVector(0, 0));
// waterfallParticleSystem.setVelocityVariance(0.1);
// waterfallParticleSystem.setGravity(createVector(0, 0.1));
// waterfallParticleSystem.setParticlesPerSecond(5);
// waterfallParticleSystem.setEmitRate(1);
// gameEngine.addParticleSystem(waterfallParticleSystem);
// gameEngine.debugEnabled = true;
// gameEngine.start();
// }
// function callBackPlayer(spriteObj) {
// // console.log("callback: " + spriteObj.getPosition());
// }
// function addBomb() {
// var xPos = random(64, width-64);
// bomb = new Sprite(null, xPos, 0, 20, 20, gameEngine.CIRCLE, {isStatic: false, restitution: 0.2, friction: 0.01});
// bomb.setUserData("bomb", true);
// gameEngine.addSprite(bomb);
// // bomb.setMass(random(1, 10));
// if (xPos< width/2) {
// bomb.setVelocity(random(0, 10), 0);
// } else {
// bomb.setVelocity(random(-10, 0), 0);
// }
// bombTail = new ParticleSystem(createVector(100, 100), smokeImage);
// bombTail.setKillSpeed(15);
// bombTail.setStartSize(50);
// bombTail.setEndSize(3);
// bombTail.setEmitterWidth(0);
// bombTail.setEmitterHeight(0);
// bombTail.setStartColor(color(255, 128, 0));
// bombTail.setEndColor(color(255, 255, 0));
// bombTail.setInitialVelocity(createVector(0, 0));
// bombTail.setVelocityVariance(0.3);
// bombTail.setGravity(createVector(0, 0));
// bombTail.setParticlesPerSecond(1);
// bombTail.setEmitRate(2);
// gameEngine.addParticleSystem(bombTail);
// bomb.attachChild(bombTail);
// }
// function mouseReleased() {
// // pickedSprite = null;
// }
// function mouseDragged() {
// if (pickedSprite != null && mouseButton == RIGHT) {
// if (pickedSprite.clearForces) {
// pickedSprite.clearForces();
// }
// pickedSprite.setPosition(mouseX, mouseY);
// }
// }
// function mousePressed() {
// if (mouseButton == CENTER) {
// player.addScale(1, 1);
// }
// if (mouseButton == RIGHT) {
// if (gameEngine.collisionsAtMouse().length > 0) {
// var picked = gameEngine.collisionsAtMouse()[gameEngine.collisionsAtMouse().length-1];
// if (picked != player) {
// pickedSprite = picked;
// }
// }
// }
// if (mouseButton == LEFT) {
// var size = random(30, 60);
// var ball = new Sprite(ballImage, mouseX, mouseY, size, size, gameEngine.CIRCLE, {isStatic: false, restitution: 0.5, friction: 0.5});
// gameEngine.addSprite(ball);
// pickedSprite.link(ball, dist(mouseX, mouseY, pickedSprite.getPosition().x, pickedSprite.getPosition().y), 0.2);
// }
// }
// function draw() {
// background(200,234,255);
// //Update the gameEngine
// gameEngine.update();
// // console.log("width: " + player.getWidth());
// //Check when to add bombs
// if (frameCount % bombInterval == 0) {
// addBomb();
// bombInterval = floor(random(200, 500));
// // player.setSleeping(true);
// }
// //Rotate the fan sprites
// bladeSprite.rotate(-8);
// //Move the platform
// movePlatformSprite.move(platformMoveSpeed, 0);
// if (dist(movePlatformSprite.getInitialPosition().x, movePlatformSprite.getInitialPosition().y,
// movePlatformSprite.getPosition().x, movePlatformSprite.getPosition().y) >= platformMoveDistance) {
// platformMoveSpeed = -platformMoveSpeed;
// }
// player.setAngle(0);
// gameEngine.setCameraPosition(player.getPosition().x, lerp(gameEngine.getCameraPosition().y, player.getPosition().y-20, 0.06));
// if (keyIsDown(LEFT_ARROW)) {
// // player.clearForces();
// player.move(-moveSpeed, 0);
// // player.rotate(-moveSpeed*2);
// }
// if (keyIsDown(RIGHT_ARROW)) {
// // player.clearForces();
// player.move(moveSpeed, 0);
// // player.rotate(moveSpeed*2);
// }
// //Draw the selected Sprite
// if (pickedSprite != null) {
// //Sets the camera position
// //gameEngine.setCameraPosition(pickedSprite.getPosition().x, pickedSprite.getPosition().y-100);
// push();
// translate(pickedSprite.getPosition().x, pickedSprite.getPosition().y);
// noFill();
// strokeWeight(2);
// stroke(200, 0, 0);
// ellipse(0, 0, 50);
// pop();
// }
// spider.rotate(1);
// // console.log("Spider angle: " + spider.getRotation());
// }
// function keyPressed() {
// if (keyCode == UP_ARROW) {
// if (player != null && onGround) {
// player.applyForce(0, -0.1);
// onGround = false
// }
// }
// if (keyCode == DELETE) {
// if (pickedSprite != null) {
// pickedSprite.destroy();
// pickedSprite = null;
// }
// }
// }