Spaces:
Running
Running
File size: 6,445 Bytes
027fc6d 6a8dd9f 93b8415 6a8dd9f 93b8415 6a8dd9f 93b8415 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Worms 3D</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #222;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
/* CONTENEDOR GENERAL */
#game-container {
width: 90vw;
height: 70vh;
background: #000;
border: 3px solid #00ff88;
margin-top: 10px;
position: relative;
}
/* BOTONES */
#ui-bar {
margin-top: 12px;
display: flex;
gap: 12px;
}
button {
padding: 14px 26px;
font-size: 18px;
font-weight: bold;
border-radius: 8px;
border: none;
background: #00ff99;
color: #000;
cursor: pointer;
}
button:hover {
background: #00d97e;
}
</style>
<!-- THREE.JS + LIBRERÍAS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/loaders/OBJLoader.js"></script>
</head>
<body>
<!-- BOTONES VISIBLES -->
<div id="ui-bar">
<button onclick="cameraFollow = true">Seguir Gusano</button>
<button onclick="cameraFollow = false">Control Libre</button>
</div>
<!-- CONTENEDOR DEL JUEGO (AGRANDADO) -->
<div id="game-container"></div>
<script>
// =======================================================
// === Three.js 3D Initialization ===
// =======================================================
let scene, camera, renderer, terrain, controls;
let wormMesh;
const keys = {};
let cameraFollow = true; // Activar o desactivar seguimiento automático
const container = document.getElementById("game-container");
const textureLoader = new THREE.TextureLoader();
const movementSpeed = 0.5;
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
camera = new THREE.PerspectiveCamera(
75,
container.clientWidth / container.clientHeight,
0.1,
1000
);
camera.position.set(0, 50, 70);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0x404040, 3);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
directionalLight.position.set(100, 100, 50);
scene.add(directionalLight);
createTerrain();
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minDistance = 20;
controls.maxDistance = 150;
loadWormModel();
window.addEventListener("keydown", (e) => keys[e.key.toLowerCase()] = true);
window.addEventListener("keyup", (e) => keys[e.key.toLowerCase()] = false);
window.addEventListener("resize", onWindowResize);
}
// =======================================================
// === Terreno Procedural ===
// =======================================================
function createTerrain() {
const texture = textureLoader.load("grass_texture.jpg");
const size = 150, segments = 64;
const geometry = new THREE.PlaneGeometry(size, size, segments, segments);
geometry.rotateX(-Math.PI / 2);
const material = new THREE.MeshPhongMaterial({
map: texture,
side: THREE.DoubleSide
});
terrain = new THREE.Mesh(geometry, material);
const verts = geometry.attributes.position.array;
const maxH = 15;
for (let i = 0; i < verts.length; i += 3) {
const x = verts[i] / size;
const y = verts[i + 2] / size;
const height =
Math.sin(x * 10) * 0.5 +
Math.cos(y * 7) * 0.8 +
Math.sin((x + y) * 5) * 0.3;
verts[i + 1] = height * maxH;
}
geometry.computeVertexNormals();
scene.add(terrain);
}
// =======================================================
// === Modelo OBJ del gusano ===
// =======================================================
function loadWormModel() {
const objLoader = new THREE.OBJLoader();
objLoader.load("cascabel123.obj", (object) => {
const material = new THREE.MeshPhongMaterial({
color: 0x68e079,
shininess: 30
});
object.traverse((child) => {
if (child.isMesh) child.material = material;
});
wormMesh = object;
wormMesh.scale.set(10, 10, 10);
wormMesh.position.set(0, 5, 0);
scene.add(wormMesh);
});
}
// =======================================================
// === Movimiento ===
// =======================================================
function moveWorm() {
if (!wormMesh) return;
let moved = false;
const prev = wormMesh.position.clone();
if (keys["w"]) { wormMesh.position.z -= movementSpeed; wormMesh.rotation.y = 0; moved = true; }
if (keys["s"]) { wormMesh.position.z += movementSpeed; wormMesh.rotation.y = Math.PI; moved = true; }
if (keys["a"]) { wormMesh.position.x -= movementSpeed; wormMesh.rotation.y = Math.PI/2; moved = true; }
if (keys["d"]) { wormMesh.position.x += movementSpeed; wormMesh.rotation.y = -Math.PI/2; moved = true; }
if (cameraFollow && moved) {
const delta = wormMesh.position.clone().sub(prev);
camera.position.add(delta);
}
}
// =======================================================
// === Render Loop ===
// =======================================================
function animate() {
requestAnimationFrame(animate);
moveWorm();
if (controls) {
if (cameraFollow && wormMesh) {
controls.target.copy(wormMesh.position);
}
controls.update();
}
renderer.render(scene, camera);
}
// =======================================================
// === Resize ===
// =======================================================
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
init();
animate();
</script>
</body>
</html> |