Repository navigation
Expand file tree
/
Copy pathFileName.cpp
More file actions
3215 lines (2933 loc) · 104 KB
/
Copy pathFileName.cpp
File metadata and controls
3215 lines (2933 loc) · 104 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
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include"tools.hpp"
#include <easyx.h>
#include <graphics.h>
#include<windows.h>
#include<vector>
#include<time.h>
#include<string>
#include<tchar.h>
#include<cstdio>
#include <mmsystem.h>
#pragma comment(lib, "Msimg32.lib")
#pragma comment(lib,"winmm.lib")
using namespace std;
//音量增加内容
//添加播放音量所需的头文件
//音量初始值 音量范围是 0-1000
const int FPS = 1000/60; //帧率
//const int FPS = 60; //帧率
ExMessage msg;
int currentVolume = 500;
bool isWin = false;
bool isLose = false;
const int frameDelay = 100;
IMAGE skill0_left[5], skill0_right[5], skill1_right[8], skill1_left[8], skill2_right[16], skill2_left[16], skill3[12];//存储技能图片资源
//图片加载相关所有全局变量
IMAGE img_player_idle_left; //玩家向左静止图像
IMAGE img_player_idle_right; //玩家向右静止图像
IMAGE img_bk_1; //背景1
IMAGE img_bk_2; //背景2
IMAGE img_bk_3; //背景3
IMAGE img_bk_4; //背景4
IMAGE img_bk_5; //背景5
IMAGE img_bk_6; //背景6
IMAGE LoseView; //游戏结束界面
IMAGE menuBackGround;
struct Collision {
double x, y, w, h;//碰撞体 左上角坐标,长 宽
};
void menuView();
void InitMenu();
void LoadIMAGE();
void putimage_alpha(int x, int y, IMAGE* img);
void SettingVeiw();
void InitSetting();
void DrawVolumeControl();
void ListenerChecker();
void IntroductionVeiw();
void InitIntroduction();
void GameLoop();
void FinishChecker();
void RoomInit();
void EndChecker();
void InitLoseView();
void InitWinView();
void winView();
void loseView();
void DrawPlatform(vector<Collision> platform);
void DrawRoom2();
void DrawRoom3();
inline void flip_image(IMAGE* src, IMAGE* dst) //水平翻转函数
{
int w = src->getwidth();
int h = src->getheight();
Resize(dst, w, h);
DWORD* src_buffer = GetImageBuffer(src);
DWORD* dst_buffer = GetImageBuffer(dst);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
int idx_src = y * w + x;
int idx_dst = y * w + (w - x - 1);
dst_buffer[idx_dst] = src_buffer[idx_src];
}
}
}
struct Atlas
{
public:
void load_from_file(LPCTSTR path_template, int num)
{
//初始化数组数据和长度
img_list.clear();
img_list.resize(num);
TCHAR path_file[256];
for (int i = 0; i < num; i++)
{
_stprintf_s(path_file, path_template, i);
loadimage(&img_list[i], path_file);
}
}
void clear()
{
img_list.clear();
}
int get_size()
{
return (int)img_list.size();
}
IMAGE* get_image(int idx)
{
if (idx < 0 || idx >= img_list.size())
return nullptr;
return &img_list[idx];
}
void add_image(const IMAGE& img)
{
img_list.push_back(img);
}
private:
std::vector<IMAGE> img_list;
};
//动画加载相关所有全局变量
Atlas atlas_player_move_left; //玩家向左移动图集
Atlas atlas_player_move_right; //玩家向右移动图集
Atlas atlas_player_attack_left; //玩家向左攻击图像
Atlas atlas_player_attack_right; //玩家向右攻击图像
Atlas atlas_player_attack_effect_left; //玩家向左攻击剑气
Atlas atlas_player_attack_effect_right; //玩家向右攻击剑气
Atlas atlas_mosquito_idle_left; //蚊子向左静止图集
Atlas atlas_mosquito_idle_right; //蚊子向右静止图集
Atlas atlas_mosquito_move_left; //蚊子向左移动图集
Atlas atlas_mosquito_move_right; //蚊子向右移动图集
Atlas atlas_mosquito_attack_left; //蚊子向左攻击图集
Atlas atlas_mosquito_attack_right; //蚊子向右攻击图集
Atlas atlas_mosquito_death_left; //蚊子向左死亡图集
Atlas atlas_mosquito_death_right; //蚊子向右死亡图集
Atlas atlas_beetle_idle_left; //蚊子向左静止图集
Atlas atlas_beetle_idle_right; //蚊子向右静止图集
Atlas atlas_beetle_move_left; //蚊子向左移动图集
Atlas atlas_beetle_move_right; //蚊子向右移动图集
Atlas atlas_beetle_attack_left; //蚊子向左攻击图集
Atlas atlas_beetle_attack_right; //蚊子向右攻击图集
Atlas atlas_beetle_death_left; //蚊子向左死亡图集
Atlas atlas_beetle_death_right; //蚊子向右死亡图集
//动画相关
struct Animation
{
public:
void reset() //重置动画状态
{
timer = 0;
idx_frame = 0;
}
void set_atlas(Atlas* new_atlas) //设置动画所需要的图集
{
reset();
atlas = new_atlas;
}
void set_loop(bool flag) //设置动画是否需要循环播放
{
is_loop = flag;
}
void set_interval(int ms) //设置帧间隔
{
interval = ms;
}
int get_idx_frame() //获取当前帧索引
{
return idx_frame;
}
IMAGE* get_frame()
{
return atlas->get_image(idx_frame);
}
bool check_finished()
{
if (is_loop) return false;
return (idx_frame == atlas->get_size() - 1);
}
void on_update(int delta) //动画更新
{
timer += delta;
if (timer >= interval)
{
timer = 0;
idx_frame++;
if (idx_frame >= atlas->get_size())
{
idx_frame = is_loop ? 0 : atlas->get_size() - 1;
}
}
}
void on_draw(int x, int y) const
{
putimage_alpha(x, y, atlas->get_image(idx_frame));
}
private:
int timer = 0; //计时器
int interval = 0; //帧间隔
int idx_frame = 0; //帧索引
bool is_loop = true; //是否循环
Atlas* atlas = nullptr;
};
void flip_atlas(Atlas& src, Atlas& dst)
{
dst.clear();
for (int i = 0; i < src.get_size(); i++)
{
IMAGE img_flipped;
flip_image(src.get_image(i), &img_flipped);
dst.add_image(img_flipped);
}
}
Animation anim_player_move_left;
Animation anim_player_move_right;
Animation anim_player_attack_left;
Animation anim_player_attack_right;
Animation anim_player_attack_effect_left;
Animation anim_player_attack_effect_right;
Animation anim_mosquito_idle_left;
Animation anim_mosquito_idle_right;
Animation anim_mosquito_move_left;
Animation anim_mosquito_move_right;
Animation anim_mosquito_attack_left;
Animation anim_mosquito_attack_right;
Animation anim_mosquito_death_left;
Animation anim_mosquito_death_right;
Animation anim_beetle_idle_left;
Animation anim_beetle_idle_right;
Animation anim_beetle_move_left;
Animation anim_beetle_move_right;
Animation anim_beetle_attack_left;
Animation anim_beetle_attack_right;
Animation anim_beetle_death_left;
Animation anim_beetle_death_right;
void AnimationBuild()
{
extern Animation anim_player_move_left;
anim_player_move_left.set_atlas(&atlas_player_move_left);
anim_player_move_left.set_interval(75);
anim_player_move_left.set_loop(true);
extern Animation anim_player_move_right;
anim_player_move_right.set_atlas(&atlas_player_move_right);
anim_player_move_right.set_interval(75);
anim_player_move_right.set_loop(true);
extern Animation anim_player_attack_left;
anim_player_attack_left.set_atlas(&atlas_player_attack_left);
anim_player_attack_left.set_interval(10000);
anim_player_attack_left.set_loop(false);
extern Animation anim_player_attack_right;
anim_player_attack_right.set_atlas(&atlas_player_attack_right);
anim_player_attack_right.set_interval(10000);
anim_player_attack_right.set_loop(false);
extern Animation anim_player_attack_effect_left;
anim_player_attack_effect_left.set_atlas(&atlas_player_attack_effect_left);
anim_player_attack_effect_left.set_interval(50000);
anim_player_attack_effect_left.set_loop(false);
extern Animation anim_player_attack_effect_right;
anim_player_attack_effect_right.set_atlas(&atlas_player_attack_effect_right);
anim_player_attack_effect_right.set_interval(50000);
anim_player_attack_effect_right.set_loop(false);
extern Animation anim_mosquito_idle_left;
anim_mosquito_idle_left.set_atlas(&atlas_mosquito_idle_left);
anim_mosquito_idle_left.set_interval(75);
anim_mosquito_idle_left.set_loop(true);
extern Animation anim_mosquito_idle_right;
anim_mosquito_idle_right.set_atlas(&atlas_mosquito_idle_right);
anim_mosquito_idle_right.set_interval(75);
anim_mosquito_idle_right.set_loop(true);
extern Animation anim_mosquito_move_left;
anim_mosquito_move_left.set_atlas(&atlas_mosquito_move_left);
anim_mosquito_move_left.set_interval(75);
anim_mosquito_move_left.set_loop(true);
extern Animation anim_mosquito_move_right;
anim_mosquito_move_right.set_atlas(&atlas_mosquito_move_right);
anim_mosquito_move_right.set_interval(75);
anim_mosquito_move_right.set_loop(true);
extern Animation anim_mosquito_attack_left;
anim_mosquito_attack_left.set_atlas(&atlas_mosquito_attack_left);
anim_mosquito_attack_left.set_interval(75);
anim_mosquito_attack_left.set_loop(true);
extern Animation anim_mosquito_attack_right;
anim_mosquito_attack_right.set_atlas(&atlas_mosquito_attack_right);
anim_mosquito_attack_right.set_interval(75);
anim_mosquito_attack_right.set_loop(true);
extern Animation anim_mosquito_death_left;
anim_mosquito_death_left.set_atlas(&atlas_mosquito_death_left);
anim_mosquito_death_left.set_interval(75);
anim_mosquito_death_left.set_loop(true);
extern Animation anim_mosquito_death_right;
anim_mosquito_death_right.set_atlas(&atlas_mosquito_death_right);
anim_mosquito_death_right.set_interval(75);
anim_mosquito_death_right.set_loop(true);
extern Animation anim_beetle_idle_left;
anim_beetle_idle_left.set_atlas(&atlas_beetle_idle_left);
anim_beetle_idle_left.set_interval(75);
anim_beetle_idle_left.set_loop(true);
extern Animation anim_beetle_idle_right;
anim_beetle_idle_right.set_atlas(&atlas_beetle_idle_right);
anim_beetle_idle_right.set_interval(75);
anim_beetle_idle_right.set_loop(true);
extern Animation anim_beetle_move_left;
anim_beetle_move_left.set_atlas(&atlas_beetle_move_left);
anim_beetle_move_left.set_interval(75);
anim_beetle_move_left.set_loop(true);
extern Animation anim_beetle_move_right;
anim_beetle_move_right.set_atlas(&atlas_beetle_move_right);
anim_beetle_move_right.set_interval(75);
anim_beetle_move_right.set_loop(true);
extern Animation anim_beetle_attack_left;
anim_beetle_attack_left.set_atlas(&atlas_beetle_attack_left);
anim_beetle_attack_left.set_interval(75);
anim_beetle_attack_left.set_loop(true);
extern Animation anim_beetle_attack_right;
anim_beetle_attack_right.set_atlas(&atlas_beetle_attack_right);
anim_beetle_attack_right.set_interval(75);
anim_beetle_attack_right.set_loop(true);
extern Animation anim_beetle_death_left;
anim_beetle_death_left.set_atlas(&atlas_beetle_death_left);
anim_beetle_death_left.set_interval(75);
anim_beetle_death_left.set_loop(true);
extern Animation anim_beetle_death_right;
anim_beetle_death_right.set_atlas(&atlas_beetle_death_right);
anim_beetle_death_right.set_interval(75);
anim_beetle_death_right.set_loop(true);
}
struct Button {
int x, y, w, h;
int lastTime=clock();
int needTime=200;
const char* text;
bool isClicked = false; // 添加点击状态
bool inButton() {
return (msg.x >= x && msg.x <= x + w && msg.y >= y && msg.y <= y + h);
}
void update() {
isClicked = false;
peekmessage(&msg, EX_MOUSE);
if (inButton()&& msg.message == WM_LBUTTONDOWN&&clock()-lastTime>=needTime) {
isClicked = true;
lastTime = clock();
//flushmessage(EX_MOUSE); // 清除消息避免重复触发
}
}
bool istrigger() {
if (isClicked) {
cout << "trigger" << endl;
}
return isClicked;
}
void init() { // 确保文本不为空
if (text == nullptr) {
text = _T("");
}
double tw = w * 0.7; // 文本宽度
double th = h * 0.7; // 文本高度
double tx = x + (w - tw) / 2;
double ty = y + (h - th) / 2;
// 按钮边框
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 3);
// 只绘制默认状态,不处理悬停效果
setfillcolor(RGB(52, 152, 219));
fillroundrect(x, y, x + w, y + h, 30, 30);
// 绘制文本
settextstyle(th, tw / _tcslen(text), _T("宋体"));
settextcolor(BLACK);
setbkmode(TRANSPARENT);
outtextxy(tx, ty, text);
}
};
//按钮
Button ListenButton[2];
Button Buttons[4];
Button BackButton;
int RoomId = 1;
struct Player {
double x, y;//左上角坐标
Collision collision;//碰撞体
double speed;//x方向速度
double vx, vy;//两个方向速度
double gravity;//重力加速度
double jumpForce;//跳跃力
bool isGround;//是否在地面
int dir;//方向+-
int hp;//血量
int maxhp;
bool isFacingLeft = false; //是否向左
int damage = 50; //伤害
//鼠标消息处理对应变量
bool isMoveLeft = false;//是否在左侧移动
bool isMoveRight = false;//是否在右侧移动
bool isAttack = false;//是否攻击
//Attack涉及变量
int interval = 500;
int timer = interval;
bool isSkillActive; //是否正在释放技能
int currentSkill; //当前技能类型(1/2/3)
int currentFrame; //当前动画帧
clock_t lastFrameTime; //上一帧时间
//冷却系统
bool isSkillOnCooldown1; //技能冷却状态
bool isSkillOnCooldown2;
bool isSkillOnCooldown3;
clock_t lastSkillTime1;
clock_t lastSkillTime2;
clock_t lastSkillTime3;
clock_t cooldownDuration1=8000;
clock_t cooldownDuration2=8000;
clock_t cooldownDuration3=8000;
//技能状态结构体 存储技能释放时的位置和方向以及技能进行到了第几帧
struct SkillState {
bool active = false;//技能是否在进行中
int frame = 0;//当前技能进行到了第几帧
clock_t lastFrameTime = 0;//上一帧的释放时间
double x1, y1; // 技能触发时的位置
int dir = 1; // 技能触发时的方向
};
SkillState skill1State, skill2State, skill3State;//给三个技能都设置相应状体
};
struct Beetle
{
public:
int hp;
Collision collision;//碰撞体
int atk;//攻击力
vector<IMAGE>runLeft;//朝左走
vector<IMAGE>runRight;//超右走
vector<IMAGE>dieLeft;//超左死
vector<IMAGE>dieRight;//朝右死
vector<IMAGE>atkLeft;//左攻击
vector<IMAGE>atkRight;//右攻击
double speed;//速度
int view;//视野,只要怪物和角色距离小于view就进入攻击状态
int atkRange;//攻击距离,同视野
double idleX1, idleX2;//待机状态的巡视范围
bool right;//朝向
bool following;//是否跟随角色
bool attacking;//攻击中
bool canAtk;//进入攻击范围
bool deathSta, deathEnd;//播放死亡动画,直接return
int deathInd;//死亡图片下标
int runInd;//行走下标
int atkInd;//攻击下标
int atkLast;//上一次攻击时间 clock()
int atkTime;//攻击需要间隔
int frame, frameMax;//攻击已间隔几帧 攻击图像之间需要间隔几帧
int maxHp;//最大血量
//动画相关变量
Animation* animIdleLeft;
Animation* animIdleRight;
Animation* animMoveLeft;
Animation* animMoveRight;
Animation* animAttackLeft;
Animation* animAttackRight;
};
struct Mosquito {
//数值
int maxHp, atk;//血量、攻击力
Collision collision;//碰撞体
double speed;//速度
int view;//视野,只要怪物和角色距离小于view就进入攻击状态
int atkRange;//攻击距离,同视野
int atkW, atkH;
int maxIdleDistance;//单向最大行走距离
int atkTime;//攻击需要间隔
int frame;//攻击已经延迟的帧数
int frameMax;//攻击延迟几帧,暂定3
//图集
vector<IMAGE>runLeft;//朝左走
vector<IMAGE>runRight;//超右走
vector<IMAGE>dieLeft;//超左死
vector<IMAGE>dieRight;//朝右死
vector<IMAGE>atkLeft;//朝右攻击
vector<IMAGE>atkRight;//朝左攻击
//状态
bool right;//朝向
bool following;//是否跟随角色
bool canAtk;//进入攻击距离
bool attacking;//攻击中
bool deathEnd;//死亡动画播放完毕,彻底死亡
//图集下标
int deathInd;//死亡图片下标
int runInd;//待机下标
int atkInd;//攻击下标
//成对变量
int hp;//血量--------最大血量maxHP
int idleDistance;//行走距离------最大行走距离idleMaxDistance
int atkLast;//上一次攻击时间 clock()-----------攻击冷却时间atkTime
};
struct Novice {
//数值
int maxHp, atk;//血量、攻击力
Collision collision;//碰撞体
Collision collisionTmp;
double speed;//速度
int view;//视线长度
int readysta;//开始观看的时间
int readyTime;//需要的观看时长
int atkRange;//近战攻击距离
//int atkW, atkH;
int atkTime;//攻击需要间隔
int frame;//攻击已经延迟的帧数
int frameMax;//攻击延迟几帧,暂定3
vector<double>tag;//观看时长达到,就朝tag冲刺
//图集
vector<IMAGE>idleLeft;//朝左待机
vector<IMAGE>idleRight;//超右待机
vector<IMAGE>dieLeft;//超左死
vector<IMAGE>dieRight;//朝右死
vector<IMAGE>atkLeft;//朝右攻击
vector<IMAGE>atkRight;//朝左攻击
vector<IMAGE>dashLeft;//朝左攻击
vector<IMAGE>dashRight;//朝右攻击
int idleIndMax;
int dieIndMax;
int atkIndMax;
int dashIndMax;
//状态
bool right;//朝向
bool readydash;//蓄力中
bool dash;//冲刺
bool canAtk;//进入攻击距离
bool attacking;//攻击中
bool deathEnd;//死亡动画播放完毕,彻底死亡
//图集下标
int deathInd;//死亡图片下标
int idleInd;//待机下标
int atkInd;//攻击下标
int dashInd;//冲刺状态
//成对变量
int hp;//血量--------最大血量maxHP
int atkLast;//上一次攻击时间 clock()-----------攻击冷却时间atkTime
};
//陷阱
struct Trap {
int x, y, w, h;
int interval = 2000;//伤害间隔为一秒,可调
int timer_player = 0; //伤害判定计时器
IMAGE img;
int damage = 5;
};
//房间--------------------------------------------------------------------------------------
vector<Collision> platforms;//平台
vector<Beetle> Beetles;//甲壳虫
vector<Mosquito> Mosquitos;//蚊子
vector<Novice> Novices;//
vector<Trap> Traps;//陷阱
int EnemyCounter1;//敌人计数器
vector<Collision> platforms2;//平台
vector<Beetle> Beetles2;//甲壳虫
vector<Mosquito> Mosquitos2;//蚊子
vector<Novice> Novices2;//蚊子
vector<Trap> Traps2;//陷阱
int EnemyCounter2;//敌人计数器
vector<Collision> platforms3;//平台
vector<Beetle> Beetles3;//甲壳虫
vector<Mosquito> Mosquitos3;//蚊子
vector<Novice> Novices3;
vector<Trap> Traps3;
int EnemyCounter3;
double playerInitx = 20, playerInity = 250;//玩家初始化位置
//----------------------------------------------------------------玩家相关------------------------------------------------------------------------------------------------------
Player player;
//把int PlayercheckObstacleCollision();的位置移动到void Playermove();
//之前,并且我们只设置了一个房间没有设置房间号的索引所以把函数的参数删去了
//玩家函数声明
void PlayerGetMsg();
int PlayercheckObstacleCollisionX2();
int PlayercheckObstacleCollisionY2();
void Playermove();
void PlayerJump();
void PlayeruseSkill(int skillType);
void PlayerupdateSkillAnimation();
void PlayerupdateCooldown();
void PlayerDrawSkill();
void PlayerPhysicsLogic();
void loadskill_skill();
void PlayerrenderPlayer();
void PlayerUpdate();
void ShowHp();
void FinishChecker();
int PlayercheckObstacleCollisionY1();
int PlayercheckObstacleCollisionX1();
bool isOverlapping(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
void PlayerAttack(int delta, vector<Mosquito>& enemys, vector<Beetle>& Beetles,vector<Novice>& Novices);
void PlayerAttack(int delta, vector<Novice>& Novices);
void PlayerAttack(int delta, vector<Mosquito>& enemys, vector<Beetle>& Beetles);
void PlayerSkillAttack(int delta, vector<Novice>& Novices);
void PlayerSkillAttack(int delta, vector<Mosquito>& enemys, vector<Beetle>& beetles);
void PlayerPhysicsLogic() {
player.isGround = false;
player.vy += player.gravity;
int collisionResult;
double lastPosY = player.collision.y;
player.collision.y += player.vy;
if (RoomId == 2) {
collisionResult = PlayercheckObstacleCollisionY2();
}
else {
collisionResult = PlayercheckObstacleCollisionY1();
}
if (collisionResult == 2) { // 底部碰撞
player.collision.y = lastPosY;
player.vy = 0;
player.isGround = true;
}
else if (collisionResult == 1) { // 顶部碰撞
player.collision.y = lastPosY;
player.vy = 0;
}
player.y = player.collision.y;
}
void PlayerrenderPlayer() {
if (player.vx == 0) {
if (player.isFacingLeft) {
drawImg(player.x, player.y, &img_player_idle_left);
}
else {
drawImg(player.x, player.y, &img_player_idle_right);
}
}
else {
if (player.isFacingLeft) {
drawImg(player.x, player.y, &img_player_idle_left);
}
else {
drawImg(player.x, player.y, &img_player_idle_right);
}
}
}
//加载技能图片(用户定义函数名)
void loadskill_skill() {
for (int i = 0; i < 8; i++) {
char str[500];
sprintf_s(str, "skill1_left\\1-%d.png", i);
loadimage(skill1_left + i, str);
sprintf_s(str, "skill1_right\\1-%d.png", i);
loadimage(skill1_right + i, str);
}
for (int i = 0; i < 16; i++) {
char str[500];
sprintf_s(str, "skill2_left\\2-%d.png", i);
loadimage(skill2_left + i, str);
sprintf_s(str, "skill2_right\\2-%d.png", i);
loadimage(skill2_right + i, str);
}
for (int i = 0; i < 12; i++) {
char str[500];
sprintf_s(str, "skill3\\3-%d.png", i);
loadimage(skill3 + i, str);
}
}
//初始化玩家状态(用户定义函数名)
void PlayerInit() {
player.x = 1000;
player.y = 300;
player.collision = { 500, 300, 57, 121 };
//player.speed = 12;
player.speed = 8;
player.vx = 0;
player.vy = 0;
player.gravity = 5;
//player.jumpForce = 12;
player.jumpForce = 50;
player.isGround = true;
player.dir = 1;
player.maxhp = 1000;
player.hp = 1000;
}
//Room2检测
int PlayercheckObstacleCollisionX2() {
double player_x1 = player.collision.x;
double player_y1 = player.collision.y;
double player_x2 = player_x1 + player.collision.w;
double player_y2 = player_y1 + player.collision.h;
int collisionDir = 0; // 0=无碰撞,3=左侧,4=右侧
for (const auto& platform : platforms2) {
double platform_x1 = platform.x;
double platform_y1 = platform.y;
double platform_x2 = platform.x + platform.w;
double platform_y2 = platform.y + platform.h;
// 快速排除不相交情况
if (player_x2 <= platform_x1 || player_x1 >= platform_x2 ||
player_y2 <= platform_y1 || player_y1 >= platform_y2) {
continue;
}
double overlapLeft = player_x2 - platform_x1;
double overlapRight = platform_x2 - player_x1;
if (overlapLeft < overlapRight) {
collisionDir = 3; // 左侧碰撞
}
else {
collisionDir = 4; // 右侧碰撞
}
break; // 只处理第一个碰撞
}
return collisionDir;
}
// 只检测Y方向碰撞
int PlayercheckObstacleCollisionY2() {
double player_x1 = player.collision.x;
double player_y1 = player.collision.y;
double player_x2 = player_x1 + player.collision.w;
double player_y2 = player_y1 + player.collision.h;
int collisionDir = 0; // 0=无碰撞,1=顶部,2=底部
for (const auto& platform : platforms2) {
double platform_x1 = platform.x;
double platform_y1 = platform.y;
double platform_x2 = platform.x + platform.w;
double platform_y2 = platform.y + platform.h;
// 快速排除不相交情况
if (player_x2 <= platform_x1 || player_x1 >= platform_x2 ||
player_y2 <= platform_y1 || player_y1 >= platform_y2) {
continue;
}
double overlapTop = player_y2 - platform_y1;
double overlapBottom = platform_y2 - player_y1;
if (overlapTop < overlapBottom) {
collisionDir = 1; // 顶部碰撞
player.isGround = true;
}
else {
collisionDir = 2; // 底部碰撞
}
break; // 只处理第一个碰撞
}
return collisionDir;
}
//Room1检测
int PlayercheckObstacleCollisionX1() {
double player_x1 = player.collision.x;
double player_y1 = player.collision.y;
double player_x2 = player_x1 + player.collision.w;
double player_y2 = player_y1 + player.collision.h;
int collisionDir = 0; // 0=无碰撞,3=左侧,4=右侧
for (const auto& platform : platforms) {
double platform_x1 = platform.x;
double platform_y1 = platform.y;
double platform_x2 = platform.x + platform.w;
double platform_y2 = platform.y + platform.h;
// 快速排除不相交情况
if (player_x2 <= platform_x1 || player_x1 >= platform_x2 ||
player_y2 <= platform_y1 || player_y1 >= platform_y2) {
continue;
}
double overlapLeft = player_x2 - platform_x1;
double overlapRight = platform_x2 - player_x1;
if (overlapLeft < overlapRight) {
collisionDir = 3; // 左侧碰撞
}
else {
collisionDir = 4; // 右侧碰撞
}
break; // 只处理第一个碰撞
}
return collisionDir;
}
// 只检测Y方向碰撞
int PlayercheckObstacleCollisionY1() {
double player_x1 = player.collision.x;
double player_y1 = player.collision.y;
double player_x2 = player_x1 + player.collision.w;
double player_y2 = player_y1 + player.collision.h;
int collisionDir = 0; // 0=无碰撞,1=顶部,2=底部
for (const auto& platform : platforms) {
double platform_x1 = platform.x;
double platform_y1 = platform.y;
double platform_x2 = platform.x + platform.w;
double platform_y2 = platform.y + platform.h;
// 快速排除不相交情况
if (player_x2 <= platform_x1 || player_x1 >= platform_x2 ||
player_y2 <= platform_y1 || player_y1 >= platform_y2) {
continue;
}
double overlapTop = player_y2 - platform_y1;
double overlapBottom = platform_y2 - player_y1;
if (overlapTop < overlapBottom) {
collisionDir = 1; // 顶部碰撞
player.isGround = true;
}
else {
collisionDir = 2; // 底部碰撞
}
break; // 只处理第一个碰撞
}
return collisionDir;
}
// 处理水平移动(用户定义函数名)
void Playermove() {
double lastPosX = player.collision.x;
player.collision.x += player.vx;
if (player.collision.x <= 0) player.collision.x = 0;
if (player.collision.x + player.collision.w >= 1260) player.collision.x = 1260 - player.collision.w;
int collisionResult;
if(RoomId==2)
{
collisionResult = PlayercheckObstacleCollisionX2();
}
else {
collisionResult = PlayercheckObstacleCollisionX1();
}
if (collisionResult == 3 || collisionResult == 4) {
player.collision.x = lastPosX;
player.vx = 0;
}
player.x = player.collision.x;
}
// 跳跃函数(用户定义函数名)
void PlayerJump() {
if (player.isGround) {
player.vy = -player.jumpForce;
player.isGround = false;
}
}
// 使用技能函数(用户定义函数名)
void PlayeruseSkill(int skillType) {
clock_t now = clock();
switch (skillType) {
case 1: // 技能1
if (!player.isSkillOnCooldown1) {
// 修正技能1的位置计算,基于玩家碰撞体的边缘
player.skill1State = { true, 0, now,
player.collision.x + (player.dir == 1 ? player.collision.w : -190), // 调整为合适的偏移量
player.collision.y + player.collision.h - 250, player.dir };
player.isSkillOnCooldown1 = true;
player.lastSkillTime1 = now;
}
break;
case 2: // 技能2
if (!player.isSkillOnCooldown2) {
// 修正技能2的位置计算,基于玩家碰撞体的边缘
player.skill2State = { true, 0, now,
player.collision.x + (player.dir == 1 ? player.collision.w : -255), // 调整为合适的偏移量
player.collision.y, player.dir };
player.isSkillOnCooldown2 = true;
player.lastSkillTime2 = now;
}
break;
case 3: // 技能3
if (!player.isSkillOnCooldown3) {
player.skill3State = { true, 0, now,
player.collision.x, player.collision.y, player.dir };
player.isSkillOnCooldown3 = true;
player.lastSkillTime3 = now;
}
break;
}
}
// 绘制技能动画(用户定义函数名)
void PlayerDrawSkill() {
// 技能3优先渲染
if (player.skill3State.active) {
drawImg(player.skill3State.x1 - 150, player.skill3State.y1 - 250, skill3 + player.skill3State.frame);
}
// 技能2
if (player.skill2State.active) {
double x = player.skill2State.x1;
// 修正技能2在左侧的渲染位置
if (player.skill2State.dir == -1) {
x = player.skill2State.x1 - 40 * player.skill2State.frame; // 向左扩展
}
else {
x = player.skill2State.x1 + 40 * player.skill2State.frame; // 向右扩展
}
drawImg(x, player.skill2State.y1,
player.skill2State.dir == 1 ? skill2_right + player.skill2State.frame : skill2_left + player.skill2State.frame);
}
// 技能1
if (player.skill1State.active) {
drawImg(player.skill1State.x1, player.skill1State.y1,
player.skill1State.dir == 1 ? skill1_right + player.skill1State.frame : skill1_left + player.skill1State.frame);
}
// 冷却状态显示
settextcolor(YELLOW);
settextstyle(16, 0, _T("宋体"));
outtextxy(10, 10, _T("技能状态:"));
// 技能1冷却
if (player.isSkillOnCooldown1) {
settextcolor(RED);
double t = max(0.0, (player.cooldownDuration1 - (clock() - player.lastSkillTime1)) / 1000.0);
TCHAR txt[50];
_stprintf_s(txt, _T("K: %.1f秒"), t);
outtextxy(10, 30, txt);
}