Skip to content

Commit

Permalink
react app doc
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Jan 25, 2024
1 parent dcc8805 commit 56e78fa
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 9 deletions.
16 changes: 8 additions & 8 deletions docs/.vitepress/cache/deps/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
{
"hash": "52bfd73e",
"browserHash": "784c2801",
"hash": "1a35c785",
"browserHash": "06015c59",
"optimized": {
"vue": {
"src": "../../../node_modules/vue/dist/vue.runtime.esm-bundler.js",
"file": "vue.js",
"fileHash": "929fd058",
"fileHash": "89d026cc",
"needsInterop": false
},
"vitepress > @vue/devtools-api": {
"src": "../../../node_modules/@vue/devtools-api/lib/esm/index.js",
"file": "vitepress___@vue_devtools-api.js",
"fileHash": "e4854535",
"fileHash": "2645e713",
"needsInterop": false
},
"vitepress > @vueuse/integrations/useFocusTrap": {
"src": "../../../node_modules/@vueuse/integrations/useFocusTrap.mjs",
"file": "vitepress___@vueuse_integrations_useFocusTrap.js",
"fileHash": "42b2aa2c",
"fileHash": "4e2d43e4",
"needsInterop": false
},
"vitepress > mark.js/src/vanilla.js": {
"src": "../../../node_modules/mark.js/src/vanilla.js",
"file": "vitepress___mark__js_src_vanilla__js.js",
"fileHash": "0d6e6fc4",
"fileHash": "3ed8ef59",
"needsInterop": false
},
"vitepress > minisearch": {
"src": "../../../node_modules/minisearch/dist/es/index.js",
"file": "vitepress___minisearch.js",
"fileHash": "b20f55c8",
"fileHash": "b062852e",
"needsInterop": false
},
"@vue/theme": {
"src": "../../../node_modules/@vue/theme/src/index.ts",
"file": "@vue_theme.js",
"fileHash": "7bb8dba9",
"fileHash": "7db5d48e",
"needsInterop": false
}
},
Expand Down
3 changes: 2 additions & 1 deletion docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ const guideMenu = [{
collapsed: false,
items: [
{ text: "Create Gui with React", link: "/gui/react" },
{ text: "Adding Tooltips to Your GUI", link: "/gui/react-tooltip" }
{ text: "Adding Tooltips to Your GUI", link: "/gui/react-tooltip" },
{ text: "Integrate in React App", link: "/gui/react-app"}
]
},
{
Expand Down
90 changes: 90 additions & 0 deletions docs/gui/react-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Integrating RpgGame Component into a React App

This guide provides step-by-step instructions on how to insert an `RpgGame` component into a React application using ViteJS.

## Prerequisites

Ensure that you have a React application set up with ViteJS. Your `package.json` should include the following scripts:

```json
"scripts": {
"dev": "rpgjs dev",
"build": "rpgjs build"
}
```

## Step 1: Create the Game Directory

Create a new directory within your source folder:

```
src/game
```

## Step 2: Configure RPG Module

In your `rpg.toml` file, set the module root and disable autostart:

```toml
modulesRoot = './src/game'
autostart = false
```

> The `autostart` option prevents the engine from automatically searching for a div with the id `#rpg`. Instead, it will use the React component.
## Step 3: Import RpgGame Component

Import the `RpgGame` component from the RPGJS client package for React:

```javascript
import { RpgGame } from '@rpgjs/client/react';
```

## Step 4: Utilize RpgGame Component

Implement the `RpgGame` component with an `onReady` function:

```jsx
const onReady = ({ server, client }) => {
// server is an instance of RpgServerEngine or null
// client is an instance of RpgClientEngine
};

<RpgGame onReady={onReady} />
```

In MMORPG mode, `server` will be null. In RPG mode, you can access both client and server sides.

## Step 5: Advanced Configuration

To add modules via the React app, use the following setup:

```javascript
const [modules, setModules] = useState<any[]>([
{
server: {
player: {
onConnected(player) {
player.setHitbox(24, 24);
player.speed = 4;
player.setGraphic('hero');
}
}
},
client: {
engine: {
onStart() {
console.log('started')
}
}
}
}
]);

<RpgGame
onReady={onReady}
modules={modules}
/>
```

> In MMORPG mode, you only have access to the client API.
11 changes: 11 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ Configuration properties for CORS middleware. Documentation: [cors](https://expr

Configuration properties for SocketIO middleware. Documentation: [socket.io](https://socket.io/docs/v4/server-initialization/)

- `socketIoClient`

All [SocketIO client configuration](https://socket.io/docs/v4/client-initialization/).

Example:

```toml
[socketIoClient]
withCredentials = true
```

- `spritesheetDirectories`: (*array*) Directories for spritesheets.

### `server`
Expand Down

0 comments on commit 56e78fa

Please sign in to comment.