{"id":19614923,"url":"https://github.com/leancloud/ballbattle-cocos","last_synced_at":"2025-07-16T05:40:49.472Z","repository":{"id":66228539,"uuid":"174942558","full_name":"leancloud/BallBattle-Cocos","owner":"leancloud","description":"模拟《球球大作战》demo","archived":false,"fork":false,"pushed_at":"2019-04-04T07:27:27.000Z","size":1395,"stargazers_count":25,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-28T02:36:25.788Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leancloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-03-11T06:53:28.000Z","updated_at":"2024-11-27T01:23:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"31c506fe-531c-423f-98f2-37c8f5977f33","html_url":"https://github.com/leancloud/BallBattle-Cocos","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/leancloud/BallBattle-Cocos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FBallBattle-Cocos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FBallBattle-Cocos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FBallBattle-Cocos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FBallBattle-Cocos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leancloud","download_url":"https://codeload.github.com/leancloud/BallBattle-Cocos/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FBallBattle-Cocos/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265485315,"owners_count":23774468,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T10:54:31.222Z","updated_at":"2025-07-16T05:40:49.463Z","avatar_url":"https://github.com/leancloud.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BallBattle\n\n## 简介\n\n《球球大作战》是一款基于大球吃小球的房间类多人实时对战游戏。\n\n这个工程是使用 [LeanCloud 实时对战服务](https://leancloud.cn/docs/multiplayer.html) + Cocos Creator 开发的模拟《球球大作战》demo。\n\n[线上体验](http://ballbattle.leanapp.cn)\n\n## 玩法\n\n输入房间 ID，加入房间（如果没有此房间，则创建）。\n用户 ID 随机生成。\n使用 ⬆️⬇️⬅️➡️ 或 WSAD 来控制小球移动，吃掉场景中的食物（三角形，方形，六边形）则会增长体重（并减少速度）；遇到其他球（玩家），碰撞之后，体重较大者获胜，较小者将会死亡并重生。\n右侧面板显示当前房间的玩家体重排行榜。\n\n## 主要功能\n\n### 匹配对战\n\n最基础的房间 ID 匹配。\n\n[更多关于房间匹配文档](https://leancloud.cn/docs/multiplayer-guide-js.html#hash786861961)\n\n### 属性同步与保存\n\n这个 demo 使用的是 Master Client 机制，但由于 Master Client 可能存在掉线等异常情况，所以需要将房间和玩家的部分数据保存至 Room Properties 和 Player Properties。\n\n[更多关于属性同步文档](https://leancloud.cn/docs/multiplayer-guide-js.html#hash-299183039)\n\n#### 房间属性\n\n- 房间用时\n- 战场的食物列表\n- 食物最大 ID\n\n[更多关于房间属性文档](https://leancloud.cn/docs/multiplayer-guide-js.html#hash1532570669)\n\n#### 玩家属性\n\n- 位置\n- 体重\n- 速度\n\n[更多关于玩家属性文档](https://leancloud.cn/docs/multiplayer-guide-js.html#hash700221845)\n\n### 自定义事件\n\n- 玩家出生：对于当前玩家，执行战场初始化逻辑；对于其他玩家，执行增加玩家逻辑。\n- 吃食物：客户端移除内存中的食物节点，同步玩家体重。\n- 杀死玩家：用于同步节点间碰撞事件。\n- 玩家重生：用于重新初始化玩家数据。\n- 生成食物：同步房间内的食物数据。\n- 玩家离开：用于移除场景和 UI 对应节点。\n- 游戏结束：用于返回主菜单场景。\n\n[更多关于自定义事件文档](https://leancloud.cn/docs/multiplayer-guide-js.html#hash1368192228)\n\n### 其他功能\n\n#### 消息处理控制\n\n由于从主场景加载到战斗场景，存在异步的资源加载过程，所以需要暂停 / 恢复消息队列的处理。流程如下：\n\n- 加入房间\n- 暂停消息处理\n- 加载战斗场景\n- 初始化战场\n- 恢复消息队列。\n\n#### 移动同步\n\n移动同步实现思路是玩家在运动状态改变时，将当前运动状态同步给其他客户端，其他客户端对玩家行为进行模拟。而在运动过程中，并不同步移动数据。\n\n运动状态包括：\n\n- 位置\n- 移动方向\n- 时间戳\n\n模拟步骤：\n\n- 在收到运动状态改变时，根据运动改变时的位置，方向，以及当前时间戳与运动改变时的时间戳的差值，计算出当前应该所在的位置 p0\n- 玩家节点当前实际所在位置 p1，p0 - p1（向量减法），即为校正后的运动路径\n- 对路径进行模拟，直至下次运动状态改变\n\n## 项目结构\n\n```\n├── Animation 动画目录\n├── Prefabs 预制目录，主要存放球，食物预制体\n├── Scene 场景目录，主菜单场景，战斗场景\n├── Script 脚本目录\n│   ├── Battle 战斗相关脚本目录\n│   │    ├── Ball.js 球节点控制脚本\n│   │    ├── BallController.js 玩家控制球脚本，生成移动数据同步给其他客户端\n│   │    ├── BallSimulator.js 玩家运动模拟脚本，根据玩家运动数据，模拟运动行为\n│   │    ├── Battle.js 战场节点总控制器，用于接收并解析战斗中的自定义事件，驱动场景节点及 UI 节点变化\n│   │    ├── BattleHelper.js 战场工具脚本\n│   │    ├── Food.js 食物节点控制脚本\n│   │    ├── Master.js 游戏逻辑脚本，用于区分 Master 客户端与普通客户端，Master 组件用于生成房间数据及逻辑判断，只有 Master 的客户端才拥有这个组件，包括最初的房间的创建者和切换后的新房主。\n│   │    ├── PlayerInfoItem.js 玩家信息 UI 节点控制脚本\n│   │    └── UI.js UI 控制脚本\n│   ├── Menu主菜单相关脚本目录\n│   │    └── Menu.js 主菜单脚本\n│   ├── Constants.js 游戏中用到的常量\n│   └── LeanCloud.js 全局存放 LeanCloud SDK 对象的脚本\n├── Texture 素材资源目录\n└── play.js LeanCloud 实时对战服务 SDK\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fballbattle-cocos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleancloud%2Fballbattle-cocos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fballbattle-cocos/lists"}