-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from RSamaium/v4.1.0
V4.1.0
- Loading branch information
Showing
44 changed files
with
2,826 additions
and
1,782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
## Trigger the GUI | ||
|
||
You have either the client side solution or the server side solution. | ||
|
||
- Advantage on the client side: instantaneous, no communication with the server | ||
- Disadvantage: the server does not have the authority and cannot trigger it for all players at once | ||
|
||
On the server side, it's the opposite :) | ||
|
||
### Client Side | ||
|
||
1. you must open the menu, as usual (here, named `my-tooltip`) | ||
|
||
```ts | ||
RpgGui.display('my-tooltip') | ||
``` | ||
|
||
You can open it whenever you want, for example, after loading a map | ||
|
||
<div class="module-api"> | ||
|
||
```ts | ||
import { RpgClient, RpgModule, RpgGui } from '@rpgjs/client' | ||
import myTooltip from './gui/tooltip.vue' | ||
import sprite from './sprite' | ||
|
||
@RpgModule<RpgClient>({ | ||
scenes: { | ||
map: { | ||
onAfterLoading() { | ||
RpgGui.display('my-tooltip') | ||
} | ||
} | ||
}, | ||
sprite, | ||
gui: [ | ||
myTooltip | ||
] | ||
}) | ||
export default class RpgClientModuleEngine {} | ||
``` | ||
|
||
<PathTo to="baseModule" file="sprite.ts" /> | ||
|
||
```ts | ||
import { RpgSprite, RpgSpriteHooks } from '@rpgjs/client' | ||
|
||
export const sprite: RpgSpriteHooks = { | ||
onInit(sprite: RpgSprite) { | ||
sprite.interactive = true | ||
sprite.on('click', () => { | ||
sprite.guiDisplay = !sprite.guiDisplay | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
</div> | ||
|
||
<div class="autoload-api"> | ||
|
||
<PathTo to="baseModule" file="scene-map.ts" /> | ||
|
||
```ts | ||
import {RpgGui } from '@rpgjs/client' | ||
|
||
export default { | ||
onAfterLoading() { | ||
RpgGui.display('my-tooltip') | ||
} | ||
} | ||
``` | ||
|
||
2. Then you can trigger the opening on the sprite | ||
|
||
<PathTo to="baseModule" file="sprite.ts" /> | ||
|
||
```ts | ||
import { RpgSprite, RpgSpriteHooks } from '@rpgjs/client' | ||
|
||
export const sprite: RpgSpriteHooks = { | ||
onInit(sprite: RpgSprite) { | ||
sprite.interactive = true | ||
sprite.on('click', () => { | ||
sprite.guiDisplay = !sprite.guiDisplay | ||
}) | ||
} | ||
} | ||
``` | ||
|
||
Clicking on the sprite opens (or closes) the tooltip | ||
|
||
</div> | ||
|
||
|
||
## Server Side | ||
|
||
> Even if we are on the server side, remember to add the GUI in the client side module | ||
<PathTo to="playerFile" /> | ||
|
||
```ts | ||
import { RpgPlayer, RpgPlayerHooks } from '@rpgjs/server' | ||
|
||
export const player: RpgPlayerHooks = { | ||
onJoinMap(player: RpgPlayer) { | ||
player.gui('my-tooltip').open() | ||
player.showAttachedGui() | ||
// you can hide with player.hideAttachedGui() | ||
} | ||
} | ||
``` | ||
|
||
We open the `my-tooltip` GUI and display the player's tooltip | ||
|
||
:::tip Tip | ||
You can indicate which tooltips you want to display by specifying the events (or players) in parameter: | ||
|
||
```ts | ||
player.showAttachedGui([otherEvent, otherPlayer]) | ||
``` | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Create a GUI attached to a sprite | ||
|
||
## Prerequisites | ||
|
||
- Know how to create a GUI and add it in the module | ||
- Be comfortable with React | ||
- Since v4.1.0 | ||
|
||
::: warning | ||
**Experimental Feature**: This feature is still in its experimental stage and may not be stable. | ||
::: | ||
|
||
## Example | ||
|
||
This is very useful to make more advanced interactions on a sprite. For example, display a tooltip or additional interactive displays. | ||
|
||
<Video src="/assets/rpgjs_gui.mp4" /> | ||
|
||
## Create Component | ||
|
||
<PathTo to="guiDir" file="tooltip.tsx" /> | ||
|
||
```tsx | ||
export default function MyTooltip({ spriteData }) { | ||
return ( | ||
<span className="tooltip"> | ||
{spriteData.position.x}, {spriteData.position.y} | ||
</span> | ||
) | ||
} | ||
|
||
MyTooltip.rpgAttachToSprite = true | ||
``` | ||
|
||
<!--@include: _trigger-tooltip.md--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Building GUI with React | ||
|
||
## Prerequisites | ||
|
||
- You must be familiar with the [React](https://react.dev) library | ||
- Since v4.1.0 | ||
|
||
::: warning | ||
**Experimental Feature**: This feature is still in its experimental stage and may not be stable. | ||
::: | ||
|
||
## Step 1: Installation | ||
|
||
Begin by installing the necessary dependencies: | ||
|
||
```bash | ||
npm install react react-dom | ||
``` | ||
|
||
## Step 2: Set Up a Basic GUI | ||
|
||
In the file <PathTo to="guiDir" file="mygui.tsx" />, create a simple React component: | ||
|
||
```tsx | ||
export default function MyGUI() { | ||
return ( | ||
<div> | ||
<h1>Hello World</h1> | ||
</div> | ||
); | ||
} | ||
``` | ||
Name function is used for identifying the GUI. Id is `my-gui`. | ||
|
||
> Id is Kebab Case of the name function | ||
## Step 3: Incorporate Context | ||
|
||
For more dynamic data, you can utilize the context from the RPG client and some React hooks. | ||
|
||
```tsx | ||
import { RpgReactContext } from '@rpgjs/client/react'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
export default function MyGUI({ foo }) { | ||
const { rpgCurrentPlayer } = useContext(RpgReactContext); | ||
const [hp, setHp] = useState(0); | ||
|
||
console.log(foo); | ||
|
||
useEffect(() => { | ||
rpgCurrentPlayer.subscribe(({ object }) => { | ||
setHp(object.hp); | ||
}); | ||
|
||
return () => { | ||
rpgCurrentPlayer.unsubscribe(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>{hp}</h1> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
## Step 4: Interact with the Server Side | ||
|
||
On the server side, you have the capability to open the GUI for players. | ||
|
||
In the file <PathTo to="serverDir" file="player.ts" />: | ||
|
||
```ts | ||
import { RpgPlayer, RpgPlayerHooks } from '@rpgjs/server'; | ||
|
||
const player: RpgPlayerHooks = { | ||
onJoinMap(player: RpgPlayer) { | ||
player.gui('my-gui').open({ | ||
foo: 'bar' // You can send props to the GUI | ||
}); | ||
} | ||
}; | ||
|
||
export default player; | ||
``` |
Oops, something went wrong.