# RpgServer

# RpgServer EntryPoint

You need to create a class that inherits RpgServerEngine

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()
  1. 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)
  2. 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


# 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.

basePath: __dirname

# globalConfig

  • Property: globalConfig
  • Type: object
  • Optional: true
  • Usage:

The general configurations of the game.

# @RpgModule< RpgServer > decorator


# imports

  • Property: imports
  • Type: { client: null | Function, server: null | Function }[]
  • Optional: true
  • Usage:

Adding sub-modules


# engine

Object containing the hooks concerning the engine

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.

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

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

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

@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

@RpgModule<RpgServer>({
    maps: [
        require('./tmx/mymap.tmx') // id is "mymap"
    ]
})
class RpgServerEngine { } 

# worldMaps

  • Since: 3.0.0-beta.8
  • Property: worldMaps
  • Type: object[]
  • Optional: true
  • Arguments:
  • Example:
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

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:

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 { }