Appearance
RpgPlayer
An RpgPlayer
instance is created each time a player is connected. In an event, you often find a player
parameter of type RpgPlayer
. You have a series of methods to apply to the player
ts
import { RpgPlayer, RpgPlayerHooks } from '@rpgjs/server'
export const player: RpgPlayerHooks = {
onConnected(player: RpgPlayer) {
// Making instructions when the player is connected
}
}
Then put the class in the RpgServer decorator.
Summary
props
- Since: 3.0.0-beta.9
- Property:
props
- Type:
object
- Optional:
true
- Usage:
Set custom properties on the player. Several interests:
- The property is shared with the client
- If you save with
player.save()
, the property will be saved to be reloaded later - If you use horizontal scaling, the property will be kept in memory if the player changes the map and this map is on another server
Example:
ts
import { RpgPlayerHooks } from '@rpgjs/server'
declare module '@rpgjs/server' {
export interface RpgPlayer {
nbWood: number
}
}
export const player: RpgPlayerHooks = {
props: {
nbWood: Number
}
}
This is a simple example. Let's say that the player can have a number of harvested woods, then
- you must specify the type for Typescript
- Add the property in props
You can also set up with this object:
{
$default: <any> (undefined by default),
$syncWithClient: <boolean> (true by default),
$permanent: <boolean> (true by default)
}
```
- Indicate if the property should be shared with the client
Example:
```ts
export const player: RpgPlayerHooks = {
props: {
secretProp: {
$syncWithClient: false
}
}
}
- Indicate if the property should be registered in a database. If the data is just temporary to use on the current map:
ts
export const player: RpgPlayerHooks = {
props: {
tmpProp: {
$permanent: false
}
}
}
onJoinMap
When the player joins the map
onConnected
- Property:
onConnected
- Type:
(player: RpgPlayer) => any
- Optional:
true
- Usage:
When the player is connected to the server
onInput
- Property:
onInput
- Type:
(player: RpgPlayer, data: { input: Direction | Control | string, moving: boolean }) => any
- Optional:
true
- Usage:
When the player presses a key on the client side
onLeaveMap
When the player leaves the map
onLevelUp
- Property:
onLevelUp
- Type:
(player: RpgPlayer, nbLevel: number) => any
- Optional:
true
- Usage:
When the player increases one level
onDead
- Property:
onDead
- Type:
(player: RpgPlayer) => any
- Optional:
true
- Usage:
When the player's HP drops to 0
onDisconnected
- Property:
onDisconnected
- Type:
(player: RpgPlayer) => any
- Optional:
true
- Usage:
When the player leaves the server
onInShape
When the player enters the shape
onOutShape
When the player leaves the shape
onMove
- Since: 3.0.0-beta.4
- Property:
onMove
- Type:
(player: RpgPlayer) => any
- Optional:
true
- Usage:
When the x, y positions change
canChangeMap
- Since: 3.0.0-beta.8
- Property:
canChangeMap
- Type:
(player: RpgPlayer, nextMap: RpgClassMap< RpgMap>) => boolean | Promise
- Optional:
true
- Usage:
Allow or not the player to switch maps. nexMap
parameter is the retrieved RpgMap class and not the instance