/
CGame.cpp
executable file
·289 lines (251 loc) · 8.6 KB
/
CGame.cpp
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
#include "CGame.h"
#include "CGLRender.h"
#include "CObjectManager.h"
#include "CMarble.h"
#include "CInputManager.h"
#include <windows.h>
#include <gl\glut.h>
#include <stdio.h>
#include <cmath>
#define NUM_MARBLES 25
#define LEFT_MB 0
#define RIGHT_MB 2
CGame::CGame()
{
CMessage* message = new CMessage();
message->text = "Dude is on";
message->xpos = 0;
message->ypos = 0;
m_messageList.push_back(message);
int time = GetTickCount();
srand(time);
sprintf(m_windowTitle, "Marbles");
for(int i=0;i<256;i++) {
m_keys[i]=false;
}
m_throbber = 0;
m_throbIncrSign = 1;
m_pause = 0;
m_viewInterp = 0;
CCamera::Instance().LookAt( 40, 20, 40, 0, 0, 0, 0, 1, 0 );
m_gameState = GS_LoadLevel;
m_tolleyPos.set(-20, 0, 50);
m_aimPos.set(0,0,0);
m_p1Tolley = new CTolley;
m_objectManager.AddObject(m_p1Tolley);
m_marbleList.push_back(m_p1Tolley);
m_p1Tolley->setPos(m_tolleyPos.x, 1, m_tolleyPos.z);
m_soundManager.init();
//m_soundManager.startMusic();
//m_p1Tolley->DisableBody();
}
CGame::~CGame()
{
;
}
//========================================================================================
// CreateMarbles(int number) creates number amount of marbles and arranges them in
// a grid at the origin
//========================================================================================
bool
CGame::CreateMarbles(int number)
{
CMarble* temp = NULL;
for (int i = 0; i < number; i++)
m_marbleList.push_back((CMarble*)m_objectManager.CreateObject(Marble_Type));
double x = m_marbleList[1]->getRadius()*2;
int cols = (int)sqrt((double)number)+1;
int z = 0;
for (int i = 0; i < cols; i++)
for (int j = 0; j < cols; j++) {
m_marbleList[z++]->setPos((cols/2-i)*x, x/2, (cols/2-j)*x);
if (z >= number) return true;
}
return true;
}
WPARAM
CGame::Start()
{
m_quit = false; // Bool Variable To Exit Loop
bool fullscreen = false;
// Create Our OpenGL Window
if (!CGLRender::Instance().CreateGLWindow(m_windowTitle,1024,768,32,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
while(!m_quit) // Loop That Runs While done=FALSE
{
if (CInputManager::Instance().CheckInput())
;
else {
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (CGLRender::Instance().getActive()) // Program Active?
{
CGLRender::Instance().StartGLScene(); // Draw The Scene
MainLoop();
//CGLRender::Instance().drawFloor();
CGLRender::Instance().EndGLScene();
} if (CInputManager::Instance().KeyState(VK_F1)) {
CInputManager::Instance().KeyUp(VK_F1);
CGLRender::Instance().KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CGLRender::Instance().CreateGLWindow(m_windowTitle,1024,768,32,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}
// Shutdown
CGLRender::Instance().KillGLWindow(); // Kill The Window
return 0; // Exit The Program
}
void
CGame::MainLoop ()
{
if (CInputManager::Instance().KeyState(VK_F2)) SoundManager::Instance().startMusic();
if (CInputManager::Instance().KeyState(VK_F3)) SoundManager::Instance().stopMusic();
int width = CGLRender::Instance().getWidth() >> 1;
int height = CGLRender::Instance().getHeight() >> 1;
if (m_gameState == GS_Menu) {
;
} else if (m_gameState == GS_LoadLevel) {
CreateMarbles(NUM_MARBLES);
m_gameState = GS_DynamicsSettle;
} else {
m_tolleyForward = m_tolleyPos-m_aimPos;
m_tolleyForward.y = 0;
m_tolleyForward.Normalize();
CVector3 up(0, 1, 0);
m_tolleyStrafe.Cross(m_tolleyForward, up);
m_tolleyStrafe.Normalize();
CTimer::Instance().FrameUpdate();
m_deltaT = CTimer::Instance().getDeltaT();
if (m_throbber < 0.0 || m_throbber > 1.0) m_throbIncrSign = -m_throbIncrSign;
m_throbber += m_deltaT*m_throbIncrSign;
ODEManager::Instance().SimLoop(m_pause);
if (m_gameState == GS_DynamicsSettle)
{
interpView();
if (CObjectManager::Instance().DynamicsDone())
m_gameState = GS_AimShot;
}
else if (m_gameState == GS_AimShot)
{
double speed = m_deltaT*5;
if (!CObjectManager::Instance().DynamicsDone()) {
m_gameState = GS_DynamicsSettle;
m_viewInterp = 0.0;
}
if (CInputManager::Instance().MouseState(LEFT_MB)) {
m_gameState = GS_ShotDetect;
ShowCursor(FALSE);
SetCursorPos(width, height);
m_forwardAim.set(0,0,0);
m_sideAim.set(0,0,0);
}
if (CInputManager::Instance().KeyState(VK_SPACE)) {
m_toggleTolleyMove = !m_toggleTolleyMove;
CInputManager::Instance().KeyUp(VK_SPACE);
}
if (m_toggleTolleyMove) {
if (CInputManager::Instance().KeyState(VK_UP)) m_tolleyPos = m_tolleyPos - m_tolleyForward*speed;
if (CInputManager::Instance().KeyState(VK_DOWN)) m_tolleyPos = m_tolleyPos + m_tolleyForward*speed;
if (CInputManager::Instance().KeyState(VK_LEFT)) m_tolleyPos = m_tolleyPos + m_tolleyStrafe*speed;
if (CInputManager::Instance().KeyState(VK_RIGHT)) m_tolleyPos = m_tolleyPos - m_tolleyStrafe*speed;
} else {
if (CInputManager::Instance().KeyState(VK_UP)) m_aimPos = m_aimPos - m_tolleyForward*speed*1.5;
if (CInputManager::Instance().KeyState(VK_DOWN)) m_aimPos = m_aimPos + m_tolleyForward*speed*1.5;
if (CInputManager::Instance().KeyState(VK_LEFT)) m_aimPos = m_aimPos + m_tolleyStrafe*speed*1.5;
if (CInputManager::Instance().KeyState(VK_RIGHT)) m_aimPos = m_aimPos - m_tolleyStrafe*speed*1.5;
}
m_p1Tolley->setPos(m_tolleyPos.x, m_tolleyPos.y, m_tolleyPos.z);
CCamera::Instance().LookAt(m_tolleyPos.x + m_tolleyForward.x*10 , 5, m_tolleyPos.z + m_tolleyForward.z*10,
m_aimPos.x, m_aimPos.y, m_aimPos.z,
0, 1, 0);
}
else if (m_gameState == GS_ShotDetect)
{
if (!CInputManager::Instance().MouseState(LEFT_MB)) {
ShootMarble(m_forwardAim, m_sideAim, m_aimPos - m_tolleyPos);
ShowCursor(TRUE);
m_gameState = GS_DynamicsSettle;
m_viewInterp = 0.0;
}
CInputManager::Instance().UpdateMouse(width, height);
m_sideAim = m_sideAim + m_tolleyForward*CInputManager::Instance().getMouseDX();
m_forwardAim = m_forwardAim + (m_tolleyStrafe*CInputManager::Instance().getMouseDY());
}
CCamera::Instance().Look();
m_tolleyPos.set(m_p1Tolley->getPos()[0],
m_p1Tolley->getPos()[1],
m_p1Tolley->getPos()[2]);
CObjectManager::Instance().UpdateObjects();
CObjectManager::Instance().DrawObjects();
//CGLRender::Instance().drawGrid();
CGLRender::Instance().drawFloor();
CGLRender::Instance().drawAim(m_aimPos.x, m_aimPos.z, m_throbber);
}
}
void printOut(int x, int y, char * text) {
glRasterPos2i(x,y);
int length=strlen(text);
for (int i = 0; i < length; i++)
{
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text[i]);
}
}
void CGame::DrawTexts()
{
for (vector<CMessage*>::iterator it = m_messageList.begin(); it < m_messageList.end(); it++) {
printOut((*it)->xpos, (*it)->ypos, (*it)->text);
}
}
void CGame::interpView ()
{
CVector3 viewPos, viewCtr;
CVector3 destPos(3, 40, 3);
CVector3 sourcePos(m_tolleyPos.x + m_tolleyForward.x*10 , 5, m_tolleyPos.z + m_tolleyForward.z*10);
if (m_viewInterp < 1) {
m_viewInterp+=m_deltaT;
viewPos = sourcePos *(1-m_viewInterp) + destPos * m_viewInterp;
viewCtr = m_aimPos *(1-m_viewInterp) + m_tolleyPos * m_viewInterp;
} else {
viewCtr = m_tolleyPos;
viewPos = destPos;//CCamera::Instance().LookAt( 3, 20, 3, 0, 0, 0, 0, 1, 0 );
}
CCamera::Instance().LookAt(viewPos.x, viewPos.y, viewPos.z,
viewCtr.x, viewCtr.y, viewCtr.z,
0, 1, 0);
}
void
CGame::CheckCollisions(dBodyID b1, dBodyID b2)
{
CGameObject* obj1 = CObjectManager::Instance().getObject(b1);
CGameObject* obj2 = CObjectManager::Instance().getObject(b2);
if (((CMarble*)obj1)->getVel() > 0.3 || ((CMarble*)obj2)->getVel() > 0.3) {
float volume = max(((CMarble*)obj1)->getVel(), ((CMarble*)obj2)->getVel());
m_soundManager.playFX(volume * 2.0f);
}
}
void
CGame::ShootMarble(CVector3 forward, CVector3 side, CVector3 aim)
{
double yVel = aim.Magnitude()/30.0;
if (yVel > 5.0) yVel = 10.0;
m_p1Tolley->setVel(aim.x*1.3,yVel, aim.z*1.3);
if (forward.Magnitude() != 0)
m_p1Tolley->AddTorque(forward.x/10, forward.y/10, forward.z/10);
if (side.Magnitude() != 0)
m_p1Tolley->AddTorque(side.x/3, side.y/3, side.z/3);
m_gameState = GS_DynamicsSettle;
}
void
CGame::OnKeyUp(WPARAM w)
{
}
void
CGame::OnKeyDown(WPARAM w)
{
}