fixed update_card_dimensions
This commit is contained in:
parent
367359e47f
commit
0319cb5a0e
2 changed files with 21 additions and 18 deletions
35
src/lib.rs
35
src/lib.rs
|
|
@ -50,12 +50,12 @@ pub fn setup(
|
|||
pub fn test_setup_hand(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
//systems: Res<Systems>,
|
||||
systems: Res<Systems>,
|
||||
query: Single<Entity, With<Hand>>
|
||||
) {
|
||||
let hand_id = query.into_inner();
|
||||
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,
|
||||
asset_server: &Res<AssetServer>,
|
||||
hand_entity: Option<Entity>,
|
||||
//systems: &Res<Systems>
|
||||
systems: &Res<Systems>
|
||||
) {
|
||||
let mut card = commands.spawn((
|
||||
Card,
|
||||
|
|
@ -74,29 +74,32 @@ pub fn create_card(
|
|||
if let Some(hand) = hand_entity {
|
||||
card.set_parent(hand);
|
||||
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(
|
||||
mut commands: Commands,
|
||||
p_query: Single<(Entity, &mut Sprite, &Children), (Added<JustUpdated>, With<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 counter = 0.;
|
||||
let mut hand_total_width = 0.;
|
||||
let mut hand_height = 0.;
|
||||
for &child_id in hand.2.iter() {
|
||||
let size = hand.2.iter().count();
|
||||
let hor_margin = 8.;
|
||||
let ver_margin = 8.;
|
||||
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 sprite_size = assets.get(child.1.image.id()).unwrap().size_f32();
|
||||
let hor_margin = Vec2::new(4., 0.);
|
||||
let ver_margin = Vec2::new(0., 4.);
|
||||
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.;
|
||||
let offset_x = (sprite_size.x + hor_margin) * (index as f32) - hand_offset;
|
||||
let offset_y = 0.;
|
||||
child.0.translation = Vec3::from((Vec2::new(offset_x, offset_y), 1.));
|
||||
}
|
||||
hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height));
|
||||
commands.entity(hand.0).remove::<JustUpdated>();
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use bevy::prelude::*;
|
|||
|
||||
use evolution_rs::{
|
||||
plugins::*,
|
||||
setup, test_setup_hand, update_hand_dimensions
|
||||
setup, test_setup_hand
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -17,6 +17,6 @@ use evolution_rs::{
|
|||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((DefaultPlugins,BasePropertiesPlugin))
|
||||
.add_systems(Startup, (setup, test_setup_hand, update_hand_dimensions).chain())
|
||||
.add_systems(Startup, (setup, test_setup_hand).chain())
|
||||
.run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue