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 crate::{animation::transform::AnimatedTransform, card::Hand};
use crate::{
animation::transform::AnimatedTransform,
card::{CardInHand, Hand, InsertCard},
};
#[derive(Component)]
pub struct DraggableCard;
@ -33,6 +36,7 @@ pub fn card_drag_start(
mut commands: Commands,
card_query: Query<(), With<DraggableCard>>,
dragged_option: Option<Single<&DraggedCard>>,
mut hand: Single<&mut Hand>,
) {
if dragged_option.is_some() {
return;
@ -44,7 +48,11 @@ pub fn card_drag_start(
commands
.entity(event.entity)
.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(
@ -59,10 +67,10 @@ pub fn card_drag_end(
_: On<Pointer<DragEnd>>,
mut commands: Commands,
dragged: Single<Entity, With<DraggedCard>>,
hand: Single<Entity, With<Hand>>,
) {
commands.trigger(InsertCard(dragged.entity()));
commands
.entity(dragged.entity())
.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 bevy::prelude::*;
use drag::*;
@ -10,7 +12,10 @@ pub struct CardPlugin;
pub struct Card;
#[derive(Component)]
pub struct Hand;
pub struct CardInHand;
#[derive(Component)]
pub struct Hand(Vec<Entity>);
#[derive(Resource)]
pub struct CardImage(Handle<Image>);
@ -18,6 +23,9 @@ pub struct CardImage(Handle<Image>);
#[derive(Event)]
pub struct AddCard;
#[derive(EntityEvent)]
pub struct InsertCard(Entity);
#[derive(Event)]
pub struct ClearCards;
@ -26,7 +34,8 @@ impl Plugin for CardPlugin {
app.add_systems(Startup, hand_setup)
.add_systems(Update, card_arrange)
.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.)))
.add_systems(PreUpdate, mouse_position_system)
.add_observer(card_drag_start)
@ -37,7 +46,7 @@ impl Plugin for CardPlugin {
fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Hand,
Hand(Vec::new()),
Transform::from_xyz(0., -360., 0.),
InheritedVisibility::VISIBLE,
));
@ -48,41 +57,79 @@ fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}
fn card_arrange(
mut query: Query<(&mut AnimatedTransform, &Sprite), With<Card>>,
hand: Single<(Entity, &Children), With<Hand>>,
mut query: Query<(&mut AnimatedTransform, &Sprite), With<CardInHand>>,
hand: Single<(Entity, &Hand, &Transform)>,
assets: Res<Assets<Image>>,
) {
let cards_amount = hand.1.len();
for (index, card) in hand.1.iter().enumerate() {
let Ok((mut transform, sprite)) = query.get_mut(card) else {
let cards_amount = hand.1.0.len();
for (index, card) in hand.1.0.iter().enumerate() {
let Ok((mut transform, sprite)) = query.get_mut(card.clone()) else {
continue;
};
let size = assets.get(&sprite.image).unwrap().size_f32();
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.y = size.y / 2.0;
transform.translation.x = left_boundary + size.x * index as f32 + hand.2.translation.x;
transform.translation.y = size.y / 2.0 + hand.2.translation.y;
}
}
fn card_add(
_: On<AddCard>,
mut commands: Commands,
hand: Single<Entity, With<Hand>>,
card_image: Res<CardImage>,
mut hand: Single<&mut Hand>,
) {
commands.spawn((
let card = commands
.spawn((
Sprite::from_image(card_image.0.clone()),
Transform::from_xyz(0., 0., 0.),
AnimatedTransform::from_xyz(0., 0., 0.),
Card,
ChildOf(hand.entity()),
DraggableCard,
CardInHand,
Pickable::default(),
));
))
.id();
hand.0.push(card);
}
fn card_clear(_: On<ClearCards>, mut commands: Commands, children: Single<&Children, With<Hand>>) {
for card in children.iter() {
commands.entity(card).despawn();
fn card_clear(_: On<ClearCards>, mut commands: Commands, mut hand: Single<&mut Hand>) {
for card in &hand.0 {
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 card;
mod dev_tools;
mod grid;
mod turns;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
turns::TurnSystemPlugin,
card::CardPlugin,
dev_tools::DevToolsPlugin,
buttons::ButtonPlugin,
animation::AnimationPlugin,
buttons::ButtonPlugin,
card::CardPlugin,
DefaultPlugins,
dev_tools::DevToolsPlugin,
turns::TurnSystemPlugin,
))
.add_systems(Startup, setup)
.run();