File size: 10,461 Bytes
2bbf99e
 
 
 
 
 
 
08e0a03
 
2bbf99e
 
08e0a03
12d84a4
473090a
89db675
 
 
12d84a4
2bbf99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473090a
 
 
 
 
 
 
 
 
 
 
 
2bbf99e
473090a
 
 
 
 
 
 
 
 
 
 
 
2bbf99e
473090a
 
 
 
2bbf99e
 
 
 
08e0a03
 
2bbf99e
 
 
 
 
 
 
 
 
 
 
 
89db675
 
 
2bbf99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473090a
2bbf99e
 
 
08e0a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbf99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473090a
 
 
 
 
 
2bbf99e
 
 
 
 
 
 
473090a
 
 
 
08e0a03
473090a
 
 
 
 
89db675
2bbf99e
89db675
 
 
 
12d84a4
89db675
12d84a4
 
473090a
12d84a4
89db675
 
12d84a4
 
 
 
 
 
 
 
 
473090a
12d84a4
89db675
473090a
 
 
89db675
 
473090a
12d84a4
 
89db675
12d84a4
 
89db675
12d84a4
 
89db675
 
 
 
08e0a03
89db675
 
2bbf99e
 
 
08e0a03
2bbf99e
 
 
 
 
 
 
08e0a03
2bbf99e
 
08e0a03
 
 
 
 
 
 
 
 
 
 
2bbf99e
 
 
 
89db675
 
 
 
 
 
 
 
 
 
2bbf99e
 
 
 
 
 
08e0a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbf99e
08e0a03
 
 
 
2bbf99e
 
 
 
 
 
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
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';

// κ²Œμž„ μƒμˆ˜
const GAME_DURATION = 180;
const MAP_SIZE = 2000;
const HELICOPTER_HEIGHT = 100; // 헬리μ½₯ν„° 고도 상ν–₯
const ENEMY_GROUND_HEIGHT = 5; // 적 지상 높이
const ENEMY_SCALE = 3;
const MAX_HEALTH = 1000;
const ENEMY_MOVE_SPEED = 0.1; // 적 이동 속도
const ENEMY_MODELS = [
    './models/enemy1.glb',
    './models/enemy12.glb',
    './models/enemy13.glb',
    './models/enemy14.glb'
];
const ENEMY_CONFIG = {
    ATTACK_RANGE: 100,
    ATTACK_INTERVAL: 2000,
    BULLET_SPEED: 2
};

// κ²Œμž„ λ³€μˆ˜
let scene, camera, renderer, controls;
let enemies = [];
let bullets = [];
let enemyBullets = [];
let playerHealth = MAX_HEALTH;
let ammo = 30;
let currentStage = 1;
let isGameOver = false;

// μ‚¬μš΄λ“œ ν’€ 클래슀
class SoundPool {
    constructor(soundUrl, poolSize = 10) {
        this.sounds = [];
        this.currentIndex = 0;
        this.poolSize = poolSize;
        
        for (let i = 0; i < poolSize; i++) {
            const sound = new Audio(soundUrl);
            sound.preload = 'auto';
            this.sounds.push(sound);
        }
    }

    play() {
        const sound = this.sounds[this.currentIndex];
        sound.pause();
        sound.currentTime = 0;
        
        const playPromise = sound.play();
        if (playPromise !== undefined) {
            playPromise.catch(error => {
                console.error("Sound play error:", error);
                this.sounds[this.currentIndex] = new Audio(sound.src);
            });
        }

        this.currentIndex = (this.currentIndex + 1) % this.poolSize;
    }
}

// μ‚¬μš΄λ“œ μ΄ˆκΈ°ν™”
const sounds = {
    bgm: new Audio('Music.wav'),
    gunshot: new SoundPool('gun.wav', 20),
    explosion: new SoundPool('explosion.wav', 10) // 폭발 μ‚¬μš΄λ“œ μΆ”κ°€
};
sounds.bgm.loop = true;

