-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualLego.cpp
More file actions
1657 lines (1366 loc) · 50.4 KB
/
virtualLego.cpp
File metadata and controls
1657 lines (1366 loc) · 50.4 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "d3dUtility.h"
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <windows.h>
#include <string>
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include <thread>
using namespace std;
// Direct3D 장치 포인터
IDirect3DDevice9* Device = NULL;
// 윈도우 크기 설정
const int Width = 1024;
const int Height = 768;
// 변환 행렬 선언 (월드, 뷰, 투영)
D3DXMATRIX g_mWorld;
D3DXMATRIX g_mView;
D3DXMATRIX g_mProj;
// 공 반지름 및 기타 상수 정의
#define M_RADIUS 0.29 // 공 반지름
#define P_RADIUS 0.21 // 플레이어 머리용 반지름
#define I_RADIUS 0.17 // 아이템용 반지름
#define M_HEIGHT 0.01
#define DECREASE_RATE 1
#define PLANE_X 6 //게임판의 x
#define PLANE_Y 9 //게임판의 Y
#define WALL_THICKNESS 0.3 //벽의 두께
#define BOX_LENGTH 0.5f // 상자 길이
#define BOX_COUNT 45 // 상자 개수
//------------------bgm과 효과음 실행용 함수-----------------
void PlaySoundMCI(const char* soundFile) {
string command = "play ";
command += soundFile;
command += " from 0 wait";
mciSendString(command.c_str(), NULL, 0, NULL);
}
//bgm 출력 종료----------------------------------------------------------------
//맵 배치에 사용 (상자는 1, 폭탄은 2)
int map[15][15] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
//게임상태 정의
enum GameState {
STATE_MENU, //인터페이스
STATE_GAME, //게임중
STATE_GAMEOVER //게임오버
};
GameState g_GameState = STATE_MENU;
void destroyItemBoxAt(int x, int z);
// 공(CSphere) 클래스 정의
class CSphere {
protected:
float center_x, center_y, center_z; // 공의 중심 좌표
float m_radius; // 공 반지름
float m_velocity_x; // x축 속도
float m_velocity_z; // z축 속도
public:
// 생성자: 공의 초기 상태를 설정
CSphere(void) {
D3DXMatrixIdentity(&m_mLocal); // 로컬 변환 행렬 초기화
ZeroMemory(&m_mtrl, sizeof(m_mtrl)); // 재질 정보 초기화
m_radius = 0;
m_velocity_x = 0;
m_velocity_z = 0;
m_pSphereMesh = NULL; // 메쉬 초기화
}
~CSphere(void) {}
// 공 객체 생성
bool create(IDirect3DDevice9* pDevice, D3DXCOLOR color = d3d::WHITE) {
if (NULL == pDevice) return false;
// 공 재질 설정
m_mtrl.Ambient = color;
m_mtrl.Diffuse = color;
m_mtrl.Specular = color;
m_mtrl.Emissive = d3d::BLACK;
m_mtrl.Power = 5.0f;
// 공 메쉬 생성
if (FAILED(D3DXCreateSphere(pDevice, getRadius(), 50, 50, &m_pSphereMesh, NULL)))
return false;
return true;
}
// 공 객체 삭제
void destroy(void) {
if (m_pSphereMesh != NULL) {
m_pSphereMesh->Release();
m_pSphereMesh = NULL;
}
}
// 공 그리기
void draw(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld) {
if (NULL == pDevice) return;
pDevice->SetTransform(D3DTS_WORLD, &mWorld); // 월드 변환 설정
pDevice->MultiplyTransform(D3DTS_WORLD, &m_mLocal); // 로컬 변환 적용
pDevice->SetMaterial(&m_mtrl); // 재질 설정
m_pSphereMesh->DrawSubset(0); // 메쉬 그리기
}
// 공 위치와 속도 업데이트
virtual void ballUpdate(float timeDiff) {
const float TIME_SCALE = 3.3; // 시간 스케일 조정
D3DXVECTOR3 cord = this->getCenter(); // 현재 공의 중심 좌표
double vx = abs(this->getVelocity_X());
double vz = abs(this->getVelocity_Z());
if (vx > 0.01 || vz > 0.01) {
float tX = cord.x + TIME_SCALE * timeDiff * m_velocity_x;
float tZ = cord.z + TIME_SCALE * timeDiff * m_velocity_z;
if (tX <= -4.5f + P_RADIUS) {
tX = -4.5 + P_RADIUS;
}
else if (tX >= 4.5 - P_RADIUS) {
tX = 4.5 - P_RADIUS;
}
if (tZ <= -4.5f + P_RADIUS) {
tZ = -4.5 + P_RADIUS;
}
else if (tZ >= 4.5 - P_RADIUS) {
tZ = 4.5 - P_RADIUS;
}
this->setCenter(tX, cord.y, tZ); // 새로운 위치 설정
}
else {
this->setPower(0, 0); // 속도가 매우 낮으면 멈춤
}
double rate = 1 - (1 - DECREASE_RATE) * timeDiff * 400;
if (rate < 0) rate = 0;
this->setPower(getVelocity_X() * rate, getVelocity_Z() * rate);
}
// 속도 및 위치 제어 메서드
double getVelocity_X() { return this->m_velocity_x; }
double getVelocity_Z() { return this->m_velocity_z; }
void setPower(double vx, double vz) { this->m_velocity_x = vx; this->m_velocity_z = vz; }
void setCenter(float x, float y, float z) {
D3DXMATRIX m;
center_x = x; center_y = y; center_z = z;
D3DXMatrixTranslation(&m, x, y, z);
setLocalTransform(m);
}
float virtual getRadius(void) const { return (float)(M_RADIUS); }
const D3DXMATRIX& getLocalTransform(void) const { return m_mLocal; }
void setLocalTransform(const D3DXMATRIX& mLocal) { m_mLocal = mLocal; }
D3DXVECTOR3 getCenter(void) const {
return D3DXVECTOR3(center_x, center_y, center_z);
}
private:
D3DXMATRIX m_mLocal; // 로컬 변환 행렬
D3DMATERIAL9 m_mtrl; // 재질 정보
ID3DXMesh* m_pSphereMesh; // 메쉬 포인터
};
// 벽(CWall) 클래스 정의
class CWall {
private:
float m_x;
float m_z;
float m_width;
float m_depth;
float m_height;
public:
CWall(void)
{
D3DXMatrixIdentity(&m_mLocal);
ZeroMemory(&m_mtrl, sizeof(m_mtrl));
m_width = 0;
m_depth = 0;
m_pBoundMesh = NULL;
}
~CWall(void) {}
public:
bool create(IDirect3DDevice9* pDevice, float ix, float iz, float iwidth, float iheight, float idepth, D3DXCOLOR color = d3d::WHITE)
{
if (NULL == pDevice)
return false;
m_mtrl.Ambient = color;
m_mtrl.Diffuse = color;
m_mtrl.Specular = color;
m_mtrl.Emissive = d3d::BLACK;
m_mtrl.Power = 5.0f;
m_width = iwidth;
m_depth = idepth;
if (FAILED(D3DXCreateBox(pDevice, iwidth, iheight, idepth, &m_pBoundMesh, NULL)))
return false;
return true;
}
void destroy(void)
{
if (m_pBoundMesh != NULL) {
m_pBoundMesh->Release();
m_pBoundMesh = NULL;
}
}
void draw(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld)
{
if (NULL == pDevice)
return;
pDevice->SetTransform(D3DTS_WORLD, &mWorld);
pDevice->MultiplyTransform(D3DTS_WORLD, &m_mLocal);
pDevice->SetMaterial(&m_mtrl);
m_pBoundMesh->DrawSubset(0);
}
void setPosition(float x, float y, float z)
{
D3DXMATRIX m;
this->m_x = x;
this->m_z = z;
D3DXMatrixTranslation(&m, x, y, z);
setLocalTransform(m);
}
float getHeight(void) const { return M_HEIGHT; }
private:
void setLocalTransform(const D3DXMATRIX& mLocal) { m_mLocal = mLocal; }
D3DXMATRIX m_mLocal;
D3DMATERIAL9 m_mtrl;
ID3DXMesh* m_pBoundMesh;
};
// 폭탄 폭발 클래스
class CExplosion : public CWall {
private:
bool e_isActive = false;
//폭발 이펙트 활성화
float e_time = 0;
//생성 후 지난 시간
float e_setTime = 1;
//지속시간 세팅
int e_indexX;
int e_indexZ;
public:
void activate(float x, float y, float z) {
e_isActive = true;
e_time = 0;
setPosition(x, y, z);
}
void explosionUpdate(float deltaTime) {
if (!e_isActive) return;
e_time += deltaTime;
if (e_time > e_setTime) {
e_isActive = false;
}
}
bool checkExp(int x, int y) {
if (e_isActive) {
if (e_indexX == x && e_indexZ == y) {
return true;
}
return false;
}
return false;
}
bool getActive() {
return e_isActive;
}
void setIndex(int x, int z) {
this->e_indexX = x;
this->e_indexZ = z;
}
};
// 폭탄 클래스
class CBoom : public CSphere {
private:
float b_time;
bool b_isActive = false;
float b_setTime = 2;
int b_numOfBoom = 1;
const int b_max = 10;
int b_indexX = 0;
int b_indexZ = 0;
int b_range = 2;
CExplosion explosion[41];
int numOfExp = 41;
bool player1 = false;
bool player2 = false;
public:
//Explosion 생성
bool createExplosion(IDirect3DDevice9* pDevice, D3DXCOLOR color) {
for (int i = 0; i < numOfExp; i++) {
if (false == explosion[i].create(pDevice, -1, -1, 0.6f, 0.1f, 0.6f, color)) return false;
}
return true;
}
//키 눌렀을 때
void pressKey(float sx, float py, float sz) {
if (!b_isActive) {
player1 = false;
player2 = false;
this->b_isActive = true;
setCenter(sx, py, sz);
this->b_time = 0;
map[b_indexZ][b_indexX] = 2;
}
}
void boomUpdate(float timeDelta) {
if (!b_isActive) {
return;
}
b_time += timeDelta;
if (b_time >= b_setTime) {
map[b_indexZ][b_indexX] = 0;
b_isActive = false;
explosion[0].activate(-4.2 + 0.6 * b_indexX, 0, 4.2 - 0.6 * b_indexZ); // 폭탄 위치
explosion[0].setIndex(b_indexX, b_indexZ);
for (int i = 0; i < b_range; i++) {
if (b_indexX + i + 1 < 15) {
if (map[b_indexZ][b_indexX + i + 1] == 1) {
destroyItemBoxAt(b_indexX + i + 1, b_indexZ); // 아이템 상자 삭제
break;
}
explosion[1 + i * 4].activate(-4.2 + 0.6 * (b_indexX + i + 1), 0, 4.2 - 0.6 * b_indexZ); // X +
explosion[1 + i * 4].setIndex(b_indexX + i + 1, b_indexZ);
}
}
for (int i = 0; i < b_range; i++) {
if (b_indexX - i - 1 >= 0) {
if (map[b_indexZ][b_indexX - i - 1] == 1) {
destroyItemBoxAt(b_indexX - i - 1, b_indexZ); // 아이템 상자 삭제
break;
}
explosion[2 + i * 4].activate(-4.2 + 0.6 * (b_indexX - i - 1), 0, 4.2 - 0.6 * b_indexZ); // X -
explosion[2 + i * 4].setIndex(b_indexX - i - 1, b_indexZ);
}
}
for (int i = 0; i < b_range; i++) {
if (b_indexZ + i + 1 < 15) {
if (map[b_indexZ + i + 1][b_indexX] == 1) {
destroyItemBoxAt(b_indexX, b_indexZ + i + 1); // 아이템 상자 삭제
break;
}
explosion[3 + i * 4].activate(-4.2 + 0.6 * b_indexX, 0, 4.2 - 0.6 * (b_indexZ + i + 1)); // Z +
explosion[3 + i * 4].setIndex(b_indexX, b_indexZ + i + 1);
}
}
for (int i = 0; i < b_range; i++) {
if (b_indexZ - i - 1 >= 0) {
if (map[b_indexZ - i - 1][b_indexX] == 1) {
destroyItemBoxAt(b_indexX, b_indexZ - i - 1); // 아이템 상자 삭제
break;
}
explosion[4 + i * 4].activate(-4.2 + 0.6 * b_indexX, 0, 4.2 - 0.6 * (b_indexZ - i - 1)); // Z -
explosion[4 + i * 4].setIndex(b_indexX, b_indexZ - i - 1);
}
}
}
}
void updateExplosions(float timeDelta) {
for (int i = 0; i < b_range * 4 + 1; ++i) {
explosion[i].explosionUpdate(timeDelta);
}
}
bool checkExplosion2(int x, int y) {
if (!player1) {
for (int i = 0; i < b_range * 4 + 1; i++) {
if (explosion[i].checkExp(x, y)) {
player1 = true;
return true;
}
}
}
return false;
}
bool checkExplosion1(int x, int y) {
if (!player2) {
for (int i = 0; i < b_range * 4 + 1; i++) {
if (explosion[i].checkExp(x, y)) {
player2 = true;
return true;
}
}
}
return false;
}
void drawExplosions(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld) {
for (int i = 0; i < b_range * 4 + 1; ++i) {
if (explosion[i].getActive()) {
explosion[i].draw(pDevice, mWorld);
}
}
}
//active 값 반환
bool getActive() {
return b_isActive;
}
//아이템 먹고 폭탄 개수 늘려주기
void setNumOfBoom(int numOfBoom) {
if (numOfBoom <= b_max) {
b_numOfBoom = numOfBoom;
}
}
int getNumOfBoom() {
return b_numOfBoom;
}
void setIndexXY(int indexX, int indexY) {
this->b_indexX = indexX;
this->b_indexZ = indexY;
}
void setBoomRange(int x) {
b_range = x;
}
};
// 플레이어 클래스
class Player : public CSphere { //플레이어 저장하는 클래스
private:
int playerLife = 3;//플레이어 목숨
int bombRange = 2;//폭탄 범위
int bombCap = 1;//폭탄용량
float playerSpeed = 1.5f; //기본으로 존재하는 플레이어 스피드
int playerIndexX;
int playerIndexY;
float animTime = 0;
bool animDraw = true;
public:
void updatePlayerIndex() {
//0,0칸은 -4.5, 4.5임 여기서 플레이어 반지름을 빼서 벽에서 못나가게 하는걸 구하자
this->playerIndexX = (int)((this->center_x + 4.5f) / 0.6f);
this->playerIndexY = (int)((4.5f - this->center_z) / 0.6f);
string message = "x: " + to_string(this->playerIndexX) + " y: " + to_string(this->playerIndexY);
//OutputDebugString((message + "\n").c_str());
}
bool getAnimDraw() {
return this->animDraw;
}
float getAnimTime() {
return this->animTime;
}
int getPlayerIndexX() {
return playerIndexX;
}
int getPlayerIndexY() {
return playerIndexY;
}
double getPlayerSpeed() { return this->playerSpeed; }
int getPlayerLife() {
return this->playerLife;
}
void setPlayerLife() {
this->playerLife -= 1;
this -> animTime = 1.0f;
char buffer[100];
sprintf(buffer, "Player Life: %d\n", this->playerLife);
OutputDebugString(buffer);
if (this->playerLife > 0) { // 목숨은 음수가 될 수 없음
}
else {
//게임오버 실행
}
}
int getBombRange() {
return bombRange;
}
void setBombRange() {//setter 대신에 그냥 아이템 먹으면 이거 증가함
this->bombRange += 1;
}
int getBombCap() {
return this->bombCap;
}
void setBombCap() {//setter 대신에 그냥 아이템 먹으면 이거 증가함
this->bombCap += 1;
}
void setPlayerSpeed() {//증가는 얼마나 증가할지 생각해야겠는데
this->playerSpeed += 0.3f;
}
float getRadius(void) const override { return (float)(P_RADIUS); };
void ballUpdate(float timeDiff) override {
const float TIME_SCALE = 3.3; // 시간 스케일 조정
D3DXVECTOR3 cord = this->getCenter(); // 현재 공의 중심 좌표
double vx = abs(this->getVelocity_X());
double vz = abs(this->getVelocity_Z());
this->updatePlayerIndex();
if (vx > 0.01 || vz > 0.01) {
float tX = cord.x + TIME_SCALE * timeDiff * m_velocity_x;
float tZ = cord.z + TIME_SCALE * timeDiff * m_velocity_z;
//벽 나가는거 막는 코드
if (tX <= -4.5f + P_RADIUS) {
tX = -4.5 + P_RADIUS;
}
else if (tX >= 4.5 - P_RADIUS) {
tX = 4.5 - P_RADIUS;
}
if (tZ <= -4.5f + P_RADIUS) {
tZ = -4.5 + P_RADIUS;
}
else if (tZ >= 4.5 - P_RADIUS) {
tZ = 4.5 - P_RADIUS;
}
int indexX = this->playerIndexX;
int indexY = this->playerIndexY;
if (m_velocity_x < 0) {//좌
//즉 현재 인덱스 x의 감소에서 체크
//[][] 두번째 인덱스
if (indexX >= 1) {
if (map[indexY][indexX - 1] >= 1) {//박스나 폭탄 지정하는 상수면 이동불가
//-4.2 + 0.6 * (indexX-1)가 상자의 center
if (tX <= -4.2 + 0.6 * (indexX - 1) + 0.15f + 0.275f) {
tX = -4.2 + 0.6 * (indexX - 1) + 0.15f + 0.275f;
}
}
}
}
else if (m_velocity_x > 0) {//우
if (indexX <= 13) {//14안넘게
if (map[indexY][indexX + 1] >= 1) {//박스나 폭탄 지정하는 상수면 이동불가
//-4.2 + 0.6 * (indexX+1)가 상자의 center
if (tX >= -4.2 + 0.6 * (indexX + 1) - 0.15f - 0.275f) {
tX = -4.2 + 0.6 * (indexX + 1) - 0.15f - 0.275f;
}
}
}
}
else if (m_velocity_z > 0) {//상
if (indexY >= 0) {
if (map[indexY - 1][indexX] >= 1) {//박스나 폭탄 지정하는 상수면 이동불가
//-4.2 + 0.6 * (indexX-1)가 상자의 center
if (tZ >= 4.2 - 0.6 * (indexY - 1) - 0.15f - 0.275f) {
tZ = 4.2 - 0.6 * (indexY - 1) - 0.15f - 0.275f;
}
}
}
}
else if (m_velocity_z < 0) {//하
if (indexY <= 13) {//14안넘게
if (map[indexY + 1][indexX] >= 1) {//박스나 폭탄 지정하는 상수면 이동불가
//-4.2 + 0.6 * (indexX-1)가 상자의 center
if (tZ <= 4.2 - 0.6 * (indexY + 1) + 0.15f + 0.275f) {
tZ = 4.2 - 0.6 * (indexY + 1) + 0.15f + 0.275f;
}
}
}
}
this->setCenter(tX, cord.y, tZ); // 새로운 위치 설정
}
}
void bindingPlayerBody(CWall& body) {
body.setPosition(this->center_x, 0.3f, this->center_z);
}
void ballAnimation(float deltaTime) {
float animTime = this->animTime;
this->animTime -= deltaTime;
animTime -= deltaTime;
if (animTime < 0) {
this->animDraw = true;
this->animTime = 0;
}
else if (animTime < 0.2) {
this->animDraw = false;
}
else if (animTime < 0.4) {
this->animDraw = true;
}
else if (animTime < 0.6) {
this->animDraw = false;
}
else if (animTime < 0.8) {
this->animDraw = true;
}
else if (animTime < 1) {
this->animDraw = false;
}
}
};
// 아이템 상자 클래스
class ItemBox {
private:
float m_length; // 정육면체 한 변의 길이
D3DXMATRIX m_mLocal; // 로컬 변환 행렬
D3DMATERIAL9 m_mtrl; // 재질 정보
ID3DXMesh* m_pBoxMesh; // 정육면체 메쉬 포인터
int m_indexX, m_indexZ; // 해당 상자의 X, Z 좌표
public:
// 생성자
ItemBox() : m_length(BOX_LENGTH), m_pBoxMesh(nullptr) {
ZeroMemory(&m_mtrl, sizeof(m_mtrl));
D3DXMatrixIdentity(&m_mLocal); // 로컬 변환 행렬 초기화
}
// 정육면체 생성
bool create(IDirect3DDevice9* pDevice, D3DXCOLOR color = D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)) {
if (pDevice == nullptr) return false;
// 정육면체 재질 설정
m_mtrl.Ambient = color;
m_mtrl.Diffuse = color;
m_mtrl.Specular = color;
m_mtrl.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
m_mtrl.Power = 5.0f;
// 정육면체 메쉬 생성
if (FAILED(D3DXCreateBox(pDevice, m_length, m_length, m_length, &m_pBoxMesh, nullptr))) {
return false;
}
return true;
}
// 정육면체 그리기
void draw(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld) {
if (pDevice == nullptr || m_pBoxMesh == nullptr) return;
pDevice->SetTransform(D3DTS_WORLD, &mWorld); // 월드 변환 설정
pDevice->MultiplyTransform(D3DTS_WORLD, &m_mLocal); // 로컬 변환 적용
pDevice->SetMaterial(&m_mtrl); // 재질 설정
m_pBoxMesh->DrawSubset(0); // 메쉬 그리기
}
// 위치 설정
void setPosition(float x, float y, float z) {
D3DXMATRIX m;
D3DXMatrixTranslation(&m, x, y, z); // 주어진 위치로 이동
m_mLocal = m; // 로컬 변환 행렬 설정
}
// 위치 및 변환 관련 메서드
const D3DXMATRIX& getLocalTransform() const {
return m_mLocal;
}
// 정육면체의 한 변의 길이 반환
float getLength() const {
return m_length;
}
// 정육면체 메쉬 삭제
void destroy() {
if (m_pBoxMesh) {
m_pBoxMesh->Release();
m_pBoxMesh = nullptr;
}
// 해당 좌표의 map 값을 0으로 설정
if (m_indexX >= 0 && m_indexZ >= 0) {
map[m_indexZ][m_indexX] = 0; // 좌표 위치에서 map 값 제거
}
}
void setIndex(float x, float z) {
m_indexX = x;
m_indexZ = z;
}
int getIndexX() const {
return m_indexX;
}
int getIndexZ() const {
return m_indexZ;
}
};
// 아이템 클래스
class CItem : public CSphere {
private:
int itemType; //1. 폭탄 개수 증가 2. 폭탄 범위 증가 3. 이속증가
bool animDir = false;//true면 내려가고 false면 올라감
public:
void setItemType(int i) {
this->itemType = i;
}
float getRadius(void) const override { return (float)(I_RADIUS); };//아이템용 radius
bool hasIntersected(Player& player) {//플레이어가 아이템의 위치로 들어왔는지 확인
D3DXVECTOR3 center1 = this->getCenter();
D3DXVECTOR3 center2 = player.getCenter();
float dx = center1.x - center2.x;
float dz = center1.z - center2.z;
float distance = sqrt(dx * dx + dz * dz);
float radiusSum = this->getRadius() + 0.17f;//여기서 임시 radius를 넣으면 된다. 상자 3루트2
return distance <= radiusSum;
}
// 충돌 처리
bool hitBy(Player& player, int itemIndex) {//플레이어가 아이템 먹었을때
if (!this->hasIntersected(player)) return false;
//1. 폭탄 개수 증가 2. 폭탄 범위 증가 3. 이속증가
switch (this->itemType)//플레이어한테 능력치 부여
{
case 1:
player.setBombCap();
break;
case 2:
player.setBombRange();
break;
case 3:
player.setPlayerSpeed();
break;
default:
break;
}
return true;
}
void ballAnimation(float deltaTime) {
float height = this->center_y;
float tX = this->center_x;
float tZ = this->center_z;
float addHeight;
if (height >= I_RADIUS * 3) {
this->animDir = true;//true면 내려잇
}
else if (height <= I_RADIUS) {
this->animDir = false;//false면 올려잇
}
if (animDir) {
height -= deltaTime;
}
else {
height += deltaTime;
}
this->setCenter(tX, height, tZ);
}
};
// 광원(CLight) 클래스
class CLight {
public:
CLight(void)
{
static DWORD i = 0;
m_index = i++;
D3DXMatrixIdentity(&m_mLocal);
::ZeroMemory(&m_lit, sizeof(m_lit));
m_pMesh = NULL;
m_bound._center = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
m_bound._radius = 0.0f;
}
~CLight(void) {}
public:
bool create(IDirect3DDevice9* pDevice, const D3DLIGHT9& lit, float radius = 0.1f)
{
if (NULL == pDevice)
return false;
if (FAILED(D3DXCreateSphere(pDevice, radius, 10, 10, &m_pMesh, NULL)))
return false;
m_bound._center = lit.Position;
m_bound._radius = radius;
m_lit.Type = lit.Type;
m_lit.Diffuse = lit.Diffuse;
m_lit.Specular = lit.Specular;
m_lit.Ambient = lit.Ambient;
m_lit.Position = lit.Position;
m_lit.Direction = lit.Direction;
m_lit.Range = lit.Range;
m_lit.Falloff = lit.Falloff;
m_lit.Attenuation0 = lit.Attenuation0;
m_lit.Attenuation1 = lit.Attenuation1;
m_lit.Attenuation2 = lit.Attenuation2;
m_lit.Theta = lit.Theta;
m_lit.Phi = lit.Phi;
return true;
}
void destroy(void)
{
if (m_pMesh != NULL) {
m_pMesh->Release();
m_pMesh = NULL;
}
}
bool setLight(IDirect3DDevice9* pDevice, const D3DXMATRIX& mWorld)
{
if (NULL == pDevice)
return false;
D3DXVECTOR3 pos(m_bound._center);
D3DXVec3TransformCoord(&pos, &pos, &m_mLocal);
D3DXVec3TransformCoord(&pos, &pos, &mWorld);
m_lit.Position = pos;
pDevice->SetLight(m_index, &m_lit);
pDevice->LightEnable(m_index, TRUE);
return true;
}
void draw(IDirect3DDevice9* pDevice)
{
if (NULL == pDevice)
return;
D3DXMATRIX m;
D3DXMatrixTranslation(&m, m_lit.Position.x, m_lit.Position.y, m_lit.Position.z);
pDevice->SetTransform(D3DTS_WORLD, &m);
pDevice->SetMaterial(&d3d::WHITE_MTRL);
m_pMesh->DrawSubset(0);
}
D3DXVECTOR3 getPosition(void) const { return D3DXVECTOR3(m_lit.Position); }
private:
DWORD m_index;
D3DXMATRIX m_mLocal;
D3DLIGHT9 m_lit;
ID3DXMesh* m_pMesh;
d3d::BoundingSphere m_bound;
};
// -----------------------------------------------------------------------------
// Global variables
// -----------------------------------------------------------------------------
CWall g_legoPlane;
CWall g_legowall[4];
CWall playerBody[2];
CLight g_light;
CWall boundaryLineByX[14];//15x15사이즈 가로눈금선
CWall boundaryLineByY[14];//15x15사이즈 세로눈금선
double g_camera_pos[3] = { 0.0, 5.0, -8.0 };
Player player[2];//플레이어 1p,2p
int ss_y = 14;
int ss_x = 12;
int validCount = 0;
CONST int MAX_BOOM = 20;
CBoom b_player1[MAX_BOOM];
CBoom b_player2[MAX_BOOM];
ID3DXFont* g_pFont = NULL; //폰트1 변수
ID3DXFont* g_pFontLarge = NULL; //폰트2 변수
// 아이템 상자
ItemBox itembox;
ItemBox* itemMap[15][15] = { nullptr };
//아이템 벡터 선언
vector<CItem> itemList;
// -----------------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------------
//박스 터졌을때 아이템 만드는지 확인하기
bool itemMake(int itemType, float pos_x, float pos_z) {
CItem item;
D3DXCOLOR itemColor;
switch (itemType + 1)//플레이어한테 능력치 부여
{
case 1:
itemColor = d3d::DARKRED;
break;
case 2:
itemColor = d3d::YELLOW;
break;
case 3:
itemColor = d3d::BLUE;
break;
}
if (false == item.create(Device, itemColor)) return false;
item.setCenter(pos_x, (float)I_RADIUS, pos_z);
item.setItemType(itemType + 1);
itemList.push_back(item);
}
void destroyAllLegoBlock(void)