cutechicken commited on
Commit
49edc42
โ€ข
1 Parent(s): b2a23a4

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +131 -15
game.js CHANGED
@@ -139,35 +139,112 @@ class TankPlayer {
139
  }
140
 
141
  move(direction) {
142
- if (!this.body) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
- const moveVector = new THREE.Vector3();
145
- moveVector.x = direction.x * this.moveSpeed;
146
- moveVector.z = direction.z * this.moveSpeed;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- this.body.position.add(moveVector);
 
 
 
 
 
 
149
  }
 
150
 
151
  rotate(angle) {
152
  if (!this.body) return;
 
 
153
  this.body.rotation.y += angle * this.turnSpeed;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
-
156
  getPosition() {
157
  return this.body ? this.body.position : new THREE.Vector3();
158
  }
159
-
160
  takeDamage(damage) {
161
  this.health -= damage;
162
  return this.health <= 0;
163
  }
164
-
165
- // ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ๋“ค
166
  startReload() {
167
  this.isReloading = true;
168
  const reloadingText = document.getElementById('reloadingText');
169
  reloadingText.style.display = 'block';
170
-
171
  setTimeout(() => {
172
  this.ammo = this.maxAmmo;
173
  this.isReloading = false;
@@ -175,7 +252,7 @@ class TankPlayer {
175
  this.updateAmmoDisplay();
176
  }, this.reloadTime);
177
  }
178
-
179
  updateAmmoDisplay() {
180
  document.getElementById('ammoDisplay').textContent = `APFSDS: ${this.ammo}/${this.maxAmmo}`;
181
  }
@@ -225,17 +302,56 @@ class Enemy {
225
  .subVectors(playerPosition, this.mesh.position)
226
  .normalize();
227
 
228
- // ํ”Œ๋ ˆ์ด์–ด์™€์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
229
  const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
230
- const minDistance = 50; // ์ตœ์†Œ ์ ‘๊ทผ ๊ฑฐ๋ฆฌ
231
 
232
  this.mesh.lookAt(playerPosition);
233
 
234
- // ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๋ฉ€๋ฆฌ ์žˆ์„ ๋•Œ๋งŒ ์ด๋™
235
  if (distanceToPlayer > minDistance) {
236
- this.mesh.position.add(direction.multiplyScalar(this.moveSpeed));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  }
238
 
 
239
  // ์ด์•Œ ์—…๋ฐ์ดํŠธ ๋ถ€๋ถ„
240
  for (let i = this.bullets.length - 1; i >= 0; i--) {
241
  const bullet = this.bullets[i];
 
139
  }
140
 
141
  move(direction) {
142
+ if (!this.body) return;
143
+
144
+ const moveVector = new THREE.Vector3();
145
+ moveVector.x = direction.x * this.moveSpeed;
146
+ moveVector.z = direction.z * this.moveSpeed;
147
+
148
+ // ์ƒˆ๋กœ์šด ์œ„์น˜ ๊ณ„์‚ฐ
149
+ const newPosition = this.body.position.clone().add(moveVector);
150
+
151
+ // ์ƒˆ๋กœ์šด ์œ„์น˜์˜ ์ง€ํ˜• ๋†’์ด ๊ฐ€์ ธ์˜ค๊ธฐ
152
+ const heightAtNewPos = window.gameInstance.getHeightAtPosition(newPosition.x, newPosition.z);
153
+
154
+ // ํƒฑํฌ ๋†’์ด๋งŒํผ ๋”ํ•ด์„œ ์ง€ํ˜• ์œ„์— ์œ„์น˜์‹œํ‚ด
155
+ newPosition.y = heightAtNewPos + TANK_HEIGHT;
156
+
157
+ // ๊ฒฝ์‚ฌ๊ฐ€ ๋„ˆ๋ฌด ๊ฐ€ํŒŒ๋ฅธ์ง€ ์ฒดํฌ
158
+ const currentHeight = this.body.position.y;
159
+ const heightDifference = Math.abs(newPosition.y - currentHeight);
160
+ const maxClimbAngle = 0.5; // ์ตœ๋Œ€ ๋“ฑ๋ฐ˜ ๊ฐ๋„
161
+
162
+ if (heightDifference / this.moveSpeed < maxClimbAngle) {
163
+ this.body.position.copy(newPosition);
164
+
165
+ // ํƒฑํฌ์˜ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ ๊ณ„์‚ฐ
166
+ const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.body.quaternion);
167
+ const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.body.quaternion);
168
 
169
+ // ํƒฑํฌ ์ฃผ๋ณ€์˜ ๋†’์ด ๊ณ„์‚ฐ
170
+ const frontHeight = window.gameInstance.getHeightAtPosition(
171
+ newPosition.x + forwardVector.x,
172
+ newPosition.z + forwardVector.z
173
+ );
174
+ const backHeight = window.gameInstance.getHeightAtPosition(
175
+ newPosition.x - forwardVector.x,
176
+ newPosition.z - forwardVector.z
177
+ );
178
+ const rightHeight = window.gameInstance.getHeightAtPosition(
179
+ newPosition.x + rightVector.x,
180
+ newPosition.z + rightVector.z
181
+ );
182
+ const leftHeight = window.gameInstance.getHeightAtPosition(
183
+ newPosition.x - rightVector.x,
184
+ newPosition.z - rightVector.z
185
+ );
186
 
187
+ // ํƒฑํฌ์˜ ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ ๋ฐ ์ ์šฉ
188
+ const pitch = Math.atan2(frontHeight - backHeight, 2);
189
+ const roll = Math.atan2(rightHeight - leftHeight, 2);
190
+
191
+ // ๊ธฐ์กด y์ถ• ํšŒ์ „์€ ์œ ์ง€ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ธฐ์šธ๊ธฐ ์ ์šฉ
192
+ const currentYRotation = this.body.rotation.y;
193
+ this.body.rotation.set(pitch, currentYRotation, roll);
194
  }
195
+ }
196
 
197
  rotate(angle) {
198
  if (!this.body) return;
199
+
200
+ // y์ถ• ํšŒ์ „ ์ ์šฉ
201
  this.body.rotation.y += angle * this.turnSpeed;
202
+
203
+ // ํ˜„์žฌ ์œ„์น˜์—์„œ์˜ ์ง€ํ˜•์— ๋งž์ถฐ ํƒฑํฌ ๊ธฐ์šธ๊ธฐ ์กฐ์ •
204
+ const position = this.body.position;
205
+ const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.body.quaternion);
206
+ const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.body.quaternion);
207
+
208
+ // ํƒฑํฌ ์ฃผ๋ณ€์˜ ๋†’์ด ๊ณ„์‚ฐ
209
+ const frontHeight = window.gameInstance.getHeightAtPosition(
210
+ position.x + forwardVector.x,
211
+ position.z + forwardVector.z
212
+ );
213
+ const backHeight = window.gameInstance.getHeightAtPosition(
214
+ position.x - forwardVector.x,
215
+ position.z - forwardVector.z
216
+ );
217
+ const rightHeight = window.gameInstance.getHeightAtPosition(
218
+ position.x + rightVector.x,
219
+ position.z + rightVector.z
220
+ );
221
+ const leftHeight = window.gameInstance.getHeightAtPosition(
222
+ position.x - rightVector.x,
223
+ position.z - rightVector.z
224
+ );
225
+
226
+ // ํƒฑํฌ์˜ ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ ๋ฐ ์ ์šฉ
227
+ const pitch = Math.atan2(frontHeight - backHeight, 2);
228
+ const roll = Math.atan2(rightHeight - leftHeight, 2);
229
+
230
+ // ํ˜„์žฌ y์ถ• ํšŒ์ „์€ ์œ ์ง€ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ธฐ์šธ๊ธฐ ์ ์šฉ
231
+ const currentYRotation = this.body.rotation.y;
232
+ this.body.rotation.set(pitch, currentYRotation, roll);
233
  }
