1
0
Fork 0

Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
0319cb5a0e fixed update_card_dimensions 2025-02-27 11:16:52 +03:00
2 changed files with 21 additions and 18 deletions

View file

@ -50,12 +50,12 @@ pub fn setup(
pub fn test_setup_hand( pub fn test_setup_hand(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
//systems: Res<Systems>, systems: Res<Systems>,
query: Single<Entity, With<Hand>> query: Single<Entity, With<Hand>>
) { ) {
let hand_id = query.into_inner(); let hand_id = query.into_inner();
for _ in 0..4 { for _ in 0..4 {
create_card(&mut commands, &asset_server, Some(hand_id)); create_card(&mut commands, &asset_server, Some(hand_id), &systems);
} }
} }
@ -63,7 +63,7 @@ pub fn create_card(
commands: &mut Commands, commands: &mut Commands,
asset_server: &Res<AssetServer>, asset_server: &Res<AssetServer>,
hand_entity: Option<Entity>, hand_entity: Option<Entity>,
//systems: &Res<Systems> systems: &Res<Systems>
) { ) {
let mut card = commands.spawn(( let mut card = commands.spawn((
Card, Card,
@ -74,29 +74,32 @@ pub fn create_card(
if let Some(hand) = hand_entity { if let Some(hand) = hand_entity {
card.set_parent(hand); card.set_parent(hand);
commands.entity(hand).insert(JustUpdated); commands.entity(hand).insert(JustUpdated);
//commands.run_system(systems.update_hand_dimensions); commands.run_system(systems.update_hand_dimensions);
} }
} }
// TODO: replace this const with getting asset dimensions from Assets<Image>
const CARD_SPRITE_SIZE: Vec2 = Vec2::new(128., 192.);
pub fn update_hand_dimensions( pub fn update_hand_dimensions(
mut commands: Commands, mut commands: Commands,
p_query: Single<(Entity, &mut Sprite, &Children), (Added<JustUpdated>, With<Hand>)>, p_query: Single<(Entity, &mut Sprite, &Children), (Added<JustUpdated>, With<Hand>)>,
mut c_query: Query<(&mut Transform, &Sprite), (With<Card>, Without<Hand>)>, mut c_query: Query<(&mut Transform, &Sprite), (With<Card>, Without<Hand>)>,
assets: Res<Assets<Image>>
) { ) {
let mut hand = p_query.into_inner(); let mut hand = p_query.into_inner();
let mut counter = 0.; let size = hand.2.iter().count();
let mut hand_total_width = 0.; let hor_margin = 8.;
let mut hand_height = 0.; let ver_margin = 8.;
for &child_id in hand.2.iter() { let sprite_size = CARD_SPRITE_SIZE;
let hand_total_width = (sprite_size.x + hor_margin) * (size as f32);
let hand_height = ver_margin * 2. + sprite_size.y;
let hand_offset = (hand_total_width - sprite_size.x - hor_margin) / 2.;
for (index, &child_id) in hand.2.iter().enumerate() {
let Ok(mut child) = c_query.get_mut(child_id) else { continue; }; let Ok(mut child) = c_query.get_mut(child_id) else { continue; };
let sprite_size = assets.get(child.1.image.id()).unwrap().size_f32(); let offset_x = (sprite_size.x + hor_margin) * (index as f32) - hand_offset;
let hor_margin = Vec2::new(4., 0.); let offset_y = 0.;
let ver_margin = Vec2::new(0., 4.); child.0.translation = Vec3::from((Vec2::new(offset_x, offset_y), 1.));
hand_total_width += sprite_size.x + hor_margin.x;
hand_height = sprite_size.y + ver_margin.y * 2.;
child.0.translation = Vec3::from((ver_margin + (sprite_size + hor_margin) * counter, 0.));
counter += 1.;
} }
hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height)); hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height));
commands.entity(hand.0).remove::<JustUpdated>(); commands.entity(hand.0).remove::<JustUpdated>();

View file

@ -2,7 +2,7 @@ use bevy::prelude::*;
use evolution_rs::{ use evolution_rs::{
plugins::*, plugins::*,
setup, test_setup_hand, update_hand_dimensions setup, test_setup_hand
}; };
@ -17,6 +17,6 @@ use evolution_rs::{
fn main() { fn main() {
App::new() App::new()
.add_plugins((DefaultPlugins,BasePropertiesPlugin)) .add_plugins((DefaultPlugins,BasePropertiesPlugin))
.add_systems(Startup, (setup, test_setup_hand, update_hand_dimensions).chain()) .add_systems(Startup, (setup, test_setup_hand).chain())
.run(); .run();
} }