-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
324 lines (224 loc) · 6.51 KB
/
Game.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
// test implementation of the game objects
var Distance = {
// square a value
sqr: function(x) {
return x * x;
},
// squared distance between 2 points
dist2: function(vec1, vec2) {
return this.sqr(vec1.x-vec2.x) + this.sqr(vec1.y-vec2.y);
},
dist: function(vec1, vec2) {
return Math.sqrt(this.dist2(vec1, vec2));
},
distToLineSegment2: function(point, line1, line2) {
// the squared length of the line
var len2 = this.dist2(line1, line2);
// line's endpoints are the same, i.e. 0 length
if(len2==0) return this.dist2(point, line1);
var dotProduct = (point.x - line1.x) * (line2.x - line1.x) +
(point.y - line1.y) * (line2.y - line1.y);
var t = dotProduct / len2;
// point beyond line1 point
if(t < 0) return this.dist2(point, line1);
// point beyond line2 point
if(t > 1) return this.dist2(point, line2);
return this.dist2(point, {
x: line1.x + t*(line2.x - line1.x),
y: line1.y + t*(line2.y - line1.y)
});
},
distToLineSegment: function(point, line1, line2) {
return Math.sqrt( this.distToLineSegment2(point, line1, line2) );
}
};
var Render = {
circle: function(ctx, centerX, centerY, radius, startAngle, endAngle, antiClockwise) {
startAngle = startAngle || 0;
endAngle = endAngle || 2*Math.PI;
antiClockwise = antiClockwise || true;
ctx.beginPath();
ctx.arc(centerX, centerY, radius, startAngle, endAngle, antiClockwise);
ctx.closePath();
ctx.fill();
},
rectangle: function() {
},
image: function() {
},
player: function(ctx, player) {
this.circle(ctx, player.x, player.y, player.hitRadius);
},
goal: function(ctx, goal) {
this.circle(ctx, goal.x, goal.y, goal.hitRadius);
},
allGoals: function(ctx, goals) {
for(var i=0; i<goals.length; i++){
this.goal(ctx, goals[i]);
}
},
urchin: function(ctx, urchin) {
var origin = urchin.vector();
ctx.strokeStyle = 'black';
ctx.lineWidth = 0.5;
// draw a circle
this.circle(ctx, origin.x, origin.y, urchin.hitRadius, 0, urchin.angle, true);
for(var i=0; i<urchin.arms.length; i++){
var arm = urchin.arms[i];
var armEnd = arm.end();
// draw a long triangle
ctx.beginPath();
ctx.moveTo(origin.x, origin.y);
ctx.moveTo(origin.x+1, origin.y+1);
ctx.lineTo(armEnd.x, armEnd.y);
ctx.lineTo(origin.x-1, origin.y-1);
ctx.closePath();
ctx.fill();
// fill the stroke
ctx.stroke();
}
},
allUrchins: function(ctx, urchins) {
for(var i=0; i<urchins.length; i++) {
this.urchin(ctx, urchins[i]);
}
}
};
// methods for handling a collision with the player
var CollisionHandler = {
urchinProximityTouched: function(player, urch) {
if(urch.activeRadius > 0){
urch.explode();
}
},
urchinBodyTouched: function(player, urch) {
},
urchinArmTouched: function(player, arm) {
}
};
function collision() {
possibleCollisions = [];
// check enemies (urchins)
for(var i=0; i<enemies.length; i++){
var enemy = enemies[i];
var distToBody = Distance.dist(player.vector(), enemy.vector());
if(distToBody < enemy.activeRadius){
CollisionHandler.urchinProximityTouched(player, enemy);
}
// check enemies' arms
for(var j=0; j<enemies[i].arms.length; j++){
var arm = enemies[i].arms[j];
var endPoints = arm.end();
var distToArm = Distance.distToLineSegment( player.vector(), arm.origin(), arm.end() );
if( distToArm < player.hitRadius ){
alert("collision!");
return true;
}
// if player is outside outer bounding box of the arm, do not run collision detection
// calculations (inefficient)
if(arm.x < (player.x-player.hitRadius) && endPoints.x < (player.x-player.hitRadius) ||
arm.x > (player.x+player.hitRadius) && endPoints.x > (player.x+player.hitRadius) ||
arm.y < (player.y-player.hitRadius) && endPoints.y < (player.y-player.hitRadius) ||
arm.y > (player.y+player.hitRadius) && endPoints.y > (player.y+player.hitRadius) ) {
continue;
} else {
possibleCollisions.push(enemies[i].arms[j]);
// run collision detection against the possible collision objects
for(var k=0; k<possibleCollisions.length; k++){
var distToPlayer = Distance.distToLineSegment(
player.vector(), possibleCollisions[k].origin(), possibleCollisions[k].end() );
if( distToPlayer < player.hitRadius ){
alert("collision!");
return true;
}
}
}
}
}
// check items
for(var i=0; i<items.length; i++){
}
// check goals
for(var i=0; i<goals.length; i++){
var goal = goals[i];
if(Distance.dist(player.vector(), goal.vector()) < (player.hitRadius+goal.hitRadius) ){
alert("win");
return true;
}
}
// visual output
collisionCounter.innerHTML = possibleCollisions.length;
return false;
}
function step() {
// wipe the canvas
ctx.clearRect(0,0, canvas.width, canvas.height);
// enemies step
for(var i=0; i<enemies.length; i++){
enemies[i].step();
}
// render the player, goals, and enemies
Render.player(ctx, player);
Render.allGoals(ctx, goals);
Render.allUrchins(ctx, enemies);
}
function main() {
var now = Date.now();
var delta = now - then;
var modifier = delta/1000;
step();
if( collision() ){
clearInterval(loop);
};
var shiftVect = {x: 0, y: 0};
if(motionDevice){
var motionCorrection = 10;
var speedCorrection = 20;
if(player.speed/speedCorrection < 1){
speedCorrection = 1;
}
// cap gamma and beta at 10
if(gamma<-10) gamma=-10;
else if(gamma>10) gamma=10;
if(beta<-10) beta=-10;
else if(beta>10) beta=10;
if(gamma >= 0) {
shiftVect.x = (player.speed/speedCorrection) * (-1)*(gamma/motionCorrection);
}
if(gamma < 0) {
shiftVect.x = (player.speed/speedCorrection) * (-1)*(gamma/motionCorrection);
}
if(beta >= 0) {
shiftVect.y = (player.speed/speedCorrection) * (-1)*(beta/motionCorrection);
}
if(beta < 0) {
shiftVect.y = (player.speed/speedCorrection) * (-1)*(beta/motionCorrection);
}
}
if(38 in keysDown) { //Up key
shiftVect.y = player.speed * modifier;
}
if(40 in keysDown) { //down key
shiftVect.y = (-1)*player.speed * modifier;
}
if(37 in keysDown) { //left key
shiftVect.x = player.speed * modifier;
}
if(39 in keysDown) { //Up key
shiftVect.x = (-1)*player.speed * modifier;
}
// shift all other objects excluding player
for(var m=1; m<allObjects.length; m++){
allObjects[m].shift(shiftVect.x, shiftVect.y);
}
levelLogicLoop();
then = now;
}
addEventListener("keydown", function(e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function(e) {
delete keysDown[e.keyCode];
}, false);
var then = Date.now();
var loop = setInterval(main, 1);