-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.h
612 lines (509 loc) · 14.4 KB
/
util.h
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
#pragma once
#pragma comment(lib, "winmm")
#include <mmsystem.h>
#include <Windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int collision_pc_map(int dx, int dy);
int collision_pc_rock(int dx, int dy);
int collision_pc_gogildong(int dx, int dy);
int collision_pc_refrigerator();
int processKeyInput();
void gildong_run(int i);
void pc_melting();
void drawStage(int isGildongRun, int idx);
void crabUpDown();
void collision_pc_crab();
void pc_damage();
void getTime(clock_t startTime);
int direction_flag = 0;
int key_flag = 0;
int poison_flag = 0;
int shield_flag = 0;
int poison_cnt = 0;
int bulkup_flag = 0;
int bulkup_cnt = 0;
void crabUpDown() {
walkForcrab--;
SDL_RenderClear(renderer);
if (curStage == 6) {
crab_img = loadTexture("./assets/crab_up.png");
for (int i = 0; i < MAX_NUM_NPC; i++)
drawTexture(crab_img, crabs[i].posX, crabs[i].posY);
}
else if (curStage >= 4) {
// 게가 올라오고 내려오고
if (walkForcrab % 2 == 0) {
crab_img = loadTexture("./assets/crab_up.png");
for (int i = 0; i < MAX_NUM_NPC; i++)
drawTexture(crab_img, crabs[i].posX, crabs[i].posY);
}
else {
crab_img = loadTexture("./assets/crab_down.png");
for (int i = 0; i < MAX_NUM_NPC; i++)
drawTexture(crab_img, crabs[i].posX, crabs[i].posY);
}
drawStage(0, -1);
SDL_RenderPresent(renderer);
}
}
// pc와 맵 충돌
int collision_pc_map(int dx, int dy) {
if (maps[curStage][pc.arrY + dy][pc.arrX + dx] == 1) return 1;
return 0;
}
void pc_poison() {
if (poison_flag == 1)
poison_cnt++;
if (poison_cnt == 3 || poison_cnt == 6) {
walkCnt--;
pc_damage(); // pc가 붉은색으로 변함
if (poison_cnt == 6) {
poison_cnt = 0;
poison_flag = 0;
}
return;
}
}
// pc와 바위 충돌
int collision_pc_rock(int dx, int dy) {
for (int i = 0; i < MAX_NUM_NPC; i++) { // 바위를 순회하며 각각 pc와 충돌검사
if (rocks[i].arrX == pc.arrX + dx && rocks[i].arrY == pc.arrY + dy) {
if (maps[curStage][rocks[i].arrY + dy][rocks[i].arrX + dx] == 1) { // 바위 뒤에 벽이 있는 경우
return 1;
}
for (int j = 0; j < MAX_NUM_NPC; j++) { // 바위 뒤에 다른 바위가 있는 경우
if (rocks[j].arrX == rocks[i].arrX + dx && rocks[j].arrY == rocks[i].arrY + dy) {
return 1;
}
}
for (int j = 0; j < MAX_NUM_NPC; j++) { // 바위 뒤에 길동이 있는 경우
if (gildongs[j].arrX == rocks[i].arrX + dx && gildongs[j].arrY == rocks[i].arrY + dy) {
return 1;
}
}
if (lock.arrX == rocks[i].arrX + dx && lock.arrY == rocks[i].arrY + dy) { // 바위뒤에 상자
return 1;
}
// 충돌한 바위는 한칸 밀려야 함
PlaySound((TEXT("./assets/pushStoneSound.wav")), NULL, SND_ASYNC);
rocks[i].posX += dx * CELL_WIDTH;
rocks[i].posY += dy * CELL_WIDTH;
rocks[i].arrX += dx;
rocks[i].arrY += dy;
walkCnt--;
pc_poison();
crabUpDown();
collision_pc_crab();
return 1;
}
}
return 0;
}
void pc_damage() {
PlaySound((TEXT("./assets/damagedSound.wav")), NULL, SND_ASYNC);
SDL_RenderClear(renderer);
// pc의 색이 변함
pc_img = direction_flag == 1 ? loadTexture("./assets/pc_right_damage.png") : loadTexture("./assets/pc_left_damage.png");
drawStage(0, -1);
SDL_RenderPresent(renderer);
Sleep(150);
for (int i = 0; i < 100; i++) SDL_PollEvent(&trashEvent);
pc_img = direction_flag == 1 ? loadTexture("./assets/pc_right.png") : loadTexture("./assets/pc_left.png");
}
//pc 와 게 충돌
void collision_pc_crab() {
for (int i = 0; i < MAX_NUM_NPC; i++) {
if (crabs[i].arrX == pc.arrX && crabs[i].arrY == pc.arrY) { // 게의위치와 pc의위치가 같다면
if (shield_flag == 1 && (walkForcrab % 2 == 0 || curStage <= 3)) {
shield_flag = 0;
return;
}
if (walkForcrab % 2 == 0 && (curStage >= 4 && curStage != 6)) { // 올라와있는 게만 충돌처리
walkCnt--;
pc_damage(); // pc가 붉은색으로 변함
return;
}
if (curStage <= 3 || curStage == 6) {
walkCnt--;
pc_damage(); // pc가 붉은색으로 변함
return;
}
}
}
}
int collision_pc_gogildong(int dx, int dy) { // pc와 길동 충돌
for (int i = 0; i < MAX_NUM_NPC; i++) {
if (gildongs[i].arrX == pc.arrX + dx && gildongs[i].arrY == pc.arrY + dy) {
crabUpDown();
collision_pc_crab();
if (maps[curStage][gildongs[i].arrY + dy][gildongs[i].arrX + dx] == 1) { // 길동 뒤에 벽 (사라짐)
walkCnt--;
gildong_run(i); // 길동이 도망가는 에니메이션
gildongs[i].posX = -100;
gildongs[i].posY = -100;
gildongs[i].arrX = -1;
gildongs[i].arrY = -1;
return 1;
}
for (int j = 0; j < MAX_NUM_NPC; j++) { // 길동 뒤에 다른 길동이 있는 경우
if (gildongs[j].arrX == gildongs[i].arrX + dx && gildongs[j].arrY == gildongs[i].arrY + dy) {
walkCnt--;
gildong_run(i); // 길동이 도망가는 에니메이션
gildongs[i].posX = -100;
gildongs[i].posY = -100;
gildongs[i].arrX = -1;
gildongs[i].arrY = -1;
return 1;
}
}
for (int j = 0; j < MAX_NUM_NPC; j++) { // 길동 뒤에 바위가 있는 경우
if (rocks[j].arrX == gildongs[i].arrX + dx && rocks[j].arrY == gildongs[i].arrY + dy) {
walkCnt--;
gildong_run(i); // 길동이 도망가는 에니메이션
gildongs[i].posX = -100;
gildongs[i].posY = -100;
gildongs[i].arrX = -1;
gildongs[i].arrY = -1;
return 1;
}
}
if (lock.arrX == gildongs[i].arrX + dx && lock.arrY == gildongs[i].arrY + dy) { // 자물쇠 뒤에 길동
return 1;
}
if (bulkup_flag == 1) {
// 길동이랑 부딪히면 길동이 사라짐
walkCnt--;
gildong_run(i); // 길동이 도망가는 에니메이션
gildongs[i].posX = -100;
gildongs[i].posY = -100;
gildongs[i].arrX = -1;
gildongs[i].arrY = -1;
bulkup_cnt++;
if (bulkup_cnt % 3 == 0) {
bulkup_flag = 0;
pc.posX += 30;
}
return 1;
}
// 충돌한 길동은 한 칸 밀려야 함
PlaySound((TEXT("./assets/pushGildongSound.wav")), NULL, SND_ASYNC);
gildongs[i].posX += dx * CELL_WIDTH;
gildongs[i].posY += dy * CELL_WIDTH;
gildongs[i].arrX += dx;
gildongs[i].arrY += dy;
pc_poison();
walkCnt--;
return 1;
}
}
return 0;
}
// pc와 냉장고 충돌 (스테이지 클리어)
int collision_pc_refrigerator() {
if (refrigerator.arrX == pc.arrX && refrigerator.arrY == pc.arrY)
return 1;
return 0;
}
// pc와 아이템 충돌검사
void collision_pc_item() {
// 키 충돌
if (key.arrX == pc.arrX && key.arrY == pc.arrY) {
PlaySound((TEXT("./assets/getItemSound.wav")), NULL, SND_ASYNC);
key_flag = 1;
key.arrX = -1;
key.arrY = -1;
key.posX = -100;
key.posY = -100;
return;
}
// 쉴드 충돌
if (shield.arrX == pc.arrX && shield.arrY == pc.arrY) {
PlaySound((TEXT("./assets/getItemSound.wav")), NULL, SND_ASYNC);
shield_flag = 1;
shield.arrX = -1;
shield.arrY = -1;
shield.posX = -100;
shield.posY = -100;
return;
}
// 독 충돌
if (poison.arrX == pc.arrX && poison.arrY == pc.arrY) {
PlaySound((TEXT("./assets/poisonSound.wav")), NULL, SND_ASYNC);
poison_flag = 1;
poison.arrX = -1;
poison.arrY = -1;
poison.posX = -100;
poison.posY = -100;
return;
}
// 벌크업 충돌
if (bulkup.arrX == pc.arrX && bulkup.arrY == pc.arrY) {
PlaySound((TEXT("./assets/getItemSound.wav")), NULL, SND_ASYNC);
bulkup_flag = 1;
bulkup.arrX = -1;
bulkup.arrY = -1;
bulkup.posX = -100;
bulkup.posY = -100;
pc.posX -= 30;
return;
}
// 신발 충돌
if (shoe.arrX == pc.arrX && shoe.arrY == pc.arrY) {
PlaySound((TEXT("./assets/getItemSound.wav")), NULL, SND_ASYNC);
walkCnt += 2; // 걸음수 하나 증가
shoe.arrX = -1;
shoe.arrY = -1;
shoe.posX = -100;
shoe.posY = -100;
return;
}
}
// pc와 자물쇠 충돌검사
int collision_pc_lock(int dx, int dy) {
if (lock.arrX == pc.arrX + dx && lock.arrY == pc.arrY + dy) {
if (key_flag == 0)
return 1;
if (key_flag == 1) {
PlaySound((TEXT("./assets/releaseLockSound.wav")), NULL, SND_ASYNC);
lock.posX = -100;
lock.posY = -100;
lock.arrX = -1;
lock.arrY = -1;
pc_poison();
crabUpDown();
return 0;
}
}
return 0;
}
int timerFlag = 0;
void drawStage(int isGildongRun, int idx) {
switch (curStage) {
case 1:
drawTexture(map1_img, 0, 0);
break;
case 2:
drawTexture(map2_img, 0, 0);
break;
case 3:
drawTexture(map3_img, 0, 0);
break;
case 4:
drawTexture(map4_img, 0, 0);
break;
case 5:
drawTexture(map5_img, 0, 0);
break;
case 6:
drawTexture(map6_img, 0, 0);
break;
case 7:
drawTexture(map7_img, 0, 0);
break;
case 8:
drawTexture(map8_img, 0, 0);
break;
}
// 바위 그리기
for (int i = 0; i < MAX_NUM_NPC; i++) {
drawTexture(rock_img, rocks[i].posX, rocks[i].posY);
}
drawTexture(refrigerator_img, refrigerator.posX, refrigerator.posY);
drawTexture(key_img, key.posX, key.posY);
drawTexture(poison_img, poison.posX, poison.posY);
drawTexture(shield_img, shield.posX, shield.posY);
drawTexture(lock_img, lock.posX, lock.posY);
drawTexture(bulkup_img, bulkup.posX, bulkup.posY);
// 게 그리기
for (int i = 0; i < MAX_NUM_NPC; i++) {
drawTexture(crab_img, crabs[i].posX, crabs[i].posY);
}
// pc 그리기
drawTexture(pc_img, pc.posX, pc.posY);
// 길동 그리기
if (!isGildongRun) {
for (int i = 0; i < MAX_NUM_NPC; i++) {
drawTexture(gildong_img, gildongs[i].posX, gildongs[i].posY);
}
}
else {
for (int i = 0; i < MAX_NUM_NPC; i++) {
if (idx == i) continue;
drawTexture(gildong_img, gildongs[i].posX, gildongs[i].posY);
}
}
// 신발 그리기
drawTexture(shoe_img, shoe.posX, shoe.posY);
// 걸음수 그리기
if (walkCnt >= 0)
drawTexture(walkCnt_imgs[walkCnt], 90, 463);
drawTexture(roundCnt_img[curStage], 1028, 463);
// 타이머
drawTexture(timerBody_img, 100, 90);
if (walkCnt > 0) getTime(startTime);
for (int i = 1; i <= (385 - 120) * (curTime / stageTime); i += 1) { // 120 ~ 385 : 게이지의 시작 ~ 끝 좌표
if (120 + i > 385) continue;
if (stageTime - curTime <= 5) {
timerFlag == 0 && PlaySound((TEXT("./assets/remain5SecondSound.wav")), NULL, SND_ASYNC);
timerFlag = 1;
drawTexture(timegage_red_img, 120 + i, 105);
}
else {
timerFlag = 0;
drawTexture(timegage_img, 120 + i, 105);
}
}
}
void pc_melting() {
if (direction_flag == 1) {
SDL_RenderClear(renderer);
pc_img = loadTexture("./assets/pc_right_melt2.png");
drawStage(0, -1);
SDL_RenderPresent(renderer);
Sleep(1000);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
pc_img = loadTexture("./assets/pc_right_melt3.png");
drawStage(0, -1);
SDL_RenderPresent(renderer);
Sleep(1000);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
}
else {
SDL_RenderClear(renderer);
pc_img = loadTexture("./assets/pc_left_melt2.png");
drawStage(0, -1);
SDL_RenderPresent(renderer);
Sleep(1000);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
pc_img = loadTexture("./assets/pc_left_melt3.png");
drawStage(0, -1);
SDL_RenderPresent(renderer);
Sleep(1000);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
}
pc_img = loadTexture("./assets/pc_left.png");
}
void gildong_run(int i) {
PlaySound((TEXT("./assets/runSound.wav")), NULL, SND_ASYNC || SND_ASYNC);
SDL_RenderClear(renderer);
gildong_run_img = loadTexture("./assets/gildong_run_1.png");
drawStage(1, i);
drawTexture(gildong_run_img, gildongs[i].posX, gildongs[i].posY);
SDL_RenderPresent(renderer);
Sleep(130);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
gildong_run_img = loadTexture("./assets/gildong_run_2.png");
drawStage(1, i);
drawTexture(gildong_run_img, gildongs[i].posX, gildongs[i].posY);
SDL_RenderPresent(renderer);
Sleep(130);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
gildong_run_img = loadTexture("./assets/gildong_run_3.png");
drawStage(1, i);
drawTexture(gildong_run_img, gildongs[i].posX, gildongs[i].posY);
SDL_RenderPresent(renderer);
Sleep(130);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
gildong_run_img = loadTexture("./assets/gildong_run_4.png");
drawStage(1, i);
drawTexture(gildong_run_img, gildongs[i].posX, gildongs[i].posY);
SDL_RenderPresent(renderer);
Sleep(130);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
SDL_RenderClear(renderer);
gildong_run_img = loadTexture("./assets/gildong_run_5.png");
drawStage(1, i);
drawTexture(gildong_run_img, gildongs[i].posX, gildongs[i].posY);
SDL_RenderPresent(renderer);
Sleep(130);
for (int i = 0; i < 10000; i++) SDL_PollEvent(&trashEvent);
}
int processKeyInput() {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN)
switch (event.key.keysym.sym) {
case 1073741903: // right
direction_flag = 1;
if (collision_pc_map(1, 0)) break;
if (collision_pc_gogildong(1, 0) == 1) break;
if (collision_pc_lock(1, 0)) break;
if (collision_pc_rock(1, 0)) break;
crabUpDown();
pc_poison();
walkCnt--;
pc.posX += CELL_WIDTH;
pc.arrX++;
collision_pc_crab();
collision_pc_item();
break;
case 1073741904: // left
direction_flag = 0;
if (collision_pc_map(-1, 0)) break;
if (collision_pc_gogildong(-1, 0)) break;
if (collision_pc_lock(-1, 0)) break;
if (collision_pc_rock(-1, 0)) break;
crabUpDown();
pc_poison();
walkCnt--;
pc.posX -= CELL_WIDTH;
pc.arrX--;
collision_pc_crab();
collision_pc_item();
break;
case 1073741905: // down
if (collision_pc_map(0, 1)) break;
if (collision_pc_gogildong(0, 1)) break;
if (collision_pc_lock(0, 1)) break;
if (collision_pc_rock(0, 1)) break;
crabUpDown();
pc_poison();
walkCnt--;
pc.posY += CELL_WIDTH;
pc.arrY++;
collision_pc_crab();
collision_pc_item();
break;
case 1073741906: // up
if (collision_pc_map(0, -1)) break;
if (collision_pc_gogildong(0, -1)) break;
if (collision_pc_lock(0, -1)) break;
if (collision_pc_rock(0, -1)) break;
crabUpDown();
pc_poison();
walkCnt--;
pc.posY -= CELL_WIDTH;
pc.arrY--;
collision_pc_crab();
collision_pc_item();
break;
case 27: // ESC
exit(0);
break;
case 114: // R
return -1;
break;
}
pc_img = direction_flag == 1 ? loadTexture("./assets/pc_right.png") : loadTexture("./assets/pc_left.png");
if (shield_flag == 1) {
if (direction_flag == 1) pc_img = loadTexture("./assets/pc_right_shield.png");
else pc_img = loadTexture("./assets/pc_left_shield.png");
}
if (bulkup_flag == 1) {
if (direction_flag == 1) pc_img = loadTexture("./assets/bulkup_right.png");
else pc_img = loadTexture("./assets/bulkup_left.png");
}
}
return 1;
}
void getTime(clock_t startTime) {
clock_t endTime = clock();
curTime = (double)(endTime - startTime) / CLOCKS_PER_SEC;
}