<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Jeu Minecraft Style</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
touch-action: none;
}
canvas { display: block; }
#ui {
position: fixed;
top: 0; left: 0;
width: 100%; height: 100%;
pointer-events: none;
z-index: 100;
}
.control {
pointer-events: auto;
position: absolute;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
user-select: none;
-webkit-user-select: none;
}
#joystick {
bottom: 30px; left: 30px;
width: 150px;
height: 150px;
border: 3px solid rgba(255, 255, 255, 0.5);
}
#joystick-handle {
width: 80px;
height: 80px;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 50%;
}
#jump-button {
bottom: 30px; right: 30px;
width: 120px;
height: 120px;
border: 3px solid rgba(255, 255, 255, 0.5);
}
#jump-button span {
font-size: 40px;
color: white;
font-weight: bold;
}
#menu-button {
top: 30px; right: 30px;
width: 60px;
height: 60px;
border-radius: 10px;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
padding: 10px;
}
#menu-button .bar {
width: 80%;
height: 5px;
background-color: white;
border-radius: 2px;
}
#menu-panel {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
width: 300px;
padding: 20px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
border-radius: 10px;
display: none;
flex-direction: column;
gap: 20px;
pointer-events: auto;
}
#menu-panel label {
display: flex;
flex-direction: column;
gap: 5px;
}
#menu-panel button {
padding: 15px;
font-size: 18px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#menu-panel button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="ui">
<div id="joystick" class="control">
<div id="joystick-handle"></div>
</div>
<div id="jump-button" class="control">
<span>SAUT</span>
</div>
<div id="menu-button" class="control">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<div id="menu-panel">
<h2>Paramètres</h2>
<label>
Distance de rendu
<input type="range" id="chunk-range" min="1" max="5" value="3">
<span id="chunk-value">3 chunks</span>
</label>
<button id="view-toggle">Vue à la 1ère personne</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
const CHUNK_SIZE = 16;
const BLOCK_SIZE = 1;
const PLAYER_HEIGHT = 1.8;
const PLAYER_WIDTH = 0.6;
const JUMP_FORCE = 0.13;
const GRAVITY = 0.008;
const MOVEMENT_SPEED = 0.06;
const CAMERA_SENSITIVITY = 0.005;
let renderDistance = 3;
let isFirstPerson = true;
let isMenuOpen = false;
let scene, camera, renderer;
let clock = new THREE.Clock();
let player = {
mesh: null,
velocity: new THREE.Vector3(),
isGrounded: false,
rotation: { y: 0, x: 0 },
cameraOffset: new THREE.Vector3(0, PLAYER_HEIGHT * 0.5, 0)
};
const ui = {
joystick: document.getElementById('joystick'),
joystickHandle: document.getElementById('joystick-handle'),
jumpButton: document.getElementById('jump-button'),
menuButton: document.getElementById('menu-button'),
menuPanel: document.getElementById('menu-panel'),
chunkRange: document.getElementById('chunk-range'),
chunkValue: document.getElementById('chunk-value'),
viewToggle: document.getElementById('view-toggle')
};
let controls = {
joystick: { active: false, x: 0, y: 0 },
isJumping: false,
look: { x: 0, y: 0 }
};
let chunks = new Map();
const materialNormal = new THREE.MeshLambertMaterial({ color: 0x888888 });
const materialBorder = new THREE.MeshLambertMaterial({ color: 0x555555 });
const playerMaterial = new THREE.MeshLambertMaterial({ color: 0x00FF00 });
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 5);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
scene.add(new THREE.AmbientLight(0x404040));
const directional = new THREE.DirectionalLight(0xffffff, 1);
directional.position.set(1, 1, 1).normalize();
scene.add(directional);
const geometry = new THREE.BoxGeometry(PLAYER_WIDTH, PLAYER_HEIGHT, PLAYER_WIDTH);
player.mesh = new THREE.Mesh(geometry, playerMaterial);
player.mesh.position.y = 10;
scene.add(player.mesh);
setupControls();
window.addEventListener('resize', onWindowResize, false);
onWindowResize();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
const noise = {
gradients: {},
randomGradient(x, y) {
const theta = Math.random() * 2 * Math.PI;
return { x: Math.cos(theta), y: Math.sin(theta) };
},
dotGridGradient(ix, iy, x, y) {
const key = `${ix},${iy}`;
if (!this.gradients[key])
this.gradients[key] = this.randomGradient(ix, iy);
const gradient = this.gradients[key];
const dx = x - ix;
const dy = y - iy;
return (dx * gradient.x + dy * gradient.y);
},
smootherStep(t) {
return t * t * t * (t * (t * 6 - 15) + 10);
},
interpolate(a0, a1, w) {
const t = this.smootherStep(w);
return a0 + t * (a1 - a0);
},
get(x, y) {
const x0 = Math.floor(x), x1 = x0 + 1;
const y0 = Math.floor(y), y1 = y0 + 1;
const sx = x - x0, sy = y - y0;
const n0 = this.dotGridGradient(x0, y0, x, y);
const n1 = this.dotGridGradient(x1, y0, x, y);
const ix0 = this.interpolate(n0, n1, sx);
const n2 = this.dotGridGradient(x0, y1, x, y);
const n3 = this.dotGridGradient(x1, y1, x, y);
const ix1 = this.interpolate(n2, n3, sx);
return this.interpolate(ix0, ix1, sy);
}
};
function generateChunk(chunkX, chunkZ) {
const key = `${chunkX},${chunkZ}`;
if (chunks.has(key)) return;
const group = new THREE.Group();
const startX = chunkX * CHUNK_SIZE;
const startZ = chunkZ * CHUNK_SIZE;
for (let x = 0; x < CHUNK_SIZE; x++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const worldX = startX + x;
const worldZ = startZ + z;
let height = Math.floor(noise.get(worldX / 10, worldZ / 10) * 5) + 3;
if (height < 1) height = 1;
for (let y = 0; y < height; y++) {
const cubeGeom = new THREE.BoxGeometry(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
let mat = materialNormal;
if (x === 0 || x === CHUNK_SIZE - 1 || z === 0 || z === CHUNK_SIZE - 1)
mat = materialBorder;
const cube = new THREE.Mesh(cubeGeom, mat);
cube.position.set(worldX, y * BLOCK_SIZE, worldZ);
group.add(cube);
}
}
}
group.position.set(startX, 0, startZ);
scene.add(group);
chunks.set(key, group);
}
function updateChunks() {
const playerChunkX = Math.floor(player.mesh.position.x / CHUNK_SIZE);
const playerChunkZ = Math.floor(player.mesh.position.z / CHUNK_SIZE);
for (let x = playerChunkX - renderDistance; x <= playerChunkX + renderDistance; x++) {
for (let z = playerChunkZ - renderDistance; z <= playerChunkZ + renderDistance; z++) {
generateChunk(x, z);
}
}
for (let [key, group] of chunks.entries()) {
const [cx, cz] = key.split(',').map(Number);
if (Math.abs(cx - playerChunkX) > renderDistance + 1 ||
Math.abs(cz - playerChunkZ) > renderDistance + 1) {
scene.remove(group);
chunks.delete(key);
}
}
}
function setupControls() {
ui.joystick.addEventListener('touchstart', onJoystickStart, { passive: false });
ui.joystick.addEventListener('touchmove', onJoystickMove, { passive: false });
ui.joystick.addEventListener('touchend', onJoystickEnd, { passive: false });
renderer.domElement.addEventListener('touchstart', onCameraLookStart, { passive: false });
renderer.domElement.addEventListener('touchmove', onCameraLookMove, { passive: false });
renderer.domElement.addEventListener('touchend', onCameraLookEnd, { passive: false });
ui.jumpButton.addEventListener('touchstart', (e) => {
e.preventDefault();
controls.isJumping = true;
}, { passive: false });
ui.jumpButton.addEventListener('touchend', (e) => {
e.preventDefault();
controls.isJumping = false;
}, { passive: false });
ui.menuButton.addEventListener('click', () => {
isMenuOpen = !isMenuOpen;
ui.menuPanel.style.display = isMenuOpen ? 'flex' : 'none';
});
ui.chunkRange.addEventListener('input', (e) => {
renderDistance = parseInt(e.target.value);
ui.chunkValue.textContent = `${renderDistance} chunks`;
});
ui.viewToggle.addEventListener('click', () => {
isFirstPerson = !isFirstPerson;
ui.viewToggle.textContent = isFirstPerson ? "Vue à la 1ère personne" : "Vue à la 3ème personne";
});
}
let lastTouch = { x: 0, y: 0 };
function onCameraLookStart(e) {
if (e.target.closest('#ui')) return;
e.preventDefault();
lastTouch.x = e.touches[0].clientX;
lastTouch.y = e.touches[0].clientY;
}
function onCameraLookMove(e) {
if (e.target.closest('#ui')) return;
e.preventDefault();
const touch = e.touches[0];
const dx = touch.clientX - lastTouch.x;
const dy = touch.clientY - lastTouch.y;
player.rotation.y -= dx * CAMERA_SENSITIVITY;
player.rotation.x -= dy * CAMERA_SENSITIVITY;
player.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, player.rotation.x));
lastTouch.x = touch.clientX;
lastTouch.y = touch.clientY;
}
function onCameraLookEnd(e) {}
function onJoystickStart(e) {
e.preventDefault();
controls.joystick.active = true;
}
function onJoystickMove(e) {
e.preventDefault();
if (!controls.joystick.active) return;
const touch = e.touches[0];
const rect = ui.joystick.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const dx = touch.clientX - centerX;
const dy = touch.clientY - centerY;
const distance = Math.min(rect.width/2, Math.hypot(dx, dy));
const angle = Math.atan2(dy, dx);
ui.joystickHandle.style.transform = `translate(${Math.cos(angle)*distance}px, ${Math.sin(angle)*distance}px)`;
controls.joystick.x = Math.cos(angle) * (distance / (rect.width/2));
controls.joystick.y = -Math.sin(angle) * (distance / (rect.height/2));
}
function onJoystickEnd(e) {
e.preventDefault();
controls.joystick.active = false;
controls.joystick.x = 0;
controls.joystick.y = 0;
ui.joystickHandle.style.transform = 'translate(0, 0)';
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
if (!isMenuOpen) {
updatePlayer(delta);
updateChunks();
updateCamera();
}
renderer.render(scene, camera);
}
function updatePlayer(delta) {
player.velocity.y -= GRAVITY;
const forward = new THREE.Vector3(0, 0, -1).applyAxisAngle(new THREE.Vector3(0, 1, 0), player.rotation.y);
const right = new THREE.Vector3(1, 0, 0).applyAxisAngle(new THREE.Vector3(0, 1, 0), player.rotation.y);
const moveDir = new THREE.Vector3();
moveDir.add(forward.multiplyScalar(controls.joystick.y));
moveDir.add(right.multiplyScalar(controls.joystick.x));
if (moveDir.length() > 0) {
moveDir.normalize();
player.velocity.x = moveDir.x * MOVEMENT_SPEED * delta * 60;
player.velocity.z = moveDir.z * MOVEMENT_SPEED * delta * 60;
} else {
player.velocity.x = 0;
player.velocity.z = 0;
}
if (controls.isJumping && player.isGrounded) {
player.velocity.y = JUMP_FORCE;
}
const nextPos = player.mesh.position.clone().add(player.velocity);
const playerBox = new THREE.Box3(
new THREE.Vector3(nextPos.x - PLAYER_WIDTH/2, nextPos.y - PLAYER_HEIGHT/2, nextPos.z - PLAYER_WIDTH/2),
new THREE.Vector3(nextPos.x + PLAYER_WIDTH/2, nextPos.y + PLAYER_HEIGHT/2, nextPos.z + PLAYER_WIDTH/2)
);
player.isGrounded = false;
chunks.forEach(chunk => {
chunk.children.forEach(cube => {
const cubeBox = new THREE.Box3().setFromObject(cube);
if (playerBox.intersectsBox(cubeBox)) {
if (player.velocity.y < 0 && (player.mesh.position.y - PLAYER_HEIGHT/2) >= cubeBox.max.y - 0.1) {
nextPos.y = cubeBox.max.y + PLAYER_HEIGHT/2;
player.velocity.y = 0;
player.isGrounded = true;
}
if (player.velocity.y > 0 && (player.mesh.position.y + PLAYER_HEIGHT/2) <= cubeBox.min.y + 0.1) {
nextPos.y = cubeBox.min.y - PLAYER_HEIGHT/2;
player.velocity.y = 0;
}
if ((nextPos.x > cubeBox.min.x && nextPos.x < cubeBox.max.x) &&
(nextPos.z > cubeBox.min.z && nextPos.z < cubeBox.max.z)) {
player.velocity.x = 0;
player.velocity.z = 0;
}
}
});
});
player.mesh.position.add(player.velocity);
if (player.mesh.position.y < -50) {
player.mesh.position.set(0, 20, 0);
}
}
function updateCamera() {
if (isFirstPerson) {
camera.position.copy(player.mesh.position).add(new THREE.Vector3(0, 0.5, 0));
} else {
const offset = new THREE.Vector3(0, 2, 5);
offset.applyAxisAngle(new THREE.Vector3(0, 1, 0), player.rotation.y);
camera.position.copy(player.mesh.position).add(offset);
}
const lookDir = new THREE.Vector3(0, 0, -1);
lookDir.applyEuler(new THREE.Euler(player.rotation.x, player.rotation.y, 0, 'YXZ'));
camera.lookAt(player.mesh.position.clone().add(lookDir));
}
init();
animate();
</script>
</body>
</html>