Feat: Add splash layer (#29)

Add splash layer, support game start, settings, exit
Modify settings layer, add "go back to splash" and "exit"
Add character threshold for history input
Closes #28
This commit is contained in:
4thfever
2026-01-13 22:00:23 +08:00
committed by GitHub
parent 224e3e76f0
commit 0d34b27fff
9 changed files with 460 additions and 9 deletions

View File

@@ -4,7 +4,10 @@ import asyncio
import webbrowser
import subprocess
import time
import threading
import signal
from contextlib import asynccontextmanager
from typing import List, Optional
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
@@ -883,6 +886,18 @@ def get_map():
}
@app.post("/api/control/reset")
def reset_game():
"""重置游戏到 Idle 状态(回到主菜单)"""
game_instance["world"] = None
game_instance["sim"] = None
game_instance["is_paused"] = True
game_instance["init_status"] = "idle"
game_instance["init_phase"] = 0
game_instance["init_progress"] = 0
game_instance["init_error"] = None
return {"status": "ok", "message": "Game reset to idle"}
@app.post("/api/control/pause")
def pause_game():
"""暂停游戏循环"""
@@ -895,6 +910,17 @@ def resume_game():
game_instance["is_paused"] = False
return {"status": "ok", "message": "Game resumed"}
@app.post("/api/control/shutdown")
async def shutdown_server():
def _shutdown():
time.sleep(1) # 给前端一点时间接收 200 OK 响应
# 这种方式适用于 uvicorn 运行环境,或者直接杀进程
os.kill(os.getpid(), signal.SIGINT)
# 异步执行关闭,确保先返回响应
threading.Thread(target=_shutdown).start()
return {"status": "shutting_down", "message": "Server is shutting down..."}
# --- 初始化状态 API ---
@@ -1551,6 +1577,11 @@ async def api_load_game(req: LoadGameRequest):
game_instance["init_start_time"] = time.time()
game_instance["init_error"] = None
game_instance["init_phase"] = 0
# 0. 扫描资源 (修复读取存档不加载头像的问题)
game_instance["init_phase_name"] = "scanning_assets"
await asyncio.to_thread(scan_avatar_assets)
game_instance["init_phase_name"] = "loading_save"
game_instance["init_progress"] = 10