🎮ArcadeLab

VICE CITY: UNDERWORLD

by CyberGlider76
524 lines11.3 KB🛠️ Three.js (3D graphics)
▶ Play

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>VICE CITY: UNDERWORLD</title>
<style>
* { box-sizing:border-box }
body {
 margin:0; overflow:hidden;
 background:#111; font-family:Arial,sans-serif;
 color:white;
}
canvas { display:block }
#hud {
 position:fixed; inset:0; pointer-events:none;
}
#logo {
 position:absolute; top:20px; left:25px;
 font-size:25px; font-weight:900;
 color:#00d9ff; text-shadow:0 0 15px #00aaff;
}
#help {
 position:absolute; bottom:20px; left:20px;
 background:#000b; padding:12px 16px;
 border-radius:8px; line-height:1.7;
 font-size:13px;
}
#status {
 position:absolute; top:20px; right:25px;
 background:#000b; padding:12px 18px;
 border-radius:8px; text-align:right;
}
#speed { font-size:26px; font-weight:bold;color:#00eaff }
#map {
 position:absolute; right:20px; bottom:20px;
 width:145px;height:145px;
 border:2px solid #00cfff;
 border-radius:50%; background:#111;
}
#message {
 position:absolute; top:48%; width:100%;
 text-align:center; font-size:20px;
 text-shadow:0 2px 5px black;
}
#start {
 position:fixed; inset:0; z-index:5;
 background:linear-gradient(#07111ddd,#101c30ee);
 display:flex; flex-direction:column;
 align-items:center; justify-content:center;
 text-align:center;
}
#start h1 {
 font-size:clamp(35px,7vw,75px);
 color:#00d9ff; margin:0;
 text-shadow:0 0 25px #0077ff;
}
#start p {color:#ddd}
button {
 background:#00bde8; border:0;
 padding:15px 35px; border-radius:8px;
 color:#001018; font-weight:bold;
 font-size:17px; cursor:pointer;
}
</style>
</head>
<body>

<div id="start">
 <h1>VICE CITY</h1>
 <h2>UNDERWORLD</h2>
 <p>Explore the city. Drive. Discover.</p>
 <button id="play">JUGAR</button>
 <p>WASD para moverte · E para conducir</p>
</div>

<div id="hud">
 <div id="logo">VICE CITY</div>
 <div id="status">
  <div id="mode">A PIE</div>
  <div id="speed">0 KM/H</div>
  <div id="money">$ 2,500</div>
 </div>
 <div id="help">
  <b>CONTROLES</b><br>
  WASD - Movimiento<br>
  Ratón - Cámara<br>
  SHIFT - Correr<br>
  SPACE - Saltar<br>
  E - Entrar / salir del coche<br>
  ESC - Liberar ratón
 </div>
 <canvas id="map" width="145" height="145"></canvas>
 <div id="message"></div>
</div>

<script type="module">
import * as THREE from
'https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js';

const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87b9d8);
scene.fog = new THREE.Fog(0x87b9d8,100,280);

const camera = new THREE.PerspectiveCamera(
 70,innerWidth/innerHeight,0.1,500
);

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;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
document.body.appendChild(renderer.domElement);

// ILUMINACIÓN
scene.add(new THREE.HemisphereLight(0xffffff,0x526078,2));

const sun = new THREE.DirectionalLight(0xffffff,2.5);
sun.position.set(70,120,50);
sun.castShadow = true;
scene.add(sun);

// MATERIALES
const mat = (color,roughness=0.85) =>
 new THREE.MeshStandardMaterial({color,roughness});

const grass = mat(0x547d4b);
const road = mat(0x30343b);
const sidewalk = mat(0x999999);
const white = mat(0xffffff);
const yellow = mat(0xffd83d);

// SUELO
const ground = new THREE.Mesh(
 new THREE.PlaneGeometry(350,350),
 grass
);
ground.rotation.x=-Math.PI/2;
ground.receiveShadow=true;
scene.add(ground);

// CIUDAD
const buildings = [];
const cars = [];

function box(w,h,d,material,x,y,z) {
 const mesh = new THREE.Mesh(
  new THREE.BoxGeometry(w,h,d),material
 );
 mesh.position.set(x,y,z);
 mesh.castShadow=true;
 mesh.receiveShadow=true;
 scene.add(mesh);
 return mesh;
}

// Carreteras
for(let i=-3;i<=3;i++){
 const p=i*40;

 const r1=box(350,0.12,12,road,0,0.06,p);
 const r2=box(12,0.12,350,road,p,0.06,0);

 // Líneas de carretera
 for(let n=-8;n<=8;n++){
  box(2,0.025,0.18,yellow,n*20,0.14,p);
  box(0.18,0.025,2,yellow,p,0.14,n*20);
 }
}

// Edificios
const colors=[
 0x9b9da7,0x697d93,0xb3a08c,
 0x59636c,0x8b8b82,0x435b70,
 0xb8b8b8,0x806d65
];

