Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
2118 lines (1895 loc) · 95.7 KB
/
Copy pathmain.py
File metadata and controls
2118 lines (1895 loc) · 95.7 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
"""
弹幕游戏主入口 - 负责初始化和游戏主循环
"""
import sys
import os
import json
import time
import queue
import threading
import ctypes
from pathlib import Path
import moderngl
# Keep the historical ``src.*`` imports stable even when launched through an
# installed console entry point or from a different working directory.
PROJECT_ROOT = Path(__file__).resolve().parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from src.core.window import GameWindow, FrameClock, EVENT_QUIT, EVENT_KEYDOWN
from src.core.input_manager import KeyboardState, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_z, KEY_ESCAPE, KEY_c
from src.core.audio_backend import init_audio_backend
from src.render import Renderer
from src.game.bullet.optimized_pool import OptimizedBulletPool
from src.game.bomb import trigger_player_bomb
from src.game.state import TIME_DOMAINS
from src.game.player import load_player
from src.game.stage import StageManager
from src.game.tasks import FRAME_DT
from src.game.boss import BossManager
from src.game.laser import LaserPool, get_laser_texture_data
from src.game.item import ItemPool
from src.game.particle import ParticlePool
from src.game.audio import GameAudioBank, AudioManager
from src.game.userdata import (
get_settings,
get_progress,
ReplayRecorder,
ReplayPlayback,
list_replays,
load_replay,
)
from src.resource.sprite import SpriteManager
from src.core import (
init_config,
get_collision_manager, init_sprite_registry,
get_project_context, EngineSession, ProjectContextError,
)
from src.resource.texture_asset import TextureAssetManager
from src.resource.service import init_resource_service
from src.render.item_renderer import ItemRenderer
from src.ui import HUD, UIRenderer
from src.ui.dialog_gl_renderer import DialogGLRenderer
from src.ui.loading_renderer import LoadingScreenRenderer
from src.ui.main_menu_renderer import MainMenuRenderer
from src.ui.pause_menu_renderer import PauseMenuRenderer
from src.ui.staff_roll_renderer import StaffRollRenderer
from src.ui.continue_menu_renderer import ContinueMenuRenderer
from src.ui.main_menu_layout import load_layout as load_main_menu_layout
from src.ui.hud import load_hud_layout
from src.ui.bitmap_font import get_font_manager
from src.devtools.hotreload import HotReloadManager
from src.compiler.content_entry import load_content_entry
from src.core.preview_protocol import (
MAX_PREVIEW_LINE_BYTES,
PREVIEW_DEFAULT_SEED,
PreviewProtocolError,
decode_message,
encode_message,
event_message,
)
from game_content.stages.stage1.stage_asset_preview import Stage1AssetPreview
from game_content.stages.stage_test.stage_script import StageTest
# ===== Debug 模式 =====
DEBUG_MODE = "--debug" in sys.argv
PROFILE_MODE = "--profile" in sys.argv
HOT_RELOAD_MODE = "--hot-reload" in sys.argv
PROFILE_REPORT_FRAMES = 120
def _get_cli_option(prefix: str):
"""读取 ``--key=value`` 或 ``--key value`` 形式的命令行参数。"""
key = prefix[:-1] if prefix.endswith("=") else prefix
args = sys.argv[1:]
for index, arg in enumerate(args):
if arg.startswith(prefix):
return arg.split("=", 1)[1].strip()
if arg == key and index + 1 < len(args):
return args[index + 1].strip()
return None
CONTENT_ENTRY_MODULE = _get_cli_option("--content-entry=") or "game_content.entry"
EDITOR_PREVIEW_RUN_ID = _get_cli_option("--editor-preview=")
EDITOR_PREVIEW_MODE = EDITOR_PREVIEW_RUN_ID is not None
AUTHORING_ROOT = _get_cli_option("--authoring-root=")
if AUTHORING_ROOT:
sys.path.insert(0, str(Path(AUTHORING_ROOT).resolve()))
CONTENT_ENTRY = load_content_entry(CONTENT_ENTRY_MODULE)
ALL_STAGES = list(CONTENT_ENTRY.stages)
START_STAGE = CONTENT_ENTRY.start_stage
STAGE_BY_ID = dict(CONTENT_ENTRY.stage_by_id)
class _EditorPreviewChannel:
"""Non-blocking stdin controls and exclusive NDJSON stdout events."""
def __init__(self, run_id: str):
self.run_id = run_id
self._protocol_output = sys.stdout.buffer
self._controls = queue.SimpleQueue()
self._closed = False
self._last_metric_frame = -1
self._last_spawned_total = 0
self.timeline_unit_id = _get_cli_option("--preview-unit-id=")
# Existing game diagnostics remain useful, but stdout is reserved for
# the protocol so the Qt client never has to guess which lines are JSON.
sys.stdout = sys.stderr
self._stdin_fd = sys.stdin.fileno()
self._reader = threading.Thread(
target=self._read_controls,
name="pystg-preview-controls",
daemon=True,
)
self._reader.start()
def _read_controls(self):
buffered = bytearray()
while not self._closed:
try:
chunk = os.read(self._stdin_fd, 4096)
except OSError:
return
if not chunk:
return
buffered.extend(chunk)
while b"\n" in buffered:
raw, _, remainder = buffered.partition(b"\n")
buffered = bytearray(remainder)
if len(raw) > MAX_PREVIEW_LINE_BYTES:
self._controls.put(
PreviewProtocolError(
"line_too_long", "preview control line is too long"
)
)
continue
try:
message = decode_message(raw)
if "command" not in message:
raise PreviewProtocolError(
"invalid_message", "game process accepts control messages only"
)
if message["run_id"] != self.run_id:
continue
self._controls.put(message)
except PreviewProtocolError as exc:
self._controls.put(exc)
if len(buffered) > MAX_PREVIEW_LINE_BYTES:
buffered.clear()
self._controls.put(
PreviewProtocolError("line_too_long", "preview control line is too long")
)
def poll(self):
values = []
while True:
try:
values.append(self._controls.get_nowait())
except queue.Empty:
return values
def emit(self, event: str, payload=None):
if self._closed:
return
try:
self._protocol_output.write(
encode_message(event_message(self.run_id, event, payload or {}))
)
self._protocol_output.flush()
except (BrokenPipeError, OSError):
self._closed = True
def close(self):
if not self._closed:
self.emit("stopped", {})
self._closed = True
def reset_metrics(self):
self._last_metric_frame = -1
self._last_spawned_total = 0
def metric_sample(self, frame, bullet_pool, laser_pool, *, force=False):
"""Return one fixed-rate aggregate sample without per-bullet hooks."""
frame = max(0, int(frame))
spawned_total = max(0, int(getattr(bullet_pool, "spawned_total", 0)))
if frame < self._last_metric_frame or spawned_total < self._last_spawned_total:
self._last_metric_frame = -1
self._last_spawned_total = spawned_total
if not force and (frame % 12 != 0 or frame == self._last_metric_frame):
return None
emitted = max(0, spawned_total - self._last_spawned_total)
alive = getattr(bullet_pool, "data", None)
live = int(alive["alive"].sum()) if alive is not None else 0
lasers = int(getattr(laser_pool, "laser_count", 0)) + int(
getattr(laser_pool, "bent_laser_count", 0)
)
self._last_metric_frame = frame
self._last_spawned_total = spawned_total
return {
"kind": "metrics",
"frame": frame,
"emitted": emitted,
"live": max(0, live),
"lasers": max(0, lasers),
}
_PREVIEW_CHANNEL = None
def _flush_preview_runtime(
channel,
stage_manager,
bullet_pool,
laser_pool,
*,
force_frame=False,
):
if channel is None:
return
while True:
events, dropped = stage_manager.drain_authoring_trace(256)
if not events and not dropped:
break
channel.emit(
"trace",
{"events": list(events), **({"dropped": dropped} if dropped else {})},
)
if not events:
break
frame = stage_manager.get_timeline_frame(channel.timeline_unit_id)
frame = max(0, int(frame or 0))
sample = channel.metric_sample(
frame,
bullet_pool,
laser_pool,
force=force_frame,
)
if sample is not None:
if stage_manager.timeline_stage_id:
sample["stage_id"] = stage_manager.timeline_stage_id
if channel.timeline_unit_id:
sample["unit_id"] = channel.timeline_unit_id
channel.emit("trace", {"events": [sample]})
if force_frame or frame % 6 == 0:
context = stage_manager.current_context
unit_frames = ({unit_id: context.get_unit_timeline_frame(unit_id)
for unit_id in list(context._authoring_unit_origins)[-128:]}
if context is not None else {})
channel.emit("frame", {"frame": frame,
"runtime_frame": stage_manager.get_frame_count(),
"stage_frame": stage_manager.get_timeline_frame(),
"stage_id": stage_manager.timeline_stage_id,
"unit_id": channel.timeline_unit_id,
"unit_frames": unit_frames})
def _stage_class_by_id(stage_id: str):
"""根据 stage.id(或类名)查找关卡类,回退内容入口的开始关。"""
sid = (stage_id or "").lower()
for cls in ALL_STAGES + [StageTest]:
if getattr(cls, "id", "").lower() == sid:
return cls
if cls.__name__.lower() == sid:
return cls
return START_STAGE
def resolve_stage_class():
"""根据 --stage 参数解析要加载的关卡类。"""
requested = _get_cli_option("--stage=")
if requested is None:
return START_STAGE
stage_arg = requested.strip().lower()
stage_map = {
"asset_preview": Stage1AssetPreview,
"preview": Stage1AssetPreview,
"assets": Stage1AssetPreview,
}
for stage_id, stage_class in STAGE_BY_ID.items():
stage_map[str(stage_id).lower()] = stage_class
stage_map[stage_class.__name__.lower()] = stage_class
stage_class = stage_map.get(stage_arg)
if stage_class is None:
print(
f"[main] 未识别的 --stage={stage_arg},"
f"回退到 {getattr(START_STAGE, 'id', START_STAGE.__name__)}"
)
return START_STAGE
print(f"[main] 当前关卡: {stage_class.__name__} (--stage={stage_arg})")
return stage_class
def _scan_wave_bookmarks(stage_class):
"""扫描 stage 包目录下 waves/ 子目录,收集 DEBUG_BOOKMARK=True 的 Wave 类。"""
import importlib
import pkgutil
from src.game.stage.wave_base import Wave
results = []
module_name = getattr(stage_class, '__module__', '')
# e.g. "game_content.stages.stage1.stage_script" -> "game_content.stages.stage1"
package_name = '.'.join(module_name.split('.')[:-1])
waves_package = f"{package_name}.waves"
try:
waves_mod = importlib.import_module(waves_package)
except ImportError:
return results
waves_path = getattr(waves_mod, '__path__', [])
for finder, mod_name, _ in pkgutil.iter_modules(waves_path):
full_name = f"{waves_package}.{mod_name}"
try:
mod = importlib.import_module(full_name)
except Exception:
continue
for attr_name in dir(mod):
obj = getattr(mod, attr_name, None)
if (
isinstance(obj, type)
and issubclass(obj, Wave)
and obj is not Wave
and getattr(obj, 'DEBUG_BOOKMARK', False)
):
results.append(obj)
# 去重,保持发现顺序
seen = set()
unique = []
for cls in results:
if id(cls) not in seen:
seen.add(id(cls))
unique.append(cls)
return unique
def build_debug_menu(stage_class):
"""从 StageScript 子类构建 Debug 跳转菜单"""
from src.game.stage.stage_base import BossDef
from src.game.stage.boss_base import BossPhaseType
entries = [{"label": "从头开始", "target": None, "is_bookmark": False}]
# ---- Bookmark 区域:Wave 类 + BossPhase ----
bookmark_entries = []
# Wave bookmarks
for wave_cls in _scan_wave_bookmarks(stage_class):
bookmark_entries.append({
"label": f"[Bookmark] Wave: {wave_cls.__name__}",
"target": {"type": "wave", "wave_class": wave_cls},
"is_bookmark": True,
})
# Boss phase bookmarks
for attr_name in vars(stage_class):
if attr_name.startswith('_'):
continue
attr = getattr(stage_class, attr_name, None)
if not isinstance(attr, BossDef):
continue
is_midboss = "mid" in attr_name.lower()
effective_type = "midboss" if is_midboss else "boss"
for i, phase in enumerate(attr.phases):
sc = phase.script_class
if sc and getattr(sc, 'DEBUG_BOOKMARK', False):
if phase.phase_type == BossPhaseType.SPELLCARD:
phase_label = phase.name or f"符卡 {i}"
else:
phase_label = f"通常攻撃 {i + 1}"
bookmark_entries.append({
"label": f"[Bookmark] {attr.name} / {phase_label}",
"target": {"type": effective_type, "phase": i},
"is_bookmark": True,
})
if bookmark_entries:
entries.append({"label": "───── Bookmarks ─────", "target": None, "is_bookmark": False, "separator": True})
entries.extend(bookmark_entries)
# ---- Boss 入口区域 ----
for attr_name in vars(stage_class):
if attr_name.startswith('_'):
continue
attr = getattr(stage_class, attr_name, None)
if not isinstance(attr, BossDef):
continue
is_midboss = "mid" in attr_name.lower()
effective_type = "midboss" if is_midboss else "boss"
type_label = "道中 Boss" if is_midboss else "Boss"
# Boss 入口(从第 0 阶段开始)
entries.append({
"label": f"{type_label}: {attr.name} ({attr.id})",
"target": {"type": effective_type, "phase": 0},
"is_bookmark": False,
})
# 每个阶段
for i, phase in enumerate(attr.phases):
if phase.phase_type == BossPhaseType.SPELLCARD:
phase_label = phase.name or f"符卡 {i}"
else:
phase_label = f"通常攻撃 {i + 1}"
entries.append({
"label": f" └ Phase {i}: {phase_label}",
"target": {"type": effective_type, "phase": i},
"is_bookmark": False,
})
return entries
def run_replay_select_menu(window, ctx, screen_size):
"""列出 userdata/replays/ 中的重放文件,让玩家选一个。返回 path 或 None。"""
from src.ui.main_menu_renderer import MainMenuRenderer
from src.core.window import FrameClock, EVENT_QUIT, EVENT_KEYDOWN
from src.core.input_manager import KEY_UP, KEY_DOWN, KEY_z, KEY_ESCAPE
replays = list_replays()
if not replays:
# 没有重放:弹一个空菜单提示
labels = ["(暂无重放,按 ESC 返回)"]
entries = [None]
else:
labels = [
f"{r['stage']:<8} | {r['character']:<6} | {r['frame_count']:>5}帧 | {r['created_at']}"
for r in replays
]
entries = [r["path"] for r in replays]
layout = {
"bg_gradient": {"top": [10, 25, 25], "bottom": [20, 40, 50]},
"title": {
"text": "选择重放",
"font_size": 36,
"color": [180, 220, 255],
"y_ratio": 0.08,
},
"options": [{"text": lbl} for lbl in labels],
"option_spacing": 28,
"option_font_size": 20,
"option_colors": {"normal": [180, 200, 200], "selected": [255, 255, 120]},
"hint": {
"text": "↑↓ 选择 Z 播放 ESC 返回",
"font_size": 18,
"color": [140, 140, 140],
"y_offset": -30,
},
}
selected_index = 0
renderer = MainMenuRenderer(ctx, screen_size[0], screen_size[1])
clock = FrameClock()
n = len(entries)
while True:
clock.tick(60)
for event in window.poll_events():
if event['type'] == EVENT_QUIT:
renderer.cleanup()
return None
if event['type'] == EVENT_KEYDOWN:
if event['key'] == KEY_UP:
selected_index = (selected_index - 1) % n
elif event['key'] == KEY_DOWN:
selected_index = (selected_index + 1) % n
elif event['key'] == KEY_z:
renderer.cleanup()
return entries[selected_index]
elif event['key'] == KEY_ESCAPE:
renderer.cleanup()
return None
ctx.viewport = window.viewport
ctx.clear(0.0, 0.0, 0.0)
renderer.render(selected_index, layout=layout)
window.swap_buffers()
def run_settings_menu(window, ctx, screen_size, audio_manager):
"""
设置菜单:分类卡片 + 滑块/开关/循环 + 实时预览。
控制:↑↓ 选项;←→ 调节(滑块/循环);Z 触发动作或切换;ESC 保存返回。
"""
from src.ui.settings_menu_renderer import SettingsMenuRenderer
from src.core.window import FrameClock, EVENT_QUIT, EVENT_KEYDOWN
from src.core.input_manager import (
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_z, KEY_ESCAPE,
)
settings = get_settings()
# 缓冲区:在菜单中编辑,统一 commit
state = {
"se_volume": settings.se_volume,
"bgm_volume": settings.bgm_volume,
"fullscreen": settings.fullscreen,
"last_character": settings.last_character,
}
char_options = ["tao", "orin", "tenshi"]
char_display = {"tao": "桃 (Tao)", "orin": "燐 (Orin)", "tenshi": "天子 (Tenshi)"}
# 项目列表:每项指向 state 的某个键 + 控件类型
# idx -> dict
def build_items() -> list:
return [
{"section": "音频", "key": "se_volume", "label": "SE 音量", "type": "slider",
"value": state["se_volume"]},
{"section": "音频", "key": "bgm_volume", "label": "BGM 音量", "type": "slider",
"value": state["bgm_volume"]},
{"section": "显示", "key": "fullscreen", "label": "全屏 (重启生效)", "type": "toggle",
"value": state["fullscreen"]},
{"section": "游戏", "key": "last_character","label": "默认自机", "type": "cycle",
"value": state["last_character"],
"display": char_display.get(state["last_character"], state["last_character"]),
"options": char_options},
{"section": "其它", "label": "重置为默认值", "type": "action", "key": "_reset"},
{"label": "返回(自动保存)", "type": "action", "key": "_back"},
]
def apply_audio_live():
settings.se_volume = state["se_volume"]
settings.bgm_volume = state["bgm_volume"]
settings.apply_audio(audio_manager)
def commit_and_save():
settings.se_volume = state["se_volume"]
settings.bgm_volume = state["bgm_volume"]
settings.fullscreen = state["fullscreen"]
settings.last_character = state["last_character"]
settings.apply_audio(audio_manager)
settings.save()
def reset_defaults():
state["se_volume"] = 0.7
state["bgm_volume"] = 0.6
state["fullscreen"] = False
state["last_character"] = "tao"
apply_audio_live()
selected = 0
renderer = SettingsMenuRenderer(ctx, screen_size[0], screen_size[1])
clock = FrameClock()
while True:
clock.tick(60)
items = build_items()
n = len(items)
for event in window.poll_events():
if event['type'] == EVENT_QUIT:
commit_and_save()
renderer.cleanup()
return
if event['type'] != EVENT_KEYDOWN:
continue
key = event['key']
cur = items[selected]
kind = cur["type"]
ckey = cur.get("key")
if key == KEY_UP:
selected = (selected - 1) % n
elif key == KEY_DOWN:
selected = (selected + 1) % n
elif key in (KEY_LEFT, KEY_RIGHT):
delta = 1 if key == KEY_RIGHT else -1
if kind == "slider" and ckey:
step = 0.05
state[ckey] = max(0.0, min(1.0, state[ckey] + delta * step))
apply_audio_live()
elif kind == "toggle" and ckey:
state[ckey] = not state[ckey]
elif kind == "cycle" and ckey:
options = cur.get("options", [])
if options:
try:
idx = options.index(state[ckey])
except ValueError:
idx = 0
state[ckey] = options[(idx + delta) % len(options)]
elif key == KEY_z:
if kind == "toggle" and ckey:
state[ckey] = not state[ckey]
elif kind == "cycle" and ckey:
options = cur.get("options", [])
if options:
try:
idx = options.index(state[ckey])
except ValueError:
idx = 0
state[ckey] = options[(idx + 1) % len(options)]
elif kind == "action":
if ckey == "_back":
commit_and_save()
renderer.cleanup()
return
elif ckey == "_reset":
reset_defaults()
elif key == KEY_ESCAPE:
commit_and_save()
renderer.cleanup()
return
# 重新构建以反映 state 变化
items = build_items()
model = {
"title": "设置",
"items": items,
"selected": selected,
"hint": "↑↓ 选项 ←→ 调节 Z 切换/确认 ESC 保存返回",
}
ctx.viewport = window.viewport
ctx.clear(0.0, 0.0, 0.0)
renderer.render(model)
window.swap_buffers()
def run_stage_select_menu(window, ctx, screen_size, stage_classes, current_class):
"""Debug: 在进入书签菜单之前让用户选择关卡。
按 Z 确认选择,ESC 跳过(保持 current_class)。
"""
from src.ui.main_menu_renderer import MainMenuRenderer
from src.core.window import FrameClock, EVENT_QUIT, EVENT_KEYDOWN
from src.core.input_manager import KEY_UP, KEY_DOWN, KEY_z, KEY_ESCAPE
entries = list(stage_classes)
labels = [f"{cls.name} ({cls.id})" for cls in entries]
layout = {
"bg_gradient": {"top": [10, 10, 25], "bottom": [25, 25, 50]},
"title": {
"text": "Debug: 选择关卡",
"font_size": 36,
"color": [180, 220, 255],
"y_ratio": 0.08,
},
"options": [{"text": lbl} for lbl in labels],
"option_spacing": 36,
"option_font_size": 26,
"option_colors": {"normal": [160, 160, 200], "selected": [255, 255, 120]},
"hint": {
"text": "↑↓ 选择关卡 Z 确认 ESC 跳过",
"font_size": 18,
"color": [140, 140, 140],
"y_offset": -30,
},
}
# 默认选中当前关卡
try:
selected_index = entries.index(current_class)
except ValueError:
selected_index = 0
renderer = MainMenuRenderer(ctx, screen_size[0], screen_size[1])
clock = FrameClock()
while True:
clock.tick(60)
for event in window.poll_events():
if event['type'] == EVENT_QUIT:
renderer.cleanup()
return current_class
if event['type'] == EVENT_KEYDOWN:
if event['key'] == KEY_UP:
selected_index = (selected_index - 1) % len(entries)
elif event['key'] == KEY_DOWN:
selected_index = (selected_index + 1) % len(entries)
elif event['key'] == KEY_z:
renderer.cleanup()
return entries[selected_index]
elif event['key'] == KEY_ESCAPE:
renderer.cleanup()
return current_class
ctx.viewport = window.viewport
ctx.clear(0.0, 0.0, 0.0)
renderer.render(selected_index, layout=layout)
window.swap_buffers()
def run_debug_menu(window, ctx, screen_size, stage_class):
"""在 GUI 中显示 Debug 跳转菜单,处理用户输入并返回选择的 target 或 None"""
from src.ui.main_menu_renderer import MainMenuRenderer
from src.core.window import FrameClock, EVENT_QUIT, EVENT_KEYDOWN
from src.core.input_manager import KEY_UP, KEY_DOWN, KEY_z, KEY_ESCAPE, KEY_b
stage_name = getattr(stage_class, 'name', stage_class.__name__)
entries = build_debug_menu(stage_class)
# 构建适合 Debug 菜单的布局
# 为了容纳更多选项,缩小字体,减小间距,并将整体上移
layout = {
"bg_gradient": {"top": [20, 10, 10], "bottom": [40, 20, 20]}, # 偏红背景区别于主菜单
"title": {
"text": f"Debug: {stage_name}",
"font_size": 36,
"color": [255, 200, 200],
"y_ratio": 0.05
},
"options": [{"text": entry["label"]} for entry in entries],
"option_spacing": 26, # 间距更紧凑
"option_font_size": 22, # 字体更小
"option_colors": {"normal": [180, 180, 180], "selected": [255, 255, 100]},
"hint": {
"text": "↑↓ 选择 Z 确认 ESC 从头 [B] 书签",
"font_size": 18,
"color": [150, 150, 150],
"y_offset": -30
}
}
renderer = MainMenuRenderer(ctx, screen_size[0], screen_size[1])
num_options = len(entries)
selected_index = 0
clock = FrameClock()
while True:
clock.tick(60)
for event in window.poll_events():
if event['type'] == EVENT_QUIT:
renderer.cleanup()
return None
if event['type'] == EVENT_KEYDOWN:
if event['key'] == KEY_UP:
selected_index = (selected_index - 1) % num_options
elif event['key'] == KEY_DOWN:
selected_index = (selected_index + 1) % num_options
elif event['key'] == KEY_z:
renderer.cleanup()
return entries[selected_index]["target"]
elif event['key'] == KEY_ESCAPE:
renderer.cleanup()
return None # 默认不跳过
elif event['key'] == KEY_b:
# 清除当前选中的 Bookmark 标记(仅对书签项生效)
if entries[selected_index].get("is_bookmark") and entries[selected_index].get("target"):
# 清除所有 Boss 阶段的 DEBUG_BOOKMARK 标记
for attr_name in vars(stage_class):
if attr_name.startswith('_'):
continue
attr = getattr(stage_class, attr_name, None)
from src.game.stage.stage_base import BossDef
if not isinstance(attr, BossDef):
continue
for phase in attr.phases:
if getattr(phase.script_class, 'DEBUG_BOOKMARK', False):
phase.script_class.DEBUG_BOOKMARK = False
# 重建菜单
entries = build_debug_menu(stage_class)
layout["options"] = [{"text": e["label"]} for e in entries]
num_options = len(entries)
selected_index = min(selected_index, num_options - 1)
ctx.viewport = window.viewport
ctx.clear(0.0, 0.0, 0.0)
renderer.render(selected_index, layout=layout)
window.swap_buffers()
def initialize_window_and_context():
"""初始化GLFW窗口和ModernGL上下文"""
if EDITOR_PREVIEW_MODE and sys.platform == "win32":
# Qt 6 uses Per-Monitor-V2. GLFW must enter the same DPI mode before
# glfw.init(), otherwise modern Windows refuses cross-process SetParent.
ctypes.windll.user32.SetProcessDpiAwarenessContext(ctypes.c_void_p(-4))
init_audio_backend()
config = init_config()
base_size = (config.base_width, config.base_height)
screen_size = (config.window_width, config.window_height)
game_viewport = config.game_viewport
# 读取持久化的 fullscreen 偏好(设置菜单里的 "全屏 (重启生效)")
settings = get_settings()
fullscreen = bool(settings.fullscreen) and not EDITOR_PREVIEW_MODE
window = GameWindow(
screen_size[0], screen_size[1],
"PySTG Editor Preview" if EDITOR_PREVIEW_MODE else "东方做题狙特别版",
fullscreen=fullscreen,
)
if fullscreen:
print(f"[main] Fullscreen ON: framebuffer={window.framebuffer_size}, "
f"viewport={window.viewport}")
ctx = moderngl.create_context()
ctx.enable(moderngl.BLEND)
ctx.blend_func = moderngl.SRC_ALPHA, moderngl.ONE_MINUS_SRC_ALPHA
game_viewport_fb = compute_game_viewport_fb(
game_viewport, screen_size, window.framebuffer_size
)
return window, ctx, base_size, screen_size, game_viewport, game_viewport_fb
def compute_game_viewport_fb(
game_viewport: tuple[int, int, int, int],
screen_size: tuple[int, int],
framebuffer_size: tuple[int, int],
) -> tuple[int, int, int, int]:
"""Scale the logical game viewport into framebuffer pixels.
Recomputed whenever the framebuffer changes: the editor embeds the game
window and resizes it externally, so the init-time ratio goes stale and
the play field would render clipped into the smaller backbuffer.
"""
fb_w, fb_h = framebuffer_size
scale_x = fb_w / screen_size[0]
scale_y = fb_h / screen_size[1]
return (
int(round(game_viewport[0] * scale_x)),
int(round(game_viewport[1] * scale_y)),
int(round(game_viewport[2] * scale_x)),
int(round(game_viewport[3] * scale_y)),
)
def load_resources(ctx, texture_asset_manager: TextureAssetManager):
"""
加载游戏资源(使用新的统一纹理资产管理系统)
Args:
ctx: ModernGL上下文
texture_asset_manager: 纹理资产管理器
Returns:
tuple: (textures, sprite_uv_map)
"""
sprite_config_folder = "assets/images"
if not texture_asset_manager.load_sprite_config_folder(sprite_config_folder):
print("Failed to load sprite configurations!")
sys.exit()
all_sprite_ids = texture_asset_manager.get_all_sprite_ids()
textures = texture_asset_manager.create_all_gl_textures(ctx, flip_y=True)
sprite_uv_map = texture_asset_manager.compute_all_sprite_uvs(flip_y=True)
stats = texture_asset_manager.get_stats()
print(f"资源加载完成: {stats['atlases']} 图集, {stats['sprites']} 精灵, {stats['animations']} 动画")
print(f"Loaded {len(textures)} texture(s) successfully")
return textures, sprite_uv_map
def initialize_sprite_registry_from_assets(sprite_manager: SpriteManager, textures: dict):
"""
用当前已加载资产重建 SpriteRegistry,确保 OptimizedBulletPool 的 sprite_idx/UV/纹理映射正确。
"""
texture_sizes = {}
for path, tex in textures.items():
texture_sizes[path] = tex.size
texture_sizes[path.replace('\\', '/')] = tex.size
texture_sizes[path.lower()] = tex.size
texture_sizes[path.replace('\\', '/').lower()] = tex.size
registry = init_sprite_registry(max_sprites=8192)
registry.register_from_sprite_manager(sprite_manager, texture_sizes, flip_y=True)
print(f"SpriteRegistry 已重建: {registry.count} sprites")
def initialize_game_objects(stage_class, audio_manager=None, background_renderer=None,
character: str = "tao"):
"""初始化游戏对象(玩家、子弹池、关卡管理器等)"""
try:
player = load_player(character)
except Exception:
print(f"[main] 加载玩家 '{character}' 失败,回退 tao")
player = load_player("tao")
print(f"已加载玩家: {player.name}")
# 使用优化版子弹池(整数 sprite 索引 + 向量化渲染数据准备)
bullet_pool = OptimizedBulletPool(max_bullets=50000)
warmup_timings = bullet_pool.warmup()
print("[Runtime] Numba warmup (ms): " + ", ".join(
f"{name}={duration:.1f}" for name, duration in warmup_timings.items()
))
laser_pool = LaserPool(max_lasers=100)
print(f"[Runtime] Laser warmup (ms): {laser_pool.warmup()}")
item_pool = ItemPool(max_items=1000)
particle_pool = ParticlePool(sprite_registry=bullet_pool.sprite_registry)
print(f"[Runtime] Particle warmup (ms): {particle_pool.warmup()}")
boss_manager = BossManager()
stage_manager = StageManager()
stage_manager.set_boss_manager(boss_manager)
stage_manager.bind_engine(
bullet_pool=bullet_pool,
laser_pool=laser_pool,
player=player,
audio_manager=audio_manager,
item_pool=item_pool,
particle_pool=particle_pool,
background_renderer=background_renderer,
player_shot=character,
)
stage_manager.load_stage(stage_class)
return player, bullet_pool, laser_pool, item_pool, stage_manager
def run_main_menu(window, ctx, screen_size, audio_manager) -> int:
"""
显示主菜单,处理用户输入。
Returns:
选中的菜单项索引(Z 确认);-1 表示退出(ESC 或关窗口)。
约定:选项顺序由 main_menu_layout.json 决定:
0 = 开始游戏
1 = 测试关卡
2 = 退出
"""
main_menu_renderer = MainMenuRenderer(ctx, screen_size[0], screen_size[1])
main_menu_layout = load_main_menu_layout("assets/ui/main_menu_layout.json")
options = main_menu_layout.get("options", [])
num_options = max(1, len(options))
# 找到"退出"选项的索引,用于 Z 确认时判断
quit_index = next(
(i for i, o in enumerate(options)
if (o.get("text", "") if isinstance(o, dict) else str(o)) in ("退出", "Quit", "Exit")),
num_options - 1,
)
selected_index = 0
clock = FrameClock()
audio_manager.play_bgm("00", announce=False)
while True:
clock.tick(60)
for event in window.poll_events():
if event['type'] == EVENT_QUIT:
audio_manager.stop_bgm(fade_ms=300)
main_menu_renderer.cleanup()
return -1
if event['type'] == EVENT_KEYDOWN:
if event['key'] == KEY_UP:
selected_index = (selected_index - 1) % num_options
elif event['key'] == KEY_DOWN:
selected_index = (selected_index + 1) % num_options
elif event['key'] == KEY_z:
audio_manager.stop_bgm(fade_ms=300)
main_menu_renderer.cleanup()
if selected_index == quit_index:
return -1
return selected_index
elif event['key'] == KEY_ESCAPE:
audio_manager.stop_bgm(fade_ms=300)
main_menu_renderer.cleanup()
return -1
ctx.viewport = window.viewport
ctx.clear(0.0, 0.0, 0.0)
main_menu_renderer.render(selected_index, layout=main_menu_layout)
window.swap_buffers()
def main():
"""游戏主函数"""
global _PREVIEW_CHANNEL
if "--help" in sys.argv or "-h" in sys.argv:
print("usage: python main.py [options]")
print()
print("选项(--key=value 与 --key value 两种写法都支持):")
print(" --content-entry=MODULE 手写关卡入口模块,默认 game_content.entry")
print(" --stage=ID 起始关卡 ID,或 asset_preview 进入资产预览")
print(" --project=PATH 项目根目录,用于解析 res:// 资源引用,默认当前目录")
print(" --debug 启用调试模式,菜单可跳转到任意波次、Boss 或符卡")
print(f" --profile 输出前 {PROFILE_REPORT_FRAMES} 帧的逐帧耗时")
print(" --hot-reload 监视弹幕别名、HUD 布局和激光配置并自动重新加载")
print(" --replay=PATH 直接回放指定的录像文件")
print(" --editor-preview=RUN_ID 编辑器预览模式,按 NDJSON 协议输出运行事件")
print(" --preview-seed=INTEGER 编辑器预览的随机种子,默认 1337")
print(" --preview-unit-id=ID 编辑器预览定位到指定 Timeline 单元")
print(" --authoring-root=PATH 追加到 sys.path 的作者工程根目录")
print()
print(f"content entry: {CONTENT_ENTRY_MODULE}")
print("stages: " + ", ".join(STAGE_BY_ID))