Skip to content

Commit

Permalink
get_rid_of_if_statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ZafeerA123 committed Nov 6, 2023
1 parent 359cf4b commit dcb8427
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 80 deletions.
50 changes: 10 additions & 40 deletions _posts/2023-10-19-Gametest1.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,53 +131,23 @@ var animId;
var currentFrame = 0;
var sec = 0;
function checkForOverlap(object1, object2) {
var pos1 = object1.ReturnPosition().slice();
var scale1 = object1.ReturnScale().slice();
var pos1 = object1.ReturnPosition();
var scale1 = object1.ReturnScale();
var xRange1 = [pos1[0], pos1[0] + scale1[0]];
var yRange1 = [pos1[1], pos1[1] + scale1[1]];

var pos2 = object2.ReturnPosition().slice();
var scale2 = object2.ReturnScale().slice();
var pos2 = object2.ReturnPosition();
var scale2 = object2.ReturnScale();
var xRange2 = [pos2[0], pos2[0] + scale2[0]];
var yRange2 = [pos2[1], pos2[1] + scale2[1]];
var yRange2 = [pos2[1], pos2[1] + scale2[1]]; // Added a missing closing bracket

if (
xRange1[0] >= xRange2[0] &&
xRange1[0] <= xRange2[1] &&
yRange1[0] >= yRange2[0] &&
yRange1[0] <= yRange2[1]
) {
return true;
function rangesOverlap(range1, range2) {
return range1[0] < range2[1] && range1[1] > range2[0];
}

if (
xRange1[0] >= xRange2[0] &&
xRange1[0] <= xRange2[1] &&
yRange1[1] >= yRange2[0] &&
yRange1[1] <= yRange2[1]
) {
return true;
}

if (
xRange1[1] >= xRange2[0] &&
xRange1[1] <= xRange2[1] &&
yRange1[0] >= yRange2[0] &&
yRange1[0] <= yRange2[1]
) {
return true;
}

if (
xRange1[1] >= xRange2[0] &&
xRange1[1] <= xRange2[1] &&
yRange1[1] >= yRange2[0] &&
yRange1[1] <= yRange2[1]
) {
return true;
}

return false;
return (
rangesOverlap(xRange1, xRange2) && rangesOverlap(yRange1, yRange2)
);
}
function frame(){ //when a frame is updated
currentFrame = (currentFrame+1)%fps;
Expand Down
50 changes: 10 additions & 40 deletions _posts/2023-10-25-Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,53 +225,23 @@ var flag3 = false; //sleep

//overlap function
function checkForOverlap(object1, object2) {
var pos1 = object1.ReturnPosition().slice();
var scale1 = object1.ReturnScale().slice();
var pos1 = object1.ReturnPosition();
var scale1 = object1.ReturnScale();
var xRange1 = [pos1[0], pos1[0] + scale1[0]];
var yRange1 = [pos1[1], pos1[1] + scale1[1]];

var pos2 = object2.ReturnPosition().slice();
var scale2 = object2.ReturnScale().slice();
var pos2 = object2.ReturnPosition();
var scale2 = object2.ReturnScale();
var xRange2 = [pos2[0], pos2[0] + scale2[0]];
var yRange2 = [pos2[1], pos2[1] + scale2[1]];
var yRange2 = [pos2[1], pos2[1] + scale2[1]]; // Added a missing closing bracket

if (
xRange1[0] >= xRange2[0] &&
xRange1[0] <= xRange2[1] &&
yRange1[0] >= yRange2[0] &&
yRange1[0] <= yRange2[1]
) {
return true;
function rangesOverlap(range1, range2) {
return range1[0] < range2[1] && range1[1] > range2[0];
}

if (
xRange1[0] >= xRange2[0] &&
xRange1[0] <= xRange2[1] &&
yRange1[1] >= yRange2[0] &&
yRange1[1] <= yRange2[1]
) {
return true;
}

if (
xRange1[1] >= xRange2[0] &&
xRange1[1] <= xRange2[1] &&
yRange1[0] >= yRange2[0] &&
yRange1[0] <= yRange2[1]
) {
return true;
}

if (
xRange1[1] >= xRange2[0] &&
xRange1[1] <= xRange2[1] &&
yRange1[1] >= yRange2[0] &&
yRange1[1] <= yRange2[1]
) {
return true;
}

return false;
return (
rangesOverlap(xRange1, xRange2) && rangesOverlap(yRange1, yRange2)
);
}

//function for switching between displays
Expand Down

0 comments on commit dcb8427

Please sign in to comment.