Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Last active August 10, 2019 12:59
Show Gist options
  • Save iliakonnov/744908c4ce35cec0542c48b096654740 to your computer and use it in GitHub Desktop.
Save iliakonnov/744908c4ce35cec0542c48b096654740 to your computer and use it in GitHub Desktop.
/**
* Collision box object.
* @param {number} x X position.
* @param {number} y Y Position.
* @param {number} w Width.
* @param {number} h Height.
*/
function CollisionBox(x, y, w, h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
};
/**
* Adjust the collision box.
* @param {!CollisionBox} box The original box.
* @param {!CollisionBox} adjustment Adjustment box.
* @return {CollisionBox} The adjusted collision box object.
*/
function createAdjustedCollisionBox(box, adjustment) {
return new CollisionBox(
box.x + adjustment.x,
box.y + adjustment.y,
box.width,
box.height);
};
/**
* Compare two collision boxes for a collision.
* @param {CollisionBox} tRexBox
* @param {CollisionBox} obstacleBox
* @return {boolean} Whether the boxes intersected.
*/
function boxCompare(tRexBox, obstacleBox) {
var crashed = false;
var tRexBoxX = tRexBox.x;
var tRexBoxY = tRexBox.y;
var obstacleBoxX = obstacleBox.x;
var obstacleBoxY = obstacleBox.y;
// Axis-Aligned Bounding Box method.
if (tRexBox.x < obstacleBoxX + obstacleBox.width &&
tRexBox.x + tRexBox.width > obstacleBoxX &&
tRexBox.y < obstacleBox.y + obstacleBox.height &&
tRexBox.height + tRexBox.y > obstacleBox.y) {
crashed = true;
}
return crashed;
};
/**
* Draw the collision boxes for debug.
*/
function drawCollisionBoxes(canvasCtx, tRexBox, obstacleBox) {
canvasCtx.save();
canvasCtx.strokeStyle = '#f00';
canvasCtx.strokeRect(tRexBox.x, tRexBox.y, tRexBox.width, tRexBox.height);
canvasCtx.strokeStyle = '#0f0';
canvasCtx.strokeRect(obstacleBox.x, obstacleBox.y,
obstacleBox.width, obstacleBox.height);
canvasCtx.restore();
};
Trex_collisionBoxes = {
DUCKING: [
new CollisionBox(1, 18, 55, 25)
],
RUNNING: [
new CollisionBox(22, 0, 17, 16),
new CollisionBox(1, 18, 30, 9),
new CollisionBox(10, 35, 14, 8),
new CollisionBox(1, 24, 29, 5),
new CollisionBox(5, 30, 21, 4),
new CollisionBox(9, 34, 15, 4)
]
};
///////////////////////////////////////////////
function check(distanceX, distanceY, width, height, opt_canvasCtx) {
var obstacle = Runner.instance_.horizon.obstacles[0];
if (!obstacle) {
return false;
}
var tRex = Runner.instance_.tRex;
////////////////////////////////////////////////////
var obstacleBoxXPos = Runner.defaultDimensions.WIDTH + obstacle.xPos;
// Adjustments are made to the bounding box as there is a 1 pixel white
// border around the t-rex and obstacles.
var tRexBox = new CollisionBox(
tRex.xPos + 1 + distanceX,
tRex.yPos + 1 + distanceY,
tRex.config.WIDTH - 2 + width,
tRex.config.HEIGHT - 2 + height);
var obstacleBox = new CollisionBox(
obstacle.xPos + 1,
obstacle.yPos + 1,
obstacle.typeConfig.width * obstacle.size - 2,
obstacle.typeConfig.height - 2);
// Debug outer box
if (opt_canvasCtx) {
drawCollisionBoxes(opt_canvasCtx, tRexBox, obstacleBox);
}
// Simple outer bounds check.
if (boxCompare(tRexBox, obstacleBox)) {
var collisionBoxes = obstacle.collisionBoxes;
var tRexCollisionBoxes = tRex.ducking ?
Trex_collisionBoxes.DUCKING : Trex_collisionBoxes.RUNNING;
// Detailed axis aligned box check.
for (var t = 0; t < tRexCollisionBoxes.length; t++) {
for (var i = 0; i < collisionBoxes.length; i++) {
// Adjust the box to actual positions.
var adjTrexBox =
createAdjustedCollisionBox(tRexCollisionBoxes[t], tRexBox);
var adjObstacleBox =
createAdjustedCollisionBox(collisionBoxes[i], obstacleBox);
var crashed = boxCompare(adjTrexBox, adjObstacleBox);
// Draw boxes for debug.
if (opt_canvasCtx) {
drawCollisionBoxes(opt_canvasCtx, adjTrexBox, adjObstacleBox);
}
if (crashed) {
return [adjTrexBox, adjObstacleBox];
}
}
}
}
return false;
}
function jump() {
Runner.instance_.onKeyDown({preventDefault:function(){}, keyCode: Object.keys(Runner.keycodes.JUMP)[0]});
}
function unjump() {
Runner.instance_.onKeyUp({preventDefault:function(){}, keyCode: Object.keys(Runner.keycodes.JUMP)[0]});
}
function duck() {
Runner.instance_.onKeyDown({preventDefault:function(){}, keyCode: Object.keys(Runner.keycodes.DUCK)[0]});
}
function unduck() {
Runner.instance_.onKeyUp({preventDefault:function(){}, keyCode: Object.keys(Runner.keycodes.DUCK)[0]});
}
function restart() {
Runner.instance_.onKeyUp({preventDefault:function(){}, keyCode: Object.keys(Runner.keycodes.RESTART)[0]});
}
function update(){
var checkDistance = 75;
var jumpCheckDistance = -10;
var yCorrection = 93-Runner.instance_.tRex.yPos + 5;
var canvas = false? Runner.instance_.canvasCtx : null;
if (check(checkDistance, yCorrection, 0, 0, canvas)) {
console.log('Jump!');
unduck();
jump();
} else if (check(jumpCheckDistance, yCorrection, 40, 0, canvas)) {
console.log('Duck!');
unjump();
duck();
}
if (Runner.instance_.crashed) {
console.log('Restarting!');
restart();
}
}
if (!real_update) {
var real_update = Runner.instance_.update
}
Runner.instance_.update = function() {
real_update.apply(Runner.instance_);
update.apply(Runner.instance_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment