From b6aebe1ed63573c6adaf3ba4a1cf3e4992ee6b2d Mon Sep 17 00:00:00 2001 From: Ray Arayilakath Date: Fri, 21 May 2021 18:21:31 +0000 Subject: [PATCH] fix: patch pointerlock not existing bug and obfuscate new code --- index.html | 2 +- script.js | 126 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 72 insertions(+), 56 deletions(-) diff --git a/index.html b/index.html index ea41d57..a93a943 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ -tether! | Swing Around a Ball of Destruction! \ No newline at end of file +tether! | Swing Around a Ball of Destruction! \ No newline at end of file diff --git a/script.js b/script.js index 96b96ac..d9ca503 100644 --- a/script.js +++ b/script.js @@ -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) ); } @@ -294,7 +294,7 @@ draw.arc = function (opts) { opts.arcCenter.y, opts.arcRadius, opts.arcStart, - opts.arcFinish, + opts.arcFinish ); }; @@ -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) { @@ -433,7 +436,7 @@ function initCanvas() { break; default: case 10: - playerRGB = "Rainbow"; + playerRGB = 'Rainbow'; console.log('Congrats on your 10 day streak!!'); break; } @@ -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); @@ -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()}:${ @@ -513,7 +514,7 @@ function timeToNextClaim() { formattedTime.getSeconds() > 9 ? '' : '0' }${formattedTime.getSeconds()}`; } else { - return 'Right Now!' + return 'Right Now!'; } } @@ -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(); }, @@ -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) { @@ -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, @@ -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); } @@ -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(); @@ -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; } @@ -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) { @@ -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); }; @@ -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()], }); @@ -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() ), }); }; @@ -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, @@ -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); @@ -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 ); }; @@ -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({ @@ -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); @@ -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; @@ -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 = @@ -1771,7 +1786,7 @@ function Game() { enemyPositionDelta.y, massPositionDelta.x, massPositionDelta.y, - 1, + 1 )) ) { enemyPosition = { @@ -1968,7 +1983,7 @@ function Game() { fromBottom, fromRight, headingText, - fillStyle, + fillStyle ) { if (achievementList.length === 0) return fromBottom; @@ -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, @@ -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) ); } }; @@ -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) && @@ -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; @@ -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(); @@ -2302,3 +2317,4 @@ window.addEventListener('scroll', function (e) { window.scrollTo(0, 0); animate(); +