<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minecraft 3D</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
touch-action: none;
}
#gameCanvas {
display: block;
}
/* Bouton de déplacement bas gauche */
#moveButton {
position: fixed;
bottom: 20px;
left: 20px;
width: 80px;
height: 80px;
background: rgba(255, 255, 255, 0.3);
border: 3px solid rgba(255, 255, 255, 0.5);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
color: white;
cursor: pointer;
user-select: none;
z-index: 100;
}
/* Joystick bas droite */
#joystickContainer {
position: fixed;
bottom: 20px;
right: 20px;
width: 120px;
height: 120px;
background: rgba(255, 255, 255, 0.2);
border: 3px solid rgba(255, 255, 255, 0.5);
border-radius: 50%;
z-index: 100;
}
#joystickKnob {
position: absolute;
width: 50px;
height: 50px;
background: rgba(255, 255, 255, 0.6);
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: none;
}
/* Bouton menu haut droite */
#menuButton {
position: fixed;
top: 20px;
right: 20px;
width: 50px;
height: 50px;
background: rgba(255, 255, 255, 0.3);
border: 2px solid rgba(255, 255, 255, 0.5);
border-radius: 8px;
cursor: pointer;
z-index: 100;
display: flex;
align-items: center;
justify-content: center;
}
#menuButton svg {
width: 30px;
height: 30px;
fill: white;
}
/* Menu pause */
#pauseMenu {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
padding: 30px;
background: rgba(211, 211, 211, 0.5);
border-radius: 20px;
display: none;
z-index: 200;
backdrop-filter: blur(10px);
}
#pauseMenu h2 {
margin: 0 0 20px 0;
text-align: center;
color: #333;
}
#pauseMenu label {
display: block;
margin-bottom: 10px;
color: #333;
}
#chunkRange {
width: 100%;
margin-bottom: 20px;
}
#resumeButton {
width: 100%;
padding: 10px;
background: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#resumeButton:hover {
background: #45a049;
}
</style>
</head>
<body>
<!-- Contrôles -->
<div id="moveButton">↑</div>
<div id="joystickContainer">
<div id="joystickKnob"></div>
</div>
<!-- Bouton menu -->
<div id="menuButton">
<svg viewBox="0 0 24 24">
<path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"/>
</svg>
</div>
<!-- Menu pause -->
<div id="pauseMenu">
<h2>Menu</h2>
<label for="chunkRange">Distance de chargement des chunks:</label>
<input type="range" id="chunkRange" min="2" max="8" value="4">
<span id="chunkValue">4</span>
<br><br>
<button id="resumeButton">Reprendre</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Variables globales
let scene, camera, renderer;
let player, playerVelocity = new THREE.Vector3();
let world = new Map();
let isPaused = false;
let isFirstPerson = true;
let chunkDistance = 4;
let tapCount = 0;
let lastTapTime = 0;
// Contrôles
let moveForward = false;
let joystickVector = new THREE.Vector2();
// Constantes
const CUBE_SIZE = 1;
const PLAYER_WIDTH = 0.6;
const PLAYER_HEIGHT = 1.6;
const PLAYER_DEPTH = 0.6;
const GRAVITY = -0.02;
const JUMP_FORCE = 0.3;
const MOVE_SPEED = 0.1;
// Initialisation Three.js
function init() {
// Scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
scene.fog = new THREE.Fog(0x87CEEB, 10, 100);
// Camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// Lumières
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(50, 100, 50);
directionalLight.castShadow = true;
directionalLight.shadow.camera.left = -50;
directionalLight.shadow.camera.right = 50;
directionalLight.shadow.camera.top = 50;
directionalLight.shadow.camera.bottom = -50;
scene.add(directionalLight);
// Joueur
const playerGeometry = new THREE.BoxGeometry(PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_DEPTH);
const playerMaterial = new THREE.MeshLambertMaterial({ color: 0x00ff00 });
player = new THREE.Mesh(playerGeometry, playerMaterial);
player.position.set(0, 10, 0);
player.castShadow = true;
scene.add(player);
// Génération du monde initial
generateWorld();
// Event listeners
setupControls();
window.addEventListener('resize', onWindowResize);
}
// Génération du monde
function generateWorld() {
const playerChunkX = Math.floor(player.position.x / 16);
const playerChunkZ = Math.floor(player.position.z / 16);
for (let x = -chunkDistance; x <= chunkDistance; x++) {
for (let z = -chunkDistance; z <= chunkDistance; z++) {
generateChunk(playerChunkX + x, playerChunkZ + z);
}
}
}
// Génération d'un chunk
function generateChunk(chunkX, chunkZ) {
const key = `${chunkX},${chunkZ}`;
if (world.has(key)) return;
const chunk = new THREE.Group();
for (let x = 0; x < 16; x++) {
for (let z = 0; z < 16; z++) {
const worldX = chunkX * 16 + x;
const worldZ = chunkZ * 16 + z;
// Génération de hauteur avec du bruit
const height = Math.floor(
5 +
Math.sin(worldX * 0.1) * 3 +
Math.cos(worldZ * 0.1) * 3 +
Math.sin(worldX * 0.05 + worldZ * 0.05) * 5
);
for (let y = 0; y < height; y++) {
const cube = createCube(worldX, y, worldZ);
chunk.add(cube);
}
}
}
scene.add(chunk);
world.set(key, chunk);
}
// Création d'un cube
function createCube(x, y, z) {
const group = new THREE.Group();
// Cube principal
const geometry = new THREE.BoxGeometry(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
const material = new THREE.MeshLambertMaterial({ color: 0x808080 });
const cube = new THREE.Mesh(geometry, material);
cube.castShadow = true;
cube.receiveShadow = true;
group.add(cube);
// Edges (bords sombres)
const edgesGeometry = new THREE.EdgesGeometry(geometry);
const edgesMaterial = new THREE.LineBasicMaterial({ color: 0x404040, linewidth: 2 });
const edges = new THREE.LineSegments(edgesGeometry, edgesMaterial);
group.add(edges);
group.position.set(x, y, z);
group.userData = { x, y, z };
return group;
}
// Contrôles
function setupControls() {
// Bouton de déplacement
const moveButton = document.getElementById('moveButton');
moveButton.addEventListener('touchstart', (e) => {
e.preventDefault();
moveForward = true;
});
moveButton.addEventListener('touchend', (e) => {
e.preventDefault();
moveForward = false;
});
moveButton.addEventListener('mousedown', () => moveForward = true);
moveButton.addEventListener('mouseup', () => moveForward = false);
// Joystick
const joystickContainer = document.getElementById('joystickContainer');
const joystickKnob = document.getElementById('joystickKnob');
let joystickActive = false;
function handleJoystick(clientX, clientY) {
const rect = joystickContainer.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const maxDistance = rect.width / 2 - 25;
let deltaX = clientX - centerX;
let deltaY = clientY - centerY;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance > maxDistance) {
deltaX = (deltaX / distance) * maxDistance;
deltaY = (deltaY / distance) * maxDistance;
}
joystickKnob.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
joystickVector.x = deltaX / maxDistance;
joystickVector.y = -deltaY / maxDistance;
}
joystickContainer.addEventListener('touchstart', (e) => {
e.preventDefault();
joystickActive = true;
const touch = e.touches[0];
handleJoystick(touch.clientX, touch.clientY);
});
joystickContainer.addEventListener('touchmove', (e) => {
e.preventDefault();
if (joystickActive) {
const touch = e.touches[0];
handleJoystick(touch.clientX, touch.clientY);
}
});
joystickContainer.addEventListener('touchend', (e) => {
e.preventDefault();
joystickActive = false;
joystickKnob.style.transform = 'translate(0, 0)';
joystickVector.set(0, 0);
});
// Triple tap pour changer de vue
renderer.domElement.addEventListener('touchstart', (e) => {
const currentTime = Date.now();
if (currentTime - lastTapTime < 300) {
tapCount++;
if (tapCount === 3) {
isFirstPerson = !isFirstPerson;
tapCount = 0;
}
} else {
tapCount = 1;
}
lastTapTime = currentTime;
});
// Menu
document.getElementById('menuButton').addEventListener('click', () => {
isPaused = true;
document.getElementById('pauseMenu').style.display = 'block';
});
document.getElementById('resumeButton').addEventListener('click', () => {
isPaused = false;
document.getElementById('pauseMenu').style.display = 'none';
});
const chunkRange = document.getElementById('chunkRange');
const chunkValue = document.getElementById('chunkValue');
chunkRange.addEventListener('input', (e) => {
chunkDistance = parseInt(e.target.value);
chunkValue.textContent = chunkDistance;
});
}
// Détection de collision
function checkCollision(position) {
const playerBox = new THREE.Box3().setFromCenterAndSize(
position,
new THREE.Vector3(PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_DEPTH)
);
// Vérifier les cubes proches
for (let x = -2; x <= 2; x++) {
for (let y = -2; y <= 2; y++) {
for (let z = -2; z <= 2; z++) {
const checkX = Math.floor(position.x) + x;
const checkY = Math.floor(position.y) + y;
const checkZ = Math.floor(position.z) + z;
const cubeBox = new THREE.Box3(
new THREE.Vector3(checkX - 0.5, checkY - 0.5, checkZ - 0.5),
new THREE.Vector3(checkX + 0.5, checkY + 0.5, checkZ + 0.5)
);