File size: 16,624 Bytes
e4c0247
 
 
975cece
a6d36b3
e4c0247
a6d36b3
 
 
 
0d86eb7
 
 
 
 
 
 
 
 
 
 
a6d36b3
 
 
 
 
 
 
 
 
 
 
 
 
0d86eb7
975cece
0d86eb7
 
 
 
975cece
0d86eb7
 
975cece
 
 
 
 
 
 
 
 
 
 
 
0d86eb7
975cece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d86eb7
e4c0247
 
 
0d86eb7
 
 
 
975cece
 
0d86eb7
a6d36b3
975cece
 
 
a6d36b3
 
 
 
 
 
 
 
 
 
 
0d86eb7
e4c0247
a6d36b3
 
 
 
 
 
975cece
 
0d86eb7
a6d36b3
 
975cece
 
 
 
 
 
0d86eb7
a6d36b3
 
 
 
975cece
a6d36b3
 
 
 
 
 
 
5ccac0e
a6d36b3
 
 
5ccac0e
a6d36b3
 
5ccac0e
a6d36b3
 
 
 
 
 
5ccac0e
975cece
5ccac0e
 
a6d36b3
0d86eb7
975cece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d86eb7
a6d36b3
 
 
 
5ccac0e
 
a6d36b3
 
 
 
 
 
 
 
 
 
 
 
 
0d86eb7
 
a6d36b3
 
 
0d86eb7
975cece
0d86eb7
 
a6d36b3
0d86eb7
975cece
0d86eb7
a6d36b3
975cece
0d86eb7
 
975cece
0d86eb7
 
 
 
 
975cece
5ccac0e
 
0d86eb7
5ccac0e
0d86eb7
 
 
 
 
 
975cece
a6d36b3
5ccac0e
975cece
0d86eb7
 
975cece
 
 
 
 
 
 
 
0d86eb7
 
 
a6d36b3
 
 
 
 
 
 
 
 
 
 
975cece
a6d36b3
 
 
 
 
 
975cece
a6d36b3
 
0d86eb7
a6d36b3
 
 
 
 
 
 
 
b01cf37
975cece
a6d36b3
975cece
 
 
 
a6d36b3
 
b01cf37
a6d36b3
 
975cece
 
 
a6d36b3
e4c0247
a6d36b3
 
 
0d86eb7
a6d36b3
 
 
5ccac0e
 
 
 
975cece
5ccac0e
 
 
 
 
 
 
 
975cece
 
5ccac0e
975cece
 
 
 
 
5ccac0e
 
 
975cece
 
5ccac0e
 
 
 
 
 
 
975cece
5ccac0e
 
 
 
 
a6d36b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
975cece
 
 
a6d36b3
5ccac0e
0d86eb7
975cece
 
 
0d86eb7
 
a6d36b3
 
0d86eb7
e4c0247
a6d36b3
 
0d86eb7
5ccac0e
 
 
 
 
 
e4c0247
a6d36b3
 
b01cf37
 
