Spaces:
Running
Running
Update game.js
Browse files
game.js
CHANGED
|
@@ -139,35 +139,112 @@ class TankPlayer {
|
|
| 139 |
}
|
| 140 |
|
| 141 |
move(direction) {
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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];
|