This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
107 lines (87 loc) · 2.51 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
class Player{
constructor(x, y, z, color){
this.radius = 1;
this.geometry = new THREE.SphereGeometry( this.radius, 0.3, 0.2 );
this.material = new THREE.MeshToonMaterial( {color: color} );
this.mesh = new THREE.Mesh( this.geometry, this.material );
this.mesh.position.x = x;
this.mesh.position.y = y;
this.mesh.position.z = z;
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
this.speed = new THREE.Vector3(0, 0, 0);
}
getMesh() {
return this.mesh;
}
getRadius() {
return this.radius;
}
getSpeed() {
return this.speed;
}
setSpeed(speed) {
this.speed = speed;
}
reset(a) {
if(a) {
this.mesh.position.set(0, 0, -4.5);
} else {
this.mesh.position.set(0, 0, 4.5);
}
this.speed.set(0, 0, 0);
}
gravity() {
this.speed.y = this.speed.y - 0.5;
if(this.speed.y < -16) this.speed.y = -16;
if(this.mesh.position.y < this.radius) {
this.mesh.position.y = this.radius + 0.1;
// this.speed.y = -1;
}
}
border() {
//X
if (this.mesh.position.x<-4) this.mesh.position.x = -4;
if (this.mesh.position.x>4) this.mesh.position.x = 4;
//Z
if (this.mesh.position.z<-8.5) this.mesh.position.z = -8.5;
if (this.mesh.position.z>8.5) this.mesh.position.z = 8.5;
//NET
if(this.mesh.position.z < 0){ // if left
if(this.mesh.position.z > -this.radius-0.1) {
// this.mesh.position.z = -this.radius-0.1
if(this.speed.z > 0){
this.speed.z = 0;
}
}
} else { // if right
if(this.mesh.position.z < this.radius+0.1) {
// this.mesh.position.z = this.radius+0.1
if(this.speed.z < 0){
this.speed.z = 0;
}
}
}
}
jump() {
if(this.mesh.position.y <= this.radius + 0.1) {
this.speed.y = 17;
}
}
update(keyDown, left, forward, right, backward, jump) {
let setspeed = 15;
this.speed = new THREE.Vector3(0, this.speed.y, 0);
if (keyDown[left]) this.speed.z = -setspeed;
if (keyDown[forward]) this.speed.x = setspeed;
if (keyDown[right]) this.speed.z = setspeed;
if (keyDown[backward]) this.speed.x = -setspeed;
if (!(keyDown[left] || keyDown[right])) this.speed.z = 0;
if (!(keyDown[forward] || keyDown[backward])) this.speed.x = 0;
if(keyDown[jump]) this.jump();
this.gravity();
this.border();
this.mesh.position.x +=this.speed.x * 0.01;
this.mesh.position.z +=this.speed.z * 0.01;
this.mesh.position.y +=this.speed.y * 0.01;
}
}