-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d-manager.js
213 lines (195 loc) · 6.09 KB
/
2d-manager.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
import * as THREE from 'three';
import {getRenderer, camera, scene, setCameraType} from './renderer.js';
import physicsManager from './physics-manager.js';
import {playersManager} from './players-manager.js';
import {PathFinder} from './npc-utils.js';
import {PointerControls} from './scene2D-utils.js';
import cameraManager from './camera-manager.js';
const localVector = new THREE.Vector3();
class Scene2DManager {
constructor() {
this.enabled = false;
this.perspective = null;
this.cameraMode = null;
this.scrollDirection = null;
this.viewSize = 0;
this.zoomFactor = 1;
this.cursorPosition = new THREE.Vector2(0, 0);
this.lastCursorPosition = null;
this.cursorSensitivity = 0.75;
this.maxCursorDistance = 3;
this.pointerControls = null;
this.pathFinder = new PathFinder({debugRender: false});
}
handleWheelEvent(e) {
switch (this.perspective) {
case 'isometric': {
this.zoomFactor = THREE.MathUtils.clamp(
(this.zoomFactor -= e.deltaY * 0.01),
1,
1.5,
);
camera.zoom = this.zoomFactor;
camera.updateProjectionMatrix();
break;
}
case 'side-scroll': {
// nothing yet
break;
}
case 'top-down': {
this.zoomFactor = THREE.MathUtils.clamp(
(this.zoomFactor -= e.deltaY * 0.001),
1,
2.5,
);
camera.zoom = this.zoomFactor;
camera.updateProjectionMatrix();
break;
}
default: {
break;
}
}
}
handleMouseMove(e) {
const {movementX, movementY} = e;
switch (this.perspective) {
case 'side-scroll': {
const cursorPosition = this.cursorPosition;
let lastCursorPosition = this.lastCursorPosition;
const size = getRenderer().getSize(localVector);
const sensitivity = this.cursorSensitivity;
const crosshairEl = document.getElementById('crosshair');
const maxCursorDistance = this.maxCursorDistance;
let clampedX = THREE.MathUtils.clamp(cursorPosition.x, 0, size.x);
let clampedY = THREE.MathUtils.clamp(cursorPosition.y, 0, size.y);
clampedX += movementX * sensitivity;
clampedY += movementY * sensitivity;
cursorPosition.set(clampedX, clampedY);
crosshairEl.style.left = clampedX + 'px';
crosshairEl.style.top = clampedY + 'px';
break;
}
case 'isometric': {
if (!this.pointerControls) {
break;
}
const cursorPosition = this.cursorPosition;
let lastCursorPosition = this.lastCursorPosition;
const size = getRenderer().getSize(localVector);
const sensitivity = this.cursorSensitivity;
const crosshairEl = document.getElementById('crosshair');
const maxCursorDistance = this.maxCursorDistance;
let clampedX = THREE.MathUtils.clamp(cursorPosition.x, 0, size.x);
let clampedY = THREE.MathUtils.clamp(cursorPosition.y, 0, size.y);
clampedX += movementX * sensitivity;
clampedY += movementY * sensitivity;
cursorPosition.set(clampedX, clampedY);
crosshairEl.style.left = clampedX + 'px';
crosshairEl.style.top = clampedY + 'px';
break;
}
default:
break;
}
}
setMode(
perspective = 'top-down',
cameraMode = 'follow',
viewSize,
scrollDirection = 'none',
controls = 'default',
) {
this.reset();
this.perspective = perspective;
this.cameraMode = cameraMode;
this.scrollDirection = scrollDirection;
this.viewSize = viewSize;
this.enabled = true;
setCameraType('orthographic', viewSize, perspective);
}
reset() {
this.enabled = false;
this.disablePointerControls();
setCameraType('perspective');
}
getPointerControls() {
return this.pointerControls;
}
enablePointerControls() {
this.pointerControls = new PointerControls();
}
disablePointerControls() {
this.pointerControls = null;
}
getPath(vec1, vec2) {
return this.pathFinder.getPath(vec1, vec2);
}
getCursorPosition() {
let vector = new THREE.Vector3();
vector.set(
(this.cursorPosition.x / window.innerWidth) * 2 - 1,
-(this.cursorPosition.y / window.innerHeight) * 2 + 1,
0,
);
vector.unproject(camera);
return new THREE.Vector3(vector.x, vector.y, 0);
}
getScreenCursorPosition() {
let vector = new THREE.Vector3();
vector.set(
(this.cursorPosition.x / window.innerWidth) * 2 - 1,
-(this.cursorPosition.y / window.innerHeight) * 2 + 1,
0,
);
return new THREE.Vector2(vector.x, vector.y);
}
castFromCursor() {
let vector = new THREE.Vector3();
let dir = new THREE.Vector3();
vector.set(
(this.cursorPosition.x / window.innerWidth) * 2 - 1,
-(this.cursorPosition.y / window.innerHeight) * 2 + 1,
-1,
);
vector.unproject(camera);
dir.set(0, 0, -1).transformDirection(camera.matrixWorld);
raycaster.set(vector, dir);
const physicsScene = physicsManager.getScene();
let ray = physicsScene.raycast(vector, camera.quaternion);
if (ray) {
return ray;
}
}
getCursorQuaternionFromOrigin(origin) {
let cursorPos = this.getCursorPosition();
let tempObj = new THREE.Object3D();
tempObj.position.copy(origin);
tempObj.lookAt(new THREE.Vector3(cursorPos.x, cursorPos.y, cursorPos.z));
tempObj.rotation.x = -tempObj.rotation.x;
tempObj.rotation.y = -tempObj.rotation.y;
return tempObj.quaternion;
}
getViewDirection() {
let viewDir = new THREE.Vector3();
playersManager.getLocalPlayer().getWorldDirection(viewDir);
return viewDir.x > 0 ? 'left' : 'right';
}
handleClick() {
if (this.pointerControls) {
this.pointerControls.handleCursorClick();
}
}
update(timestamp, timeDiff) {
//const localPlayer = playersManager.getLocalPlayer();
if (this.perspective === 'top-down') {
cameraManager.exitPointerLock();
}
if (this.pointerControls) {
this.pointerControls.update(timestamp, timeDiff);
}
}
}
const scene2DManager = new Scene2DManager();
export default scene2DManager;