Coder Social home page Coder Social logo

drugmoon / multiplayer-turn-based-game-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leancloud/multiplayer-turn-based-game-demo

0.0 0.0 0.0 2.37 MB

回合制对战游戏示例

Home Page: https://turn-game.leanapp.cn

JavaScript 5.46% TypeScript 94.54%

multiplayer-turn-based-game-demo's Introduction

回合制在线对战 Demo

这款 Demo 是用 LeanCloud 实时对战Client Engine 实现的一款回合制对战游戏,全部服务端及客户端的代码总共花费了约 7 天的时间。试玩链接:https://turn-game.leanapp.cn

主要界面如下:

启动游戏

服务端

登录 LeanCloud 控制台,进入「游戏」-「Client Engine」-「部署」,选择「开始试用」。

安装 LeanCloud 命令行工具

cd turn-based-game-client-engine
lean login
lean switch

分组时选择 _client-engine 分组。

npm install
DEBUG=ClientEngine*,RPS*,Play lean up

客户端

客户端代码位于 ./turn-based-game-client/assets ,找到 Global.ts 文件,修改其中的 APP_ID 和 APP_KEY 和服务端选择的应用相同,roomRequestUrl 使用 localhost:3000

安装依赖:

npm install

依赖安装完成后,使用 Cocos Creator 运行 ./turn-based-game-client 中的项目。

实现方式

客户端之间用实时对战云来通讯。MasterClient 在 Client Engine 中控制房间内的逻辑。在游戏过程中,客户端将全部的事件发送给 MasterClient,由 MasterClient 在服务端执行相关逻辑后,再通过自定义事件指示客户端播放相关动画。

具体流程如下:

  • 客户端请求 Client Engine 获得房间名称、加入房间。
  • 房间人满后,位于 Client Engine 中的 MasterClient 为两个客户端分配人物属性。
  • MasterClient 向房间内广播「游戏开始」事件,客户端收到事件后加载 Room 场景。
  • 客户端做出选择,并将选项发送给 MasterClient。
  • MasterClient 收到两方客户端的选项后,计算英雄血值,并广播「开始本轮对战」事件。
  • 客户端播放对战动画,播放完成后根据服务端的计算结果校准 UI。
  • 重复每轮对战,直到某一名英雄血值为 0。

服务端代码

这里的服务端代码指的是 Client Engine 中的游戏逻辑代码,位于 ./turn-based-game-client-engine/src

├── configs.ts        // 配置文件
├── index.ts          // 项目入口
├── reception.ts      // Reception 类实现文件,GameManager 的子类,负责管理 Game,在这个文件中撰写了创建 Game 的自定义方法
└── turn-based-game.ts       // 实现了 TurnBasedGame 类,用于控制房间内的具体逻辑。

服务端的代码沿用了 LeanCloud Client Engine 示例项目中的代码,仅修改了示例项目的 rps-game.tsturn-based-game.ts,在 turn-based-game.ts 文件中撰写了自己的游戏逻辑。

关于如何使用 Client Engine 开发游戏逻辑,请参考 LeanCloud 官方文档:《你的第一个 Client Engine 小游戏》

客户端代码

客户端位于 ./turn-based-game-client/assets

├── Global.ts        // 一些全局的配置或变量
├── Events.ts        // 接收所有通过 LeanCloud 实时对战云发送的自定义事件。
├── Hero.ts          // 在这里撰写英雄的属性及自定义方法
├── Login.ts      // 登录场景文件
├── Lobby.ts      // 大厅场景文件
└── Room.ts       // 房间场景文件

主要功能实现介绍

快速开始

随便找一个有空位的房间快速开始。服务端代码位于 index.ts 文件,详情请参考 LeanCloud 文档快速开始

客户端代码位于 Lobby.ts,详细介绍请参考入口 API:快速开始

设置人物属性

当房间人满后,MasterClient 会设置位于房间内的人物属性,包括血值、攻击力、防御力、治疗能力、速度等,此段逻辑位于服务端 turn-based-game.ts 文件中。设置人物属性的详细介绍请参考 LeanCloud 文档玩家自定义属性

房间内对战

房间内对战主要依靠自定义事件进行通信,主要有这些自定义事件:

  • MasterClient 广播事件,位于服务端代码 turn-based-game.ts 文件中。
    • 游戏开始(game-start
    • 游戏结束(game-over
    • 开始播放动画(begin-round-anim
  • 客户端事件,位于客户端代码 Room.tsEvents.ts 文件中。
    • 发送选项给 MasterClient(action

multiplayer-turn-based-game-demo's People

Contributors

nicecui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.