Spaces:
Running
Running
File size: 13,165 Bytes
e4c0247 0d86eb7 a6d36b3 e4c0247 a6d36b3 0d86eb7 a6d36b3 0d86eb7 5ccac0e 0d86eb7 a6d36b3 0d86eb7 a6d36b3 0d86eb7 e4c0247 0d86eb7 5ccac0e 0d86eb7 a6d36b3 5ccac0e a6d36b3 0d86eb7 e4c0247 a6d36b3 5ccac0e 0d86eb7 a6d36b3 0d86eb7 a6d36b3 5ccac0e a6d36b3 5ccac0e a6d36b3 5ccac0e a6d36b3 5ccac0e a6d36b3 0d86eb7 a6d36b3 5ccac0e a6d36b3 0d86eb7 a6d36b3 5ccac0e a6d36b3 0d86eb7 a6d36b3 0d86eb7 5ccac0e a6d36b3 5ccac0e a6d36b3 5ccac0e a6d36b3 5ccac0e a6d36b3 5ccac0e 0d86eb7 a6d36b3 0d86eb7 a6d36b3 0d86eb7 a6d36b3 0d86eb7 a6d36b3 0d86eb7 5ccac0e 0d86eb7 5ccac0e 0d86eb7 5ccac0e 0d86eb7 a6d36b3 5ccac0e 0d86eb7 a6d36b3 0d86eb7 a6d36b3 0d86eb7 a6d36b3 b01cf37 a6d36b3 5ccac0e a6d36b3 b01cf37 a6d36b3 e4c0247 a6d36b3 0d86eb7 a6d36b3 5ccac0e a6d36b3 5ccac0e 0d86eb7 5ccac0e a6d36b3 0d86eb7 a6d36b3 0d86eb7 e4c0247 a6d36b3 0d86eb7 5ccac0e e4c0247 a6d36b3 b01cf37 6ff4d9b |
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
<!DOCTYPE html>
<html>
<head>
<title>3D Survival Game</title>
<meta charset="utf-8">
<style>
body {
margin: 0;
overflow: hidden;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 10px;
font-family: Arial;
font-size: 14px;
z-index: 100;
border-radius: 5px;
user-select: none;
}
#timer {
position: absolute;
top: 10px;
right: 10px;
color: white;
background: rgba(0,0,0,0.7);
padding: 10px;
font-family: Arial;
font-size: 14px;
z-index: 100;
border-radius: 5px;
}
#targetAlert {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: yellow;
background: rgba(0,0,0,0.8);
padding: 20px;
font-family: Arial;
font-size: 24px;
z-index: 100;
border-radius: 10px;
display: none;
}
</style>
</head>
<body>
<div id="info">
Click to start<br>
WASD - Move<br>
Mouse - Look around<br>
Find and touch your clone to win!<br>
Avoid the enemies!
</div>
<div id="timer">Safe Time: 10s</div>
<div id="targetAlert">Find and touch your clone to win!</div>
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.157.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.157.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
// ๊ธฐ๋ณธ ๋ณ์
let scene, camera, renderer, controls;
let enemies = [], target;
let isGameOver = false;
let isSafePeriod = true;
let startTime = 0;
const SAFE_TIME = 10; // 10์ด ์์ ์๊ฐ
// Scene ์ด๊ธฐํ
function initScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
scene.fog = new THREE.Fog(0x87ceeb, 0, 300);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 0);
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);
// ์กฐ๋ช
const ambientLight = new THREE.AmbientLight(0xffffff, 1.0);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 1.2);
dirLight.position.set(50, 50, 0);
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
dirLight.shadow.camera.far = 300;
scene.add(dirLight);
// ๋ฐ์ฌ๊ด ์ถ๊ฐ
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 0.6);
scene.add(hemiLight);
}
// ์งํ ์์ฑ
function createTerrain() {
const geometry = new THREE.PlaneGeometry(300, 300, 50, 50);
const material = new THREE.MeshStandardMaterial({
color: 0x3a8c3a,
roughness: 0.6,
metalness: 0.1
});
const vertices = geometry.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
vertices[i + 2] = Math.random() * 3;
}
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
const terrain = new THREE.Mesh(geometry, material);
terrain.rotation.x = -Math.PI / 2;
terrain.receiveShadow = true;
scene.add(terrain);
}
// ๋๋ฌด ์์ฑ
function createTrees() {
const treeGeo = new THREE.CylinderGeometry(0, 4, 15, 8);
const treeMat = new THREE.MeshStandardMaterial({
color: 0x0d5c0d,
roughness: 0.8,
metalness: 0.2
});
for (let i = 0; i < 100; i++) {
const tree = new THREE.Mesh(treeGeo, treeMat);
tree.position.set(
(Math.random() - 0.5) * 250,
7.5,
(Math.random() - 0.5) * 250
);
tree.castShadow = true;
tree.receiveShadow = true;
scene.add(tree);
}
}
// ๋ชฉํ๋ฌผ(Clone) ์์ฑ
function spawnTarget() {
const loader = new GLTFLoader();
loader.load('me.glb', (gltf) => {
target = gltf.scene;
target.scale.set(0.5, 0.5, 0.5);
target.position.set(
(Math.random() - 0.5) * 200,
0,
(Math.random() - 0.5) * 200
);
target.traverse((node) => {
if (node.isMesh) {
node.material.metalness = 0.3;
node.material.roughness = 0.5;
node.castShadow = true;
node.receiveShadow = true;
}
});
scene.add(target);
document.getElementById('targetAlert').style.display = 'block';
setTimeout(() => {
document.getElementById('targetAlert').style.display = 'none';
}, 3000);
});
}
// ์ ๋ก๋
function loadEnemies() {
const loader = new GLTFLoader();
loader.load('enemy.glb', (gltf) => {
const enemyModel = gltf.scene;
for (let i = 0; i < 5; i++) {
const enemy = enemyModel.clone();
enemy.scale.set(0.5, 0.5, 0.5);
const angle = (i / 5) * Math.PI * 2;
const radius = 100;
enemy.position.set(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
);
enemy.traverse((node) => {
if (node.isMesh) {
node.material.metalness = 0.3;
node.material.roughness = 0.5;
node.castShadow = true;
node.receiveShadow = true;
}
});
scene.add(enemy);
enemies.push({
model: enemy,
speed: 0.2,
velocity: new THREE.Vector3(),
rotation: new THREE.Euler()
});
}
console.log('Enemies loaded');
});
}
// ์ปจํธ๋กค ์ด๊ธฐํ
function initControls() {
controls = new PointerLockControls(camera, document.body);
const moveState = {
forward: false,
backward: false,
left: false,
right: false
};
document.addEventListener('keydown', (event) => {
switch(event.code) {
case 'KeyW': moveState.forward = true; break;
case 'KeyS': moveState.backward = true; break;
case 'KeyA': moveState.left = true; break;
case 'KeyD': moveState.right = true; break;
}
});
document.addEventListener('keyup', (event) => {
switch(event.code) {
case 'KeyW': moveState.forward = false; break;
case 'KeyS': moveState.backward = false; break;
case 'KeyA': moveState.left = false; break;
case 'KeyD': moveState.right = false; break;
}
});
document.addEventListener('click', () => {
if (!startTime) {
startTime = Date.now();
setTimeout(spawnTarget, Math.random() * 5000 + 5000);
}
controls.lock();
});
controls.addEventListener('lock', () => {
document.getElementById('info').style.display = 'none';
});
controls.addEventListener('unlock', () => {
document.getElementById('info').style.display = 'block';
});
return moveState;
}
// ์ ์
๋ฐ์ดํธ
function updateEnemies() {
if (!isSafePeriod) {
enemies.forEach(enemy => {
const direction = new THREE.Vector3();
direction.subVectors(camera.position, enemy.model.position);
direction.y = 0;
direction.normalize();
// ๋ถ๋๋ฌ์ด ์๋ ๋ณํ
enemy.velocity.lerp(direction.multiplyScalar(enemy.speed), 0.02);
enemy.model.position.add(enemy.velocity);
// ๋ถ๋๋ฌ์ด ํ์
const targetRotation = Math.atan2(direction.x, direction.z);
enemy.model.rotation.y = THREE.MathUtils.lerp(
enemy.model.rotation.y,
targetRotation,
0.05
);
// ์ถฉ๋ ์ฒดํฌ
if (enemy.model.position.distanceTo(camera.position) < 3) {
gameOver(false);
}
});
}
}
// ๊ฒ์ ์ค๋ฒ ์ฒ๋ฆฌ
function gameOver(won) {
if (!isGameOver) {
isGameOver = true;
controls.unlock();
setTimeout(() => {
alert(won ? 'You found your clone and won!' : 'Game Over! You were caught!');
location.reload();
}, 100);
}
}
// ๊ฒ์ ์ด๊ธฐํ
function init() {
initScene();
createTerrain();
createTrees();
loadEnemies();
const moveState = initControls();
// ๊ฒ์ ๋ฃจํ
function animate() {
requestAnimationFrame(animate);
if (controls.isLocked && !isGameOver) {
// ์์ ์๊ฐ ์ฒดํฌ
if (startTime) {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const remaining = SAFE_TIME - elapsed;
if (remaining > 0) {
document.getElementById('timer').textContent = `Safe Time: ${remaining}s`;
} else {
document.getElementById('timer').textContent = '';
isSafePeriod = false;
}
}
// ์ด๋ ์ฒ๋ฆฌ
const speed = 0.3;
if (moveState.forward) controls.moveForward(speed);
if (moveState.backward) controls.moveForward(-speed);
if (moveState.left) controls.moveRight(-speed);
if (moveState.right) controls.moveRight(speed);
// ์ ์
๋ฐ์ดํธ
updateEnemies();
// ๋ชฉํ๋ฌผ ์ฒดํฌ
if (target) {
target.rotation.y += 0.01;
if (camera.position.distanceTo(target.position) < 3) {
gameOver(true);
}
}
}
renderer.render(scene, camera);
}
animate();
}
// ํ๋ฉด ํฌ๊ธฐ ์กฐ์ ์ฒ๋ฆฌ
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ๊ฒ์ ์์
init();
</script>
</body>
</html> |