-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript_3.1.txt
More file actions
2399 lines (2174 loc) · 100 KB
/
Copy pathscript_3.1.txt
File metadata and controls
2399 lines (2174 loc) · 100 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
// 新增怪物6 奖励系统 & 武器(炸弹枪/加特林/左轮/电击枪)升级奖励
import {build_game,
update_loop,
enable_debug,
set_fps,
set_dimensions,
debug_log,
create_sprite,
get_loop_count,
input_key_down,
query_position,
query_scale,
update_to_top,
update_scale,
update_position,
update_rotation,
query_pointer_position,
update_color,
update_text,
create_rectangle,
create_text,
input_left_mouse_down,
gameobjects_overlap,
create_audio,
play_audio,
loop_audio
} from "arcade_2d";
//world properties
//世界大小
const height=720;
const width=720*1.1;
set_dimensions([width,height]); //设置大小
//对象管理
let objects=[];
let enermys=[];
let players=[];
// 地图移动相关
let tile_sprites = []; // 所有 tile sprite
let player_map_px = 0; // 玩家在地图像素空间中的 X 坐标
let player_map_py = 0; // 玩家在地图像素空间中的 Y 坐标
const tile_pixel = 40; // 16 * 2.5
//basic settings
//enable_debug();
//帧数
let fps=60;
set_fps(fps);
//player
let player_speed=2.5;
let player_hp=5;
const player_max_hp = 5;
const player_scale=2.5; //玩家贴图缩放
let player_hurt_cd = 0;
const player_hurt_interval = 120;
let player_hit_timer = 0;
let player_frame2 = undefined;
let player_anim_timer = 0;
let player_moving = false;
let player_dir_sign = 1;
const die_s1 = create_audio("https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/main/1.0.m4a", 0.8);
const die_s2 = create_audio("https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/main/2.0.m4a", 0.8);
const die_s3 = create_audio("https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/main/4.0.m4a", 0.8);
const bgm = create_audio("https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/battle.mp3", 0.5);
const shoot_sound = create_audio("https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/main/gun1.m4a", 0.8);
const hpBarWidth = 200;
const hpBarHeight = 14;
let hpBarBackground = undefined;
let hpBarFill = undefined;
let xpBarBackground = undefined;
let xpBarFill = undefined;
let gameover_active = false;
let gameover_card = undefined;
let gameover_text = undefined;
let player_level = 1; // 等级
let player_kills = 0; // 当前等级杀敌数
let total_kills = 0; // 玩家总击杀数
// 右上角UI组件变量
let ui_level_text = undefined;
let ui_kills_text = undefined;
//level up
let levelup_active = false;
let levelup_click_lock = false;
let levelup_options = [];
const upgrade_names = ['Multishot -15%dmg','Range','FireRate','Size +40%dmg','Damage +50%','Move Speed','PrepareForBoth','KeepAway','Bloodlust','ClusterBomb','Homing','GatlingGun','Revolver','TaserGun'];
let has_backshot = false;
let has_knockback = false;
let has_bloodlust = false;
let blood_kills = 0;
let has_homing = false;
// 特殊武器模式变量
let is_bomb_mode = false; // 炸弹武器模式
let bomb_ammo = 0; // 炸弹剩余发数
let is_gatling_mode = false;// 加特林模式
let gatling_ammo = 0; // 加特林剩余发数
let is_revolver_mode = false;// 左轮手枪模式
let revolver_ammo = 0; // 左轮剩余发数
let is_taser_mode = false; // 电击枪模式
let taser_ammo = 0; // 电击枪剩余发数
// boss
let last_boss_level = 0; // 上次生成boss的等级
let lv_title_card = undefined;
let lv_title_text = undefined;
let lv_cards = []; // 卡牌矩形
let lv_texts = []; // 卡牌文字
//gun
const gun_scale=0.6; //gun贴图缩放
let normal_gun_sprite = undefined; // 普通枪贴图
let bomb_gun_sprite = undefined; // 炸弹枪贴图
let gatling_gun_sprite = undefined;// 加特林枪贴图
let revolver_gun_sprite = undefined;// 左轮枪贴图
let taser_gun_sprite = undefined; // 电击枪贴图
let revolver_aim_line = undefined; // 左轮辅助瞄准射线
let taser_aim_line = undefined; // 电击枪辅助瞄准射线
//bullet
let bullet_radius = 4; // 子弹半径
let bullet_speed = 8; // 子弹初始速度(像素/帧)
let bullet_range = 200; // 子弹射程 = 4瓦片距离(像素)
let bullets_per_shot = 1; // 单次发射子弹数
let bullet_total_spread = 0.3; // 总散射角(弧度),约 17°
let bullet_fire_rate = 40; // 射速(帧间隔)
let bullet_damage = 1; // 子弹伤害倍率
const bullet_pool_size = 500; // 预创建子弹池大小
let bullet_life = math_round(bullet_range / bullet_speed); // 存活帧数 = 射程/速度
let b_sprites = []; // 子弹精灵(预创建,复用)
let b_active = []; // 是否活跃
let b_vx = []; // X速度
let b_vy = []; // Y速度
let b_life = []; // 已存活帧数
let b_type = []; // 0: 普通, 1: 左轮, 2: 电击枪
let fire_cooldown = 0; // 射击冷却帧数
//enemy bullet
const eb_pool_size = 50;
const eb_speed = 3.5;
const eb_max_life = 120;
let eb_sprites = [];
let eb_active = [];
let eb_vx = [];
let eb_vy = [];
let eb_life = [];
//particle
const particle_pool_size = 200; // 粒子池大小
const particle_count = 8; // 每次爆炸粒子数
const particle_speed = 3; // 粒子速度
const particle_life = 20; // 粒子存活帧数
let p_sprites = []; // 粒子精灵
let p_active = []; // 是否活跃
let p_vx = []; // X速度
let p_vy = []; // Y速度
let p_life = []; // 剩余帧数
let p_is_bomb = []; // 是否为爆炸怪粒子
let p_is_heal = []; // 是否为治疗粒子
// 开始界面相关
let game_started = false;
let start_bg = undefined;
let start_title = undefined;
let start_go_sprite = undefined; // GO 按钮贴图
//fire light cover
let fireLightCoverTimer=0;
// ==========================================
// 独立的经验与升级管理函数
// ==========================================
function check_player_levelup(exp_amount) { //奖励怪掉落经验时也调用
player_kills = player_kills + exp_amount;
total_kills = total_kills + exp_amount;
if (has_bloodlust) {
blood_kills = blood_kills + exp_amount;
if (blood_kills >= 5) {
blood_kills = 0;
if (player_hp < player_max_hp) { player_hp = player_hp + 1; }
}
}
let need = 3 + 2 * player_level;
if (player_kills >= need) {
player_level = player_level + 1;
player_kills = 0;
if (player_level / 10 === math_floor(player_level / 10)) {
player_hp = player_max_hp;
}
// 每5级生成Boss
spawn_boss();
show_levelup();
}
}
function player() { //玩家类
return message => {
if (message === "create_player") {
//不接受参数 创建玩家(贴图对象)(贴图放大了2.5倍)
const player_ins= create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/main/npc_elf.png');
return update_scale(player_ins,[player_scale,player_scale]);
}
if (message === "update_position") {
//接受一个object 即玩家 将其位置更新在屏幕中心
return player=>update_position(player,[width/2,height/2]);
}
if (message === "damage") {
//接受一个number 受伤函数 使玩家hp减少 开启闪红
return amount => {
player_hp = player_hp - amount;
player_hit_timer = 10;
};
}
if (message === "update_color") {
//根据闪红计时器更新玩家颜色
return player_obj => {
if (player_hit_timer > 0) {
player_hit_timer = player_hit_timer - 1;
update_color(player_obj, [255, 0, 0, 255]);
} else {
update_color(player_obj, [255, 255, 255, 255]);
}
};
}
if (message === "determine_direction") {
//接受一个对象,和number 即玩家和x轴速度 结果是根据速度改变玩家朝向
return (object,speed_x) => {
if(speed_x>0){
player_dir_sign = 1;
} else {
player_dir_sign = -1;
}
};
}
};
}
//enemy
function enemy(initial_hp, initial_speed, texture_url, behavior, if_reward) { //敌人类
let max_hp = initial_hp;
let hp = 0;
const speed = initial_speed;
let sprite = undefined;
const enemy_scale = 2.5;
let hit_timer = 0;
let move_timer = 0;
let fire_timer = 30;
let stun_timer = 0; // 静止电击计时器
return message => {
if (message === "init") {
//只在初始化时调用 创建隐藏的敌人贴图
return () => {
sprite = create_sprite(texture_url);
update_scale(sprite, [0, 0]);
update_position(sprite, [-9999, -9999]);
return sprite;
};
}
if (message === "spawn") {
//激活沉睡的敌人 传送到出生点 hp随玩家等级动态变化
return (start_x, start_y) => {
hp = math_round(max_hp * (player_level + 1) / 7); //基于玩家等级赋予血量
if (hp < 1) { hp = 1; }
hit_timer = 0;
stun_timer = 0;
move_timer = math_floor(math_random() * 100);
update_rotation(sprite, 0);
if (behavior === 6) {
update_color(sprite, [255, 215, 0, 255]); // 金色寻宝怪
} else {
update_color(sprite, [255, 255, 255, 255]);
}
if (behavior === 8) {
update_scale(sprite, [-5.0, 5.0]); // Boss体型2倍大
} else {
update_scale(sprite, [-enemy_scale, enemy_scale]);
}
update_position(sprite, [start_x, start_y]);
};
}
if (message === "is_alive") {
//查询敌人是否存活
return () => hp > 0;
}
if (message === "stun") {
//接受一个number 电击枪效果:使敌人静止若干帧并变为湛蓝色
return duration => {
if (hp > 0) {
if (behavior === 8) {
stun_timer = math_floor(duration / 4); // Boss抗性高
} else {
stun_timer = duration;
}
update_color(sprite, [0, 150, 255, 255]); // 变成湛蓝色
}
};
}
if (message === "set_hp") {
//接受一个number 电击枪效果:将敌人生命值设为指定数值
return amount => { if (hp > 0) {
if (behavior === 8) {
hp = math_floor(max_hp / 2); // Boss免疫秒杀,仅半血
} else {
hp = amount;
}
} };
}
if (message === "damage") {
//接受一个number 使敌人hp减少 hp归零时隐藏 受伤闪红
return amount => {
if (hp > 0) {
hp = hp - amount;
hit_timer = 10;
update_color(sprite, [255, 0, 0, 255]);
//如果死亡
if (hp <= 0) { //随机播放dead音效
let rs = math_random();
if (rs < 0.3) { play_audio(die_s1); }
else if (rs < 0.6) { play_audio(die_s2); }
else { play_audio(die_s3); }
//粒子特效
let die_x = query_position(sprite)[0];
let die_y = query_position(sprite)[1];
particles()('emit')(die_x, die_y);
//掉落奖励
if (if_reward === 1) {
game_rewards()('spawn')(die_x, die_y);
}
//爆炸怪 死亡AOE
if (behavior === 5) {
particles()('bomb_emit')(die_x, die_y);
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')()) {
let ep = query_position(enermys[i]('get_sprite')());
let edx = die_x - ep[0];
let edy = die_y - ep[1];
if (math_sqrt(edx*edx + edy*edy) < 53) {
enermys[i]('damage')(math_round(max_hp * 2 / 3));
}
}
}
if (math_abs(die_x - width/2) < 53 && math_abs(die_y - height/2) < 53 &&
(die_x-width/2)*(die_x-width/2) + (die_y-height/2)*(die_y-height/2) < 2809) {
player()('damage')(2);
}
}
//加经验升级
if (behavior === 8) {
check_player_levelup(8); // Boss给8倍经验
// Boss死亡多爆些粒子
particles()('bomb_emit')(die_x, die_y);
} else {
check_player_levelup(1);
}
//缩放到0 放到地图外
update_scale(sprite, [0, 0]);
update_position(sprite, [-9999, -9999]);
}
}
};
}
if (message === "chase_player") {
//行为模式+行走动画 遇墙分轴滑动 闪红
return () => {
if (sprite !== undefined && hp > 0) {
if (hit_timer > 0) {
hit_timer = hit_timer - 1;
if (hit_timer <= 0) {
if (behavior === 6) {
update_color(sprite, [255, 215, 0, 255]); // 金色寻宝怪
} else {
update_color(sprite, [255, 255, 255, 255]);
}
}
}
// 电击眩晕:静止不动,计时器倒计数
if (stun_timer > 0) {
stun_timer = stun_timer - 1;
if (stun_timer <= 0 && behavior === 6) {
update_color(sprite, [255, 215, 0, 255]);
} else if (stun_timer <= 0) {
update_color(sprite, [255, 255, 255, 255]);
}
} else {
move_timer = move_timer + 1;
let pos = query_position(sprite);
let ex = pos[0];
let ey = pos[1];
let dx = (width / 2) - ex;
let dy = (height / 2) - ey;
let dist = math_sqrt(dx * dx + dy * dy);
// 视野机制:超出width/2的敌人不追踪
if (dist > 0 && dist <= width / 2 * 1.5) { // 视野1.5倍
let dir_x = dx / dist;
let dir_y = dy / dist;
let mx = 0;
let my = 0;
let cur_sp = speed;
let cycle = 0;
if (behavior === 1) {
mx = dir_x * speed;
my = dir_y * speed;
} else if (behavior === 2) {
let wave = math_sin(move_timer * 0.08) * 1.5;
mx = dir_x * speed + (-dir_y * wave);
my = dir_y * speed + (dir_x * wave);
} else if (behavior === 3) {
cycle = move_timer % 100;
if (cycle < 20) { cur_sp = speed * 16; }
else if (cycle < 85) { cur_sp = speed; }
else { cur_sp = 0; }
mx = dir_x * cur_sp;
my = dir_y * cur_sp;
} else if (behavior === 4) {
if (dist > 400) { mx = 0; my = 0; }
else if (dist > 300) { mx = dir_x * speed; my = dir_y * speed; }
else { mx = 0; my = 0; }
fire_timer = fire_timer - 1;
if (fire_timer <= 0 && dist <= 300) {
enemy_bullets()('fire')(ex, ey, width / 2, height / 2);
fire_timer = 60;
}
} else if (behavior === 5) {
mx = dir_x * speed;
my = dir_y * speed;
} else if (behavior === 6) {
// 寻宝怪:急速远离玩家
if (dist < 200) {
mx = -dir_x * speed * 1.5;
my = -dir_y * speed * 1.5;
}
} else if (behavior === 7) {
// 治疗师:缓慢靠近,定期治疗附近怪物
mx = dir_x * speed * 0.4;
my = dir_y * speed * 0.4;
fire_timer = fire_timer - 1;
if (fire_timer <= 0) {
fire_timer = 300;
particles()('heal_emit')(ex, ey);
for (let h = 0; h < array_length(enermys); h = h + 1) {
if (enermys[h]('is_alive')()) {
let hp_pos = query_position(enermys[h]('get_sprite')());
let hdx = hp_pos[0] - ex;
let hdy = hp_pos[1] - ey;
if (hdx*hdx + hdy*hdy < 14400) {
enermys[h]('heal')(2);
particles()('heal_emit')(hp_pos[0], hp_pos[1]);
}
}
}
}
} else if (behavior === 8) {
// Boss:追击玩家,速度中等,体型庞大
mx = dir_x * speed;
my = dir_y * speed;
}
let wobble = 0;
let boss_scale = 5.0;
let cur_scale = behavior === 8 ? boss_scale : enemy_scale;
let sy_anim = cur_scale;
if (cur_sp > 0) {
let freq = (behavior === 3 && cycle < 20) ? 0.26 : 0.2;
wobble = math_sin(move_timer * freq) * 0.10;
sy_anim = cur_scale + math_abs(math_sin(move_timer * freq)) * (behavior === 8 ? 0.5 : 0.3);
}
update_rotation(sprite, wobble);
let empx = player_map_px + (ex - width / 2);
let empy = player_map_py + (ey - height / 2);
let nx = ex;
let ny = ey;
let tx = math_floor(empx / tile_pixel);
let ty = math_floor(empy / tile_pixel);
if (!is_wall(tile_map, math_floor((empx + mx) / tile_pixel), ty)) {
nx = ex + mx;
}
if (!is_wall(tile_map, tx, math_floor((empy + my) / tile_pixel))) {
ny = ey + my;
}
update_position(sprite, [nx, ny]);
if (dx > 0) {
update_scale(sprite, [-enemy_scale, sy_anim]);
} else {
update_scale(sprite, [enemy_scale, sy_anim]);
}
}
}
} // end of stun else
};
}
if (message === "update_map_offset") {
//接受两个number 地图移动时敌人同步偏移
return (dx, dy) => {
if (sprite !== undefined && hp > 0) {
let pos = query_position(sprite);
update_position(sprite, [pos[0] - dx, pos[1] - dy]);
}
};
}
if (message === "get_sprite") {
//返回敌人sprite 用于碰撞检测
return () => sprite;
}
if (message === "get_behavior") {
//返回敌人行为模式 用于击退判断
return () => behavior;
}
if (message === "heal") { //治疗师专用
return amount => { if (hp > 0) { hp = hp + amount; if (hp > max_hp) { hp = max_hp; } } };
}
if (message === "push") {
//接受两个number 推开敌人(敌人间碰撞用) 遇墙则取消
return (dx, dy) => {
if (sprite !== undefined && hp > 0) {
let pos = query_position(sprite);
let nx = pos[0] + dx;
let ny = pos[1] + dy;
let mpx = player_map_px + (nx - width / 2);
let mpy = player_map_py + (ny - height / 2);
let tl = math_floor((mpx - 20) / tile_pixel);
let tr = math_floor((mpx + 19) / tile_pixel);
let tt = math_floor((mpy - 20) / tile_pixel);
let tb = math_floor((mpy + 19) / tile_pixel);
if (!is_wall(tile_map, tl, tt) && !is_wall(tile_map, tr, tt) &&
!is_wall(tile_map, tl, tb) && !is_wall(tile_map, tr, tb)) {
update_position(sprite, [nx, ny]);
}
}
};
}
};
}
//spawner
function enemy_spawner(spawn_amount, spawn_rate) { //刷怪器
let spawn_cooldown = spawn_rate;
return message => {
if (message === "update") {
//每帧调用 冷却完毕时从池中激活沉睡敌人 数量随等级动态
return () => {
spawn_cooldown = spawn_cooldown - 1;
if (spawn_cooldown <= 0) {
// 1-6级倍率增长,7级起每级+1只而非*1.2
let mult = 1;
if (player_level <= 6) {
for (let k = 1; k < player_level; k = k + 1) { mult = mult * 1.2; }
} else {
for (let k = 1; k <= 5; k = k + 1) { mult = mult * 1.2; }
}
let amount = math_round(spawn_amount * mult);
if (player_level > 6) { amount = amount + (player_level - 6); }
if (player_level <= 2) { amount = amount + 6; }
let spawned = 0;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (spawned < amount) {
if (!enermys[i]('is_alive')()) {
let tries = 0;
let ok = false;
let sx = 0;
let sy = 0;
while (!ok && tries < 10) {
let a = math_random() * 2 * math_PI;
let r = 600;
sx = width / 2 + r * math_cos(a);
sy = height / 2 + r * math_sin(a);
let mpx = player_map_px + (sx - width / 2);
let mpy = player_map_py + (sy - height / 2);
let tx = math_floor(mpx / tile_pixel);
let ty = math_floor(mpy / tile_pixel);
if (!is_wall(tile_map, tx, ty)) { ok = true; }
tries = tries + 1;
}
if (ok) {
enermys[i]('spawn')(sx, sy);
spawned = spawned + 1;
}
}
}
}
spawn_cooldown = spawn_rate;
}
};
}
};
}
//boss spawn
function spawn_boss() {
if (player_level <= last_boss_level) { return 0; }
if (player_level / 5 !== math_floor(player_level / 5)) { return 0; }
for (let i = max_pool_size; i < max_pool_size + boss_pool_size; i = i + 1) {
if (!enermys[i]('is_alive')()) {
let tries = 0;
let ok = false;
let sx = 0;
let sy = 0;
while (!ok && tries < 20) {
let a = math_random() * 2 * math_PI;
let r = 500 + math_random() * 200;
sx = width / 2 + r * math_cos(a);
sy = height / 2 + r * math_sin(a);
let mpx = player_map_px + (sx - width / 2);
let mpy = player_map_py + (sy - height / 2);
let tx = math_floor(mpx / tile_pixel);
let ty = math_floor(mpy / tile_pixel);
if (!is_wall(tile_map, tx, ty)) { ok = true; }
tries = tries + 1;
}
if (ok) {
enermys[i]('spawn')(sx, sy);
last_boss_level = player_level;
}
break;
}
}
}
//reward
// ==========================================
// 掉落物单体函数
// ==========================================
function reward(texture_url) { //奖励怪掉落物品
let active = false;
let sprite = undefined;
let type = 0; // 0: 恢复HP, 1: 获得经验
return message => {
if (message === "init") {
return () => {
sprite = create_sprite(texture_url);
update_scale(sprite, [0, 0]);
update_position(sprite, [-9999, -9999]);
return sprite;
};
}
if (message === "spawn") {
return (x, y) => {
active = true;
type = math_random() < 0.3 ? 0 : 1;
update_position(sprite, [x, y]);
update_scale(sprite, [2.0, 2.0]);
if (type === 0) {
update_color(sprite, [255, 0, 0, 255]); // 红色 = 血包
} else {
update_color(sprite, [0, 200, 255, 255]); // 浅蓝色 = 经验晶体
}
};
}
if (message === "update") {
return () => {
if (active && sprite !== undefined) {
let pos = query_position(sprite);
let dx = (width / 2) - pos[0];
let dy = (height / 2) - pos[1];
let dist_sq = dx * dx + dy * dy;
if (dist_sq < 1225) { // 35*35范围内自动吸
active = false;
update_scale(sprite, [0, 0]);
update_position(sprite, [-9999, -9999]);
if (type === 0) {
player_hp = player_hp + 1;
if (player_hp > player_max_hp) {
player_hp = player_max_hp;
}
} else {
check_player_levelup(12); // 捡到经验包获得12点经验(3倍)
}
}
}
};
}
if (message === "update_map_offset") {
return (dx, dy) => {
if (active && sprite !== undefined) {
let pos = query_position(sprite);
update_position(sprite, [pos[0] - dx, pos[1] - dy]);
}
};
}
if (message === "is_active") {
return () => active;
}
};
}
// ==========================================
// 掉落物对象池
// ==========================================
const reward_pool_size = 30; //预创建掉落物池
const global_rewards = [];
const reward_texture = "https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/HPreward.png";
function game_rewards() { //掉落物管理
return message => {
if (message === "spawn") {
return (x, y) => {
for (let i = 0; i < reward_pool_size; i = i + 1) {
if (!global_rewards[i]("is_active")()) {
global_rewards[i]("spawn")(x, y);
break; // 只 spawn 一个 避免全部重叠
}
}
};
}
if (message === "update") {
return () => {
for (let i = 0; i < reward_pool_size; i = i + 1) {
global_rewards[i]("update")();
}
};
}
if (message === "update_map_offset") {
return (dx, dy) => {
for (let i = 0; i < reward_pool_size; i = i + 1) {
global_rewards[i]("update_map_offset")(dx, dy);
}
};
}
};
}
//map
const floor_texture_density = 0.3; // 纹理地板密度(0~1),0=纯地板,1=全纹理
function generateTileMap(width, height) {
//接受两个number 生成2D数组 这个数组被用于生成瓦片地图
const border = 8;
let map = [];
for (let y = 0; y < height; y = y + 1) {
map[y] = [];
for (let x = 0; x < width; x = x + 1) {
if (y < border || y >= height - border ||
x < border || x >= width - border) {
map[y][x] = 'wallTop';
} else if (y === border) {
if (x === border) {
map[y][x] = 'wallLeftBottom';
} else if (x === width - border - 1) {
map[y][x] = 'bottomRightWall';
} else {
map[y][x] = 'bottomWall';
}
} else if (y === height - border - 1) {
if (x === border) {
map[y][x] = 'topLeftWall';
} else if (x === width - border - 1) {
map[y][x] = 'topRightWall';
} else {
map[y][x] = 'topWall';
}
} else if (x === border) {
map[y][x] = 'rightWall';
} else if (x === width - border - 1) {
map[y][x] = 'LeftWall';
} else if (y === border + 1 || y === border + 2) {
map[y][x] = 'wallCenter';
} else {
let r = math_random();
if (r < floor_texture_density / 2) {
map[y][x] = 'texture1Floor';
} else if (r < floor_texture_density) {
map[y][x] = 'texture2Floor';
} else {
map[y][x] = 'floor';
}
}
}
}
return map;
}
function map(){ //地图类
function choose_tile_according_to_tilemap(tile_name){
//接受一个string 根据tile_name的名字 返回 地图图片
if (tile_name === 'LeftWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/LeftWall.png');
}
if (tile_name === 'bottomRightWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/bottomRightWall.png');
}
if (tile_name === 'bottomWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/bottomWall.png');
}
if (tile_name === 'floor') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/floor.png');
}
if (tile_name === 'rightWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/rightWall.png');
}
if (tile_name === 'texture1Floor') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/texture1Floor.png');
}
if (tile_name === 'texture2Floor') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/texture2Floor.png');
}
if (tile_name === 'topLeftWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/topLeftWall.png');
}
if (tile_name === 'topRightWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/topRightWall.png');
}
if (tile_name === 'topWall') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/topWall.png');
}
if (tile_name === 'wallCenter') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/wallCenter.png');
}
if (tile_name === 'wallLeftBottom') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/wallLeftBottom.png');
}
if (tile_name === 'wallRoot') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/wallRoot.png');
}
if (tile_name === 'wallTop') {
return create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/%E5%88%87%E5%A5%BD%E7%9A%84%E5%A2%99%E5%92%8C%E5%9C%B0%E6%9D%BFpng/wallTop.png');
}
}
return message => {
if (message==='generate_map') {
//接受一个2D数组 根据数组生成瓦片地图
return tile_map=>{
tile_sprites = [];
for (let y=0;y< array_length(tile_map) ; y=y+1){
for (let x=0 ; x < array_length(tile_map[y]) ; x=x+1){
let tile=choose_tile_according_to_tilemap(tile_map[y][x]);
update_position(tile,[
width/2 + 16*2.5*(x - (array_length(tile_map[0]) - 1) / 2),
height/2 + 16*2.5*(y - (array_length(tile_map) - 1) / 2)]);
tile_sprites[array_length(tile_sprites)] = tile;
}
}
player_map_px = ((array_length(tile_map[0]) - 1) / 2) * tile_pixel;
player_map_py = ((array_length(tile_map) - 1) / 2) * tile_pixel;
};
}
};
}
//particle
//levelup
function show_levelup() { //升级UI:随机选3个选项 显示卡牌 每三级多一张武器卡
let picked = [];
let pool_size = array_length(upgrade_names);
let weapon_level = (player_level / 3 === math_floor(player_level / 3));
let card_count = weapon_level ? 4 : 3;
//抽卡
for (let i = 0; i < card_count; i = i + 1) {
let found = false;
let r = 0;
while (!found) {
r = math_floor(math_random() * pool_size);
found = true;
if (bullet_fire_rate <= 2 && r === 2) { found = false; }
if (has_backshot && r === 6) { found = false; }
if (has_knockback && r === 7) { found = false; }
if (has_bloodlust && r === 8) { found = false; }
if (has_homing && r === 10) { found = false; }
if (is_bomb_mode && r === 9) { found = false; }
if (is_gatling_mode && r === 11) { found = false; }
if (is_revolver_mode && r === 12) { found = false; }
if (is_taser_mode && r === 13) { found = false; }
// 前3张普通卡:不能是武器类型
if (i < 3 && (r === 9 || r === 11 || r === 12 || r === 13)) { found = false; }
// 第4张武器卡:只能是武器类型,加特林低概率
if (i === 3 && r !== 9 && r !== 11 && r !== 12 && r !== 13) { found = false; }
if (i === 3 && r === 11 && math_random() > 0.3) { found = false; }
for (let j = 0; j < array_length(picked); j = j + 1) {
if (picked[j] === r) { found = false; }
}
}
picked[array_length(picked)] = r;
}
//创建矩形选项
update_position(lv_title_card, [width / 2, height / 2 - 80]);
update_scale(lv_title_card, [3, 0.8]);
update_color(lv_title_card, [80, 80, 80, 255]);
update_to_top(lv_title_card);
update_text(lv_title_text, 'LEVEL UP!');
update_position(lv_title_text, [width / 2, height / 2 - 80]);
update_scale(lv_title_text, [1, 1]);
update_to_top(lv_title_text);
for (let i = 0; i < card_count; i = i + 1) {
let cx = width / 2 + (i - (card_count - 1) / 2) * 180;
let cy = height / 2;
update_position(lv_cards[i], [cx, cy]);
update_scale(lv_cards[i], [3.2, 1.2]);
update_color(lv_cards[i], [50, 50, 50, 255]);
update_to_top(lv_cards[i]);
update_text(lv_texts[i], upgrade_names[picked[i]]);
update_color(lv_texts[i], [255, 255, 255, 255]);
let on = upgrade_names[picked[i]];
// 武器卡红色标记,其他稀有卡金色
let is_weapon = (on === 'ClusterBomb' || on === 'GatlingGun' || on === 'Revolver' || on === 'TaserGun');
let is_rare = (on === 'Multishot -15%dmg' || on === 'FireRate' || on === 'PrepareForBoth' || on === 'KeepAway' || on === 'Homing');
if (is_weapon) {
update_color(lv_texts[i], [255, 40, 40, 255]); // 红色
} else if (is_rare) {
update_color(lv_texts[i], [255, 200, 0, 255]); // 金色
}
update_position(lv_texts[i], [cx, cy]);
update_scale(lv_texts[i], [1, 1]);
update_to_top(lv_texts[i]);
levelup_options[i] = picked[i];
}
levelup_active = true;
levelup_click_lock = true;
}
function hide_levelup() { //隐藏升级卡牌
update_position(lv_title_card, [-9999, -9999]);
update_scale(lv_title_card, [0, 0]);
update_position(lv_title_text, [-9999, -9999]);
update_scale(lv_title_text, [0, 0]);
for (let i = 0; i < 4; i = i + 1) {
update_position(lv_cards[i], [-9999, -9999]);
update_scale(lv_cards[i], [0, 0]);
update_position(lv_texts[i], [-9999, -9999]);
update_scale(lv_texts[i], [0, 0]);
}
levelup_active = false;
}
function apply_upgrade(index) { //应用升级选项
let opt = upgrade_names[index];
if (opt === 'Multishot -15%dmg'){ bullets_per_shot = bullets_per_shot + 2; bullet_damage = bullet_damage * 0.85; bullet_total_spread = bullet_total_spread + 0.15; }
if (opt === 'Range') { bullet_range = bullet_range + 120; bullet_life = math_round(bullet_range / bullet_speed); }
if (opt === 'FireRate') { bullet_fire_rate = bullet_fire_rate - 8; if (bullet_fire_rate < 2) { bullet_fire_rate = 2; } }
if (opt === 'Size +40%dmg') { bullet_radius = bullet_radius + 2; bullet_damage = bullet_damage * 1.4; }
if (opt === 'Damage +50%') { bullet_damage = bullet_damage * 1.5; }
if (opt === 'Move Speed') { player_speed = player_speed + 1.5; }
if (opt === 'PrepareForBoth') { has_backshot = true; }
if (opt === 'KeepAway') { has_knockback = true; }
if (opt === 'Bloodlust') { has_bloodlust = true; }
if (opt === 'ClusterBomb') { is_gatling_mode = false; is_revolver_mode = false; is_taser_mode = false; is_bomb_mode = true; bomb_ammo = 10; }