76 lines
1.7 KiB
Rust
76 lines
1.7 KiB
Rust
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<MousePosition>,
|
|
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<Pointer<DragStart>>,
|
|
mut commands: Commands,
|
|
card_query: Query<(), With<DraggableCard>>,
|
|
dragged_option: Option<Single<&DraggedCard>>,
|
|
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::<CardInHand>();
|
|
|
|
if let Some(pos) = hand.0.iter().position(|ent| *ent == event.entity) {
|
|
hand.0.remove(pos);
|
|
}
|
|
}
|
|
|
|
pub fn card_drag(
|
|
_: On<Pointer<Drag>>,
|
|
mut card_transform: Single<&mut AnimatedTransform, With<DraggedCard>>,
|
|
mouse_pos: Res<MousePosition>,
|
|
) {
|
|
card_transform.translation = mouse_pos.0;
|
|
}
|
|
|
|
pub fn card_drag_end(
|
|
_: On<Pointer<DragEnd>>,
|
|
mut commands: Commands,
|
|
dragged: Single<Entity, With<DraggedCard>>,
|
|
) {
|
|
commands.trigger(InsertCard(dragged.entity()));
|
|
commands
|
|
.entity(dragged.entity())
|
|
.remove::<DraggedCard>()
|
|
.insert(CardInHand);
|
|
}
|