Skip to content

Commit

Permalink
Update source.js
Browse files Browse the repository at this point in the history
  • Loading branch information
7ih authored May 24, 2021
1 parent 76c5b04 commit 2550025
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,9 @@ function Tether() {
var self = this;

document.addEventListener('mousemove', function (e) {
if (self.lastInteraction === 'mouse' && document.pointerLockElement !== canvas)
game.lastMousePosition = {x: e.layerX, y: e.layerY};
self.lastInteraction = 'mouse';
if (document.pointerLockElement !== canvas) {
game.lastMousePosition = {x: e.layerX, y: e.layerY};
}
});

document.addEventListener('touchend', function (e) {
Expand All @@ -833,11 +832,8 @@ function Tether() {
document.mozPointerLockElement === canvas
)
self.locked = false;
else {
else
self.locked = true;
paused = true;
game.lastMousePosition = {x: NaN, y: NaN};
}
}

if ('onpointerlockchange' in document)
Expand All @@ -848,6 +844,7 @@ function Tether() {
function handleTouch(e) {
e.preventDefault();
self.lastInteraction = 'touch';
if (document.pointerLockElement) document.exitPointerLock();
touch = e.changedTouches[0];
game.lastMousePosition = {x: touch.clientX, y: touch.clientY};
}
Expand Down Expand Up @@ -1589,7 +1586,7 @@ function Game() {
self.lastMousePosition = {x: NaN, y: NaN};

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

self.background = new Background();
self.ended = null;
Expand Down Expand Up @@ -1883,10 +1880,7 @@ function Game() {
};

self.drawLogo = function () {
var opacity;
if (!game.started) opacity = 1;
else opacity = Math.pow(1 - game.timeElapsed / 50, 3);

var opacity = game.started ? Math.pow(1 - game.timeElapsed / 50, 3) : 1;
if (opacity < 0.001) return;

draw({
Expand Down Expand Up @@ -1914,9 +1908,9 @@ function Game() {
draw({
type: 'text',
text:
{touch: 'tap', mouse: 'click'}[self.tether.lastInteraction] + 'to start',
{touch: 'tap', mouse: 'click'}[self.tether.lastInteraction] + ' to start',
fillStyle: rgbWithOpacity([0, 0, 0], opacity),
fontSize: 28,
fontSize: 24,
textPosition: {
x: width / 2,
y: height / 3 + 80,
Expand All @@ -1930,12 +1924,14 @@ function Game() {
var opacity = -Math.sin((game.timeElapsed - game.ended) * 3);
if (opacity < 0) opacity = 0;

var fontSize = Math.min(width / 5, height / 8);

draw({
type: 'text',
text:
{touch: 'tap', mouse: 'click'}[self.tether.lastInteraction] + ' to retry',
fontSize: Math.min(width / 5, height / 8),
textPosition: {x: width / 2, y: height / 2},
fontSize: fontSize,
textPosition: {x: width / 2, y: height / 2 - fontSize / 2},
fillStyle: rgbWithOpacity([0, 0, 0], opacity),
});
};
Expand Down Expand Up @@ -2159,13 +2155,11 @@ function Game() {
};

self.positionShouldMute = function (position) {
if (self.started || !self.ended) return false;
self.proximityToMuteButton = vectorMagnitude(
forXAndY([muteButtonPosition, position], forXAndY.subtract),
);
return (
(!self.started || self.ended) &&
self.proximityToMuteButton < muteButtonProximityThreshold
);
return (self.proximityToMuteButton < muteButtonProximityThreshold);
};

self.eventShouldPlay = function (e) {
Expand All @@ -2175,7 +2169,7 @@ function Game() {
var touch = e.changedTouches[0];
position = {x: touch.pageX, y: touch.pageY};
} else {
position = {x: e.layerX, y: e.layerY};
position = (game.lastMousePosition || {x: e.layerX, y: e.layerY});
}

return self.positionShouldPlay(position);
Expand Down Expand Up @@ -2310,7 +2304,7 @@ function Game() {
};

self.end = function () {
if (document.exitPointerLock) document.exitPointerLock();
if (document.pointerLockElement) document.exitPointerLock();
logScore(self.score);
self.ended = self.timeElapsed;
self.tether.locked = true;
Expand All @@ -2336,7 +2330,7 @@ function handleClick(e) {
saveCookie(musicMutedCookieKey, 'false');
}
} else if(game.eventShouldPlay(e)) {
paused = !paused
paused = !paused;
} else if (game.ended) {
game.reset(0);
}
Expand All @@ -2350,7 +2344,9 @@ document.addEventListener('click', handleClick);
document.addEventListener('keydown', handleKey);

canvas.addEventListener('mousemove', function (e) {
if (document.pointerLockElement === canvas) {
if (game.tether.lastInteraction === 'touch' && document.pointerLockElement)
document.exitPointerLock();
else if (document.pointerLockElement === canvas) {
if (game.tether.locked) game.tether.locked = false;

game.lastMousePosition.x += e.movementX;
Expand Down Expand Up @@ -2389,10 +2385,34 @@ function animate() {
requestFrame(animate);
if(!paused) {
game.step();
if(pauseDelay !== 0) pauseDelay = 0;
if (pauseDelay !== 0) {
pauseDelay = 0;
if (canvas.requestPointerLock) canvas.requestPointerLock();
game.player.teleportTo({
x: game.lastMousePosition.x + 50,
y: game.lastMousePosition.y + 50
});
}
} else if (paused && pauseDelay !== 2) {
game.step();
pauseDelay++;
if (pauseDelay === 2) {
if (document.pointerLockElement) document.exitPointerLock();

var fontSize = Math.min(width / 5, height / 8);

draw({
type: 'text',
text:
{touch: 'tap', mouse: 'click'}[game.tether.lastInteraction] + ' to unpause',
fillStyle: "#000",
fontSize: fontSize,
textPosition: {
x: width / 2,
y: height / 2 - fontSize / 2,
},
});
}
}
}

Expand Down

0 comments on commit 2550025

Please sign in to comment.