-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlManager.cpp
More file actions
301 lines (251 loc) · 8.38 KB
/
Copy pathControlManager.cpp
File metadata and controls
301 lines (251 loc) · 8.38 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "settings.h"
#include "commons.h"
#include "ControlManager.h"
#include "commons.h"
#include <cml/cml.h>
using namespace control;
using namespace geometry;
using namespace cml;
using namespace std;
#define GOTO "GOTO"
namespace game_utils
{
namespace managers
{
CControlManager::CControlManager(): CManager()
{
FPS = false;
}
CControlManager::~CControlManager()
{
}
bool CControlManager::init()
{
viewFrustum.initialize(CV_SETTINGS_FOV,(GLfloat)CV_SETTINGS_WINDOW_WIDTH/(GLfloat)CV_SETTINGS_WINDOW_HEIGHT,0.01f,10000.0f);
//camera.setPosition(vector3f(CV_BLOCK_WIDTH*42.0f,CV_CAMERA_INITIAL_HEIGHT,CV_BLOCK_DEPTH*42.0f));
camera.setPosition(vector3f(CV_BLOCK_WIDTH*4.0f,CV_CAMERA_INITIAL_HEIGHT,CV_BLOCK_DEPTH*4.0f));
CSettingsManager *sManager = CV_GAME_MANAGER->getSettingsManager();
camRotYSpeed = sManager->getSetting_Float(CV_SETTINGS_CAMERA_ROTATE_Y_SPEED);
camZoomSpeed = sManager->getSetting_Float(CV_SETTINGS_CAMERA_ZOOM_SPEED);
camMoveSpeed = sManager->getSetting_Float(CV_SETTINGS_CAMERA_MOVE_SPEED);
camMLookSpeed = sManager->getSetting_Float(CV_SETTINGS_CAMERA_MLOOK_SPEED);
camInitXRot = sManager->getSetting_Float(CV_SETTINGS_CAMERA_INITIAL_X_ROTATION);
camera.rotateX(camInitXRot);
// register camera as a possible collision entity with terrain
CV_GAME_MANAGER->getCollisionManager()->registerEntity_T(&camera);
// register to console
CV_GAME_MANAGER->getConsole()->registerClass(this,"CONTROL MANAGER");
CV_GAME_MANAGER->getConsole()->addParam(GOTO,"(x y) Jumps to a block on x,y location. x and y must be (>=0 && <84)");
return true;
}
bool CControlManager::update()
{
input.update();
vector3f camPos = camera.getPosition();
GLfloat delta = CV_GAME_MANAGER->getDeltaTime();
//zoom with scroll wheel
float scroll = -(float)input.checkScroll()/120.0f;
camera.move(vector3f(0.0f,camZoomSpeed*delta*scroll,0.0f));
//clip camera pos
if (camera.getPosition()[1]>=CV_CAMERA_MAX_HEIGHT)
{
camPos[1] = CV_CAMERA_MAX_HEIGHT;
camera.setPosition(camPos);
}
else if (camera.getPosition()[1]<CV_CAMERA_MIN_HEIGHT)
{
camPos[1] = CV_CAMERA_MIN_HEIGHT;
camera.setPosition(camPos);
}
if (input.isMiddleMouseDown())
{
//if the middle mouse button is down, we can rotate the view with the mouse
camera.moveForward(!FPS?CV_BLOCK_DEPTH*(camPos[1]+2.0f):0.0f);
camera.rotateY(camRotYSpeed*delta*input.getMouseMoveX());
camera.rotateX(camRotYSpeed*delta*input.getMouseMoveY());
camera.moveForward(!FPS?-CV_BLOCK_DEPTH*(camPos[1]+2.0f):0.0f);
}else
{
//move camera when mouse is near edges of screen
#ifdef WIN32
POINT pt;
GetCursorPos(&pt);
RECT rect;
GetWindowRect(CV_WINDOW_HANDLE,&rect);
#else
// TODO Tequila: This is only correct in fullscreen and input grab mode
MOUSEPOINT pt;
RECT rect;
SDL_GetMouseState(&pt.x, &pt.y);
rect.left = 0;
rect.right = CV_SETTINGS_WINDOW_WIDTH;
rect.top = 0;
rect.bottom = CV_SETTINGS_WINDOW_HEIGHT;
#endif
int border=20;
if(pt.x<rect.left+border)
camera.strafeLeft(camMoveSpeed*delta);
else if(pt.x>rect.right-border)
camera.strafeLeft(-camMoveSpeed*delta);
GLfloat tmpRotX = camera.getRotateX();
camera.rotateX(!FPS?-tmpRotX:0.0f);
if(pt.y<rect.top+border)
camera.moveForward(camMoveSpeed*delta);
else if(pt.y>rect.bottom-border)
camera.moveForward(-camMoveSpeed*delta);
camera.rotateX(!FPS?tmpRotX:0.0f);
if (input.isKeyDown(VK_CONTROL))
{
// handle rotation and zoom-in/out
bool rotate = (input.isKeyDown(VK_LEFT) || input.isKeyDown(VK_RIGHT));
bool zoom = (input.isKeyDown(VK_UP) || input.isKeyDown(VK_DOWN)) && !FPS; // no zoom in FPS
if (rotate)
{
camera.moveForward(!FPS?CV_BLOCK_DEPTH*(camPos[1]+2.0f):0.0f);
camera.rotateY(camRotYSpeed*delta*(input.isKeyDown(VK_LEFT)?-1.0f:1.0f));
camera.moveForward(!FPS?-CV_BLOCK_DEPTH*(camPos[1]+2.0f):0.0f);
}
else if (zoom)
{
camera.move(vector3f(0.0f,camZoomSpeed*delta*(input.isKeyDown(VK_UP)?-1.0f:1.0f),0.0f));
if (camera.getPosition()[1]>=CV_CAMERA_MAX_HEIGHT)
{
camPos[1] = CV_CAMERA_MAX_HEIGHT;
camera.setPosition(camPos);
}
else if (camera.getPosition()[1]<CV_CAMERA_MIN_HEIGHT)
{
camPos[1] = CV_CAMERA_MIN_HEIGHT;
camera.setPosition(camPos);
}
}
}
else
{
// handle standard movement
bool l_r = input.isKeyDown(VK_LEFT) || input.isKeyDown(VK_RIGHT);
bool u_d = input.isKeyDown(VK_UP) || input.isKeyDown(VK_DOWN);
if (l_r)
{
camera.strafeLeft(camMoveSpeed*delta*(input.isKeyDown(VK_SHIFT)?10.0f:1.0f)*(input.isKeyDown(VK_LEFT)?1.0f:-1.0f));
}
if (u_d)
{
GLfloat tmpRotX = camera.getRotateX();
camera.rotateX(!FPS?-tmpRotX:0.0f);
camera.moveForward(camMoveSpeed*delta*(input.isKeyDown(VK_SHIFT)?10.0f:1.0f)*(input.isKeyDown(VK_UP)?1.0f:-1.0f));
camera.rotateX(!FPS?tmpRotX:0.0f);
}
// if we are in the FPS mode we have mouse look
if (FPS)
{
#ifdef WIN32
POINT pt;
GetCursorPos(&pt);
#else
MOUSEPOINT pt;
SDL_GetMouseState(&pt.x, &pt.y);
#endif
camera.rotateY((GLfloat)(pt.x-CV_SETTINGS_WINDOW_WIDTH_HALF)*camMLookSpeed*2.0f);
camera.rotateX((GLfloat)(pt.y-CV_SETTINGS_WINDOW_HEIGHT_HALF)*camMLookSpeed*2.0f);
#ifdef WIN32
SetCursorPos(CV_SETTINGS_WINDOW_WIDTH_HALF,CV_SETTINGS_WINDOW_HEIGHT_HALF);
#else
SDL_WarpMouse(CV_SETTINGS_WINDOW_WIDTH_HALF, CV_SETTINGS_WINDOW_HEIGHT_HALF);
#endif
}
// insert resets the keyboard
if (input.isKeyDown(VK_INSERT))
{
camPos[1] = CV_CAMERA_INITIAL_HEIGHT;
camera.setPosition(camPos);
}
// TEMP: jump into FPS mode
if (input.isKeyDown(VK_F1) && !FPS)
{
GLint centerX = (GLint)(camera.getPosition()[0]/CV_BLOCK_WIDTH);
GLint centerY = (GLint)(camera.getPosition()[2]/CV_BLOCK_DEPTH);
if (CV_GAME_MANAGER->getLevelManager()->getBlock(centerX,centerY)->isLow())
{
FPS=true;
camera.rotateX(-camInitXRot*delta);
camPos[1]=0.2f;
camera.setPosition(camPos);
// position the mouse to screen center
#ifdef WIN32
SetCursorPos(CV_SETTINGS_WINDOW_WIDTH_HALF,CV_SETTINGS_WINDOW_HEIGHT_HALF);
#else
SDL_WarpMouse(CV_SETTINGS_WINDOW_WIDTH_HALF, CV_SETTINGS_WINDOW_HEIGHT_HALF);
#endif
}
}
else if (input.isKeyDown(VK_F2) && FPS)
{
FPS=false;
camera.rotateX(camInitXRot);
camPos[1]=CV_CAMERA_INITIAL_HEIGHT;
camera.setPosition(camPos);
}
// temp adjust X angle
if (input.isKeyDown(VK_HOME))
{
camera.rotateX(camRotYSpeed*delta);
}
else if (input.isKeyDown(VK_END))
{
camera.rotateX(-camRotYSpeed*delta);
}
}
}
// collision detection only when in FPS mode
camera.setActive(FPS);
// now update the frustum with new camera params
viewFrustum.update(&camera);
return true;
}
bool CControlManager::shutdown()
{
return true;
}
CCamera *CControlManager::getCamera()
{
return &camera;
}
CFrustum *CControlManager::getViewFrustum()
{
return &viewFrustum;
}
CInput *CControlManager::getInput()
{
return &input;
}
bool CControlManager::isFPS()
{
return FPS;
}
string CControlManager::onAction(string keyword, string params)
{
string checkResult = "";
std::vector<string> tParams;
if (keyword==GOTO)
{
if (!CConsoleListener::checkParams(params,2,checkResult,tParams))
{
return checkResult;
}
GLint x = CConversions::strToInt(tParams[0]);
GLint y = CConversions::strToInt(tParams[1]);
if (x<0 || y<0 || x>=(GLint)CV_LEVEL_MAP_SIZE || y>=(GLint)CV_LEVEL_MAP_SIZE)
{
return "Error: Coordinates out of this level!";
}
vector3f newCamPos = vector3f( (GLfloat)x*CV_BLOCK_WIDTH,
camera.getPosition()[1],
(GLfloat)y*CV_BLOCK_DEPTH);
camera.setPosition(newCamPos);
return "Camera position set to "+CConversions::vec3fToStr(newCamPos);
}
return "<>";
}
};
};