forked from artandtech-miniconf/lca2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadergame.js
369 lines (331 loc) · 9.74 KB
/
headergame.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
(function () {
var requestAnimationFrame = window.requestAnimationFrame
|| window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var canvas = document.getElementById("canvas"),
width = document.documentElement.clientWidth,
height = document.documentElement.clientHeight / 2,
ctx = canvas.getContext("2d"),
player = {
x: width / 2,
y: height - 15,
width: 50,
height: 50,
speed: 3,
velX: 0,
velY: 0,
jumping: false,
grounded: false
},
keys = [],
friction = 0.8,
gravity = 0.1;
var boxes = [];
var triangles = [];
// dimensions
//left side
boxes.push({
x: 0,
y: 0,
width: 5,
height: height
});
//bottom
boxes.push({
x: 0,
y: height - 2,
width: width,
height: 50
});
//right side
boxes.push({
x: width,
y: 0,
width: 5,
height: height
});
function generateTriangles() {
triangles = destroyTriangles(triangles);
var triangleWidthHeight = 50;
var numberoftriangles = Math.floor(width / (triangleWidthHeight+100));
var trianglerows = Math.floor(height / (triangleWidthHeight+100));
var total = Math.floor(numberoftriangles);
var startx = 50;
var starty = 50;
var fill = 100;
for (var p = 0; p < trianglerows; p++){
for (var k = 0; k < numberoftriangles; k++) {
triangles.push({
xA: startx + (k*(fill+triangleWidthHeight)), //same as xC
yA: starty + (p*(fill+triangleWidthHeight)), //same as yB
xB: startx + ((k+1)*triangleWidthHeight)+(k*fill), //same as yC
yB: starty + (p*(fill+triangleWidthHeight)), //same as yA
xC: startx + (k*(fill+triangleWidthHeight)), //same as xA
yC: starty + ((p+1)*triangleWidthHeight)+(p*fill) //same as xB
});
}
}
}
function destroyTriangles(trianglesarray) {
if (triangles.length > 0) {
trianglesarray = [];
}
return trianglesarray;
}
canvas.width = width;
canvas.height = height
function update() {
if (triangles.length <= 0){
generateTriangles();
}
if(canvas.width !== innerWidth){
canvas.width = innerWidth;
//var width = innerWidth; // this line does something funky and I need to find out why. I think I know why
width = innerWidth;
player.x = width/2;
player.y = height - 15;
boxes[1] = ({
x: 0,
y: height - 2,
width: canvas.width,
height: 50
});
boxes[2] = ({
x: canvas.width - 1,
y: 0,
width: 5,
height: height
});
generateTriangles();
}
// check keys
if (keys[38] || keys[32]) {
// up arrow or space
if (!player.jumping && player.grounded) {
player.jumping = true;
player.grounded = false;
player.velY = -player.speed * 2;
}
}
if (keys[39]) {
// right arrow
if (player.velX < player.speed) {
player.velX++;
}
}
if (keys[37]) {
// left arrow
if (player.velX > -player.speed) {
player.velX--;
}
}
player.velX *= friction;
player.velY += gravity;
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "#0F7C11";
ctx.beginPath();
var trianglesList = [];
player.grounded = false;
for (var i = 0; i < boxes.length; i++) {
//ctx.rect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);
for (var j = 0; j < triangles.length; j++) {
ctx.beginPath();
ctx.moveTo(triangles[j].xA,triangles[j].yA);
ctx.lineTo(triangles[j].xB,triangles[j].yB);
ctx.lineTo(triangles[j].xC,triangles[j].yC);
ctx.lineTo(triangles[j].xA,triangles[j].yA);
ctx.strokeStyle = "#0F7C11"
ctx.stroke();
}
var dir = colCheck(player, boxes[i]);
if (dir === "l" || dir === "r") {
player.velX = 0;
player.jumping = false;
} else if (dir === "b") {
player.grounded = true;
player.jumping = false;
} else if (dir === "t") {
player.velY *= -1;
}
}
var collisionTriangles = determineLikelyCollisionTriangles(player, triangles, 100);
var collided = colTCheck(player, collisionTriangles);
if(collided){
player.velY = -player.velY;
player.velX = -player.velX;
}
if(player.grounded){
player.velY = 0;
}
player.x += player.velX;
player.y += player.velY;
/*ctx.fill();*/
ctx.fillStyle = "#ccd5c5";
ctx.fillRect(player.x, player.y, player.width, player.height);
//window.addEventListener('resize',resizeCanvas, false);
requestAnimationFrame(update);
}
function colCheck(shapeA, shapeB) {
// get the vectors to check against
var vX = (shapeA.x + (shapeA.width / 2)) - (shapeB.x + (shapeB.width / 2)),
vY = (shapeA.y + (shapeA.height / 2)) - (shapeB.y + (shapeB.height / 2)),
// add the half widths and half heights of the objects
hWidths = (shapeA.width / 2) + (shapeB.width / 2),
hHeights = (shapeA.height / 2) + (shapeB.height / 2),
colDir = null;
// if the x and y vector are less than the half width or half height,
//they we must be inside the object, causing a collision
if (Math.abs(vX) < hWidths && Math.abs(vY) < hHeights) {
// figures out on which side we are colliding (top, bottom, left, or right)
var oX = hWidths - Math.abs(vX),
oY = hHeights - Math.abs(vY);
if (oX >= oY) {
if (vY > 0) {
colDir = "t";
shapeA.y += oY;
} else {
colDir = "b";
shapeA.y -= oY;
}
} else {
if (vX > 0) {
colDir = "l";
shapeA.x += oX;
} else {
colDir = "r";
shapeA.x -= oX;
}
}
}
return colDir;
}
//
/* TRIANGLE COLLISION CODE */
//
function createPoint(x,y){
return {x: x,
y: y
};
}
//turns point into 2D vector
function createVector2D(start, finish){
return {dx: finish.x - start.x,
dy: finish.y - start.y,
};
}
//norm also means length of 2Dvector
function Norm2D(v){
return Math.sqrt(Math.pow(v.dx, 2)+Math.pow(v.dy, 2));
}
//turns 2D vector into 3D vector
function expand2DVector(v){
return {dx: v.dx, dy: v.dy, dz: 0};
}
//possibly add check for 3D and if not turn into 3D
function dotProduct3D(v1, v2){
return v1.dx*v2.dx+v1.dy*v2.dy+v1.dz*v2.dz;
}
//possibly add check for 3D vector and if not turn into 3D vector
function crossProduct(v1, v2){
cx = (v1.dy*v2.dz) - (v1.dz*v2.dy);
cy = (v1.dz*v2.dx) - (v1.dx*v2.dz);
cz = (v1.dx*v2.dy) - (v1.dy*v2.dx);
return {dx: cx, dy: cy, dz: cz};
}
function turnTriangleintoList(triangle){
var a = createPoint(triangle.xA, triangle.yA);
var b = createPoint(triangle.xB, triangle.yB);
var c = createPoint(triangle.xC, triangle.yC);
return [a,b,c];
}
function calculatePlayerCenter(player){
return createPoint(player.x + player.width/2, player.y - player.height/2);
}
function approximateTriangleCenter(triangle){
// this function needs a list of triangle points. see turnTriangleintoList()
//potentially change to get true center which is start point (x+1/3width),(y-1/3width)
var x = 0;
var y = 0;
for(var i=0; i<3; i++){
x += triangle[i].x;
y += triangle[i].y;
}
x = x / 3;
y = y / 3;
return createPoint(x, y);
}
function turnPlayerIntoList(player){
var topleft = createPoint(player.x, player.y);
var topright = createPoint((player.x + player.width), player.y);
var bottomright = createPoint((player.x + player.width),
(player.y + player.height));
var bottomleft = createPoint(player.x, (player.y + player.height));
return [topleft, topright, bottomright, bottomleft];
}
function collidePointwithPolygon(Polygon, point){
// the polygon has to be a list of the points that are the
// convex hull of the Polygon running clockwise around the
// polygon
var collision = true;
for(var i=0; i < Polygon.length; i++) {
// maybe check if 2D or 3D first
var PolyV = expand2DVector(createVector2D(Polygon[i], Polygon[(i+1)%Polygon.length]));
var VtoPoint = expand2DVector(createVector2D(Polygon[i], point));
var VofXProduct = crossProduct(PolyV, VtoPoint);
if(VofXProduct.dz < 0){
collision = false;
break;
}
}
return collision;
}
function collidetwoPolygons(Poly1, Poly2){
//check if any point of Poly2 collides with Poly1
var collision = false;
for(var i=0; i<Poly2.length; i++){
collision = collidePointwithPolygon(Poly1, Poly2[i]);
if(collision == true){
return collision;
}
}
// check if any point of Poly1 collides with Poly2
for(var i=0; i<Poly1.length; i++){
collision = collidePointwithPolygon(Poly2, Poly1[i]);
if(collision == true){
return collision;
}
}
return collision;
}
function colTCheck(player, triangles){
var playerList = turnPlayerIntoList(player);
for(var i=0; i<triangles.length; i++){
var currTList = turnTriangleintoList(triangles[i]);
var collision = collidetwoPolygons(playerList, currTList);
}
return collision;
}
function determineLikelyCollisionTriangles(player, triangles, maxdist){
var likelytriangles = [];
var pcenter = calculatePlayerCenter(player);
for(var i=0; i<triangles.length; i++){
var t = turnTriangleintoList(triangles[i]);
var tcenter = approximateTriangleCenter(t);
var dist = Norm2D(createVector2D(pcenter, tcenter));
if(dist < maxdist){
likelytriangles.push(triangles[i]);
}
}
return likelytriangles;
}
document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
window.addEventListener("load", function () {
update();
});