234
+
235
  getPosition() {
236
  return this.body ? this.body.position : new THREE.Vector3();
237
  }
238
+
239
  takeDamage(damage) {
240
  this.health -= damage;
241
  return this.health <= 0;
242
  }
243
+
 
244
  startReload() {
245
  this.isReloading = true;
246
  const reloadingText = document.getElementById('reloadingText');
247
  reloadingText.style.display = 'block';
 
248
  setTimeout(() => {
249
  this.ammo = this.maxAmmo;
250
  this.isReloading = false;
 
252
  this.updateAmmoDisplay();
253
  }, this.reloadTime);
254
  }
255
+
256
  updateAmmoDisplay() {
257
  document.getElementById('ammoDisplay').textContent = `APFSDS: ${this.ammo}/${this.maxAmmo}`;
258
  }
 
302
  .subVectors(playerPosition, this.mesh.position)
303
  .normalize();
304
 
 
305
  const distanceToPlayer = this.mesh.position.distanceTo(playerPosition);
306
+ const minDistance = 50;
307
 
308
  this.mesh.lookAt(playerPosition);
309
 
 
310
  if (distanceToPlayer > minDistance) {
311
+ const moveVector = direction.multiplyScalar(this.moveSpeed);
312
+ const newPosition = this.mesh.position.clone().add(moveVector);
313
+
314
+ // ์ƒˆ๋กœ์šด ์œ„์น˜์˜ ์ง€ํ˜• ๋†’์ด ๊ฐ€์ ธ์˜ค๊ธฐ
315
+ const heightAtNewPos = window.gameInstance.getHeightAtPosition(newPosition.x, newPosition.z);
316
+ newPosition.y = heightAtNewPos + TANK_HEIGHT;
317
+
318
+ // ๊ฒฝ์‚ฌ ์ฒดํฌ
319
+ const heightDifference = Math.abs(newPosition.y - this.mesh.position.y);
320
+ const maxClimbAngle = 0.5;
321
+
322
+ if (heightDifference / this.moveSpeed < maxClimbAngle) {
323
+ this.mesh.position.copy(newPosition);
324
+
325
+ // ์  ํƒฑํฌ ๊ธฐ์šธ๊ธฐ ์กฐ์ •
326
+ const forwardVector = new THREE.Vector3(0, 0, 1).applyQuaternion(this.mesh.quaternion);
327
+ const rightVector = new THREE.Vector3(1, 0, 0).applyQuaternion(this.mesh.quaternion);
328
+
329
+ const frontHeight = window.gameInstance.getHeightAtPosition(
330
+ newPosition.x + forwardVector.x,
331
+ newPosition.z + forwardVector.z
332
+ );
333
+ const backHeight = window.gameInstance.getHeightAtPosition(
334
+ newPosition.x - forwardVector.x,
335
+ newPosition.z - forwardVector.z
336
+ );
337
+ const rightHeight = window.gameInstance.getHeightAtPosition(
338
+ newPosition.x + rightVector.x,
339
+ newPosition.z + rightVector.z
340
+ );
341
+ const leftHeight = window.gameInstance.getHeightAtPosition(
342
+ newPosition.x - rightVector.x,
343
+ newPosition.z - rightVector.z
344
+ );
345
+
346
+ const pitch = Math.atan2(frontHeight - backHeight, 2);
347
+ const roll = Math.atan2(rightHeight - leftHeight, 2);
348
+
349
+ this.mesh.rotation.x = pitch;
350
+ this.mesh.rotation.z = roll;
351
+ }
352
  }
353
 
354
+
355
  // ์ด์•Œ ์—…๋ฐ์ดํŠธ ๋ถ€๋ถ„
356
  for (let i = this.bullets.length - 1; i >= 0; i--) {
357
  const bullet = this.bullets[i];