Skip to content

Commit

Permalink
fix: patch pointerlock not existing bug and obfuscate new code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Arayilakath committed May 21, 2021
1 parent 5347638 commit b6aebe1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 56 deletions.
2 changes: 1 addition & 1 deletion index.html

Large diffs are not rendered by default.

126 changes: 71 additions & 55 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function pointInPolygon(point, polygon) {

function vectorMagnitude(vector) {
return Math.abs(
Math.pow(Math.pow(vector.x, 2) + Math.pow(vector.y, 2), 1 / 2),
Math.pow(Math.pow(vector.x, 2) + Math.pow(vector.y, 2), 1 / 2)
);
}

Expand Down Expand Up @@ -294,7 +294,7 @@ draw.arc = function (opts) {
opts.arcCenter.y,
opts.arcRadius,
opts.arcStart,
opts.arcFinish,
opts.arcFinish
);
};

Expand Down Expand Up @@ -396,7 +396,10 @@ function initCanvas() {
) {
saveCookie(lastDayCookieKey, currentDate.getTime());
saveCookie(streakCountCookieKey, 0);
} else if (later48Hours > Number(new Date()) && Number(new Date()) > later24Hours) {
} else if (
later48Hours > Number(new Date()) &&
Number(new Date()) > later24Hours
) {
saveCookie(streakCountCookieKey, (streak += 1));
saveCookie(lastDayCookieKey, currentDate.getTime());
} else if (Number(new Date()) < later24Hours) {
Expand Down Expand Up @@ -433,7 +436,7 @@ function initCanvas() {
break;
default:
case 10:
playerRGB = "Rainbow";
playerRGB = 'Rainbow';
console.log('Congrats on your 10 day streak!!');
break;
}
Expand All @@ -453,10 +456,8 @@ function initCanvas() {
canvas.style.width = width.toString() + 'px';
canvas.style.height = height.toString() + 'px';

canvas.requestPointerLock =
canvas.requestPointerLock || canvas.mozRequestPointerLock;
document.exitPointerLock =
document.exitPointerLock || document.mozExitPointerLock;
canvas.requestPointerLock = canvas.requestPointerLock || canvas.mozRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock;

for (var key in localStorage) {
var value = localStorage.getItem(key);
Expand Down Expand Up @@ -503,8 +504,8 @@ function timeToNextClaim() {
var deadline = lastDate.getTime() + 86400000;
var timeRemaining = deadline - new Date();
var formattedTime = new Date(timeRemaining);
if(formattedTime > 0) {

if (formattedTime > 0) {
return `${
formattedTime.getHours() > 9 ? '' : '0'
}${formattedTime.getHours()}:${
Expand All @@ -513,7 +514,7 @@ function timeToNextClaim() {
formattedTime.getSeconds() > 9 ? '' : '0'
}${formattedTime.getSeconds()}`;
} else {
return 'Right Now!'
return 'Right Now!';
}
}

Expand Down Expand Up @@ -633,7 +634,7 @@ Mass.prototype = {

reactToVelocity: function () {
this.setPosition(
forXAndY([this.position, this.velocity], forXAndY.aPlusBTimesSpeed),
forXAndY([this.position, this.velocity], forXAndY.aPlusBTimesSpeed)
);
this.collideWithWalls();
},
Expand All @@ -649,7 +650,7 @@ Mass.prototype = {
var self = this;
var projectedVelocity = forXAndY(
[this.velocity, this.velocityDelta()],
forXAndY.aPlusBTimesSpeed,
forXAndY.aPlusBTimesSpeed
);

this.velocity = forXAndY([projectedVelocity], function (projected) {
Expand All @@ -671,17 +672,19 @@ Mass.prototype = {
},

getCurrentColor: function () {
if(this.rgb === 'Rainbow') {
if(hslVal !== 360) hslVal++;
if (this.rgb === 'Rainbow') {
if (hslVal !== 360) hslVal++;
else hslVal = 0;
}
return this.rgb === 'Rainbow' ? hsl(hslVal) : rgbWithOpacity(this.rgb, this.getOpacity());
return this.rgb === 'Rainbow'
? hsl(hslVal)
: rgbWithOpacity(this.rgb, this.getOpacity());
},

draw: function () {
var radius = this.radius;
if (this.visibleRadius !== null) radius = this.visibleRadius;

draw({
type: 'arc',
arcRadius: radius,
Expand Down Expand Up @@ -712,7 +715,7 @@ Mass.prototype = {
var magnitude = Math.random() * 40;
var velocity = forXAndY(
[vectorAt(angle, magnitude), this.velocity],
forXAndY.add,
forXAndY.add
);
new FireParticle(this.position, velocity);
}
Expand Down Expand Up @@ -823,17 +826,19 @@ function Tether() {
});

function exitTether() {
if(document.pointerLockElement === canvas || document.mozPointerLockElement === canvas)
if (document.pointerLockElement === canvas || document.mozPointerLockElement === canvas)
self.locked = false;
else {
document.exitPointerLock();
if(document.exitPointerLock) document.exitPointerLock();
self.locked = true;
game.lastMousePosition = {x: NaN, y: NaN};
}
}
}

if ("onpointerlockchange" in document) document.addEventListener('pointerlockchange', exitTether);
else if ("onmozpointerlockchange" in document) document.addEventListener('mozpointerlockchange', exitTether);
if ('onpointerlockchange' in document)
document.addEventListener('pointerlockchange', exitTether);
else if ('onmozpointerlockchange' in document)
document.addEventListener('mozpointerlockchange', exitTether);

function handleTouch(e) {
e.preventDefault();
Expand All @@ -850,7 +855,8 @@ function Tether() {
extend(Mass, Tether);

Tether.prototype.setPosition = function (position) {
if (this.lastInteraction !== 'mouse' || document.pointerLockElement === canvas) Mass.prototype.setPosition.call(this, position);
if (this.lastInteraction !== 'mouse' || document.pointerLockElement === canvas)
Mass.prototype.setPosition.call(this, position);
if (this.position !== this.positionOnPreviousFrame) {
this.pointsScoredSinceLastInteraction = 0;
}
Expand All @@ -862,11 +868,14 @@ Tether.prototype.step = function () {
if (
this.unlockable &&
vectorMagnitude(
forXAndY([this.position, game.lastMousePosition], forXAndY.subtract),
forXAndY([this.position, game.lastMousePosition], forXAndY.subtract)
) < leniency
) {
canvas.requestPointerLock();
if (!(this.lastInteraction !== 'mouse' || document.pointerLockElement === canvas)) return;
if (
!(this.lastInteraction !== 'mouse' || document.pointerLockElement === canvas)
)
return;
this.locked = false;

if (!game.started) {
Expand Down Expand Up @@ -908,7 +917,7 @@ extend(Mass, Player);
Player.prototype.step = function () {
this.force = forXAndY(
[this.tether.position, this.position],
forXAndY.subtract,
forXAndY.subtract
);
Mass.prototype.step.call(this);
};
Expand All @@ -933,7 +942,13 @@ function Cable(tether, player) {
draw({
type: 'line',
stroke: true,
strokeStyle: `${playerRGB === 'Rainbow' ? `${hsl(hslVal)}` : `rgba(${playerRGB[0] ?? 20}, ${playerRGB[1] ?? 20}, ${playerRGB[2] ?? 200}, 1)`}`,
strokeStyle: `${
playerRGB === 'Rainbow'
? `${hsl(hslVal)}`
: `rgba(${playerRGB[0] ?? 20}, ${playerRGB[1] ?? 20}, ${
playerRGB[2] ?? 200
}, 1)`
}`,
linePaths: [self.line()],
});

Expand Down Expand Up @@ -1043,7 +1058,7 @@ Enemy.prototype.drawWarning = function () {
Math.pow(1 - timeUntilSpawn, 3),
strokeStyle: rgbWithOpacity(
this.rgbWarning || this.rgb,
(1 - timeUntilSpawn) * this.getOpacity(),
(1 - timeUntilSpawn) * this.getOpacity()
),
});
};
Expand Down Expand Up @@ -1139,7 +1154,7 @@ Eye.prototype.drawWarning = function () {
lineWidth: ((2 * this.shadowRadius) / 2) * Math.pow(1 - timeUntilSpawn, 3),
strokeStyle: rgbWithOpacity(
this.rgbWarning || this.rgb,
(1 - timeUntilSpawn) * this.getOpacity() * this.shadowOpacity,
(1 - timeUntilSpawn) * this.getOpacity() * this.shadowOpacity
),
arcCenter: this.position,
arcRadius: this.shadowRadius / 2 + Math.pow(timeUntilSpawn, 2) * 700,
Expand All @@ -1165,7 +1180,7 @@ Eye.prototype.drawIris = function () {

var irisVector = vectorAt(
vectorAngle(targetVector),
awakeness * this.radius * Math.pow(relativeDistance, 1 / 2) * 0.7,
awakeness * this.radius * Math.pow(relativeDistance, 1 / 2) * 0.7
);

var centreOfIris = forXAndY([this.position, irisVector], forXAndY.add);
Expand Down Expand Up @@ -1294,7 +1309,7 @@ FireParticle.prototype.getCurrentColor = function () {
var intensity = this.velocity.x / this.initialIntensity;
return rgbWithOpacity(
this.rgbForIntensity(intensity),
Math.pow(intensity, 0.25) * this.opacity,
Math.pow(intensity, 0.25) * this.opacity
);
};

Expand All @@ -1309,7 +1324,7 @@ FireParticle.prototype.draw = function () {
var maturity = 1 - 1 / (timeAlive / 3 + 1);
var velocityButSmallerWhenYoung = forXAndY(
[this.velocity, {x: maturity, y: maturity}],
forXAndY.multiply,
forXAndY.multiply
);

draw({
Expand Down Expand Up @@ -1351,20 +1366,20 @@ Exhaust.prototype.rgbForIntensity = function (intensity) {
function TeleportDust(source) {
var randomDelta = vectorAt(
Math.random() * Math.PI * 2,
Math.random() * source.radius * 0.1,
Math.random() * source.radius * 0.1
);

var velocityMultiplier = (Math.random() * 1) / 10;
var baseVelocity = forXAndY(
[source.teleportDelta, {x: velocityMultiplier, y: velocityMultiplier}],
forXAndY.multiply,
forXAndY.multiply
);
var velocity = forXAndY([baseVelocity, randomDelta], forXAndY.add);

var distanceFromStart = Math.random();
var vectorFromStart = forXAndY(
[source.teleportDelta, {x: distanceFromStart, y: distanceFromStart}],
forXAndY.multiply,
forXAndY.multiply
);
var basePosition = forXAndY([source.position, vectorFromStart], forXAndY.add);
var position = forXAndY([basePosition, randomDelta], forXAndY.add);
Expand Down Expand Up @@ -1574,7 +1589,7 @@ function Game() {
self.lastMousePosition = {x: NaN, y: NaN};

self.reset = function (waveIndex) {
document.exitPointerLock();
if(document.exitPointerLock) document.exitPointerLock();

self.background = new Background();
self.ended = null;
Expand Down Expand Up @@ -1682,7 +1697,7 @@ function Game() {
self.proximityToMuteButton = maximumPossibleDistanceBetweenTwoMasses;
} else {
self.proximityToMuteButton = vectorMagnitude(
forXAndY([muteButtonPosition, self.lastMousePosition], forXAndY.subtract),
forXAndY([muteButtonPosition, self.lastMousePosition], forXAndY.subtract)
);
}
self.clickShouldMute =
Expand Down Expand Up @@ -1771,7 +1786,7 @@ function Game() {
enemyPositionDelta.y,
massPositionDelta.x,
massPositionDelta.y,
1,
1
))
) {
enemyPosition = {
Expand Down Expand Up @@ -1968,7 +1983,7 @@ function Game() {
fromBottom,
fromRight,
headingText,
fillStyle,
fillStyle
) {
if (achievementList.length === 0) return fromBottom;

Expand Down Expand Up @@ -2016,7 +2031,7 @@ function Game() {
indicatedPosition = game.lastMousePosition;
}
var distanceFromCorner = vectorMagnitude(
lineDelta([indicatedPosition, {x: width, y: height}]),
lineDelta([indicatedPosition, {x: width, y: height}])
);
var distanceRange = [
maximumPossibleDistanceBetweenTwoMasses / 10,
Expand Down Expand Up @@ -2096,14 +2111,14 @@ function Game() {
fromBottom,
fromRight,
'Locked',
rgbWithOpacity([0, 0, 0], listingOpacity * 0.5),
rgbWithOpacity([0, 0, 0], listingOpacity * 0.5)
);
this.drawAchievements(
unlockedAchievements,
fromBottom,
fromRight,
'Unlocked',
rgbWithOpacity([0, 0, 0], listingOpacity),
rgbWithOpacity([0, 0, 0], listingOpacity)
);
}
};
Expand All @@ -2123,7 +2138,7 @@ function Game() {

self.positionShouldMute = function (position) {
self.proximityToMuteButton = vectorMagnitude(
forXAndY([muteButtonPosition, position], forXAndY.subtract),
forXAndY([muteButtonPosition, position], forXAndY.subtract)
);
return (
(!self.started || self.ended) &&
Expand Down Expand Up @@ -2222,7 +2237,7 @@ function Game() {
};

self.end = function () {
document.exitPointerLock();
if(document.exitPointerLock) document.exitPointerLock();
logScore(self.score);
self.ended = self.timeElapsed;
self.tether.locked = true;
Expand Down Expand Up @@ -2254,18 +2269,18 @@ function handleClick(e) {

document.addEventListener('click', handleClick);

canvas.addEventListener('mousemove', function (e) {
if (document.pointerLockElement === canvas) {
game.lastMousePosition.x += e.movementX;
game.lastMousePosition.y += e.movementY;
canvas.addEventListener('mousemove', function (e) {
if (document.pointerLockElement === canvas) {
game.lastMousePosition.x += e.movementX;
game.lastMousePosition.y += e.movementY;

if (game.lastMousePosition.x < 0) game.lastMousePosition.x = 0;
else if (game.lastMousePosition.x > width) game.lastMousePosition.x = width;
if (game.lastMousePosition.x < 0) game.lastMousePosition.x = 0;
else if (game.lastMousePosition.x > width) game.lastMousePosition.x = width;

if (game.lastMousePosition.y < 0) game.lastMousePosition.y = 0;
else if (game.lastMousePosition.y > height) game.lastMousePosition.y = height;
}
});
if (game.lastMousePosition.y < 0) game.lastMousePosition.y = 0;
else if (game.lastMousePosition.y > height) game.lastMousePosition.y = height;
}
});

document.addEventListener('touchstart', function (e) {
lastTouchStart = new Date().getTime();
Expand Down Expand Up @@ -2302,3 +2317,4 @@ window.addEventListener('scroll', function (e) {
window.scrollTo(0, 0);

animate();

0 comments on commit b6aebe1

Please sign in to comment.