Skip to main content

Movement System Guide

The RPGJS movement system provides a flexible and extensible way to handle entity movement in your game. This guide covers how to use the built-in movement strategies and how to create custom ones.

Overview

The movement system is built around the Strategy Pattern, separating movement logic from physics simulation. This allows you to:
  • Combine multiple movement strategies on a single entity
  • Create complex movement patterns by composing simple ones
  • Add custom movement behaviors without modifying existing code
  • Handle time-based movements that work regardless of frame rate

Architecture

Using the Movement System

The movement system can be used in two ways:

Interrupting client-predicted movement

When client prediction is enabled, movement inputs can already be buffered when another action starts. For blocking actions such as a Zelda-like sword attack, stop local movement and discard pending predicted inputs before the action animation starts:
This prevents held directional inputs from being replayed during server reconciliation. Once movement is allowed again, normal keyboard repeat can send new movement inputs.

Core Movement Methods

Required Imports

Managing Movement Strategies

Built-in Movement Strategies

1. Linear Movement

Constant velocity movement in a specified direction.

2. Dash Movement

Quick burst of speed in a direction for a limited time.

3. Knockback Effect

Push effect that gradually decreases over time.

4. Path Following

Follow a sequence of waypoints.

5. Oscillation

Back-and-forth movement patterns.

6. AI Target Seeking

Intelligent movement toward targets with local obstacle avoidance. moveTo does not compute a full path; it steers around nearby blocking hitboxes and events while moving toward the target.

7. Ice Movement

Slippery surface physics with gradual acceleration and inertia.

8. Projectile Movement

Ballistic trajectories for projectiles.

Combining Movements

Parallel Movements

Multiple movements can be active simultaneously:

Sequential Movements

Chain movements using CompositeMovement:

Complex Combinations

Creating Custom Movement Strategies

Basic Custom Movement

Implement the MovementStrategy interface:

Best Practices

Performance Considerations

  1. Limit Active Movements: Too many simultaneous movements can impact performance
  1. Clean Up Finished Movements: The system automatically removes finished movements, but you can manually clean up if needed

Movement Coordination

  1. Check Before Adding: Prevent conflicting movements
  1. State-Based Movement: Use different movements for different game states

Debugging Movements