Appearance
RpgServer
RpgServer EntryPoint
You need to create a class that inherits RpgServerEngine
ts
import modules from './to/path/modules'
import { entryPoint } from '@rpgjs/server'
const rpgGame = entryPoint(modules, {
io, // io is socket.io instance
basePath: __dirname
})
rpgGame.start()
- Put the
modules
in the entry point with entryPoint. You need to put the socket.io instance and the project path (to find the maps) - the function returns an instance of
RpgServerEngine
. As soon as your server is ready (listening on the port, etc.), start the RPG server.
Entry Point properties
Summary
io
- Property:
io
- Type:
SocketIO or other
- Optional:
false
- Usage:
Represents socket io but you can put something else (which is of the same scheme as socket io)
basePath
- Property:
basePath
- Type:
string
- Optional:
false
- Usage:
It allows you to know where the maps are located. Usually put __dirname
for the current directory.
ts
basePath: __dirname
globalConfig
- Property:
globalConfig
- Type:
object
- Optional:
true
- Usage:
The general configurations of the game.
@RpgModule< RpgServer > decorator
hooks
WARNING
Stability: 1 - Experimental This feature is subject to change, and is gated by a command line flag. It may change or be removed in future versions.
- Since: 4.0.0
- Property:
hooks
- Type:
{ player: string[], engine: string[] }
- Optional:
true
- Example:
ts
import { RpgServer, RpgModule } from '@rpgjs/server'
@RpgModule<RpgServer>({
hooks: {
player: ['onAuth']
}
})
class RpgServerEngine { }
Emit the hook:
ts
server.module.emit('server.player.onAuth', player)
When we issue a hook, it has to be in form:
<side>.<property>.<function>
And listen to the hook:
ts
import { RpgPlayerHooks, RpgPlayer } from '@rpgjs/server'
const player: RpgPlayerHooks = {
onAuth(player: RpgPlayer) {
console.log('player is authenticated')
}
}
- Usage:
Add hooks to the player or engine. All modules can listen to the hook
imports
- Property:
imports
- Type:
{ client: null | Function, server: null | Function }[]
- Optional:
true
- Usage:
Adding sub-modules
engine
- Property:
engine
- Type:
RpgServerEngineHooks
- Optional:
true
- Usage:
Object containing the hooks concerning the engine
ts
import { RpgServerEngine, RpgServerEngineHooks, RpgModule, RpgClient } from '@rpgjs/server'
const engine: RpgEngineHooks = {
onStart(server: RpgServerEngine) {
console.log('server is started')
}
}
@RpgModule<RpgServer>({
engine
})
class RpgServerModule {}
player
- Property:
player
- Type:
RpgClassPlayer< RpgPlayer>
- Optional:
true
- Usage:
Give the player
object hooks. Each time a player connects, an instance of RpgPlayer
is created.
ts
import { RpgPlayer, RpgServer, RpgPlayerHooks, RpgModule } from '@rpgjs/server'
const player: RpgPlayerHooks = {
onConnected(player: RpgPlayer) {
}
}
@RpgModule<RpgServer>({
player
})
class RpgServerEngine { }
database
- Property:
database
- Type:
{ [dataName]: data }
- Optional:
true
- Usage:
References all data in the server. it is mainly used to retrieve data according to their identifier
ts
import { RpgServer, RpgModule } from '@rpgjs/server'
import { Potion } from 'my-database/items/potion'
@RpgModule<RpgServer>({
database: {
Potion
}
})
class RpgServerEngine { }
maps
- Property:
maps
- Type:
RpgClassMap< RpgMap>[]
- Optional:
true
- Usage:
Array of all maps. Each element is an RpgMap
class
ts
import { RpgMap, MapData, RpgServer, RpgModule } from '@rpgjs/server'
@MapData({
id: 'town',
file: require('./tmx/mymap.tmx'),
name: 'Town'
})
class TownMap extends RpgMap { }
@RpgModule<RpgServer>({
maps: [
TownMap
]
})
class RpgServerEngine { }
It is possible to just give the object as well
ts
@RpgModule<RpgServer>({
maps: [
{
id: 'town',
file: require('./tmx/mymap.tmx'),
name: 'Town'
}
]
})
class RpgServerEngine { }
Since version 3.0.0-beta.8, you can just pass the path to the file. The identifier will then be the name of the file
ts
@RpgModule<RpgServer>({
maps: [
require('./tmx/mymap.tmx') // id is "mymap"
]
})
class RpgServerEngine { }
events
- Since: 4.0.0
- Property:
events
- Type:
RpgClassEvent< RpgEvent>[]
- Optional:
true
- Usage:
Array of all events. Each element is an RpgEvent
class Events can be used by placing a shape with the name of the event on Tiled Map Editor
worldMaps
- Since: 3.0.0-beta.8
- Property:
worldMaps
- Type:
object[]
- Optional:
true
- Example:
ts
import myworld from 'myworld.world'
@RpgModule<RpgServer>({
worldMaps: [
myworld
]
})
class RpgServerEngine { }
- Usage:
Loads the content of a .world
file from Tiled Map Editor into the map scene
Note, that if the map already exists (i.e. you have already defined an RpgMap), the world will retrieve the already existing map. Otherwise it will create a new map
damageFormulas
- Property:
damageFormulas
- Type:
object
- Optional:
false
- Usage:
Combat formula used in the method player.applyDamage(). There are already formulas in the RPGJS engine but you can customize them
ts
damageFormulas: {
damageSkill: (a, b, skill) => number,
damagePhysic: (a, b) => number,
// damage: the damages calculated from the previous formulas
damageCritical: (damage, a, b) => number
coefficientElementsa : (a, b, bDef) => number
}
a
represents the attacker's parameters b
represents the defender's parameters
Example:
ts
import { RpgModule, RpgServer, Presets } from '@rpgjs/server'
const { ATK, PDEF } = Presets
@RpgModule<RpgServer>({
damageFormulas: {
damagePhysic(a, b) {
let damage = a[ATK] - b[PDEF]
if (damage < 0) damage = 0
return damage
}
}
})
class RpgServerEngine { }