Drag and Drop System
The drag and drop system allows players to move game objects like cards between different zones using intuitive mouse-based interactions.
Components
Draggable
The Draggable
component marks entities that can be dragged and contains:
#![allow(unused)] fn main() { pub struct Draggable { /// Whether the entity is currently being dragged pub dragging: bool, /// Offset from the mouse cursor to the entity's origin pub drag_offset: Vec2, /// Z-index for rendering order pub z_index: f32, } }
Core System
The drag system is implemented in drag_system
which handles:
- Detecting when a draggable entity is clicked
- Starting the drag operation with the appropriate offset
- Moving the entity with the mouse cursor
- Handling release events to end the drag operation
Z-index Management
The system automatically manages z-indices to ensure that dragged objects appear on top of other game elements. When multiple draggable entities overlap, the one with the highest z-index is selected for dragging.
Integration Points
Card Integration
The drag and drop system integrates with the card visualization system to allow players to:
- Move cards between hand, battlefield, and other zones
- Organize cards within a zone
- Reveal cards or interact with them in specific ways
Targeting Integration
Drag and drop operations are used in conjunction with the targeting system to select targets for spells and abilities. This creates a fluid interaction where players can drag a card to play it and then naturally continue to select targets.
Plugin Setup
To enable drag and drop functionality, the DragPlugin
must be added to your Bevy app:
#![allow(unused)] fn main() { app.add_plugins(DragPlugin); }
Implementation Example
Below is a simplified example of how to make an entity draggable:
#![allow(unused)] fn main() { // Spawn a card entity with a draggable component commands.spawn(( card_bundle, Draggable { dragging: false, drag_offset: Vec2::ZERO, z_index: 1.0, }, )); }
Customization
The drag and drop system can be customized by:
- Adding different visual feedback during dragging
- Implementing custom drop target detection
- Creating drag constraints for certain game zones
For more information on implementing custom drag behaviors, see the game state integration documentation.