<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>CITY LIFE 3D LEGENDARY</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
overflow: hidden;
background: #87ceeb;
font-family: Arial;
touch-action: none;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: #111d;
padding: 12px;
border-radius: 12px;
z-index: 5;
font-weight: bold;
line-height: 1.9;
}
#info span {
color: #ffda36;
}
#controls {
position: absolute;
bottom: 15px;
width: 100%;
display: flex;
justify-content: space-between;
padding: 0 10px;
z-index: 5;
pointer-events: none;
}
.group {
display: flex;
gap: 7px;
align-items: end;
}
button {
pointer-events: auto;
border: 2px solid #ffffff88;
background: #111d;
color: white;
font-size: 23px;
font-weight: bold;
border-radius: 14px;
padding: 13px;
min-width: 52px;
touch-action: none;
}
#enter {
background: #087fca;
font-size: 15px;
}
#horn {
background: #a44b00;
font-size: 16px;
}
#sound {
position: absolute;
right: 10px;
top: 10px;
z-index: 6;
}
#notice {
position: absolute;
top: 38%;
left: 50%;
transform: translate(-50%, -50%);
color: #ffdf28;
font-size: 34px;
font-weight: bold;
text-shadow: 2px 3px 5px black;
opacity: 0;
pointer-events: none;
z-index: 8;
}
</style>
</head>
<body>
<div id="info">
🏙️ CITY LIFE 3D<br />
<span id="mode">الوضع: المشي</span><br />
<span id="speed">السرعة: 0</span><br />
🪙 العملات: <span id="coins">0</span>
</div>
<button id="sound">🔊 الصوت</button>
<div id="notice">+1 🪙</div>
<div id="controls">
<div class="group">
<button id="left">⬅️</button>
<button id="right">➡️</button>
</div>
<div class="group">
<button id="back">⬇️</button>
<button id="forward">⬆️</button>
<button id="horn">📯</button>
<button id="enter">🚘 دخول</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
if (!window.THREE) {
document.body.innerHTML = "تعذر تحميل Three.js. تأكد من اتصال الإنترنت.";
throw new Error("Three.js missing");
}
// ======================================
// 1. CITY WORLD
// ======================================
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
scene.fog = new THREE.Fog(0x87ceeb, 110, 360);
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.1, 600);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xffffff, 0x56606b, 1.8));
const sun = new THREE.DirectionalLight(0xffffff, 2.2);
sun.position.set(50, 100, 50);
sun.castShadow = true;
scene.add(sun);
// Ground
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(400, 400),
new THREE.MeshStandardMaterial({ color: 0x398c42 }),
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.1;
scene.add(ground);
// Roads
const roadMat = new THREE.MeshStandardMaterial({
color: 0x292d32,
roughness: 0.95,
});
function road(x, z, w, d) {
const r = new THREE.Mesh(new THREE.BoxGeometry(w, 0.12, d), roadMat);
r.position.set(x, 0, z);
scene.add(r);
}
for (let i = -160; i <= 160; i += 80) {
road(i, 0, 14, 400);
road(0, i, 400, 14);
}
// Road lines
const lineMat = new THREE.MeshBasicMaterial({ color: 0xffffff });
for (let i = -180; i < 180; i += 20) {
for (let j = -160; j <= 160; j += 80) {
const l = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.035, 7), lineMat);
l.position.set(i, 0.08, j);
scene.add(l);
}
}
// Sidewalks
const sidewalkMat = new THREE.MeshStandardMaterial({
color: 0x999999,
});
function sidewalk(x, z, w, d) {
const s = new THREE.Mesh(new THREE.BoxGeometry(w, 0.2, d), sidewalkMat);
s.position.set(x, 0.04, z);
scene.add(s);
}
for (let i = -160; i <= 160; i += 80) {
sidewalk(i + 11, 0, 4, 400);
sidewalk(i - 11, 0, 4, 400);
sidewalk(0, i + 11, 400, 4);
sidewalk(0, i - 11, 400, 4);
}
// ======================================
// 2. BUILDINGS
// ======================================
const buildingColors = [
0xd5d5d5, 0xc6b6a2, 0xb2c8db, 0xd5b7a0, 0x9999aa, 0xe5d9c7, 0x777f91, 0xc5d0d8,
];
const windowMat = new THREE.MeshStandardMaterial({
color: 0x74c9ec,
metalness: 0.3,
roughness: 0.25,
});
function building(x, z) {
const h = 12 + Math.random() * 35;
const w = 9 + Math.random() * 8;
const d = 9 + Math.random() * 8;
const b = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
new THREE.MeshStandardMaterial({
color: buildingColors[Math.floor(Math.random() * buildingColors.length)],
}),
);
b.position.set(x, h / 2, z);
b.castShadow = true;
b.receiveShadow = true;
scene.add(b);
// Windows on front
for (let y = 3; y < h - 2; y += 5) {
for (let xx = -w / 2 + 2; xx < w / 2 - 1; xx += 3) {
const win = new THREE.Mesh(new THREE.BoxGeometry(1.3, 2, 0.12), windowMat);
win.position.set(x + xx, y, z - d / 2 - 0.08);
scene.add(win);
}
}
// Roof
const roof = new THREE.Mesh(
new THREE.BoxGeometry(w + 0.4, 0.3, d + 0.4),
new THREE.MeshStandardMaterial({ color: 0x555b65 }),
);
roof.position.set(x, h + 0.15, z);
scene.add(roof);
}
for (let x = -140; x <= 140; x += 40) {
for (let z = -140; z <= 140; z += 40) {
if (Math.abs(x) % 80 < 10 || Math.abs(z) % 80 < 10) continue;
building(x, z);
}
}
// ======================================
// 3. NEW SPORTS CAR
// ======================================
const car = new THREE.Group();
function carPart(w, h, d, color, x, y, z, metal = 0, rough = 0.35) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
new THREE.MeshStandardMaterial({
color,
metalness: metal,
roughness: rough,
}),
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
car.add(mesh);
return mesh;
}
// Main chassis
carPart(2.65, 0.55, 5.3, 0x0757dc, 0, 0.82, 0, 0.65);
carPart(2.5, 0.32, 2.1, 0x087bff, 0, 1.12, 1.55, 0.7);
carPart(2.45, 0.3, 1.3, 0x0750bd, 0, 1.1, -2.0, 0.6);
// Cabin
carPart(1.95, 0.8, 2.5, 0x092c57, 0, 1.68, -0.35, 0.4);
// Windshield
const glassMat = new THREE.MeshStandardMaterial({
color: 0x61c9ed,
metalness: 0.55,
roughness: 0.12,
transparent: true,
opacity: 0.8,
});
function glass(w, h, d, x, y, z, rx = 0, ry = 0) {
const m = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), glassMat);
m.position.set(x, y, z);
m.rotation.x = rx;
m.rotation.y = ry;
car.add(m);
}
glass(1.85, 0.58, 0.12, 0, 1.75, 0.95, -0.32);
glass(1.85, 0.58, 0.12, 0, 1.75, -1.65, 0.32);
// Side windows
glass(0.12, 0.5, 1.55, -1.01, 1.77, -0.3);
glass(0.12, 0.5, 1.55, 1.01, 1.77, -0.3);
// Mirrors
carPart(0.3, 0.18, 0.35, 0x071c36, -1.35, 1.65, 0.65);
carPart(0.3, 0.18, 0.35, 0x071c36, 1.35, 1.65, 0.65);
// Front grille
carPart(1.0, 0.22, 0.1, 0x111820, 0, 0.75, 2.69);
// Headlights
const headMat = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0xffffff,
emissiveIntensity: 1.5,
});
for (const x of [-0.9, 0.9]) {
const light = new THREE.Mesh(new THREE.BoxGeometry(0.65, 0.18, 0.1), headMat);
light.position.set(x, 1.12, 2.68);
car.add(light);
}
// Rear lights
const rearMat = new THREE.MeshStandardMaterial({
color: 0xff1010,
emissive: 0xff0000,
emissiveIntensity: 1.2,
});
for (const x of [-0.9, 0.9]) {
const light = new THREE.Mesh(new THREE.BoxGeometry(0.65, 0.18, 0.1), rearMat);
light.position.set(x, 1.1, -2.68);
car.add(light);
}
// Sport spoiler
carPart(0.16, 0.55, 0.16, 0x111820, -0.8, 1.45, -2.4);
carPart(0.16, 0.55, 0.16, 0x111820, 0.8, 1.45, -2.4);
carPart(2.5, 0.12, 0.55, 0x071c36, 0, 1.75, -2.4);
// Wheels and metallic rims
const wheels = [];
for (const x of [-1.35, 1.35]) {
for (const z of [-1.75, 1.75]) {
const wheelGroup = new THREE.Group();
const tire = new THREE.Mesh(
new THREE.CylinderGeometry(0.5, 0.5, 0.34, 24),
new THREE.MeshStandardMaterial({
color: 0x111111,
roughness: 0.95,
}),
);
tire.rotation.z = Math.PI / 2;
wheelGroup.add(tire);
const rim = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.29, 0.36, 20),
new THREE.MeshStandardMaterial({
color: 0xbfc9d4,
metalness: 0.95,
roughness: 0.2,
}),
);
rim.rotation.z = Math.PI / 2;
wheelGroup.add(rim);
// Hub
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.09, 0.38, 12),
new THREE.MeshStandardMaterial({ color: 0x333333, metalness: 0.8 }),
);
hub.rotation.z = Math.PI / 2;
wheelGroup.add(hub);
wheelGroup.position.set(x, 0.5, z);
car.add(wheelGroup);
wheels.push(wheelGroup);
}
}
// Spoiler details and bumpers
carPart(2.7, 0.22, 0.18, 0x111820, 0, 0.52, 2.75);
carPart(2.7, 0.22, 0.18, 0x111820, 0, 0.52, -2.75);
car.position.set(4, 0, 0);
scene.add(car);
// ======================================
// 4. PLAYER
// ======================================
const player = new THREE.Group();
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 1.2, 0.45),
new THREE.MeshStandardMaterial({ color: 0xff3333 }),
);
body.position.y = 1.2;
player.add(body);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.4, 16, 16),
new THREE.MeshStandardMaterial({ color: 0xffc69a }),
);
head.position.y = 2.05;
player.add(head);
player.position.set(0, 0, 5);
scene.add(player);
// ======================================
// 5. GOLD COINS
// ======================================
let coins = [];
let collected = 0;
const goldMat = new THREE.MeshStandardMaterial({
color: 0xffc400,
metalness: 0.85,
roughness: 0.18,
emissive: 0x805000,
emissiveIntensity: 0.3,
});
const coinGeo = new THREE.TorusGeometry(0.5, 0.14, 12, 24);
function createCoin(x, z) {
const coin = new THREE.Mesh(coinGeo, goldMat);
coin.position.set(x, 1.4, z);
coin.rotation.x = Math.PI / 2;
scene.add(coin);
coins.push(coin);
}
// Spread coins on roads
for (let r = -160; r <= 160; r += 80) {
for (let i = -150; i <= 150; i += 20) {
createCoin(r + 3, i);
createCoin(i, r - 3);
}
}
let noticeTimeout;
function showCoin() {
const notice = document.getElementById("notice");
notice.textContent = "+1 🪙";
notice.style.opacity = 1;
clearTimeout(noticeTimeout);
noticeTimeout = setTimeout(() => {
notice.style.opacity = 0;
}, 650);
}
function collectCoins(target) {
for (let i = coins.length - 1; i >= 0; i--) {
const coin = coins[i];
const dx = target.position.x - coin.position.x;
const dz = target.position.z - coin.position.z;
if (dx * dx + dz * dz < 5) {
scene.remove(coin);
coins.splice(i, 1);
collected++;
document.getElementById("coins").textContent = collected;
showCoin();
playCoinSound();
}
}
}
function animateCoins(time) {
coins.forEach((coin) => {
coin.rotation.z += 0.045;
coin.position.y = 1.4 + Math.sin(time * 0.003 + coin.position.x) * 0.18;
});
}
// ======================================
// 6. AUDIO SYSTEM
// ======================================
let audio = null;
let engine = null;
let engine2 = null;
let engineGain = null;
let engineFilter = null;
let noiseGain = null;
let soundEnabled = true;
function startAudio() {
if (audio) {
if (audio.state === "suspended") audio.resume();
return;
}
audio = new (window.AudioContext || window.webkitAudioContext)();
// Engine oscillators
engine = audio.createOscillator();
engine.type = "sawtooth";
engine2 = audio.createOscillator();
engine2.type = "triangle";
engineFilter = audio.createBiquadFilter();
engineFilter.type = "lowpass";
engineFilter.frequency.value = 350;
engineGain = audio.createGain();
engineGain.gain.value = 0;
engine.connect(engineFilter);
engine2.connect(engineFilter);
engineFilter.connect(engineGain);
engineGain.connect(audio.destination);
engine.start();
engine2.start();
// Low rumbling noise
const buffer = audio.createBuffer(1, audio.sampleRate * 2, audio.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < data.length; i++) {
data[i] = Math.random() * 2 - 1;
}
const noise = audio.createBufferSource();
noise.buffer = buffer;
noise.loop = true;
const noiseFilter = audio.createBiquadFilter();
noiseFilter.type = "lowpass";
noiseFilter.frequency.value = 130;
noiseGain = audio.createGain();
noiseGain.gain.value = 0;
noise.connect(noiseFilter);
noiseFilter.connect(noiseGain);
noiseGain.connect(audio.destination);
noise.start();
// Engine vibration
const lfo = audio.createOscillator();
const lfoGain = audio.createGain();
lfo.frequency.value = 22;
lfoGain.gain.value = 0.009;
lfo.connect(lfoGain);
lfoGain.connect(engineGain.gain);
lfo.start();
}
// Short sound effects
function playSound(freq, duration, volume = 0.1) {
if (!soundEnabled) return;
startAudio();
const osc = audio.createOscillator();
const gain = audio.createGain();
osc.type = "sine";
osc.frequency.setValueAtTime(freq, audio.currentTime);
gain.gain.setValueAtTime(volume, audio.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audio.currentTime + duration);
osc.connect(gain);
gain.connect(audio.destination);
osc.start();
osc.stop(audio.currentTime + duration);
}
// Engine changes with speed
function updateEngine() {
if (!audio || !engine) return;
const active = inCar && soundEnabled;
const rpm = Math.abs(carSpeed);
engine.frequency.setTargetAtTime(45 + rpm * 3.2, audio.currentTime, 0.08);
engine2.frequency.setTargetAtTime(90 + rpm * 5, audio.currentTime, 0.08);
engineGain.gain.setTargetAtTime(active ? 0.085 : 0, audio.currentTime, 0.15);
noiseGain.gain.setTargetAtTime(active ? 0.025 : 0, audio.currentTime, 0.15);
engineFilter.frequency.setTargetAtTime(250 + rpm * 32, audio.currentTime, 0.12);
}
// Coin sound
function playCoinSound() {
if (!soundEnabled) return;
startAudio();
[660, 880, 1100].forEach((freq, i) => {
const osc = audio.createOscillator();
const gain = audio.createGain();
const start = audio.currentTime + i * 0.055;
osc.frequency.value = freq;
gain.gain.setValueAtTime(0.12, start);
gain.gain.exponentialRampToValueAtTime(0.001, start + 0.2);
osc.connect(gain);
gain.connect(audio.destination);
osc.start(start);
osc.stop(start + 0.22);
});
}
// Horn
function hornSound() {
if (!soundEnabled) return;
startAudio();
const o1 = audio.createOscillator();
const o2 = audio.createOscillator();
const gain = audio.createGain();
o1.type = "square";
o2.type = "square";
o1.frequency.value = 350;
o2.frequency.value = 440;
gain.gain.setValueAtTime(0.1, audio.currentTime);
gain.gain.exponentialRampToValueAtTime(0.001, audio.currentTime + 0.65);
o1.connect(gain);
o2.connect(gain);
gain.connect(audio.destination);
o1.start();
o2.start();
o1.stop(audio.currentTime + 0.7);
o2.stop(audio.currentTime + 0.7);
}
// Sound toggle
document.getElementById("sound").onclick = () => {
soundEnabled = !soundEnabled;
document.getElementById("sound").textContent = soundEnabled ? "🔊 الصوت" : "🔇 صامت";
if (soundEnabled) {
startAudio();
playSound(500, 0.15);
} else if (engineGain) {
engineGain.gain.setTargetAtTime(0, audio.currentTime, 0.1);
noiseGain.gain.setTargetAtTime(0, audio.currentTime, 0.1);
}
};
// ======================================
// 7. CONTROLS
// ======================================
let inCar = false;
let angle = 0;
let carSpeed = 0;
let keys = {};
let footTimer = 0;
function press(id, key) {
const el = document.getElementById(id);
el.addEventListener("pointerdown", (e) => {
e.preventDefault();
keys[key] = true;
startAudio();
});
["pointerup", "pointerleave", "pointercancel"].forEach((ev) => {
el.addEventListener(ev, () => {
keys[key] = false;
});
});
}
press("forward", "forward");
press("back", "back");
press("left", "left");
press("right", "right");
document.addEventListener("keydown", (e) => {
keys[e.key.toLowerCase()] = true;
startAudio();
if (e.key.toLowerCase() === "e") toggleCar();
if (e.key.toLowerCase() === "m") hornSound();
});
document.addEventListener("keyup", (e) => {
keys[e.key.toLowerCase()] = false;
});
// ======================================
// 8. ENTER AND EXIT CAR
// ======================================
function toggleCar() {
startAudio();
if (!inCar) {
if (player.position.distanceTo(car.position) < 7) {
inCar = true;
player.visible = false;
document.getElementById("mode").textContent = "الوضع: قيادة السيارة";
document.getElementById("enter").textContent = "🚶 خروج";
playSound(180, 0.3);
}
} else {
inCar = false;
player.visible = true;
player.position.copy(car.position);
player.position.x += 3;
carSpeed = 0;
document.getElementById("mode").textContent = "الوضع: المشي";
document.getElementById("enter").textContent = "🚘 دخول";
playSound(280, 0.3);
}
}
document.getElementById("enter").onclick = toggleCar;
document.getElementById("horn").onclick = hornSound;
// ======================================
// 9. MOVEMENT
// ======================================
function update(dt) {
const forward = keys.forward || keys.arrowup || keys.w;
const back = keys.back || keys.arrowdown || keys.s;
const left = keys.left || keys.arrowleft || keys.a;
const right = keys.right || keys.arrowright || keys.d;
if (inCar) {
if (forward) carSpeed = Math.min(carSpeed + 20 * dt, 35);
else if (back) carSpeed = Math.max(carSpeed - 25 * dt, -12);
else carSpeed *= 0.98;
if (left) angle += 2 * dt * (carSpeed >= 0 ? 1 : -1);
if (right) angle -= 2 * dt * (carSpeed >= 0 ? 1 : -1);
car.rotation.y = angle;
car.position.x += Math.sin(angle) * carSpeed * dt;
car.position.z += Math.cos(angle) * carSpeed * dt;
wheels.forEach((w) => {
w.rotation.x += carSpeed * dt * 0.5;
});
document.getElementById("speed").textContent =
"السرعة: " + Math.round(Math.abs(carSpeed));
} else {
const move = (forward ? 1 : 0) - (back ? 1 : 0);
const turn = (left ? 1 : 0) - (right ? 1 : 0);
if (move !== 0) {
angle += turn * dt * 3;
player.position.x += Math.sin(angle) * move * 12 * dt;
player.position.z += Math.cos(angle) * move * 12 * dt;
player.rotation.y = angle;
footTimer += dt;
if (footTimer > 0.4) {
playSound(90, 0.07, 0.035);
footTimer = 0;
}
} else {
footTimer = 0;
}
document.getElementById("speed").textContent = "السرعة: مشي";
}
// World limits
const target = inCar ? car : player;
target.position.x = THREE.MathUtils.clamp(target.position.x, -185, 185);
target.position.z = THREE.MathUtils.clamp(target.position.z, -185, 185);
// Collect coins
collectCoins(target);
}
// ======================================
// 10. CAMERA
// ======================================
const offset = new THREE.Vector3(0, 7, -12);
let lastTime = 0;
function animate(time) {
requestAnimationFrame(animate);
const dt = Math.min((time - lastTime) / 1000, 0.05);
lastTime = time;
update(dt);
updateEngine();
animateCoins(time);
const target = inCar ? car : player;
const cameraOffset = offset.clone();
if (inCar) cameraOffset.set(0, 8, -15);
cameraOffset.applyAxisAngle(new THREE.Vector3(0, 1, 0), angle);
const desired = target.position.clone().add(cameraOffset);
camera.position.lerp(desired, 0.09);
camera.lookAt(target.position.x, target.position.y + 1, target.position.z);
renderer.render(scene, camera);
}
animate(0);
// ======================================
// 11. RESIZE
// ======================================
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>