use bevy::prelude::*; use crate::{ animation::transform::AnimatedTransform, card::{CardInHand, Hand, InsertCard}, }; #[derive(Component)] pub struct DraggableCard; #[derive(Component)] pub struct DraggedCard; #[derive(Resource)] pub struct MousePosition(pub Vec3); pub fn mouse_position_system( mut mouse: ResMut, window: Single<&Window>, camera: Single<(&Camera, &GlobalTransform)>, ) { if let Some(Ok(mouse_pos)) = window .cursor_position() .and_then(|viewport_pos| Some(camera.0.viewport_to_world_2d(camera.1, viewport_pos))) { mouse.0 = Vec3 { x: mouse_pos.x, y: mouse_pos.y, z: 0., } } } pub fn card_drag_start( event: On>, mut commands: Commands, card_query: Query<(), With>, dragged_option: Option>, mut hand: Single<&mut Hand>, ) { if dragged_option.is_some() { return; } if let Err(_) = card_query.get(event.entity) { return; } commands .entity(event.entity) .insert(DraggedCard) .remove::(); if let Some(pos) = hand.0.iter().position(|ent| *ent == event.entity) { hand.0.remove(pos); } } pub fn card_drag( _: On>, mut card_transform: Single<&mut AnimatedTransform, With>, mouse_pos: Res, ) { card_transform.translation = mouse_pos.0; } pub fn card_drag_end( _: On>, mut commands: Commands, dragged: Single>, ) { commands.trigger(InsertCard(dragged.entity())); commands .entity(dragged.entity()) .remove::() .insert(CardInHand); }