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: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 Pathfinding
Intelligent movement toward targets with obstacle avoidance.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