Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
245 lines (183 loc) · 7.25 KB
/
Copy pathscript.js
File metadata and controls
245 lines (183 loc) · 7.25 KB
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
"use strict";
const VP_WIDTH = 920, VP_HEIGHT = 690; //declare variables to hold the viewport size
const MAX_CRATES = 18; //declare a variable to hold the max number of crates
const MAX_SPECIALS = 2;
const CRATE_WIDTH = 30, CRATE_HEIGHT = 35;
const FUZZBALL_X = 150, FUZZBALL_Y = 590; //declare a starting point for the fuzzball
const FIZZBALL_D = 30; //declare a diameter for the fuzzball
//declare global variables to hold the framework objects
var viewport, world, engine, body, elastic_constraint;
var playerScore = 0;
// define our categories (as bit fields, there are up to 32 available) - we will use them to allow/non allow mouse interaction
// https://brm.io/matter-js/docs/classes/MouseConstraint.html#properties
var notinteractable = 0x0001, interactable = 0x0002;
var crates = []; //create an empty array that will be used to hold all the crates instances
var ground;
var leftwall;
var rightwall;
var specials = [];
var fuzzball;
var launcher;
function apply_velocity() {
Matter.Body.setVelocity( fuzzball.body, {x: get_random(0, 20), y: get_random(0, 20)*-1});
};
function apply_angularvelocity() {
for(let i = 0; i < crates.length; i++) {
Matter.Body.setAngularVelocity( crates[i].body, Math.PI/get_random(3, 20));
}
};
function apply_force() { //apply the same force to all crates
for(let i = 0; i < crates.length; i++) {
Matter.Body.applyForce( crates[i].body, {
x: crates[i].body.position.x,
y: crates[i].body.position.y
}, {
x: 0.05,
y: get_random(50, 200)*-1
});
}
};
function get_random(min, max) { //return a 'fake' random number base on the specified range
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function preload() {
//p5 defined function
}
function score(points) {
let effectspeed = 60;
let animatespeed = 500;
$("#scoreboard").finish();
document.getElementById('points').innerHTML = "+" + points;
$('#scoreboard').removeAttr('style'); //remove any applied styles
$("#scoreboard").fadeIn(effectspeed, function() {
$("#scoreboard").animate({
top: '+=50px',
opacity: 0
}, animatespeed);
});
playerScore += points;
document.getElementById('status').innerHTML = "Score: " + playerScore;
}
function setup() {
//this p5 defined function runs automatically once the preload function is done
viewport = createCanvas(VP_WIDTH, VP_HEIGHT); //set the viewport (canvas) size
viewport.parent("viewport_container"); //attach the created canvas to the target div
//enable the matter engine
engine = Matter.Engine.create();
world = engine.world;
body = Matter.Body;
//enable the 'matter' mouse controller and attach it to the viewport object using P5s elt property
let vp_mouse = Matter.Mouse.create(viewport.elt); //the 'elt' is essentially a pointer the the underlying HTML element
vp_mouse.pixelRatio = pixelDensity(); //update the pixel ratio with the p5 density value; this supports retina screens, etc
let options = {
mouse: vp_mouse,
collisionFilter: {
mask: interactable //specify the collision catagory (multiples can be OR'd using '|' )
}
}
elastic_constraint = Matter.MouseConstraint.create(engine, options); //see docs on https://brm.io/matter-js/docs/classes/Constraint.html#properties
Matter.World.add(world, elastic_constraint); //add the elastic constraint object to the world
level1();
//attach some useful events to the matter engine; https://brm.io/matter-js/docs/classes/Engine.html#events
Matter.Events.on(engine, 'collisionEnd', collisions);
frameRate(60);
world.gravity.y = 1.0;
}
function level1(replay = false) {
if(replay == true) { //if this is a 'reply' we need to remove all the objects before recrating them
ground.remove();
leftwall.remove();
rightwall.remove();
fuzzball.remove();
launcher.remove();
for(let i = 0; i < MAX_SPECIALS; i++) {
specials[i].remove();
}
for(let i = 0; i < MAX_CRATES; i++) {
crates[i].remove();
}
}
ground = new c_ground(VP_WIDTH/2, VP_HEIGHT+20, VP_WIDTH, 40, "ground"); //create a ground object using the ground class
leftwall = new c_ground(0, VP_HEIGHT/2, 20, VP_HEIGHT, "leftwall"); //create a left wall object using the ground class
rightwall = new c_ground(VP_WIDTH, VP_HEIGHT/2, 20, VP_HEIGHT, "rightwall"); //create a right wall object using the ground class
fuzzball = new c_fuzzball(FUZZBALL_X, FUZZBALL_Y, FIZZBALL_D, "fuzzball"); //create a fuzzball object
for(let i = 0; i < MAX_SPECIALS; i++) {
specials[i] = new c_special(get_random(300, 640), get_random(VP_HEIGHT-600, VP_HEIGHT-120), 70, 20, "special");
}
//loop through each of the crates indexes
for(let i = 0; i < MAX_CRATES; i++) { //loop for each instance of a crates
let top = (-CRATE_HEIGHT * MAX_CRATES ) - 100;
let offset = (i * CRATE_HEIGHT) * 3;
crates[i] = new c_crate(700, (top + offset), CRATE_WIDTH, CRATE_HEIGHT, "crate");
}
//create a launcher object using the fuzzball body
launcher = new c_launcher(FUZZBALL_X, FUZZBALL_Y-100, fuzzball.body);
}
function collisions(event) {
//runs as part of the matter engine after the engine update, provides access to a list of all pairs that have ended collision in the current frame (if any)
event.pairs.forEach((collide) => { //event.pairs[0].bodyA.label
console.log(collide.bodyA.label + " - " + collide.bodyB.label);
if(
(collide.bodyA.label == "fuzzball" && collide.bodyB.label == "crate") ||
(collide.bodyA.label == "crate" && collide.bodyB.label == "fuzzball")
) {
console.log("interesting collision");
score(100);
}
});
}
function paint_background() {
//access the game object for the world, use this as a background image for the game
background('#4c738b');
ground.show(); //execute the show function for the boundary objects
leftwall.show();
rightwall.show();
}
function paint_assets() {
for(let i = 0; i < crates.length; i++) { //loop through the crates array and show each
crates[i].show()
}
for(let i = 0; i < MAX_SPECIALS; i++) {
specials[i].show(); //show the specials
}
fuzzball.show(); //show the fuzzball
launcher.show(); //show the launcher indicator
}
function draw() {
//this p5 defined function runs every refresh cycle
//special.rotate();
paint_background(); //paint the default background
Matter.Engine.update(engine); //run the matter engine update
paint_assets(); //paint the assets
if(elastic_constraint.body !== null) {
let pos = elastic_constraint.body.position; //create an shortcut alias to the position (makes a short statement)
fill("#ff0000"); //set a fill colour
ellipse(pos.x, pos.y, 20, 20); //indicate the body that has been selected
let mouse = elastic_constraint.mouse.position;
stroke("#00ff00");
line(pos.x, pos.y, mouse.x, mouse.y);
}
//https://brm.io/matter-js/docs/classes/SAT.html#methods
//if(Matter.SAT.collides(fuzzball.body, ground.body).collided == true) {
// console.log("fuzzball to ground");
//}
}
function keyPressed() {
if (keyCode === ENTER) {
console.log("enter key press");
fuzzball.remove();
fuzzball = new c_fuzzball(FUZZBALL_X, FUZZBALL_Y, FIZZBALL_D, "fuzzball");
launcher.attach(fuzzball.body);
}
if(keyCode === 32) {
console.log("space key press");
launcher.release(); //execute the release method
}
}
function mouseReleased() {
setTimeout(() => {
launcher.release();
}, 60);
}