Skip to main content

Move Routes

The moveRoutes method allows you to give a character (player or event) a sequence of movement instructions to follow. This is useful for creating patrol paths, scripted movements, or complex movement patterns.

Basic Usage

The moveRoutes method accepts an array of movement instructions and returns a Promise that resolves when all routes are completed.

Movement Instructions

Direction Enums

You can use the Direction enum directly:

Move Helper Functions

The Move object provides convenient helper functions for common movements:

Tile-Based Movements

Tile movements calculate the distance based on tile size and player speed:

Turn Commands

Turn commands change the character’s direction without moving:

Wait/Delay

You can add delays between movements:

Callback Functions

You can use callback functions to generate dynamic routes:

Nested Arrays

Routes can be nested and will be automatically flattened:

Advanced Features

Stuck Detection

When a character gets blocked by an obstacle or collision, you can handle it using the onStuck callback:

Stuck Detection Options

  • onStuck: Callback function called when the player is blocked
    • Parameters:
      • player: The player instance that is stuck
      • target: The target position the player was trying to reach
      • currentPosition: The current position of the player
    • Return value:
      • false or undefined: Cancel the route
      • true: Continue trying to reach the target
  • stuckTimeout: Time in milliseconds to wait before considering the player stuck (default: 500ms)
    • The player must be unable to make progress for this duration before onStuck is called
  • stuckThreshold: Minimum distance change in pixels to consider movement progress (default: 1 pixel)
    • If the player moves less than this distance over the stuckTimeout period, they are considered stuck

Example: Handling Obstacles

Example: Continue After Obstacle Removal

Combining Movement Types

You can combine different movement types in a single route:

Player Movement

For player-specific movements, you can use callbacks to check player state:

Event Movement

Events can also use moveRoutes for patrol patterns:

Infinite Routes

For characters that need to repeat a movement pattern indefinitely:

Route Completion

The moveRoutes method returns a Promise that resolves when all routes are completed: