-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
216 lines (179 loc) · 6.04 KB
/
main.js
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
import * as THREE from "three";
import Player from "./player.js";
import Weapon from "./weapon.js";
import io from "socket.io-client";
class Game {
constructor() {
this.connectSocket();
this.initScene();
this.initPlayer();
this.initWeapon();
this.addEventListeners();
this.animate();
this.playerHeight = 250;
this.playerDistance = 75;
this.members = [];
}
initScene() {
this.scene = new THREE.Scene();
// Perspective camera for 3D effect
this.camera = new THREE.PerspectiveCamera(
75, // Field of view
window.innerWidth / window.innerHeight, // Aspect ratio
0.1, // Near clipping plane
1000 // Far clipping plane
);
// Position the camera behind and above the player, looking at the player
this.camera.position.set(0, 200, 30); // Adjust the Y (height) and Z (distance)
this.camera.lookAt(0, 0, 0); // Look at the origin (the player)
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
// Texture loader
const textureLoader = new THREE.TextureLoader();
const groundTexture = textureLoader.load("textures/grass.png"); // Load grass texture
groundTexture.wrapS = THREE.RepeatWrapping; // Repeat the texture horizontally
groundTexture.wrapT = THREE.RepeatWrapping; // Repeat the texture vertically
groundTexture.repeat.set(40, 40); // How many times the texture repeats on the plane
// Ground with texture
const groundGeometry = new THREE.PlaneGeometry(2000, 2000, 32, 32);
const groundMaterial = new THREE.MeshStandardMaterial({
map: groundTexture,
});
this.ground = new THREE.Mesh(groundGeometry, groundMaterial);
this.ground.rotation.x = -Math.PI / 2; // Lay flat
this.ground.position.y = 0;
// Allow the ground to receive shadows
this.ground.receiveShadow = true;
this.scene.add(this.ground);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040);
this.scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(100, 200, 100);
directionalLight.castShadow = true;
this.scene.add(directionalLight);
window.addEventListener("resize", this.onWindowResize.bind(this));
}
joinRoom() {
this.socket.emit("JOIN", {
member: {
id: this.socket.id,
name: "PlayerX",
position: this.player.mesh.position,
},
});
this.watchJoin();
this.watchLeave();
}
watchMembers() {
this.socket.on("MEMBERS", (data) => {
this.members = data.map((datumn) => {
const player = new Player(this.scene, this.socket, datumn.member);
this.scene.add(player.mesh);
const member = { id: datumn.member.id, player };
return member;
});
});
}
watchJoin() {
this.socket.on("JOIN", (data) => {
console.log("JOIN event received:", data);
const player = new Player(this.scene, this.socket, data.member);
this.members = [...this.members, { id: data.member.id, player }];
this.scene.add(player.mesh);
console.log("MEMBERS:", this.members);
});
}
watchLeave() {
this.socket.on("LEAVE", (data) => {
console.log("LEAVE event received:", data);
const leavingPlayer = this.members.find(
(member) => member.id === data.id
);
if (leavingPlayer) {
this.scene.remove(leavingPlayer.player.mesh);
}
this.members = this.members.filter((member) => member.id !== data.id);
});
}
// Initialize the player
initPlayer() {
this.player = new Player(this.scene, this.socket);
this.scene.add(this.player.mesh);
}
initWeapon() {
this.weapon = new Weapon(
this.scene,
this.player,
this.camera,
this.ground,
this.socket
);
}
// Handle window resizing
onWindowResize() {
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.camera.left = -window.innerWidth / 2;
this.camera.right = window.innerWidth / 2;
this.camera.top = window.innerHeight / 2;
this.camera.bottom = -window.innerHeight / 2;
this.camera.updateProjectionMatrix();
}
addEventListeners() {
document.addEventListener(
"keydown",
this.player.onKeyDown.bind(this.player)
);
document.addEventListener("keyup", this.player.onKeyUp.bind(this.player));
// On scroll, change the distance of the camera from the player
document.addEventListener("wheel", (event) => {
if (this.playerDistance < 75 && event.deltaY < 0) return;
if (this.playerDistance > 250 && event.deltaY > 0) return;
this.playerDistance += event.deltaY * 0.1;
this.playerHeight += event.deltaY * 0.1;
});
// bind weapons
document.addEventListener(
"mousedown",
this.weapon.onMouseDown.bind(this.weapon)
);
document.addEventListener(
"mousemove",
this.weapon.onMouseMove.bind(this.weapon)
);
}
connectSocket() {
console.log("Connecting to server");
this.socket = io("http://localhost:3000");
this.socket.on("connect", () => {
console.log("WELCOME ", this.socket.id);
this.joinRoom();
this.watchMembers();
});
this.socket.on("POSITION", (data) => {
const member = this.members.find((member) => member.id === data.id);
if (member) {
member.player.mesh.position.set(
data.position.x,
data.position.y,
data.position.z
);
}
});
this.socket.on("disconnect", () => {
this.socket.emit("LEAVE", { id: this.socket.id });
});
}
animate() {
requestAnimationFrame(this.animate.bind(this));
this.player.update();
const playerPosition = this.player.mesh.position;
this.camera.position.x = playerPosition.x;
this.camera.position.y = playerPosition.y + this.playerHeight;
this.camera.position.z = playerPosition.z + this.playerDistance;
this.camera.lookAt(playerPosition);
this.renderer.render(this.scene, this.camera);
}
}
new Game();