-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
RSamaium
committed
Dec 9, 2023
1 parent
472989a
commit edbab50
Showing
13 changed files
with
228 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# React Hooks | ||
|
||
## Introduction | ||
|
||
React hooks are a powerful feature in React that allow you to use state and other React features without writing a class. In the context of RPGJS, a framework for creating RPG games, React hooks can be particularly useful for managing game state and interactions. | ||
|
||
## 1. `useEventPropagator()` | ||
|
||
This hook is used to propagate events within the canvas element of your RPGJS game. | ||
|
||
### Importing | ||
|
||
```javascript | ||
import { useEventPropagator } from '@rpgjs/client/react'; | ||
``` | ||
|
||
### Usage | ||
|
||
```javascript | ||
export default function Test() { | ||
const propagate = useEventPropagator(); | ||
|
||
return <div ref={propagate}>test</div>; | ||
} | ||
``` | ||
|
||
In this example, the `useEventPropagator` hook is used to create a `propagate` function. This function is then passed to a `div` element as a reference (`ref`). This setup allows events within the `div` to be propagated through the RPGJS game canvas. | ||
|
||
--- | ||
|
||
## 2. `RpgReactContext` | ||
|
||
This hook provides access to the RPGJS context, allowing you to interact with various game states like the current player's health points (HP). | ||
|
||
### Importing | ||
|
||
```javascript | ||
import { RpgReactContext } from '@rpgjs/client/react'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
``` | ||
|
||
### Usage | ||
|
||
```javascript | ||
export default function MyGUI({ foo }) { | ||
const { rpgCurrentPlayer } = useContext(RpgReactContext); | ||
const [hp, setHp] = useState(0); | ||
|
||
useEffect(() => { | ||
const subscription = rpgCurrentPlayer.subscribe(({ object }) => { | ||
setHp(object.hp); | ||
}); | ||
|
||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>{hp}</h1> | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
In this example, `RpgReactContext` is used to access the current player's state. The `useContext` hook retrieves the `rpgCurrentPlayer` from `RpgReactContext`. We then use `useState` to manage the player's HP locally. The `useEffect` hook is used to subscribe to changes in the player's HP, updating the local state accordingly. When the component unmounts, the subscription is unsubscribed. |
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,17 @@ | ||
# Directive for VueJS | ||
|
||
## `v-propagate` | ||
|
||
The `v-propagate` directive is straightforward to use. Simply add it to any element in your VueJS template to enable event propagation for that element within the RPGJS canvas. | ||
|
||
### Example | ||
|
||
```vue | ||
<template> | ||
<div v-propagate> | ||
Test | ||
</div> | ||
</template> | ||
``` | ||
|
||
In this example, the `v-propagate` directive is attached to a `div` element. Any events that occur within this `div` will be propagated through the RPGJS game canvas. This is particularly useful for integrating VueJS-based GUI elements with the RPGJS game canvas, allowing for seamless interaction between the GUI and the game. |
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
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
Oops, something went wrong.