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: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.canMove is not changed by recoil,
so a dialog or attack lock remains owned by the system that created it.
The impulse decays without accumulating velocity each tick. Completion and
cancellation release the recoil phase; overlapping hits keep it active until the
last recoil ends. Collision detection still runs through the physics engine.
This behavior is shared by standalone RPG and MMORPG runtimes. The authoritative
runtime synchronizes the transient player.knockbackActive() phase. Game code may
read it, but should start recoil with player.knockback(...), not write the phase.
During this phase the client accepts server positions without replaying predicted
movement or old trajectory frames. Visual position changes ease over 80 ms, with
a short settling window after landing; jumps exceeding 128 pixels on an axis are
applied immediately. Smoothing affects only rendering, never the collision body.
The client does not predict damage or a recoil before the server confirms it.
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 usingCompositeMovement:
Complex Combinations
Creating Custom Movement Strategies
Basic Custom Movement
Implement theMovementStrategy interface:
Best Practices
Performance Considerations
- Limit Active Movements: Too many simultaneous movements can impact performance
- Clean Up Finished Movements: The system automatically removes finished movements, but you can manually clean up if needed
Movement Coordination
- Check Before Adding: Prevent conflicting movements
- State-Based Movement: Use different movements for different game states