Skip to content
On this page

Movement Commands


Change Speed

  • Enum: number
TagDescription
Speed.Slowest0.2
Speed.Slower0.5
Speed.Slow1
Speed.Normal3
Speed.Fast5
Speed.Faster7
Speed.Fastest10
  • Property: player.speed
  • Type: number
  • Optional: false
  • Default: 3
  • Usage:

Changes the player's speed

ts
player.speed = 1

You can use Speed enum

ts
import { Speed } from '@rpgjs/server'
player.speed = Speed.Slow

Block movement

  • Property: player.canMove
  • Type: boolean
  • Optional: false
  • Default: true
  • Usage:

Blocks the movement. The player will not be able to move even if he presses the direction keys on the keyboard.

ts
player.canMove = false

Go through to other player

  • Property: player.throughOtherPlayer
  • Type: boolean
  • Optional: false
  • Default: true
  • Usage:

The player passes through the other players (or vice versa). But the player does not go through the events.

ts
player.throughOtherPlayer = true

Go through the player

  • Property: player.through
  • Type: boolean
  • Optional: false
  • Default: false
  • Usage:

The player goes through the event or the other players (or vice versa)

ts
player.through = true

Change Frequency

  • Enum: number
TagDescription
Frequency.Lowest600
Frequency.Lower400
Frequency.Low200
Frequency.High100
Frequency.Higher50
Frequency.Highest25
Frequency.None0
  • Property: player.speed
  • Type: number
  • Optional: false
  • Default: 0
  • Usage:

The frequency allows to put a stop time between each movement in the array of the moveRoutes() method. The value represents a dwell time in milliseconds. The higher the value, the slower the frequency.

ts
player.frequency = 400

You can use Frequency enum

ts
import { Frequency } from '@rpgjs/server'
player.frequency = Frequency.Low

Give an itinerary

  • Method: player.moveRoutes(routes)
  • Arguments:
    • {Array< Move>} routes. (Optional: false)
  • Return: Promise
  • Example:
ts
import { Move } from '@rpgjs/server'

await player.moveRoutes([ Move.tileLeft(), Move.tileDown(2) ])
// The path is over when the promise is resolved
  • Usage:

Gives an itinerary.

You can create your own motion function:

ts
import { Direction } from '@rpgjs/server'

const customMove = () => {
     return [Direction.Left, Direction.Up]
}

player.moveRoutes([ customMove() ])

Your function can also return a function:

ts
import { Direction, RpgPlayer } from '@rpgjs/server'

// This function can be found in another file. By returning a function, you have access to the player who is making a move.
const customMove = (otherPlayer: RpgPlayer) => {
    return (player: RpgPlayer, map) => {
        return otherPlayer.position.x > player.position.x ? Direction.Left : Direction.Right
    }
}

player.moveRoutes([ customMove(otherPlayer) ])

the function contains two parameters:

  • player: the player concerned by the movement
  • map: The information of the current map

Infinite Move Routes

  • Method: player.infiniteMoveRoute(routes)
  • Arguments:
    • {Array< Move>} routes. (Optional: false)
  • Return: void
  • Example:
ts
import { Move } from '@rpgjs/server'

player.infiniteMoveRoute([ Move.tileRandom() ])
  • Usage:

Giving a path that repeats itself in a loop to a character

You can stop the movement at any time with breakRoutes() and replay it with replayRoutes().


Stop an infinite movement

  • Method: player.breakRoutes(force=false)
  • Arguments:
    • {boolean} force. Forces the stop of the infinite movement (Optional: true)
  • Return: void
  • Example:
ts
import { Move } from '@rpgjs/server'

player.infiniteMoveRoute([ Move.tileRandom() ])
player.breakRoutes(true)
  • Usage:

Works only for infinite movements. You must first use the method infiniteMoveRoute()


Replay an infinite movement

  • Method: player.replayRoutes()
  • Return: void
  • Example:
ts
import { Move } from '@rpgjs/server'

player.infiniteMoveRoute([ Move.tileRandom() ])
player.breakRoutes(true)
player.replayRoutes()
  • Usage:

Works only for infinite movements. You must first use the method infiniteMoveRoute() If the road was stopped with breakRoutes(), you can restart it with this method


Move To

  • Since: 3.2.0
  • Method: player.moveTo()
  • Arguments:
    • { RpgPlayer RpgEvent RpgShape|Position} target. the target (Optional: false)
    • {object} options. - animate. Set a boolean to use default parameters (Optional: true)
    • {boolean} options.infinite. - moves infinitely towards the target, you have to stop its movement manually with the method stopMoveTo() (Optional: true)
    • {() => void} options.onComplete. - Callback when the event arrives at the destination (Optional: true)
    • {(duration:number) => void} options.onStuck. - callback when the event is blocked against a wall. Duration gives you the duration (in frames) of the blocking time (Optional: true)
  • Return: Observable
  • Example:
ts
import { Move } from '@rpgjs/server'

player.moveTo(otherPlayer).subscribe()
  • Usage:

Move the event to another event, a player, a shape or a specific position. The event will avoid obstacles, but you can tell if it is stuck or has completed its path


Stop Move To

  • Since: 3.2.0
  • Method: player.stopMoveTo()
  • Return: void
  • Usage:

Stops the movement of the player who moves towards his target

Movements List

All the movements below can be used in the moveRoutes() array.

Summary


Move

  • Enum: Object
TagDescription
Move.right(repeat=1)Movement of a number of pixels on the right
Move.left(repeat=1)Movement of a number of pixels on the left
Move.up(repeat=1)Movement of a number of pixels on the up
Move.down(repeat=1)Movement of a number of pixels on the down
Move.random(repeat=1)Movement of a number of pixels in a random direction
Move.towardPlayer(player, repeat=1)Moves a number of pixels in the direction of the designated player
Move.awayFromPlayer(player, repeat=1)Moves a number of pixels in the opposite direction of the designated player
Move.tileRight(repeat=1)Movement of a number of tiles on the right
Move.tileLeft(repeat=1)Movement of a number of tiles on the left
Move.tileUp(repeat=1)Movement of a number of tiles on the up
Move.tileDown(repeat=1)Movement of a number of tiles on the down
Move.tileRandom(repeat=1)Movement of a number of tiles in a random direction
Move.tileTowardPlayer(player, repeat=1)Moves a number of tiles in the direction of the designated player
Move.tileAwayFromPlayer(player, repeat=1)Moves a number of tiles in the opposite direction of the designated player
Move.turnRight()Turn to the right
Move.turnLeft()Turn to the left
Move.turnUp()Turn to the up
Move.turnDown()Turn to the down
Move.turnRandom()Turn to random direction
Move.turnAwayFromPlayer(player)Turns in the opposite direction of the designated player
Move.turnTowardPlayer(player)Turns in the direction of the designated player
  • Usage:

The movement in number of pixels depends on the speed of the player.