for(let x=-140;x<=140;x+=20){
 for(let z=-140;z<=140;z+=20){
  if(Math.abs(x%40)<1 || Math.abs(z%40)<1) continue;

  const w=12+Math.random()*4;
  const d=12+Math.random()*4;
  const h=8+Math.random()*45;

  const building=box(
   w,h,d,
   mat(colors[Math.floor(Math.random()*colors.length)]),
   x,h/2,z
  );
  buildings.push(building);

  // Ventanas
  for(let y=4;y<h-2;y+=5){
   for(let side of [-1,1]){
    const win=box(
     1.2,1.8,0.12,
     mat(0x75c5d8,0.3),
     x+side*w/2,y,z
    );
    win.position.z=z;
   }
  }
 }
}

// JUGADOR
const player = new THREE.Group();
const body = new THREE.Mesh(
 new THREE.CapsuleGeometry(0.48,1,4,8),
 mat(0x202bce)
);
body.position.y=1.25;
body.castShadow=true;
player.add(body);

const head = new THREE.Mesh(
 new THREE.SphereGeometry(0.35,16,16),
 mat(0xe5b58c)
);
head.position.y=2.1;
player.add(head);

scene.add(player);
player.position.set(3,0,3);

// COCHES
function createCar(x,z,color){
 const car=new THREE.Group();

 const chassis=new THREE.Mesh(
  new THREE.BoxGeometry(2.4,0.85,4.3),
  mat(color,0.4)
 );
 chassis.position.y=0.85;
 chassis.castShadow=true;
 car.add(chassis);

 const cabin=new THREE.Mesh(
  new THREE.BoxGeometry(1.9,0.8,2),
  mat(0x273e4c,0.3)
 );
 cabin.position.set(0,1.55,-0.15);
 car.add(cabin);

 const wheelMat=mat(0x151515);

 for(const sx of [-1,1]){
  for(const sz of [-1.35,1.35]){
   const wheel=new THREE.Mesh(
    new THREE.CylinderGeometry(0.4,0.4,0.25,16),
    wheelMat
   );
   wheel.rotation.z=Math.PI/2;
   wheel.position.set(sx*1.18,0.5,sz);
   car.add(wheel);
  }
 }

 car.position.set(x,0,z);
 scene.add(car);

 const data={
  object:car,
  speed:0,
  angle:0
 };
 cars.push(data);
 return data;
}

createCar(7,12,0xff3333);
createCar(-30,8,0x2879ff);
createCar(45,-12,0xffffff);
createCar(8,55,0xffd000);
createCar(-65,-50,0x22aa77);

let activeCar=null;
let cameraYaw=0;
let cameraPitch=0.25;
let keys={};
let jumping=false;
let jumpVelocity=0;
let playerVelocity=new THREE.Vector3();

const message=document.getElementById('message');
const mode=document.getElementById('mode');
const speedText=document.getElementById('speed');

document.addEventListener('keydown',e=>{
 keys[e.code]=true;

 if(e.code==='Space' && !activeCar && !jumping){
  jumping=true;
  jumpVelocity=9;
 }

 if(e.code==='KeyE') toggleCar();
});

document.addEventListener('keyup',e=>{
 keys[e.code]=false;
});

document.addEventListener('mousemove',e=>{
 if(document.pointerLockElement){
  cameraYaw-=e.movementX*0.003;
  cameraPitch-=e.movementY*0.003;
  cameraPitch=THREE.MathUtils.clamp(cameraPitch,-0.2,0.9);
 }
});

document.addEventListener('click',()=>{
 if(document.getElementById('start').style.display==='none'){
  renderer.domElement.requestPointerLock?.();
 }
});

function toggleCar(){
 if(activeCar){
  const car=activeCar;
  player.position.copy(car.object.position);
  player.position.x+=Math.cos(car.angle)*3;
  player.position.z+=Math.sin(car.angle)*3;
  activeCar=null;
  mode.textContent='A PIE';
  message.textContent='Has salido del vehículo';
  setTimeout(()=>message.textContent='',1800);
  return;
 }

 let closest=null;
 let distance=5;

 for(const car of cars){
  const d=player.position.distanceTo(car.object.position);
  if(d<distance){
   distance=d;
   closest=car;
  }
 }

 if(closest){
  activeCar=closest;
  mode.textContent='CONDUCIENDO';
  message.textContent='¡Disfruta del viaje!';
  setTimeout(()=>message.textContent='',1800);
 }
}

// COLISIONES SENCILLAS
function collides(pos){
 for(const b of buildings){
  const dx=Math.abs(pos.x-b.position.x);
  const dz=Math.abs(pos.z-b.position.z);
  if(dx<b.geometry.parameters.width/2+0.8 &&
     dz<b.geometry.parameters.depth/2+0.8){
   return true;
  }
 }
 return false;
}

// ACTUALIZACIÓN
const clock=new THREE.Clock();
const cameraTarget=new THREE.Vector3();

