Skip to content

Commit

Permalink
Merge pull request #298 from Anushka-Pote/main
Browse files Browse the repository at this point in the history
Added keys for moves(R,L,U,D,B,F) and rotation (arrow keys)
  • Loading branch information
Dev-tanay authored Jun 16, 2024
2 parents b422a5f + 6f4cc34 commit 9a90437
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
<p>This can be done in two ways:</p>
<ol type="i">
<li>Twisting the segments</li>
<p>This can be done by dragging the cursor over the segment you want to rotate.</p>
<p>This can be done by dragging the cursor over the segment you want to rotate or letters(r l u b d f) for the move(Hold Shift for prime/anticlockwise moves)</li>
</p>
<img src="images/twist1.png" width="300">
<img src="images/twist2.png" width="300">
<li>Changing the view of the cube</li>
<p>This can be done by dragging the cursor in an arrow around the cube.</p>
<p>This can be done by dragging the cursor in an arrow or arrowkeys (up,down,right,left) around the cube.</p>
<img src="images/changeview.png" width="300">
</ol>

Expand Down
145 changes: 144 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4124,5 +4124,148 @@ musicButton.addEventListener('click', () => {
}
});

//rotation functions
function rotateU() {
const move = { axis: 'y', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 1, 0), axis: move.axis, angle: move.angle });
}

function rotateUPrime() {
const move = { axis: 'y', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 1, 0), axis: move.axis, angle: move.angle });
}

function rotateR() {
const move = { axis: 'x', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(1, 0, 0), axis: move.axis, angle: move.angle });
}

function rotateRPrime() {
const move = { axis: 'x', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(1, 0, 0), axis: move.axis, angle: move.angle });
}

function rotateF() {
const move = { axis: 'z', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 0, 1), axis: move.axis, angle: move.angle });
}

function rotateFPrime() {
const move = { axis: 'z', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 0, 1), axis: move.axis, angle: move.angle });
}

function rotateL() {
const move = { axis: 'x', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(-1, 0, 0), axis: move.axis, angle: move.angle });
}

function rotateLPrime() {
const move = { axis: 'x', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(-1, 0, 0), axis: move.axis, angle: move.angle });
}

function rotateD() {
const move = { axis: 'y', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, -1, 0), axis: move.axis, angle: move.angle });
}

function rotateDPrime() {
const move = { axis: 'y', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, -1, 0), axis: move.axis, angle: move.angle });
}

function rotateB() {
const move = { axis: 'z', angle: Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 0, -1), axis: move.axis, angle: move.angle });
}

function rotateBPrime() {
const move = { axis: 'z', angle: -Math.PI / 2 };
game.controls.keyboardMove('LAYER', { position: new THREE.Vector3(0, 0, -1), axis: move.axis, angle: move.angle });
}

// Function to handle view rotation based on direction
function rotateView(direction, isPrime = false) {
const move = {
'up': { axis: 'x', angle: isPrime ? Math.PI / 2 : -Math.PI / 2 },
'down': { axis: 'x', angle: isPrime ? -Math.PI / 2 : Math.PI / 2 },
'left': { axis: 'y', angle: isPrime ? -Math.PI / 2 : Math.PI / 2 },
'right': { axis: 'y', angle: isPrime ? Math.PI / 2 : -Math.PI / 2 }
}[direction];
if (move) {
game.controls.keyboardMove('CUBE', move);
}
}

let isPaused = false;

function togglePause() {
if (isPaused) {
game.timer.start(true);
game.controls.enable();
console.log('Game resumed');
} else {
game.timer.stop();
game.controls.disable();
console.log('Game paused');
}
isPaused = !isPaused;
}

// Add event listener for keydown
document.addEventListener('keydown', function(event) {
const isPrime = event.shiftKey;
const key = event.key.toLowerCase();

if (event.key === 'p') {
togglePause();
return;
}

if (isPaused) return;

switch (key) {
case 'arrowup':
rotateView('up', isPrime);
break;
case 'arrowdown':
rotateView('down', isPrime);
break;
case 'arrowleft':
rotateView('left', isPrime);
break;
case 'arrowright':
rotateView('right', isPrime);
break;
case 'u':
isPrime ? rotateUPrime() : rotateU();
break;
case 'r':
isPrime ? rotateRPrime() : rotateR();
break;
case 'f':
isPrime ? rotateFPrime() : rotateF();
break;
case 'l':
isPrime ? rotateLPrime() : rotateL();
break;
case 'd':
isPrime ? rotateDPrime() : rotateD();
break;
case 'b':
isPrime ? rotateBPrime() : rotateB();
break;
default:
break;
}
});

document.addEventListener('mousemove', function() {
if (isPaused) {
togglePause();
}
});

window.version = '0.99.2';
window.game = new Game();
window.game = new Game();

0 comments on commit 9a90437

Please sign in to comment.