// 이동 μƒνƒœ
const moveState = {
    forward: false,
    backward: false,
    left: false,
    right: false
};

function init() {
    console.log('Game initialized');
    console.log('Available enemy models:', ENEMY_MODELS);

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0x87ceeb);
    scene.fog = new THREE.Fog(0x87ceeb, 0, 1500);

    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 3000);
    camera.position.set(0, HELICOPTER_HEIGHT, 0);

    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    document.body.appendChild(renderer.domElement);

    const ambientLight = new THREE.AmbientLight(0xffffff, 1.0);
    scene.add(ambientLight);

    const dirLight = new THREE.DirectionalLight(0xffffff, 1.0);
    dirLight.position.set(100, 100, 50);
    dirLight.castShadow = true;
    scene.add(dirLight);

    controls = new PointerLockControls(camera, document.body);

    document.addEventListener('click', onClick);
    document.addEventListener('keydown', onKeyDown);
    document.addEventListener('keyup', onKeyUp);
    window.addEventListener('resize', onWindowResize);

    createTerrain();
    console.log('Starting to load enemies...');
    loadEnemies();
}

// 폭발 효과 생성 ν•¨μˆ˜
function createExplosion(position) {
    const particleCount = 30;
    const particles = [];

    for (let i = 0; i < particleCount; i++) {
        const particle = new THREE.Mesh(
            new THREE.SphereGeometry(0.3),
            new THREE.MeshBasicMaterial({ 
                color: 0xff4400,
                transparent: true,
                opacity: 1
            })
        );
        
        particle.position.copy(position);
        particle.velocity = new THREE.Vector3(
            (Math.random() - 0.5) * 2,
            Math.random() * 2,
            (Math.random() - 0.5) * 2
        );
        
        particles.push(particle);
        scene.add(particle);
    }

    // 폭발 μ• λ‹ˆλ©”μ΄μ…˜
    const animateExplosion = () => {
        particles.forEach((particle, i) => {
            particle.position.add(particle.velocity);
            particle.material.opacity -= 0.02;
            
            if (particle.material.opacity <= 0) {
                scene.remove(particle);
                particles.splice(i, 1);
            }
        });
        
        if (particles.length > 0) {
            requestAnimationFrame(animateExplosion);
        }
    };
    
    animateExplosion();
    sounds.explosion.play();
}

function createTerrain() {
    const geometry = new THREE.PlaneGeometry(MAP_SIZE, MAP_SIZE, 200, 200);
    const material = new THREE.MeshStandardMaterial({
        color: 0xD2B48C,
        roughness: 0.8,
        metalness: 0.2
    });

    const vertices = geometry.attributes.position.array;
    for (let i = 0; i < vertices.length; i += 3) {
        vertices[i + 2] = Math.sin(vertices[i] * 0.01) * Math.cos(vertices[i + 1] * 0.01) * 20;
    }

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

    addObstacles();
}

function createTemporaryEnemy(position) {
    const geometry = new THREE.BoxGeometry(5, 5, 5);
    const material = new THREE.MeshPhongMaterial({ color: 0xff0000 });
    const cube = new THREE.Mesh(geometry, material);
    cube.position.copy(position);
    return cube;
}

