-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.js
85 lines (61 loc) · 2.27 KB
/
box.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
function checkForCollision(enemie, mario, opt_canvasCtx) {
if(enemie.dead){
return false;
}
var enemieBoxXPos = Game.defaultDimensions.WIDTH + enemie.xPos;
var marioBox = new CollisionBox( mario.xPos + 8, mario.yPos, mario.config.WIDTH - 25, mario.config.HEIGHT - 10);
var enemieBox = new CollisionBox(enemie.xPos + 10, enemie.getYPos() ,enemie.getWidth() - 20, enemie.getHeight() - 5);
// Debug outer box
if (opt_canvasCtx) {
drawCollisionBoxes(opt_canvasCtx, marioBox, enemieBox);
}
// Simple outer bounds check.
if (boxCompare(marioBox, enemieBox)) {
return true;
}
return false;
};
function createAdjustedCollisionBox(box, adjustment) {
return new CollisionBox( box.x + adjustment.x, box.y + adjustment.y, box.width, box.height);
};
function drawCollisionBoxes(canvasCtx, marioBox, enemieBox) {
canvasCtx.save();
canvasCtx.strokeStyle = '#f00';
canvasCtx.strokeRect(marioBox.x, marioBox.y, marioBox.width, marioBox.height);
canvasCtx.strokeStyle = '#0f0';
canvasCtx.strokeRect(enemieBox.x, enemieBox.y, enemieBox.width, enemieBox.height);
canvasCtx.restore();
};
function boxCompare(marioBox, enemieBox) {
var crashed = false;
var marioBoxX = marioBox.x;
var marioBoxY = marioBox.y;
var enemieBoxX = enemieBox.x;
var enemieBoxY = enemieBox.y;
// Axis-Aligned Bounding Box method.
if (marioBox.x < enemieBoxX + enemieBox.width &&
marioBox.x + marioBox.width > enemieBoxX &&
marioBox.y < enemieBox.y + enemieBox.height &&
marioBox.height + marioBox.y > enemieBox.y) {
crashed = true;
}
return crashed;
};
function isJumpingOn(enemie, mario){
var marioBox = new CollisionBox( mario.xPos + 8, mario.yPos, mario.config.WIDTH - 25, mario.config.HEIGHT - 10);
var enemieBox = new CollisionBox(enemie.xPos + 10, enemie.getYPos() ,enemie.getWidth() - 20, enemie.getHeight() - 5);
var jumpOn = false;
// Axis-Aligned Bounding Box method.
if (marioBox.y + marioBox.height > enemieBox.y && marioBox.y + marioBox.height < enemieBox.y + enemieBox.height &&
(marioBox.x >= enemieBox.x || marioBox.x + marioBox.width <= enemieBox.x + enemieBox.width ) &&
mario.jumpVelocity > 0) {
jumpOn = true;
}
return jumpOn;
}
function CollisionBox(x, y, w, h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
};