three-base 2.0.123 → 2.0.125
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FPScontrol.d.ts +4 -0
- package/FPScontrol.js +35 -0
- package/package.json +1 -1
package/FPScontrol.d.ts
CHANGED
|
@@ -16,13 +16,17 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
16
16
|
STEPS_PER_FRAME: number;
|
|
17
17
|
speed: number;
|
|
18
18
|
camera: Camera;
|
|
19
|
+
inertia: boolean;
|
|
19
20
|
octreeMap: Map<number, Octree>;
|
|
20
21
|
octree: Octree;
|
|
21
22
|
addOctree(obj: Object3D): Octree;
|
|
22
23
|
playerCollider: Capsule;
|
|
23
24
|
clock: Clock;
|
|
25
|
+
prePlayerVelocity: Vector3;
|
|
24
26
|
playerVelocity: Vector3;
|
|
27
|
+
jumping: boolean;
|
|
25
28
|
playerDirection: Vector3;
|
|
29
|
+
prePlayerOnFloor: boolean;
|
|
26
30
|
playerOnFloor: boolean;
|
|
27
31
|
mouseTime: number;
|
|
28
32
|
isMousedown: boolean;
|
package/FPScontrol.js
CHANGED
|
@@ -31,6 +31,8 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
31
31
|
STEPS_PER_FRAME = 5; // 每帧碰撞计算的次数
|
|
32
32
|
speed = 1; // 速度
|
|
33
33
|
camera;
|
|
34
|
+
// 移动时是否保持惯性 由外部来配置
|
|
35
|
+
inertia = true;
|
|
34
36
|
// 八叉树数组
|
|
35
37
|
octreeMap = new Map();
|
|
36
38
|
octree = (() => {
|
|
@@ -67,8 +69,12 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
67
69
|
playerCollider = new Capsule(new Vector3(0, 0.35, 0), new Vector3(0, 1, 0), 0.35);
|
|
68
70
|
// 时钟对象
|
|
69
71
|
clock = new Clock();
|
|
72
|
+
prePlayerVelocity = new Vector3();
|
|
70
73
|
playerVelocity = new Vector3();
|
|
74
|
+
// 起跳中
|
|
75
|
+
jumping = false;
|
|
71
76
|
playerDirection = new Vector3();
|
|
77
|
+
prePlayerOnFloor = false;
|
|
72
78
|
playerOnFloor = false;
|
|
73
79
|
mouseTime = 0;
|
|
74
80
|
isMousedown = false;
|
|
@@ -119,6 +125,7 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
119
125
|
// 玩家移动控制器
|
|
120
126
|
controls = (deltaTime) => {
|
|
121
127
|
const speedDelta = deltaTime * (this.playerOnFloor ? 25 : 8) * this.speed;
|
|
128
|
+
// const speedDelta = this.speed;
|
|
122
129
|
if (this.keyStates["KeyW"]) {
|
|
123
130
|
this.playerVelocity.add(this.getForwardVector().multiplyScalar(speedDelta));
|
|
124
131
|
}
|
|
@@ -131,9 +138,22 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
131
138
|
if (this.keyStates["KeyD"]) {
|
|
132
139
|
this.playerVelocity.add(this.getSideVector().multiplyScalar(speedDelta));
|
|
133
140
|
}
|
|
141
|
+
if (this.playerOnFloor &&
|
|
142
|
+
!this.keyStates["KeyW"] &&
|
|
143
|
+
!this.keyStates["KeyS"] &&
|
|
144
|
+
!this.keyStates["KeyA"] &&
|
|
145
|
+
!this.keyStates["KeyD"]) {
|
|
146
|
+
if (!this.inertia) {
|
|
147
|
+
// 关闭惯性时就大幅增加动能的衰减
|
|
148
|
+
this.playerVelocity.x *= 0.85;
|
|
149
|
+
this.playerVelocity.z *= 0.85;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
134
152
|
if (this.playerOnFloor) {
|
|
135
153
|
if (this.keyStates["Space"]) {
|
|
136
154
|
this.playerVelocity.y = 15;
|
|
155
|
+
this.jumping = true;
|
|
156
|
+
this.dispatchEvent({ type: "jump_start" });
|
|
137
157
|
}
|
|
138
158
|
}
|
|
139
159
|
};
|
|
@@ -161,8 +181,23 @@ export default class FPScontrol extends EventDispatcher {
|
|
|
161
181
|
if (!this.playerOnFloor) {
|
|
162
182
|
this.playerVelocity.addScaledVector(result.normal, -result.normal.dot(this.playerVelocity));
|
|
163
183
|
}
|
|
184
|
+
const { x: preX, y: preY, z: preZ } = this.prePlayerVelocity;
|
|
185
|
+
const { x, y, z } = this.playerVelocity;
|
|
186
|
+
if (Math.abs(preX + preZ) < 0.1 && Math.abs(x + z) > 0.1) {
|
|
187
|
+
this.dispatchEvent({ type: "move_start" });
|
|
188
|
+
}
|
|
189
|
+
else if (Math.abs(preX + preZ) > 0.1 && Math.abs(x + z) < 0.1) {
|
|
190
|
+
this.dispatchEvent({ type: "move_end" });
|
|
191
|
+
}
|
|
192
|
+
this.prePlayerVelocity.copy(this.playerVelocity);
|
|
164
193
|
this.playerCollider.translate(result.normal.multiplyScalar(result.depth));
|
|
165
194
|
}
|
|
195
|
+
if (this.jumping && !this.prePlayerOnFloor && this.playerOnFloor) {
|
|
196
|
+
// 分发落地事件
|
|
197
|
+
this.jumping = false;
|
|
198
|
+
this.dispatchEvent({ type: "jump_end" });
|
|
199
|
+
}
|
|
200
|
+
this.prePlayerOnFloor = this.playerOnFloor;
|
|
166
201
|
}
|
|
167
202
|
// 更新控制器的位置
|
|
168
203
|
teleportPlayerIfOob = () => {
|