Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responsive #3

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bOt.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
rel="stylesheet"
/>
<link rel="stylesheet" href="styles.css" />
<link rel="icon" href="bOt.jpg" />
</head>
<body>
<div id="loading-screen">
Expand All @@ -30,8 +31,43 @@ <h1>3D-MAZE</h1>
<button id="play-button">Play</button>
</div>

<button id="fullscreenToggle" class="control-button hidden game-control">
<svg
id="fullscreenIcon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
>
<path
d="M344 0L488 0c13.3 0 24 10.7 24 24l0 144c0 9.7-5.8 18.5-14.8 22.2s-19.3 1.7-26.2-5.2l-39-39-87 87c-9.4 9.4-24.6 9.4-33.9 0l-32-32c-9.4-9.4-9.4-24.6 0-33.9l87-87L327 41c-6.9-6.9-8.9-17.2-5.2-26.2S334.3 0 344 0zM168 512L24 512c-13.3 0-24-10.7-24-24L0 344c0-9.7 5.8-18.5 14.8-22.2s19.3-1.7 26.2 5.2l39 39 87-87c9.4-9.4 24.6-9.4 33.9 0l32 32c9.4 9.4 9.4 24.6 0 33.9l-87 87 39 39c6.9 6.9 8.9 17.2 5.2 26.2s-12.5 14.8-22.2 14.8z"
/>
</svg>
<svg
id="exitFullscreenIcon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"
style="display: none"
>
<path
d="M439 7c9.4-9.4 24.6-9.4 33.9 0l32 32c9.4 9.4 9.4 24.6 0 33.9l-87 87 39 39c6.9 6.9 8.9 17.2 5.2 26.2s-12.5 14.8-22.2 14.8H296c-13.3 0-24-10.7-24-24V72c0-9.7 5.8-18.5 14.8-22.2s19.3-1.7 26.2 5.2l39 39 87-87zM72 272h144c13.3 0 24 10.7 24 24v144c0 9.7-5.8 18.5-14.8 22.2s-19.3 1.7-26.2-5.2l-39-39-87 87c-9.4 9.4-24.6 9.4-33.9 0L7 473c-9.4-9.4-9.4-24.6 0-33.9l87-87-39-39c-6.9-6.9-8.9-17.2-5.2-26.2s12.5-14.8 22.2-14.8z"
/>
</svg>
</button>

<button id="viewToggle" class="hidden">Change View</button>

<div id="landscape-prompt" class="hidden">
Please rotate your device to landscape mode for the best experience.
</div>

<div id="joystick-container" class="hidden">
<div id="left-joystick" class="joystick"></div>
<div id="right-joystick" class="joystick"></div>
</div>

<div id="cursor-notification" class="hidden">Press ESC to show cursor</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nipplejs/0.9.0/nipplejs.min.js"></script>
<script src="script.js"></script>
</body>
</html>
181 changes: 172 additions & 9 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,29 @@ const gameState = {
cellSize2D: null,
maze2DRepresentation: null,
endReached: false,
isTouchDevice: false,
leftJoystick: null,
rightJoystick: null,
};

