-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
134 lines (114 loc) · 3.11 KB
/
player.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
import * as THREE from "three";
class Player {
constructor(scene, socket, member = null) {
this.movement = {
left: false,
right: false,
forward: false,
backward: false,
};
this.scene = scene;
this.socket = socket;
this.initModel();
this.speed = 3;
this.lean = 0.15;
if (member) {
this.mesh.position.copy(member.position);
}
}
emitPosition() {
if (this.isEmitting) return;
this.isEmitting = true;
this.socket.emit("POSITION", {
id: this.socket.id,
position: this.mesh.position,
});
setTimeout(() => {
this.isEmitting = false;
}, 35);
}
initModel() {
// Pentagon-shaped UFO Body
const bodyGeometry = new THREE.CylinderGeometry(15, 15, 5);
const bodyMaterial = new THREE.MeshToonMaterial({
color: 0x4b7bb9,
emissive: 0x4b7bb9,
emissiveIntensity: 0.5,
});
this.body = new THREE.Mesh(bodyGeometry, bodyMaterial);
// UFO Dome (top part)
const domeGeometry = new THREE.SphereGeometry(10, 32, 2); // Dome shape
const domeMaterial = new THREE.MeshToonMaterial({
color: 0xff780a,
emissive: 0xff0000,
emissiveIntensity: 0.5,
});
this.dome = new THREE.Mesh(domeGeometry, domeMaterial);
// Position the dome on top of the body
this.dome.position.y = 7; // Adjust Y to sit on top of the disk
// Group all parts together
this.mesh = new THREE.Group();
this.mesh.add(this.body);
this.mesh.add(this.dome);
// Add UFO to the scene
this.scene.add(this.mesh);
}
// Move the player based on movement input
update() {
this.mesh.rotation.x = 0;
this.mesh.rotation.z = 0;
this.mesh.position.y = 100;
if (this.movement.forward) this.mesh.position.z -= this.speed;
if (this.movement.backward) this.mesh.position.z += this.speed;
if (this.movement.left) this.mesh.position.x -= this.speed;
if (this.movement.right) this.mesh.position.x += this.speed;
// Adjust lean
if (this.movement.forward) this.mesh.rotation.x = -this.lean;
if (this.movement.backward) this.mesh.rotation.x = this.lean;
if (this.movement.left) this.mesh.rotation.z = this.lean;
if (this.movement.right) this.mesh.rotation.z = -this.lean;
if (
this.movement.forward ||
this.movement.backward ||
this.movement.left ||
this.movement.right
) {
this.emitPosition();
}
}
// Handle keydown events
onKeyDown(event) {
switch (event.key) {
case "w":
this.movement.forward = true;
break;
case "a":
this.movement.left = true;
break;
case "s":
this.movement.backward = true;
break;
case "d":
this.movement.right = true;
break;
}
}
// Handle keyup events
onKeyUp(event) {
switch (event.key) {
case "w":
this.movement.forward = false;
break;
case "a":
this.movement.left = false;
break;
case "s":
this.movement.backward = false;
break;
case "d":
this.movement.right = false;
break;
}
}
}
export default Player;