function update(dt){
 if(activeCar){
  const car=activeCar;

  if(keys.KeyW) car.speed+=18*dt;
  if(keys.KeyS) car.speed-=22*dt;
  if(!keys.KeyW && !keys.KeyS) car.speed*=0.97;

  car.speed=THREE.MathUtils.clamp(car.speed,-12,35);

  if(Math.abs(car.speed)>0.2){
   const steer=(keys.KeyA?1:0)-(keys.KeyD?1:0);
   car.angle+=steer*dt*1.7*Math.sign(car.speed);
  }

  car.object.position.x+=Math.sin(car.angle)*car.speed*dt;
  car.object.position.z+=Math.cos(car.angle)*car.speed*dt;

  if(collides(car.object.position)){
   car.object.position.x-=Math.sin(car.angle)*car.speed*dt;
   car.object.position.z-=Math.cos(car.angle)*car.speed*dt;
   car.speed=0;
  }

  car.object.rotation.y=car.angle;
  player.position.copy(car.object.position);

  speedText.textContent=Math.abs(Math.round(car.speed*3.6))+' KM/H';

 }else{
  const forward=new THREE.Vector3(
   -Math.sin(cameraYaw),0,-Math.cos(cameraYaw)
  );
  const right=new THREE.Vector3(
   Math.cos(cameraYaw),0,-Math.sin(cameraYaw)
  );

  let move=new THREE.Vector3();

  if(keys.KeyW) move.add(forward);
  if(keys.KeyS) move.sub(forward);
  if(keys.KeyD) move.add(right);
  if(keys.KeyA) move.sub(right);

  if(move.length()>0){
   move.normalize();
   const velocity=keys.ShiftLeft?10:5;
   const next=player.position.clone().addScaledVector(move,velocity*dt);

   if(!collides(next)){
    player.position.copy(next);
   }

   player.rotation.y=Math.atan2(move.x,move.z);
  }

  if(jumping){
   player.position.y+=jumpVelocity*dt;
   jumpVelocity-=22*dt;
   if(player.position.y<=0){
    player.position.y=0;
    jumping=false;
   }
  }

  speedText.textContent='0 KM/H';
 }

 // CÁMARA
 const target=activeCar?
 activeCar.object.position:player.position;

 const angle=activeCar?activeCar.angle:cameraYaw;

 const distance=activeCar?11:8;
 const height=activeCar?5:4;

 const desired=new THREE.Vector3(
  target.x+Math.sin(angle+cameraYaw)*distance*Math.cos(cameraPitch),
  target.y+height+Math.sin(cameraPitch)*distance,
  target.z+Math.cos(angle+cameraYaw)*distance*Math.cos(cameraPitch)
 );

 camera.position.lerp(desired,Math.min(1,dt*5));

 cameraTarget.set(target.x,target.y+1.3,target.z);
 camera.lookAt(cameraTarget);

 drawMap();
}

// MINIMAPA
const map=document.getElementById('map');
const ctx=map.getContext('2d');

function drawMap(){
 ctx.clearRect(0,0,145,145);
 ctx.fillStyle='#18252b';
 ctx.fillRect(0,0,145,145);

 ctx.strokeStyle='#777';
 ctx.lineWidth=5;

 for(let i=-3;i<=3;i++){
  const p=72+i*18;
  ctx.beginPath();
  ctx.moveTo(p,0);ctx.lineTo(p,145);
  ctx.moveTo(0,p);ctx.lineTo(145,p);
  ctx.stroke();
 }

 const pos=activeCar?activeCar.object.position:player.position;

 for(const car of cars){
  ctx.fillStyle='#ffad36';
  ctx.beginPath();
  ctx.arc(72+(car.object.position.x-pos.x)*1.3,
          72+(car.object.position.z-pos.z)*1.3,
          3,0,Math.PI*2);
  ctx.fill();
 }

 ctx.fillStyle='#00eaff';
 ctx.beginPath();
 ctx.arc(72,72,5,0,Math.PI*2);
 ctx.fill();
}

// BUCLE
function animate(){
 requestAnimationFrame(animate);
 const dt=Math.min(clock.getDelta(),0.05);
 update(dt);
 renderer.render(scene,camera);
}
animate();

document.getElementById('play').onclick=()=>{
 document.getElementById('start').style.display='none';
 renderer.domElement.requestPointerLock?.();
};

window.addEventListener('resize',()=>{
 camera.aspect=innerWidth/innerHeight;
 camera.updateProjectionMatrix();
 renderer.setSize(innerWidth,innerHeight);
});
</script>
</body>
</html>

Game Source: VICE CITY: UNDERWORLD

Creator: CyberGlider76

Libraries: three

Complexity: complex (524 lines, 11.3 KB)

The full source code is displayed above on this page.

Remix Instructions

To remix this game, copy the source code above and modify it. Add a ARCADELAB header at the top with "remix_of: vice-city-underworld-cyberglider76" to link back to the original. Then publish at arcadelab.ai/publish.