975cece
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<!DOCTYPE html>
<html>
<head>
    <title>3D Shooter 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;
        }
        #crosshair {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: rgba(255, 255, 255, 0.8);
            font-size: 24px;
            z-index: 100;
            user-select: none;
            pointer-events: none;
        }
        #healthBar {
            position: absolute;
            bottom: 20px;
            left: 20px;
            width: 200px;
            height: 20px;
            background: rgba(0,0,0,0.5);
            border: 2px solid white;
            z-index: 100;
            border-radius: 10px;
            overflow: hidden;
        }
        #health {
            width: 100%;
            height: 100%;
            background: #ff3333;
            transition: width 0.3s;
        }
        #ammo {
            position: absolute;
            bottom: 20px;
            right: 20px;
            color: white;
            background: rgba(0,0,0,0.7);
            padding: 10px;
            font-family: Arial;
            font-size: 18px;
            z-index: 100;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div id="info">
        Click to start<br>
        WASD - Move<br>
        Mouse - Look around<br>
        Left Click - Shoot<br>
        R - Reload
    </div>
    <div id="timer">Safe Time: 10s</div>
    <div id="crosshair">+</div>
    <div id="healthBar"><div id="health"></div></div>
    <div id="ammo">Ammo: 30/30</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 = [], bullets = [];
        let enemyBullets = [];
        let isGameOver = false;
        let isSafePeriod = true;
        let startTime = 0;
        let playerHealth = 100;
        let ammo = 30;
        const maxAmmo = 30;
        const SAFE_TIME = 10;
        const raycaster = new THREE.Raycaster();
        const mouse = new THREE.Vector2();

        // Scene μ΄ˆκΈ°ν™”
        function initScene() {
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0x87ceeb);
            scene.fog = new THREE.Fog(0x87ceeb, 0, 500);

            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 createBullet(isEnemy = false) {
            const bulletGeometry = new THREE.SphereGeometry(0.1, 8, 8);
            const bulletMaterial = new THREE.MeshBasicMaterial({ 
                color: isEnemy ? 0xff0000 : 0xffff00 
            });
            const bullet = new THREE.Mesh(bulletGeometry, bulletMaterial);
            
            if (!isEnemy) {
                bullet.position.copy(camera.position);
                const direction = new THREE.Vector3();
                camera.getWorldDirection(direction);
                bullet.velocity = direction.multiplyScalar(2);
            }
            
            scene.add(bullet);
            return bullet;
        }

        // λ°œμ‚¬
        function shoot() {
            if (ammo <= 0) return;
            
            ammo--;
            updateAmmoDisplay();
            
            const bullet = createBullet();
            bullets.push(bullet);

            // λ°œμ‚¬ μ‚¬μš΄λ“œλ‚˜ μ΄νŽ™νŠΈλ₯Ό μΆ”κ°€ν•  수 있음
        }

        // 적 λ°œμ‚¬
        function enemyShoot(enemy) {
            const bullet = createBullet(true);
            bullet.position.copy(enemy.model.position);
            
            const direction = new THREE.Vector3();
            direction.subVectors(camera.position, enemy.model.position);
            direction.normalize();
            bullet.velocity = direction.multiplyScalar(1);
            
            enemyBullets.push(bullet);
        }

        // μ΄μ•Œ μ—…λ°μ΄νŠΈ
        function updateBullets() {
            // ν”Œλ ˆμ΄μ–΄ μ΄μ•Œ
            for (let i = bullets.length - 1; i >= 0; i--) {
                bullets[i].position.add(bullets[i].velocity);
                
                // 적과의 좩돌 체크
                enemies.forEach(enemy => {
                    if (bullets[i].position.distanceTo(enemy.model.position) < 2) {
                        scene.remove(bullets[i]);
                        bullets.splice(i, 1);
                        enemy.health -= 25;
                        
                        if (enemy.health <= 0) {
                            scene.remove(enemy.model);
                            enemies = enemies.filter(e => e !== enemy);
                        }
                        return;
                    }
                });

                // 수λͺ… μ œν•œ
                if (bullets[i] && bullets[i].position.distanceTo(camera.position) > 100) {
                    scene.remove(bullets[i]);
                    bullets.splice(i, 1);
                }
            }

            // 적 μ΄μ•Œ
            for (let i = enemyBullets.length - 1; i >= 0; i--) {
                enemyBullets[i].position.add(enemyBullets[i].velocity);
                
                // ν”Œλ ˆμ΄μ–΄μ™€μ˜ 좩돌 체크
                if (enemyBullets[i].position.distanceTo(camera.position) < 2) {
                    scene.remove(enemyBullets[i]);
                    enemyBullets.splice(i, 1);
                    playerHealth -= 10;
                    updateHealthBar();
                    
                    if (playerHealth <= 0) {
                        gameOver(false);
                    }
                    continue;
                }

                // 수λͺ… μ œν•œ
                if (enemyBullets[i] && enemyBullets[i].position.distanceTo(camera.position) > 100) {
                    scene.remove(enemyBullets[i]);
                    enemyBullets.splice(i, 1);
                }
            }
        }

        function updateHealthBar() {
            const healthBar = document.getElementById('health');
            healthBar.style.width = `${playerHealth}%`;
        }

        function updateAmmoDisplay() {
            document.getElementById('ammo').textContent = `Ammo: ${ammo}/${maxAmmo}`;
        }

        // 재μž₯μ „
        function reload() {
            ammo = maxAmmo;
            updateAmmoDisplay();
        }

        // μ§€ν˜• 생성
        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 loadEnemies() {
            const loader = new GLTFLoader();
            loader.load('enemy.glb', (gltf) => {
                console.log('Enemy model loaded successfully');
                const enemyModel = gltf.scene;
                
                for (let i = 0; i < 5; i++) {
                    const enemy = enemyModel.clone();
                    enemy.scale.set(1, 1, 1); // 크기 증가
                    
                    const angle = (i / 5) * Math.PI * 2;
                    const radius = 50; // 더 κ°€κΉκ²Œ μ‹œμž‘
                    enemy.position.set(
                        Math.cos(angle) * radius,
                        2, // μ§€λ©΄μ—μ„œ μ•½κ°„ μœ„λ‘œ
                        Math.sin(angle) * radius
                    );
                    
                    enemy.traverse((node) => {
                        if (node.isMesh) {
                            node.material = node.material.clone(); // 재질 볡제
                            node.material.metalness = 0.3;
                            node.material.roughness = 0.5;
                            node.castShadow = true;
                            node.receiveShadow = true;
                        }
                    });
                    
                    scene.add(enemy);
                    enemies.push({
                        model: enemy,
                        health: 100,
                        speed: 0.2,
                        velocity: new THREE.Vector3(),
                        lastShot: 0
                    });
                }
            }, 
            // λ‘œλ”© 진행상황
            (xhr) => {
                console.log((xhr.loaded / xhr.total * 100) + '% loaded');
            },
            // μ—λŸ¬ 처리
            (error) => {
                console.error('Error loading enemy model:', error);
            });
        }

        // 컨트둀 μ΄ˆκΈ°ν™”
        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;
                    case 'KeyR': reload(); 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 (controls.isLocked) {
                    shoot();
                } else {
                    controls.lock();
                }
            });

            controls.addEventListener('lock', () => {
                document.getElementById('info').style.display = 'none';
                if (!startTime) {
                    startTime = Date.now();
                }
            });

            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);

                    // νšŒμ „
                    enemy.model.lookAt(camera.position);

                    // λ°œμ‚¬ 둜직
                    const now = Date.now();
                    if (now - enemy.lastShot > 2000) { // 2μ΄ˆλ§ˆλ‹€ λ°œμ‚¬
                        enemyShoot(enemy);
                        enemy.lastShot = now;
                    }
                });
            }

}

        // κ²Œμž„ μ˜€λ²„ 처리
        function gameOver(won) {
            if (!isGameOver) {
                isGameOver = true;
                controls.unlock();
                setTimeout(() => {
                    alert(won ? 'You won!' : 'Game Over! You were eliminated!');
                    location.reload();
                }, 100);
            }
        }

        // κ²Œμž„ μ΄ˆκΈ°ν™”
        function init() {
            initScene();
            createTerrain();
            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);

                    // μ΄μ•Œ μ—…λ°μ΄νŠΈ
                    updateBullets();

                    // 적 μ—…λ°μ΄νŠΈ
                    updateEnemies();

                    // 승리 쑰건 체크
                    if (!isSafePeriod && enemies.length === 0) {
                        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>