forked from CAU-LetsCode/OOP-Proj4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualLego.cpp
410 lines (346 loc) · 11.7 KB
/
virtualLego.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
////////////////////////////////////////////////////////////////////////////////
//
// File: virtualLego.cpp
//
// Original Author: ¹ÚâÇö Chang-hyeon Park,
// Modified by Bong-Soo Sohn and Dong-Jun Kim
//
// Originally programmed for Virtual LEGO.
// Modified later to program for Virtual Billiard.
//
////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <array>
#include "CLight.h"
#include "d3dUtility.h"
#include "d3dfont.h"
#include "Platform.h"
#include "Jumper.h"
#include "DisplayText.h"
#include "Map.h"
#include "Goal.h"
#include "Status.h"
using std::array;
// -----------------------------------------------------------------------------
// Transform matrices
// -----------------------------------------------------------------------------
D3DXMATRIX g_mWorld;
D3DXMATRIX g_mView;
D3DXMATRIX g_mProj;
// -----------------------------------------------------------------------------
// Global variables
// -----------------------------------------------------------------------------
// window size
const int Width = 1980;
const int Height = 1080;
HWND window;
IDirect3DDevice9* Device = NULL;
#define NUM_PLATFORM 16
Jumper g_jumper;
CLight g_light;
D3DLIGHT9 lit;
Status status;
DisplayText displayText(Width, Height);
Map map1(16), map2(16), map3(16), map4(16);
Goal goal1(1), goal2(2), goal3(3);
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
// initialization
bool Setup() {
D3DXMatrixIdentity(&g_mWorld);
D3DXMatrixIdentity(&g_mView);
D3DXMatrixIdentity(&g_mProj);
if (!displayText.create("Times New Roman", 16, Device)) return false;
// stage 1
map1.setPosition(0, 0, 0, 0);
map1.setPosition(1, -0.5, 0, 0.3);
map1.setPosition(2, +0.5, 0, 0.3);
map1.setPosition(3, -1.0, 0, 0.6);
map1.setPosition(4, +1.0, 0, 0.6);
map1.setPosition(5, -1.5, 0, 0.9);
map1.setPosition(6, +1.5, 0, 0.9);
map1.setPosition(7, -2.0, 0, 1.2);
map1.setPosition(8, +2.0, 0, 1.2);
map1.setPosition(9, +1.5, 0, 1.5);
map1.setPosition(10, -1.5, 0, 1.5);
map1.setPosition(11, +1.0, 0, 1.8);
map1.setPosition(12, -1.0, 0, 1.8);
map1.setPosition(13, +0.5, 0, 2.1);
map1.setPosition(14, -0.5, 0, 2.1);
map1.setPosition(15, +0.0, 0, 2.4);
goal1.setPosition(+0.0, 0, 2.8);
// stage 2
map2.setPosition(0, 20, 0, 0);
map2.setPosition(1, 20 + 0.5, 0, 0.3);
map2.setPosition(2, 20 + 1.0, 0, 0.6);
map2.setPosition(3, 20 + 1.5, 0, 0.3);
map2.setPosition(4, 20 + 1.5, 0, 0.9);
map2.setPosition(5, 20 + 2.0, 0, 0.6);
map2.setPosition(6, 20 + 1.0, 0, 1.2);
map2.setPosition(7, 20 + 2.0, 0, 1.2);
map2.setPosition(8, 20 + 2.5, 0, 1.5);
map2.setPosition(9, 20 + 3.0, 0, 1.8);
map2.setPosition(10, 20 + 3.5, 0, 2.1);
map2.setPosition(11, 20 + 4.7, 0, 0.6);
map2.setPosition(12, 20 + 5.2, 0, 0.9);
map2.setPosition(13, 20 + 5.7, 0, 0.6);
map2.setPosition(14, 20 + 6.2, 0, 0.9);
map2.setPosition(15, 20 + 6.7, 0, 1.2);
goal2.setPosition(20 + 6.7, 0, 1.6);
// stage 3
map3.setPosition(0, 40, 0, 0);
map3.setPosition(1, 40 + 0.5, 0, 0.3);
map3.setPosition(2, 40, 0, 0.6);
map3.setPosition(3, 40 + 0.5, 0, 0.9);
map3.setPosition(4, 40 + 1.0, 0, 1.2);
map3.setPosition(5, 40 + 0.5, 0, 1.5);
map3.setPosition(6, 40, 0, 1.8);
map3.setPosition(7, 40 - 0.5, 0, 2.1);
map3.setPosition(8, 40 - 2.0 + 0.2, 0, 0.3);
map3.setPosition(9, 40 - 2.5 + 0.2, 0, 0);
map3.setPosition(10, 40 - 3.0 + 0.2, 0, 0.3);
map3.setPosition(11, 40 - 3.5 + 0.2, 0, 0.6);
map3.setPosition(12, 40 - 3.0 + 0.2, 0, 0.9);
map3.setPosition(13, 40 - 3.5 + 0.2, 0, 1.2);
map3.setPosition(14, 40 - 4.0 + 0.2, 0, 1.5);
map3.setPosition(15, 40 - 4.5 + 0.2, 0, 1.8);
goal3.setPosition(40 - 5.0, 0, 0);
// final stage
map4.setPosition(0, 60, 0, 0);
for (int i = 0; i < NUM_PLATFORM; i++) {
map1.g_platforms[i].create(Device, d3d::GREEN);
map2.g_platforms[i].create(Device, d3d::GREEN);
map3.g_platforms[i].create(Device, d3d::GREEN);
map4.g_platforms[i].create(Device, d3d::GREEN);
}
goal1.create(Device);
goal2.create(Device);
goal3.create(Device);
// create jumper
if (!g_jumper.create(Device)) return false;
g_jumper.setPosition(0, 0.005, 1);
g_jumper.setVelocity(0, 0);
// light setting
D3DXVECTOR3 m = g_jumper.getPosition();
::ZeroMemory(&lit, sizeof(lit));
lit.Type = D3DLIGHT_POINT;
lit.Diffuse = d3d::WHITE;
lit.Specular = d3d::WHITE * 1.0f;
lit.Ambient = d3d::WHITE * 1.0f;
lit.Position = D3DXVECTOR3(m.x, 3.0f, m.z);
lit.Range = 100.0f;
lit.Attenuation0 = 0.0f;
lit.Attenuation1 = 0.9f;
lit.Attenuation2 = 0.0f;
float radius = 0.01f;
if (false == g_light.create(Device, lit, radius)) return false;
// Position and aim the camera.
D3DXVECTOR3 pos(m.x, 3.0f, m.z);
D3DXVECTOR3 target(m.x, 0.0f, m.z);
D3DXVECTOR3 up(0.0f, 0.0f, 1.0f); // camera's rotation
D3DXMatrixLookAtLH(&g_mView, &pos, &target, &up);
Device->SetTransform(D3DTS_VIEW, &g_mView);
// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_mProj, D3DX_PI / 4, (float)Width / (float)Height, 1.0f, 100.0f);
Device->SetTransform(D3DTS_PROJECTION, &g_mProj);
// Set render states.
Device->SetRenderState(D3DRS_LIGHTING, TRUE);
Device->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
g_light.setLight(Device, g_mWorld);
return true;
}
// set of destroy function
void Cleanup(void)
{
for (int i = 0; i < NUM_PLATFORM; i++) {
//g_platforms[i].destroy();
map1.g_platforms[i].destroy();
map2.g_platforms[i].destroy();
map3.g_platforms[i].destroy();
map4.g_platforms[i].destroy();
}
g_light.destroy();
displayText.destroy();
goal1.destroy();
goal2.destroy();
goal3.destroy();
}
// Update
bool Display(float timeDelta)
{
int i = 0;
int j = 0;
if (Device)
{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00afafaf, 1.0f, 0);
Device->BeginScene();
displayText.update();
// draw platforms
for (int i = 0; i < NUM_PLATFORM; i++) {
//g_platforms[i].draw(Device, g_mWorld);
map1.g_platforms[i].draw(Device, g_mWorld);
map2.g_platforms[i].draw(Device, g_mWorld);
map3.g_platforms[i].draw(Device, g_mWorld);
map4.g_platforms[i].draw(Device, g_mWorld);
}
goal1.hitBy(g_jumper);
goal2.hitBy(g_jumper);
goal3.hitBy(g_jumper);
// intersect between jumper and platform
if (status.getIsGameOver()) {
g_jumper.setPosition(0, 0, 0.5);
g_jumper.setVelocity(0, 0);
}
else {
g_jumper.jumperUpdate(timeDelta);
}
for (int i = 0; i < NUM_PLATFORM; i++) {
if ((g_jumper.hasIntersected(map1.g_platforms[i])) || (g_jumper.hasIntersected(map2.g_platforms[i])) || (g_jumper.hasIntersected(map3.g_platforms[i])) || (g_jumper.hasIntersected(map4.g_platforms[i]))) {
g_jumper.setVelocity(g_jumper.getVelocity_X(), 0);
if (g_jumper.isFirstTouch()) {
g_jumper.setFirstTouch(false);
g_jumper.whereIdx = i;
}
else {
g_jumper.setOnPlatform(true);
}
}
else if (g_jumper.whereIdx == i) {
g_jumper.setVelocity(g_jumper.getVelocity_X(), g_jumper.getVelocity_Z());
g_jumper.setOnPlatform(false);
g_jumper.setFirstTouch(true);
g_jumper.whereIdx = -1;
}
}
// draw jumper
g_jumper.draw(Device, g_mWorld);
// draw goal
goal1.draw(Device, g_mWorld);
goal2.draw(Device, g_mWorld);
goal3.draw(Device, g_mWorld);
D3DXVECTOR3 m = g_jumper.getPosition();
D3DXVECTOR3 pos(m.x, 3.0f, m.z);
D3DXVECTOR3 target(m.x, 0.0f, m.z);
D3DXVECTOR3 up(0.0f, 0.0f, 1.0f); // camera's rotation
D3DXMatrixLookAtLH(&g_mView, &pos, &target, &up);
Device->SetTransform(D3DTS_VIEW, &g_mView);
lit.Position = D3DXVECTOR3(m.x, 3.0f, m.z);
Device->SetLight(0, &lit);
Device->EndScene();
Device->Present(0, 0, 0, 0);
Device->SetTexture(0, NULL);
}
if (!status.getIsGameOver() && g_jumper.getPosition().z < -3) {
status.setIsGameOver(true);
status.setNumFinalStage();
status.setNumStage(1);
}
return true;
}
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static bool wire = false;
static bool isReset = true;
static int old_x = 0;
static int old_y = 0;
static enum { WORLD_MOVE, LIGHT_MOVE, BLOCK_MOVE } move = WORLD_MOVE;
switch (msg) {
case WM_DESTROY: {
::PostQuitMessage(0);
break;
}
case WM_KEYDOWN: {
switch (wParam) {
case VK_ESCAPE:
::DestroyWindow(hwnd);
break;
case VK_RETURN:
if (NULL != Device) {
wire = !wire;
Device->SetRenderState(D3DRS_FILLMODE,
(wire ? D3DFILL_WIREFRAME : D3DFILL_SOLID));
}
break;
case VK_SPACE:
if (g_jumper.isOnPlatform()) {
D3DXVECTOR3 m = g_jumper.getPosition();
g_jumper.setPosition(m.x, m.y, m.z + 0.05);
g_jumper.setVelocity(g_jumper.getVelocity_X(), 0.5);
g_jumper.setOnPlatform(false);
g_jumper.setFirstTouch(true);
g_jumper.whereIdx = -1;
}
break;
case VK_LEFT:
if (true) {
g_jumper.setVelocity(-0.2, g_jumper.getVelocity_Z());
g_jumper.setMoveState(MOVESTATE::LEFT);
}
break;
case VK_RIGHT:
if (true) {
g_jumper.setVelocity(0.2, g_jumper.getVelocity_Z());
g_jumper.setMoveState(MOVESTATE::RIGHT);
}
break;
case 0x59: // Y
if (status.getIsGameOver()) {
status.setIsGameOver(false);
}
break;
case 0x4E: // N
if (status.getIsGameOver()) {
//gameover
//todo
}
break;
}
break;
}
case WM_KEYUP: {
switch (wParam) {
case VK_LEFT:
if (g_jumper.getMoveState() == MOVESTATE::LEFT) {
g_jumper.setVelocity(0, g_jumper.getVelocity_Z());
g_jumper.setMoveState(MOVESTATE::STOP);
}
case VK_RIGHT:
if (g_jumper.getMoveState() == MOVESTATE::RIGHT) {
g_jumper.setVelocity(0, g_jumper.getVelocity_Z());
g_jumper.setMoveState(MOVESTATE::STOP);
}
}
break;
}
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
srand(static_cast<unsigned int>(time(NULL)));
if (!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if (!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop(Display);
Cleanup();
Device->Release();
return 0;
}