-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.txt
More file actions
882 lines (765 loc) · 33.6 KB
/
Copy pathscript.txt
File metadata and controls
882 lines (765 loc) · 33.6 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
//296行前为各种类和函数的声明,296行之后为游戏开始构建(初始化,主循环)
import {build_game,
update_loop,
enable_debug,
set_fps,
set_dimensions,
debug_log,
create_sprite,
get_loop_count,
input_key_down,
query_position,
update_to_top,
update_scale,
update_position,
update_rotation,
query_pointer_position,
update_color,
create_rectangle,
input_left_mouse_down,
gameobjects_overlap
} from "arcade_2d";
//world properties
//世界大小
const height=720;
const width=720;
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=120;
set_fps(fps);
//player
const player_speed=1;
let player_hp=5;
const player_scale=2.5; //玩家贴图缩放
//gun
const gun_scale=1; //gun贴图缩放
//bullet
const bullet_radius = 4; // 子弹半径
const bullet_speed = 8; // 子弹初始速度(像素/帧)
const bullet_range = 120; // 子弹射程 = 3瓦片距离(像素)
const bullets_per_shot = 2; // 单次发射子弹数
const bullet_total_spread = 0.3; // 总散射角(弧度),约 17°
const bullet_fire_rate = 15; // 射速(帧间隔)
const bullet_pool_size = 50; // 预创建子弹池大小
const 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 fire_cooldown = 0; // 射击冷却帧数
//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 = []; // 剩余帧数
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;
};
}
if (message === "determine_direction") {
//接受一个对象,和number 即玩家和x轴速度 结果是根据速度改变玩家朝向
return (object,speed_x) => {
if(speed_x>0){
update_scale(object,[player_scale,player_scale]);
} else {
update_scale(object,[-player_scale,player_scale]);
}
};
}
};
}
//enemy
function enemy(initial_hp, initial_speed, texture_url) { //敌人类
let max_hp = initial_hp;
let hp = 0;
const speed = initial_speed;
let sprite = undefined;
const enemy_scale = 2.5;
let hit_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") {
//激活沉睡的敌人 传送到出生点
return (start_x, start_y) => {
hp = max_hp;
hit_timer = 0;
update_color(sprite, [255, 255, 255, 255]);
update_scale(sprite, [-enemy_scale, enemy_scale]);
update_position(sprite, [start_x, start_y]);
};
}
if (message === "is_alive") {
//查询敌人是否存活
return () => hp > 0;
}
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) {
let die_x = query_position(sprite)[0];
let die_y = query_position(sprite)[1];
particles()('emit')(die_x, die_y);
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) {
update_color(sprite, [255, 255, 255, 255]);
}
}
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);
if (dist > 0) {
let mx = (dx / dist) * speed;
let my = (dy / dist) * speed;
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, enemy_scale]);
} else {
update_scale(sprite, [enemy_scale, enemy_scale]);
}
}
};
}
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 === "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) {
let spawned = 0;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (spawned < spawn_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;
}
};
}
};
}
//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
function particles(){ //粒子类
return message => {
if (message === "emit") {
//接受两个number x,y 在该位置生成爆炸粒子
return (x, y) => {
let emitted = 0;
for (let i = 0; i < particle_pool_size && emitted < particle_count; i = i + 1) {
if (!p_active[i]) {
let a = math_random() * 2 * math_PI;
let sp = particle_speed * math_random();
update_position(p_sprites[i], [x, y]);
update_color(p_sprites[i], [255, 255, 255, 255]);
update_scale(p_sprites[i], [1, 1]);
p_active[i] = true;
p_vx[i] = sp * math_cos(a);
p_vy[i] = sp * math_sin(a);
p_life[i] = particle_life;
emitted = emitted + 1;
}
}
};
}
if (message === "update_all") {
//遍历活跃粒子 移动、衰减、归零后停用
return () => {
for (let i = 0; i < particle_pool_size; i = i + 1) {
if (p_active[i]) {
let pos = query_position(p_sprites[i]);
let nx = pos[0] + p_vx[i];
let ny = pos[1] + p_vy[i];
update_position(p_sprites[i], [nx, ny]);
p_life[i] = p_life[i] - 1;
let a = math_round(255 * p_life[i] / particle_life);
update_color(p_sprites[i], [255, 255, 255, a]);
if (p_life[i] <= 0) {
p_active[i] = false;
update_position(p_sprites[i], [-9999, -9999]);
update_scale(p_sprites[i], [0, 0]);
}
}
}
};
}
};
}
function gun(){ //枪类
const gun_offset = 20; // 枪离屏幕中心的距离(像素)
return message => {
if (message === "create_gun") {
//不接受参数 创建枪对象
const gun_ins= create_sprite('https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/Desert%20Eagle.png');
return update_scale(gun_ins,[gun_scale,gun_scale]);
}
if (message === "update_gun") {
//接受gun对象 将其放在屏幕中心到鼠标的连线上 并旋转指向鼠标
return gun => {
let mouse_pos = query_pointer_position();
let dx = mouse_pos[0] - width / 2;
let dy = mouse_pos[1] - height / 2;
let angle = math_atan2(dy, dx);
let gun_x = width / 2 + gun_offset * math_cos(angle);
let gun_y = height / 2 + gun_offset * math_sin(angle);
update_position(gun, [gun_x, gun_y]);
return update_rotation(gun, angle);
};
}
};
}
function bullets(){ //子弹类
const spawn_dist = 30; // 子弹生成点离屏幕中心的距离
return message => {
if (message === "fire") {
//接受鼠标位置 按bullets_per_shot散射发射多颗子弹
return mouse_pos => {
let dx = mouse_pos[0] - width / 2;
let dy = mouse_pos[1] - height / 2;
let base = math_atan2(dy, dx);
let step = bullets_per_shot > 1
? bullet_total_spread / (bullets_per_shot - 1)
: 0;
let half = bullet_total_spread / 2;
let fired = 0;
for (let i = 0; i < bullet_pool_size && fired < bullets_per_shot; i = i + 1) {
if (!b_active[i]) {
let a = base - half + fired * step;
let sx = width / 2 + spawn_dist * math_cos(a);
let sy = height / 2 + spawn_dist * math_sin(a);
update_scale(b_sprites[i], [1, 1]);
update_position(b_sprites[i], [sx, sy]);
b_active[i] = true;
b_vx[i] = bullet_speed * math_cos(a);
b_vy[i] = bullet_speed * math_sin(a);
b_life[i] = 0;
fired = fired + 1;
}
}
};
}
if (message === "update_all") {
//遍历池中活跃子弹 移动、衰减、碰撞、越界则停用
return () => {
for (let i = 0; i < bullet_pool_size; i = i + 1) {
if (b_active[i]) {
b_life[i] = b_life[i] + 1;
let sf = 1 - b_life[i] / bullet_life;
let cvx = b_vx[i] * sf;
let cvy = b_vy[i] * sf;
let pos = query_position(b_sprites[i]);
let nx = pos[0] + cvx;
let ny = pos[1] + cvy;
update_position(b_sprites[i], [nx, ny]);
let mpx = player_map_px + (nx - width / 2);
let mpy = player_map_py + (ny - height / 2);
let tx = math_floor(mpx / tile_pixel);
let ty = math_floor(mpy / tile_pixel);
let hit = false;
for (let j = 0; j < array_length(enermys) && !hit; j = j + 1) {
if (enermys[j]('is_alive')()) {
let ep = query_position(enermys[j]('get_sprite')());
let edx = nx - ep[0];
let edy = ny - ep[1];
if (math_sqrt(edx * edx + edy * edy) < 24) {
enermys[j]('damage')(1);
hit = true;
}
}
}
if (sf <= 0 || is_wall(tile_map, tx, ty) || hit) {
b_active[i] = false;
update_scale(b_sprites[i], [0, 0]);
update_position(b_sprites[i], [-9999, -9999]);
}
}
}
};
}
};
}
//移动
//以下三个函数为阻止玩家离开边界的函数
function is_wall(tile_map, x, y) { //无需在意
if (x < 0 || x >= array_length(tile_map[0]) ||
y < 0 || y >= array_length(tile_map)) {
return true;
}
let t = tile_map[y][x];
return t === 'wallTop' ||
t === 'wallCenter' ||
t === 'wallLeftBottom' || t === 'bottomWall' ||
t === 'bottomRightWall' || t === 'rightWall' ||
t === 'LeftWall' ||
t === 'topLeftWall' || t === 'topWall' ||
t === 'topRightWall';
}
function can_move_x(tile_map, px, py, dx) { //无需在意
if (dx > 0) {
let check_x = math_floor((px + dx + tile_pixel - 1) / tile_pixel);
let top_y = math_floor(py / tile_pixel);
let bot_y = math_floor((py + tile_pixel - 1) / tile_pixel);
return !(is_wall(tile_map, check_x, top_y) ||
is_wall(tile_map, check_x, bot_y));
} else if (dx < 0) {
let check_x = math_floor((px + dx) / tile_pixel);
let top_y = math_floor(py / tile_pixel);
let bot_y = math_floor((py + tile_pixel - 1) / tile_pixel);
return !(is_wall(tile_map, check_x, top_y) ||
is_wall(tile_map, check_x, bot_y));
}
return true;
}
function can_move_y(tile_map, px, py, dy) { //无需在意
if (dy > 0) {
let left_x = math_floor(px / tile_pixel);
let right_x = math_floor((px + tile_pixel - 1) / tile_pixel);
let check_y = math_floor((py + dy + tile_pixel - 1) / tile_pixel);
return !(is_wall(tile_map, left_x, check_y) ||
is_wall(tile_map, right_x, check_y));
} else if (dy < 0) {
let left_x = math_floor(px / tile_pixel);
let right_x = math_floor((px + tile_pixel - 1) / tile_pixel);
let check_y = math_floor((py + dy) / tile_pixel);
return !(is_wall(tile_map, left_x, check_y) ||
is_wall(tile_map, right_x, check_y));
}
return true;
}
//输入
function input_listener(){
//全局输入监听
let dx = 0;
let dy = 0;
if (input_key_down('w')) { dy = dy - player_speed; }
if (input_key_down('s')) { dy = dy + player_speed; }
if (input_key_down('a')) {
dx = dx - player_speed;
player()('determine_direction')(player_instance,dx);
}
if (input_key_down('d')) {
dx = dx + player_speed;
player()('determine_direction')(player_instance,dx);
}
if (dx !== 0 && dy !== 0) {
let diag = player_speed / math_sqrt(2);
dx = dx > 0 ? diag : -diag;
dy = dy > 0 ? diag : -diag;
}
if (dx !== 0 && can_move_x(tile_map, player_map_px, player_map_py, dx)) {
let was_hit = false;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')() &&
gameobjects_overlap(player_instance, enermys[i]('get_sprite')())) {
was_hit = true;
}
}
player_map_px = player_map_px + dx;
for (let i = 0; i < array_length(tile_sprites); i = i + 1) {
let pos = query_position(tile_sprites[i]);
update_position(tile_sprites[i], [pos[0] - dx, pos[1]]);
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
enermys[i]('update_map_offset')(dx, 0);
}
let now_hit = false;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')() &&
gameobjects_overlap(player_instance, enermys[i]('get_sprite')())) {
now_hit = true;
}
}
if (!was_hit && now_hit) {
player_map_px = player_map_px - dx;
for (let i = 0; i < array_length(tile_sprites); i = i + 1) {
let pos = query_position(tile_sprites[i]);
update_position(tile_sprites[i], [pos[0] + dx, pos[1]]);
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
enermys[i]('update_map_offset')(-dx, 0);
}
}
}
if (dy !== 0 && can_move_y(tile_map, player_map_px, player_map_py, dy)) {
let was_hit = false;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')() &&
gameobjects_overlap(player_instance, enermys[i]('get_sprite')())) {
was_hit = true;
}
}
player_map_py = player_map_py + dy;
for (let i = 0; i < array_length(tile_sprites); i = i + 1) {
let pos = query_position(tile_sprites[i]);
update_position(tile_sprites[i], [pos[0], pos[1] - dy]);
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
enermys[i]('update_map_offset')(0, dy);
}
let now_hit = false;
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')() &&
gameobjects_overlap(player_instance, enermys[i]('get_sprite')())) {
now_hit = true;
}
}
if (!was_hit && now_hit) {
player_map_py = player_map_py - dy;
for (let i = 0; i < array_length(tile_sprites); i = i + 1) {
let pos = query_position(tile_sprites[i]);
update_position(tile_sprites[i], [pos[0], pos[1] + dy]);
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
enermys[i]('update_map_offset')(0, -dy);
}
}
}
}
// 游戏初始化
//map
const tile_map=generateTileMap(60,55);
map()('generate_map')(tile_map);
//player
const player_instance = player()('create_player');
player()('update_position')(player_instance);
//gun
const gun_instance = gun()('create_gun');
//bullet pool
for (let i = 0; i < bullet_pool_size; i = i + 1) {
b_sprites[i] = create_rectangle(bullet_radius * 2, bullet_radius * 2);
update_position(b_sprites[i], [-100, -100]);
b_active[i] = false;
b_vx[i] = 0;
b_vy[i] = 0;
b_life[i] = 0;
}
//enemy pool
const elf_url = 'https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/monster/monster_chort.png';
const slime_url = 'https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/monster/monster_bat.png';
const orc_url = 'https://raw.githubusercontent.com/StoreManager-Riddle/SourceAcademyProject/refs/heads/TEST/monster/monster_ogre.png';
const max_pool_size = 50;
for (let i = 0; i < max_pool_size; i = i + 1) {
let rand = math_random();
let new_enemy = undefined;
if (rand < 0.4) {
new_enemy = enemy(3, 0.5, elf_url);
} else if (rand < 0.8) {
new_enemy = enemy(1, 1.2, slime_url);
} else {
new_enemy = enemy(10, 0.2, orc_url);
}
new_enemy('init')();
enermys[i] = new_enemy;
}
const spawner_instance = enemy_spawner(3, 360);
//particle pool
for (let i = 0; i < particle_pool_size; i = i + 1) {
p_sprites[i] = create_rectangle(4, 4);
update_position(p_sprites[i], [-9999, -9999]);
update_scale(p_sprites[i], [0, 0]);
p_active[i] = false;
p_vx[i] = 0;
p_vy[i] = 0;
p_life[i] = 0;
}
//游戏主循环
update_loop((game_state) => {
// Update gameobjects inside update_loop
input_listener();
spawner_instance('update')();
gun()('update_gun')(gun_instance);
if (fire_cooldown > 0) {
fire_cooldown = fire_cooldown - 1;
}
if (input_left_mouse_down() && fire_cooldown <= 0) {
bullets()('fire')(query_pointer_position());
fire_cooldown = bullet_fire_rate;
}
bullets()('update_all')();
particles()('update_all')();
for (let i = 0; i < array_length(enermys); i = i + 1) {
enermys[i]('chase_player')();
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
for (let j = i + 1; j < array_length(enermys); j = j + 1) {
if (enermys[i]('is_alive')() && enermys[j]('is_alive')()) {
let p1 = query_position(enermys[i]('get_sprite')());
let p2 = query_position(enermys[j]('get_sprite')());
let edx = p1[0] - p2[0];
let edy = p1[1] - p2[1];
let ed = math_sqrt(edx * edx + edy * edy);
if (ed < 40 && ed > 0) {
let overlap = (40 - ed) / 2;
let px = (edx / ed) * overlap;
let py = (edy / ed) * overlap;
enermys[i]('push')(px, py);
enermys[j]('push')(-px, -py);
}
}
}
}
for (let i = 0; i < array_length(enermys); i = i + 1) {
if (enermys[i]('is_alive')()) {
let p_enemy = query_position(enermys[i]('get_sprite')());
// 计算怪物与屏幕中心(玩家)的 X 和 Y 距离
let edx = p_enemy[0] - width / 2;
let edy = p_enemy[1] - height / 2;
let ed = math_sqrt(edx * edx + edy * edy);
// 设定安全距离为 40 像素
if (ed < 30 && ed > 0) {
// 如果靠得太近,算出重叠了多少
let overlap = 30 - ed;
// 将 100% 的重叠距离反向施加给怪物(玩家是中心不可撼动的)
let px = (edx / ed) * overlap;
let py = (edy / ed) * overlap;
enermys[i]('push')(px, py);
}
}
}
// Using game_state as a counter
if (game_state[0] === undefined) {
game_state[0] = 0;
}
game_state[0] = game_state[0] + 1;
});
build_game();