// Global functions
function movePlayer() {
const moveVector = new THREE.Vector3();
if (gameState.keys.KeyW) moveVector.z -= 1;
if (gameState.keys.KeyS) moveVector.z += 1;
if (gameState.keys.KeyA) moveVector.x -= 1;
if (gameState.keys.KeyD) moveVector.x += 1;
const speedMultiplier = gameState.isTouchDevice ? 0.5 : 1; // Adjust this value as needed

if (gameState.isTouchDevice) {
moveVector.z = gameState.keys.KeyW - gameState.keys.KeyS;
moveVector.x = gameState.keys.KeyD - gameState.keys.KeyA;
} else {
if (gameState.keys.KeyW) moveVector.z -= 1;
if (gameState.keys.KeyS) moveVector.z += 1;
if (gameState.keys.KeyA) moveVector.x -= 1;
if (gameState.keys.KeyD) moveVector.x += 1;
}

moveVector.applyQuaternion(gameState.camera.quaternion);
moveVector.y = 0; // Prevent vertical movement
moveVector.normalize().multiplyScalar(gameState.moveSpeed);
moveVector.normalize().multiplyScalar(gameState.moveSpeed * speedMultiplier);

const newPosition = gameState.playerPosition.clone().add(moveVector);
if (!checkCollision(newPosition)) {
Expand Down Expand Up @@ -162,6 +172,14 @@ function handleArrowKeyRotation() {
updateCameraPosition();
}

function isTouchDevice() {
return (
"ontouchstart" in window ||
navigator.maxTouchPoints > 0 ||
navigator.msMaxTouchPoints > 0
);
}

function initGame() {
// Initialize Three.js scene, camera, and renderer
gameState.scene = new THREE.Scene();
Expand Down Expand Up @@ -538,7 +556,19 @@ function initGame() {

// Lock pointer on click
gameState.renderer.domElement.addEventListener("click", () => {
gameState.renderer.domElement.requestPointerLock();
if (!gameState.isTouchDevice) {
gameState.renderer.domElement.requestPointerLock();
showCursorNotification();
}
});

// Add an event listener for exiting pointer lock
document.addEventListener("pointerlockchange", () => {
if (document.pointerLockElement !== gameState.renderer.domElement) {
hideCursorNotification();
} else {
showCursorNotification();
}
});

// Add top-down camera
Expand Down Expand Up @@ -609,8 +639,8 @@ function initGame() {
playerMarker.style.position = "absolute";
playerMarker.style.width = "0";
playerMarker.style.height = "0";
playerMarker.style.borderLeft = `${cellSize * 0.5}px solid transparent`;
playerMarker.style.borderRight = `${cellSize * 0.5}px solid transparent`;
playerMarker.style.borderLeft = `${cellSize * 0.4}px solid transparent`;
playerMarker.style.borderRight = `${cellSize * 0.4}px solid transparent`;
playerMarker.style.borderBottom = `${cellSize * 1}px solid red`;
playerMarker.style.transform = "translate(-50%, -50%)";
mazeContainer.appendChild(playerMarker);
Expand Down Expand Up @@ -679,6 +709,39 @@ function initGame() {

document.getElementById("viewToggle").addEventListener("click", toggleView);

// Add this function to toggle fullscreen
function toggleFullscreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen().catch((e) => {
console.error(`Error attempting to enable fullscreen: ${e.message}`);
});
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
}

// Add this function to update the fullscreen button icon
function updateFullscreenButtonIcon() {
const fullscreenIcon = document.getElementById("fullscreenIcon");
const exitFullscreenIcon = document.getElementById("exitFullscreenIcon");
if (document.fullscreenElement) {
fullscreenIcon.style.display = "none";
exitFullscreenIcon.style.display = "block";
} else {
fullscreenIcon.style.display = "block";
exitFullscreenIcon.style.display = "none";
}
}

// Set up fullscreen button
const fullscreenToggle = document.getElementById("fullscreenToggle");
fullscreenToggle.addEventListener("click", toggleFullscreen);

// Update fullscreen button icon when fullscreen state changes
document.addEventListener("fullscreenchange", updateFullscreenButtonIcon);

function animate() {
requestAnimationFrame(animate);
movePlayer();
Expand All @@ -699,6 +762,8 @@ function initGame() {

if (gameState.currentCamera === gameState.camera) {
gameState.renderer.render(gameState.scene, gameState.currentCamera);
} else {
updatePlayerMarker2D(); // Update 2D marker even when in 2D view
}
}

Expand All @@ -707,10 +772,108 @@ function initGame() {

console.log("Maze created");

// Show the toggle button when the game starts
// Show the toggle buttons when the game starts
document.getElementById("viewToggle").classList.remove("hidden");
document.getElementById("fullscreenToggle").classList.add("visible");

gameState.isTouchDevice = isTouchDevice();

if (gameState.isTouchDevice) {
setupJoysticks();
document.getElementById("joystick-container").style.display = "flex";
} else {
document.getElementById("joystick-container").style.display = "none";
}

// Initialize fullscreen button icon
updateFullscreenButtonIcon();

// Add these functions at the appropriate place in your script

function showCursorNotification() {
if (!gameState.isTouchDevice) {
const notification = document.getElementById("cursor-notification");
notification.style.display = "block";
}
}

function hideCursorNotification() {
const notification = document.getElementById("cursor-notification");
notification.style.display = "none";
}

// Modify the DOMContentLoaded event listener to hide the notification on mobile
document.addEventListener("DOMContentLoaded", () => {
// ... existing code ...

if (isTouchDevice()) {
document.getElementById("cursor-notification").style.display = "none";
}

// ... rest of the existing code ...
});
}

function setupJoysticks() {
const joystickOptions = {
mode: "static",
position: { left: "50%", top: "50%" },
color: "white",
size: 120,
lockX: false,
lockY: false,
dynamicPage: true,
};

gameState.leftJoystick = nipplejs.create({
...joystickOptions,
zone: document.getElementById("left-joystick"),
});

gameState.rightJoystick = nipplejs.create({
...joystickOptions,
zone: document.getElementById("right-joystick"),
});

gameState.leftJoystick.on("move", (evt, data) => {
const force = Math.min(data.force, 1); // Reduce movement speed by half
const angle = data.angle.radian + Math.PI / 2; // Rotate angle by 90 degrees
gameState.keys.KeyW = Math.cos(angle) * force;
gameState.keys.KeyS = -Math.cos(angle) * force;
gameState.keys.KeyA = -Math.sin(angle) * force;
gameState.keys.KeyD = Math.sin(angle) * force;
});

gameState.leftJoystick.on("end", () => {
gameState.keys.KeyW = 0;
gameState.keys.KeyS = 0;
gameState.keys.KeyA = 0;
gameState.keys.KeyD = 0;
});

gameState.rightJoystick.on("move", (evt, data) => {
const force = Math.min(data.force, 1) * 0.01; // Reduce rotation speed
const angle = data.angle.radian + Math.PI / 2; // Rotate angle by 90 degrees
gameState.playerRotationY -= Math.sin(angle) * force;
gameState.playerRotationX -= Math.cos(angle) * force;
gameState.playerRotationX = Math.max(
-Math.PI / 2,
Math.min(Math.PI / 2, gameState.playerRotationX)
);
updateCameraPosition();
});
}

// Add an event listener for device orientation changes
window.addEventListener("orientationchange", () => {
const landscapePrompt = document.getElementById("landscape-prompt");
if (window.orientation === 0 || window.orientation === 180) {
landscapePrompt.style.display = "flex";
} else {
landscapePrompt.style.display = "none";
}
});

// Event listener for DOMContentLoaded
document.addEventListener("DOMContentLoaded", () => {
const loadingScreen = document.getElementById("loading-screen");
Expand Down
Loading