game-architecture by opusgamelabs/game-creator
npx skills add https://github.com/opusgamelabs/game-creator --skill game-architecture构建结构良好的浏览器游戏的参考知识。这些模式适用于 Three.js(3D)和 Phaser(2D)游戏。
详细参考请查看本目录中的配套文件:
system-patterns.md — 对象池、增量时间标准化、资源释放、波次/生成系统、增益/强化系统、触觉反馈、资产管理核心循环优先:在任何润色之前先实现最小的游戏玩法循环。顺序是:输入 -> 移动 -> 失败条件 -> 计分 -> 重新开始。只有在核心循环正常工作后,才添加视觉效果、音频或增强效果。保持初始范围小:1个场景/关卡,1个机制,1个失败条件。
事件驱动通信:模块之间绝不通过导入进行通信。所有跨模块的消息传递都通过一个具有预定义事件常量的单例 EventBus 进行。
集中式状态:一个单一的 GameState 单例持有所有游戏状态。系统直接读取状态并通过事件修改它。状态不分散在各个模块中。
配置集中化:每个魔法数字、平衡值、资源路径、生成点、计时值都放在 Constants.js 中。游戏逻辑文件不包含任何硬编码值。
编排器模式:一个 Game.js 类初始化所有系统,管理游戏流程(启动 -> 游戏玩法 -> 死亡/胜利 -> 重新开始),并运行主循环。系统不自我初始化。默认没有标题屏幕 — 直接启动进入游戏玩法。只有在用户明确要求时才添加标题/菜单场景。
重启安全且确定性:游戏玩法必须能在完整的重启周期中干净地存活。 恢复一个完全干净的状态。所有事件监听器都在清理/关闭时被移除。没有陈旧的引用、残留的计时器、泄漏的补间动画或孤立的物理体在重启后存活。通过连续重启3次进行测试 — 第三次运行的行为必须与第一次完全相同。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
GameState.reset()清晰的关注点分离:代码按功能层组织:
core/ - 基础(Game、EventBus、GameState、Constants)systems/ - 引擎级系统(输入、物理、音频、粒子)gameplay/ - 游戏机制(玩家、敌人、武器、计分)level/ - 世界构建(关卡构建、资源加载)ui/ - 界面(菜单、HUD、覆盖层)使用按功能区域分组的 domain:action 格式:
export const Events = {
// 玩家
PLAYER_DAMAGED: 'player:damaged',
PLAYER_HEALED: 'player:healed',
PLAYER_DIED: 'player:died',
// 敌人
ENEMY_SPAWNED: 'enemy:spawned',
ENEMY_KILLED: 'enemy:killed',
// 游戏流程
GAME_STARTED: 'game:started',
GAME_PAUSED: 'game:paused',
GAME_OVER: 'game:over',
// 系统
ASSETS_LOADED: 'assets:loaded',
LOADING_PROGRESS: 'loading:progress'
};
始终传递结构化的数据对象,而不是原始值:
// 好
eventBus.emit(Events.PLAYER_DAMAGED, { amount: 10, source: 'enemy', damageType: 'melee' });
// 不好
eventBus.emit(Events.PLAYER_DAMAGED, 10);
将状态组织成清晰的领域:
class GameState {
constructor() {
this.player = { health, maxHealth, speed, inventory, buffs };
this.combat = { killCount, waveNumber, score };
this.game = { started, paused, isPlaying };
}
}
适用于 2D 和 3D 游戏的标准流程:
启动/加载 -> 游戏玩法 <-> 暂停菜单(如果需要)
-> 游戏结束 -> 游戏玩法(重新开始)
默认没有标题屏幕。 游戏直接启动进入游戏玩法。Play.fun 小组件在游戏顶部的死区处理分数显示、排行榜和钱包连接,因此不需要游戏内的分数 HUD。只有在用户明确要求时才添加标题/菜单场景。
physics.add.collider() 或 physics.add.overlap() 将其连接到其他物体,将没有游戏玩法效果。每个边界或障碍物都需要与其应交互的实体进行显式的碰撞连接。创建任何静态体后,立即添加碰撞器调用。shutdown() 中移除事件监听器、销毁计时器和释放资源会导致重启后出现幽灵行为、事件重复触发和内存泄漏。在认为游戏完成之前,验证所有项目:
mute-button 规则npm run build 成功且无错误每周安装次数
105
代码仓库
GitHub 星标数
26
首次出现
2026年2月21日
安全审计
安装于
claude-code83
opencode57
gemini-cli55
github-copilot55
amp55
codex55
Reference knowledge for building well-structured browser games. These patterns apply to both Three.js (3D) and Phaser (2D) games.
For detailed reference, see companion files in this directory:
system-patterns.md — Object pooling, delta-time normalization, resource disposal, wave/spawn systems, buff/powerup system, haptic feedback, asset managementCore Loop First : Implement the minimum gameplay loop before any polish. The order is: input -> movement -> fail condition -> scoring -> restart. Only after the core loop works should you add visuals, audio, or juice. Keep initial scope small: 1 scene/level, 1 mechanic, 1 fail condition.
Event-Driven Communication : Modules never import each other for communication. All cross-module messaging goes through a singleton EventBus with predefined event constants.
Centralized State : A single GameState singleton holds all game state. Systems read state directly and modify it through events. No scattered state across modules.
Configuration Centralization : Every magic number, balance value, asset path, spawn point, and timing value goes in Constants.js. Game logic files contain zero hardcoded values.
Orchestrator Pattern : One Game.js class initializes all systems, manages game flow (boot -> gameplay -> death/win -> restart), and runs the main loop. Systems don't self-initialize. No title screen by default — boot directly into gameplay. Only add a title/menu scene if the user explicitly asks for one.
Restart-Safe and Deterministic : Gameplay must survive full restart cycles cleanly. GameState.reset() restores a complete clean slate. All event listeners are removed in cleanup/shutdown. No stale references, lingering timers, leaked tweens, or orphaned physics bodies survive across restarts. Test by restarting 3x in a row — the third run must behave identically to the first.
Clear Separation of Concerns : Code is organized into functional layers:
core/ - Foundation (Game, EventBus, GameState, Constants)systems/ - Engine-level systems (input, physics, audio, particles)gameplay/ - Game mechanics (player, enemies, weapons, scoring)level/ - World building (level construction, asset loading)ui/ - Interface (menus, HUD, overlays)Use domain:action format grouped by feature area:
export const Events = {
// Player
PLAYER_DAMAGED: 'player:damaged',
PLAYER_HEALED: 'player:healed',
PLAYER_DIED: 'player:died',
// Enemy
ENEMY_SPAWNED: 'enemy:spawned',
ENEMY_KILLED: 'enemy:killed',
// Game flow
GAME_STARTED: 'game:started',
GAME_PAUSED: 'game:paused',
GAME_OVER: 'game:over',
// System
ASSETS_LOADED: 'assets:loaded',
LOADING_PROGRESS: 'loading:progress'
};
Always pass structured data objects, never primitives:
// Good
eventBus.emit(Events.PLAYER_DAMAGED, { amount: 10, source: 'enemy', damageType: 'melee' });
// Bad
eventBus.emit(Events.PLAYER_DAMAGED, 10);
Organize state into clear domains:
class GameState {
constructor() {
this.player = { health, maxHealth, speed, inventory, buffs };
this.combat = { killCount, waveNumber, score };
this.game = { started, paused, isPlaying };
}
}
Standard flow for both 2D and 3D games:
Boot/Load -> Gameplay <-> Pause Menu (if requested)
-> Game Over -> Gameplay (restart)
No title screen by default. Games boot directly into gameplay. The Play.fun widget handles score display, leaderboards, and wallet connect in a deadzone at the top of the game, so no in-game score HUD is needed. Only add a title/menu scene if the user explicitly requests one.
physics.add.collider() or physics.add.overlap() has no gameplay effect. Every boundary or obstacle needs explicit collision wiring to the entities it should interact with. After creating any static body, immediately add the collider call.shutdown() causes ghost behavior, double-firing events, and memory leaks after restart.Before considering a game complete, verify all items:
mute-button rulenpm run build succeeds with no errorsWeekly Installs
105
Repository
GitHub Stars
26
First Seen
Feb 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code83
opencode57
gemini-cli55
github-copilot55
amp55
codex55
API设计指南:REST/GraphQL设计原则、规范文档与版本控制策略
11,500 周安装