Small hand rework, simplest cards shuffle

This commit is contained in:
Rendo 2026-01-26 00:48:56 +05:00
commit 1f389f5285
5 changed files with 90 additions and 31 deletions

Binary file not shown.

View file

@ -1,6 +1,9 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{animation::transform::AnimatedTransform, card::Hand}; use crate::{
animation::transform::AnimatedTransform,
card::{CardInHand, Hand, InsertCard},
};
#[derive(Component)] #[derive(Component)]
pub struct DraggableCard; pub struct DraggableCard;
@ -33,6 +36,7 @@ pub fn card_drag_start(
mut commands: Commands, mut commands: Commands,
card_query: Query<(), With<DraggableCard>>, card_query: Query<(), With<DraggableCard>>,
dragged_option: Option<Single<&DraggedCard>>, dragged_option: Option<Single<&DraggedCard>>,
mut hand: Single<&mut Hand>,
) { ) {
if dragged_option.is_some() { if dragged_option.is_some() {
return; return;
@ -44,7 +48,11 @@ pub fn card_drag_start(
commands commands
.entity(event.entity) .entity(event.entity)
.insert(DraggedCard) .insert(DraggedCard)
.remove_parent_in_place(); .remove::<CardInHand>();
if let Some(pos) = hand.0.iter().position(|ent| *ent == event.entity) {
hand.0.remove(pos);
}
} }
pub fn card_drag( pub fn card_drag(
@ -59,10 +67,10 @@ pub fn card_drag_end(
_: On<Pointer<DragEnd>>, _: On<Pointer<DragEnd>>,
mut commands: Commands, mut commands: Commands,
dragged: Single<Entity, With<DraggedCard>>, dragged: Single<Entity, With<DraggedCard>>,
hand: Single<Entity, With<Hand>>,
) { ) {
commands.trigger(InsertCard(dragged.entity()));
commands commands
.entity(dragged.entity()) .entity(dragged.entity())
.remove::<DraggedCard>() .remove::<DraggedCard>()
.set_parent_in_place(hand.entity()); .insert(CardInHand);
} }

View file

@ -1,3 +1,5 @@
use std::f32;
use crate::animation::transform::AnimatedTransform; use crate::animation::transform::AnimatedTransform;
use bevy::prelude::*; use bevy::prelude::*;
use drag::*; use drag::*;
@ -10,7 +12,10 @@ pub struct CardPlugin;
pub struct Card; pub struct Card;
#[derive(Component)] #[derive(Component)]
pub struct Hand; pub struct CardInHand;
#[derive(Component)]
pub struct Hand(Vec<Entity>);
#[derive(Resource)] #[derive(Resource)]
pub struct CardImage(Handle<Image>); pub struct CardImage(Handle<Image>);
@ -18,6 +23,9 @@ pub struct CardImage(Handle<Image>);
#[derive(Event)] #[derive(Event)]
pub struct AddCard; pub struct AddCard;
#[derive(EntityEvent)]
pub struct InsertCard(Entity);
#[derive(Event)] #[derive(Event)]
pub struct ClearCards; pub struct ClearCards;
@ -26,7 +34,8 @@ impl Plugin for CardPlugin {
app.add_systems(Startup, hand_setup) app.add_systems(Startup, hand_setup)
.add_systems(Update, card_arrange) .add_systems(Update, card_arrange)
.add_observer(card_add) .add_observer(card_add)
.add_observer(card_clear); .add_observer(card_clear)
.add_observer(insert_card);
app.insert_resource(MousePosition(Vec3::new(0., 0., 0.))) app.insert_resource(MousePosition(Vec3::new(0., 0., 0.)))
.add_systems(PreUpdate, mouse_position_system) .add_systems(PreUpdate, mouse_position_system)
.add_observer(card_drag_start) .add_observer(card_drag_start)
@ -37,7 +46,7 @@ impl Plugin for CardPlugin {
fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) { fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(( commands.spawn((
Hand, Hand(Vec::new()),
Transform::from_xyz(0., -360., 0.), Transform::from_xyz(0., -360., 0.),
InheritedVisibility::VISIBLE, InheritedVisibility::VISIBLE,
)); ));
@ -48,41 +57,79 @@ fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
} }
fn card_arrange( fn card_arrange(
mut query: Query<(&mut AnimatedTransform, &Sprite), With<Card>>, mut query: Query<(&mut AnimatedTransform, &Sprite), With<CardInHand>>,
hand: Single<(Entity, &Children), With<Hand>>, hand: Single<(Entity, &Hand, &Transform)>,
assets: Res<Assets<Image>>, assets: Res<Assets<Image>>,
) { ) {
let cards_amount = hand.1.len(); let cards_amount = hand.1.0.len();
for (index, card) in hand.1.iter().enumerate() { for (index, card) in hand.1.0.iter().enumerate() {
let Ok((mut transform, sprite)) = query.get_mut(card) else { let Ok((mut transform, sprite)) = query.get_mut(card.clone()) else {
continue; continue;
}; };
let size = assets.get(&sprite.image).unwrap().size_f32(); let size = assets.get(&sprite.image).unwrap().size_f32();
let left_boundary = -size.x * (cards_amount as f32) / 2.0 + size.x / 2.0; let left_boundary = -size.x * (cards_amount as f32) / 2.0 + size.x / 2.0;
transform.translation.x = left_boundary + size.x * index as f32; transform.translation.x = left_boundary + size.x * index as f32 + hand.2.translation.x;
transform.translation.y = size.y / 2.0; transform.translation.y = size.y / 2.0 + hand.2.translation.y;
} }
} }
fn card_add( fn card_add(
_: On<AddCard>, _: On<AddCard>,
mut commands: Commands, mut commands: Commands,
hand: Single<Entity, With<Hand>>,
card_image: Res<CardImage>, card_image: Res<CardImage>,
mut hand: Single<&mut Hand>,
) { ) {
commands.spawn(( let card = commands
.spawn((
Sprite::from_image(card_image.0.clone()), Sprite::from_image(card_image.0.clone()),
Transform::from_xyz(0., 0., 0.), Transform::from_xyz(0., 0., 0.),
AnimatedTransform::from_xyz(0., 0., 0.), AnimatedTransform::from_xyz(0., 0., 0.),
Card, Card,
ChildOf(hand.entity()),
DraggableCard, DraggableCard,
CardInHand,
Pickable::default(), Pickable::default(),
)); ))
.id();
hand.0.push(card);
} }
fn card_clear(_: On<ClearCards>, mut commands: Commands, children: Single<&Children, With<Hand>>) { fn card_clear(_: On<ClearCards>, mut commands: Commands, mut hand: Single<&mut Hand>) {
for card in children.iter() { for card in &hand.0 {
commands.entity(card).despawn(); let entity = card.clone();
commands.entity(entity).despawn();
} }
hand.0.clear();
}
fn insert_card(
event: On<InsertCard>,
mut hand_query: Single<(Entity, &mut Hand)>,
transform_query: Query<&Transform, With<Card>>,
) {
let Ok(inserted_transform) = transform_query.get(event.0) else {
return;
};
let mut decided_index: usize = 0;
let mut decided_diff: f32 = f32::INFINITY;
for (index, card) in hand_query.1.0.iter().enumerate() {
let Ok(card_transform) = transform_query.get(card.clone()) else {
continue;
};
let calculated_diff =
(inserted_transform.translation - card_transform.translation).length();
if decided_diff > calculated_diff {
decided_diff = calculated_diff;
decided_index = index;
}
}
if let Some(pos) = hand_query.1.0.iter().position(|ent| *ent == event.0) {
hand_query.1.0.remove(pos);
}
hand_query.1.0.insert(decided_index, event.0);
} }

3
src/grid.rs Normal file
View file

@ -0,0 +1,3 @@
use bevy::prelude::*;
pub struct GridPlugin;

View file

@ -4,17 +4,18 @@ mod animation;
mod buttons; mod buttons;
mod card; mod card;
mod dev_tools; mod dev_tools;
mod grid;
mod turns; mod turns;
fn main() { fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
DefaultPlugins,
turns::TurnSystemPlugin,
card::CardPlugin,
dev_tools::DevToolsPlugin,
buttons::ButtonPlugin,
animation::AnimationPlugin, animation::AnimationPlugin,
buttons::ButtonPlugin,
card::CardPlugin,
DefaultPlugins,
dev_tools::DevToolsPlugin,
turns::TurnSystemPlugin,
)) ))
.add_systems(Startup, setup) .add_systems(Startup, setup)
.run(); .run();