Spaces:
Running
Running
cutechicken
commited on
Commit
โข
53ab759
1
Parent(s):
5a6ad6d
Update game.js
Browse files
game.js
CHANGED
@@ -40,11 +40,11 @@ class TankPlayer {
|
|
40 |
|
41 |
async initialize(scene, loader) {
|
42 |
try {
|
43 |
-
const bodyResult = await loader.loadAsync('/models/
|
44 |
this.body = bodyResult.scene;
|
45 |
this.body.position.copy(this.position);
|
46 |
|
47 |
-
const turretResult = await loader.loadAsync('/models/
|
48 |
this.turret = turretResult.scene;
|
49 |
|
50 |
this.turretGroup.position.y = 0.2;
|
@@ -105,12 +105,6 @@ class TankPlayer {
|
|
105 |
update(mouseX, mouseY, scene) {
|
106 |
if (!this.body || !this.turretGroup) return;
|
107 |
|
108 |
-
// ํฌํ ํ์ ๊ณ์ฐ ์์
|
109 |
-
const rotationAngle = Math.atan2(mouseX, mouseY);
|
110 |
-
this.turretRotation = rotationAngle;
|
111 |
-
this.turretGroup.rotation.y = this.turretRotation;
|
112 |
-
|
113 |
-
// ์ด์ ์
๋ฐ์ดํธ ๋ก์ง
|
114 |
for (let i = this.bullets.length - 1; i >= 0; i--) {
|
115 |
const bullet = this.bullets[i];
|
116 |
bullet.position.add(bullet.velocity);
|
@@ -121,6 +115,13 @@ class TankPlayer {
|
|
121 |
this.bullets.splice(i, 1);
|
122 |
}
|
123 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
}
|
125 |
|
126 |
move(direction) {
|
@@ -164,7 +165,7 @@ class Enemy {
|
|
164 |
|
165 |
async initialize(loader) {
|
166 |
try {
|
167 |
-
const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/
|
168 |
const result = await loader.loadAsync(modelPath);
|
169 |
this.mesh = result.scene;
|
170 |
this.mesh.position.copy(this.position);
|
@@ -509,54 +510,53 @@ async addDesertDecorations() {
|
|
509 |
}
|
510 |
|
511 |
handleMovement() {
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
if (this.keys.left) this.tank.rotate(-1);
|
525 |
-
if (this.keys.right) this.tank.rotate(1);
|
526 |
-
|
527 |
-
direction.applyEuler(this.tank.body.rotation);
|
528 |
-
this.tank.move(direction);
|
529 |
-
}
|
530 |
-
|
531 |
-
// ๋ง์ฐ์ค ์์ง์์ ๋ฐ๋ฅธ ํฌํ ํ์ ์ฒ๋ฆฌ
|
532 |
-
const tankRotation = this.tank.body.rotation.y;
|
533 |
-
const mouseAngle = Math.atan2(this.mouse.x, -this.mouse.y);
|
534 |
-
const finalRotation = mouseAngle + tankRotation;
|
535 |
|
536 |
-
if (this.tank.
|
537 |
-
|
538 |
-
}
|
539 |
-
|
540 |
-
// ์นด๋ฉ๋ผ ์์น ์
๋ฐ์ดํธ
|
541 |
-
const tankPos = this.tank.getPosition();
|
542 |
-
const cameraDistance = 30;
|
543 |
-
const cameraHeight = 15;
|
544 |
-
const lookAtHeight = 5;
|
545 |
|
546 |
-
this.
|
547 |
-
|
548 |
-
|
549 |
-
tankPos.z - Math.cos(finalRotation) * cameraDistance
|
550 |
-
);
|
551 |
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
this.camera.lookAt(lookAtPoint);
|
559 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
560 |
}
|
561 |
|
562 |
createBuildings() {
|
|
|
40 |
|
41 |
async initialize(scene, loader) {
|
42 |
try {
|
43 |
+
const bodyResult = await loader.loadAsync('/models/abramsBody.glb');
|
44 |
this.body = bodyResult.scene;
|
45 |
this.body.position.copy(this.position);
|
46 |
|
47 |
+
const turretResult = await loader.loadAsync('/models/abramsTurret.glb');
|
48 |
this.turret = turretResult.scene;
|
49 |
|
50 |
this.turretGroup.position.y = 0.2;
|
|
|
105 |
update(mouseX, mouseY, scene) {
|
106 |
if (!this.body || !this.turretGroup) return;
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
for (let i = this.bullets.length - 1; i >= 0; i--) {
|
109 |
const bullet = this.bullets[i];
|
110 |
bullet.position.add(bullet.velocity);
|
|
|
115 |
this.bullets.splice(i, 1);
|
116 |
}
|
117 |
}
|
118 |
+
|
119 |
+
// ํฌํ ํ์ ๋ฐฉํฅ ์์
|
120 |
+
if (this.turretGroup) {
|
121 |
+
const rotationAngle = -Math.atan2(mouseX, mouseY); // ๋ถํธ๋ฅผ ๋ฐ๋๋ก ํ์ฌ ํ์ ๋ฐฉํฅ ๋ณ๊ฒฝ
|
122 |
+
this.turretRotation = rotationAngle;
|
123 |
+
this.turretGroup.rotation.y = this.turretRotation;
|
124 |
+
}
|
125 |
}
|
126 |
|
127 |
move(direction) {
|
|
|
165 |
|
166 |
async initialize(loader) {
|
167 |
try {
|
168 |
+
const modelPath = this.type === 'tank' ? '/models/enemy1.glb' : '/models/enemy4.glb';
|
169 |
const result = await loader.loadAsync(modelPath);
|
170 |
this.mesh = result.scene;
|
171 |
this.mesh.position.copy(this.position);
|
|
|
510 |
}
|
511 |
|
512 |
handleMovement() {
|
513 |
+
if (!this.tank.isLoaded || this.isGameOver) return;
|
514 |
+
|
515 |
+
const direction = new THREE.Vector3();
|
516 |
+
|
517 |
+
if (this.keys.forward) direction.z += 1;
|
518 |
+
if (this.keys.backward) direction.z -= 1;
|
519 |
+
if (this.keys.left) direction.x -= 1;
|
520 |
+
if (this.keys.right) direction.x += 1;
|
521 |
+
|
522 |
+
if (direction.length() > 0) {
|
523 |
+
direction.normalize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
524 |
|
525 |
+
if (this.keys.left) this.tank.rotate(-1);
|
526 |
+
if (this.keys.right) this.tank.rotate(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
|
528 |
+
direction.applyEuler(this.tank.body.rotation);
|
529 |
+
this.tank.move(direction);
|
530 |
+
}
|
|
|
|
|
531 |
|
532 |
+
// ํฌํ ํ์ ์
๋ฐ์ดํธ
|
533 |
+
const mouseVector = new THREE.Vector2(this.mouse.x, -this.mouse.y);
|
534 |
+
const rotationAngle = -Math.atan2(mouseVector.x, mouseVector.y); // ๋ถํธ ๋ณ๊ฒฝ
|
535 |
+
|
536 |
+
if (this.tank.turretGroup) {
|
537 |
+
this.tank.turretGroup.rotation.y = rotationAngle;
|
|
|
538 |
}
|
539 |
+
|
540 |
+
// ์นด๋ฉ๋ผ ์์น ์
๋ฐ์ดํธ
|
541 |
+
const tankPos = this.tank.getPosition();
|
542 |
+
const cameraDistance = 30;
|
543 |
+
const cameraHeight = 15;
|
544 |
+
const lookAtHeight = 5;
|
545 |
+
|
546 |
+
const turretRotation = this.tank.turretGroup.rotation.y + this.tank.body.rotation.y;
|
547 |
+
this.camera.position.set(
|
548 |
+
tankPos.x - Math.sin(turretRotation) * cameraDistance,
|
549 |
+
tankPos.y + cameraHeight,
|
550 |
+
tankPos.z - Math.cos(turretRotation) * cameraDistance
|
551 |
+
);
|
552 |
+
|
553 |
+
const lookAtPoint = new THREE.Vector3(
|
554 |
+
tankPos.x + Math.sin(turretRotation) * 10,
|
555 |
+
tankPos.y + lookAtHeight,
|
556 |
+
tankPos.z + Math.cos(turretRotation) * 10
|
557 |
+
);
|
558 |
+
|
559 |
+
this.camera.lookAt(lookAtPoint);
|
560 |
}
|
561 |
|
562 |
createBuildings() {
|