76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
use bevy::prelude::*;
|
|
|
|
pub mod drag;
|
|
|
|
pub struct CardPlugin;
|
|
|
|
#[derive(Component)]
|
|
pub struct Card;
|
|
|
|
#[derive(Component)]
|
|
pub struct Hand;
|
|
|
|
#[derive(Resource)]
|
|
pub struct CardImage(Handle<Image>);
|
|
|
|
impl Plugin for CardPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Startup, (hand_setup, test_card_setup).chain())
|
|
.add_systems(Update, card_arrange);
|
|
}
|
|
}
|
|
|
|
fn hand_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn((Hand, Transform::from_xyz(0., -360., 0.)));
|
|
|
|
commands.insert_resource(CardImage(
|
|
asset_server.load::<Image>("sprites/cards/units/fosma.png"),
|
|
));
|
|
}
|
|
|
|
fn test_card_setup(
|
|
mut commands: Commands,
|
|
hand: Single<Entity, With<Hand>>,
|
|
card_image: Res<CardImage>,
|
|
) {
|
|
commands.spawn((
|
|
Sprite::from_image(card_image.0.clone()),
|
|
Transform::from_xyz(0., 0., 0.),
|
|
Card,
|
|
ChildOf(hand.entity()),
|
|
));
|
|
|
|
commands.spawn((
|
|
Sprite::from_image(card_image.0.clone()),
|
|
Transform::from_xyz(0., 0., 0.),
|
|
Card,
|
|
ChildOf(hand.entity()),
|
|
));
|
|
|
|
commands.spawn((
|
|
Sprite::from_image(card_image.0.clone()),
|
|
Transform::from_xyz(0., 0., 0.),
|
|
Card,
|
|
ChildOf(hand.entity()),
|
|
));
|
|
}
|
|
|
|
fn card_arrange(
|
|
query: Query<(&mut Transform, &Sprite, &ChildOf), With<Card>>,
|
|
hand: Single<(Entity, &Children), With<Hand>>,
|
|
assets: Res<Assets<Image>>,
|
|
) {
|
|
let cards_amount = hand.1.len();
|
|
let mut card_id = 0.;
|
|
for (mut transform, sprite, child_of) in query {
|
|
if child_of.parent() != hand.0 {
|
|
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 * card_id;
|
|
transform.translation.y = size.y / 2.0;
|
|
|
|
card_id += 1.0;
|
|
}
|
|
}
|