function loadEnemies() {
    const loader = new GLTFLoader();
    const enemyCount = 3 + currentStage;

    for (let i = 0; i < enemyCount; i++) {
        const angle = (i / enemyCount) * Math.PI * 2;
        const radius = 200;
        const position = new THREE.Vector3(
            Math.cos(angle) * radius,
            ENEMY_GROUND_HEIGHT,
            Math.sin(angle) * radius
        );

        const tempEnemy = createTemporaryEnemy(position);
        scene.add(tempEnemy);
        
        const modelPath = ENEMY_MODELS[i % ENEMY_MODELS.length];
        console.log('Loading enemy model:', modelPath);

        loader.load(
            modelPath,
            (gltf) => {
                console.log('Successfully loaded enemy model:', modelPath);
                const enemy = gltf.scene;
                enemy.scale.set(ENEMY_SCALE, ENEMY_SCALE, ENEMY_SCALE);
                enemy.position.copy(position);

                enemy.rotation.y = Math.PI;

                enemy.traverse((node) => {
                    if (node.isMesh) {
                        node.castShadow = true;
                        node.receiveShadow = true;
                        node.material.metalness = 0.2;
                        node.material.roughness = 0.8;
                    }
                });

                scene.remove(tempEnemy);
                scene.add(enemy);

                const index = enemies.findIndex(e => e.model === tempEnemy);
                if (index !== -1) {
                    enemies[index].model = enemy;
                    enemies[index].muzzleFlash = createMuzzleFlash();
                    enemy.add(enemies[index].muzzleFlash);
                }
            },
            (xhr) => {
                console.log(`${modelPath}: ${(xhr.loaded / xhr.total * 100)}% loaded`);
            },
            (error) => {
                console.error('Error loading enemy model:', modelPath, error);
            }
        );

        enemies.push({
            model: tempEnemy,
            health: 100,
            speed: ENEMY_MOVE_SPEED,
            lastAttackTime: 0
        });
    }
}

// λ‚˜λ¨Έμ§€ ν•¨μˆ˜λ“€μ€ 이전과 λ™μΌν•˜κ²Œ μœ μ§€λ˜λ©°, updateEnemies와 updateBullets ν•¨μˆ˜λ§Œ μˆ˜μ •λ©λ‹ˆλ‹€.

function updateEnemies() {
    const currentTime = Date.now();
    
    enemies.forEach(enemy => {
        const direction = new THREE.Vector3();
        direction.subVectors(camera.position, enemy.model.position);
        direction.y = 0; // yμΆ• 이동 μ œν•œ
        direction.normalize();
        
        const newPosition = enemy.model.position.clone()
            .add(direction.multiplyScalar(enemy.speed));
        newPosition.y = ENEMY_GROUND_HEIGHT;
        enemy.model.position.copy(newPosition);

        // 적이 ν”Œλ ˆμ΄μ–΄λ₯Ό 바라보도둝 μ„€μ •
        enemy.model.lookAt(new THREE.Vector3(
            camera.position.x,
            enemy.model.position.y,
            camera.position.z
        ));

        const distanceToPlayer = enemy.model.position.distanceTo(camera.position);
        if (distanceToPlayer < ENEMY_CONFIG.ATTACK_RANGE && 
            currentTime - enemy.lastAttackTime > ENEMY_CONFIG.ATTACK_INTERVAL) {
            
            if (enemy.muzzleFlash) {
                enemy.muzzleFlash.material.opacity = 1;
                enemy.muzzleFlash.visible = true;
                setTimeout(() => {
                    enemy.muzzleFlash.visible = false;
                    enemy.muzzleFlash.material.opacity = 0;
                }, 100);
            }

            enemyBullets.push(createEnemyBullet(enemy));
            enemy.lastAttackTime = currentTime;
        }
    });
}

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) < 5) {
                scene.remove(bullets[i]);
                bullets.splice(i, 1);
                enemy.health -= 25;
                
                // 피격 효과
                createExplosion(enemy.model.position.clone());
                
                if (enemy.health <= 0) {
                    // 파괴 효과
                    createExplosion(enemy.model.position.clone());
                    createExplosion(enemy.model.position.clone().add(new THREE.Vector3(2, 0, 2)));
                    createExplosion(enemy.model.position.clone().add(new THREE.Vector3(-2, 0, -2)));
                    scene.remove(enemy.model);
                    enemies = enemies.filter(e => e !== enemy);
                }
            }
        });

        if (bullets[i] && bullets[i].position.distanceTo(camera.position) > 1000) {
            scene.remove(bullets[i]);
            bullets.splice(i, 1);
        }
    }
}

// κ²Œμž„ μ‹œμž‘
init();
gameLoop();