diff --git a/assets/sprites/hand_9s.png b/assets/sprites/hand_9s.png new file mode 100644 index 0000000..19a05d6 Binary files /dev/null and b/assets/sprites/hand_9s.png differ diff --git a/src/lib.rs b/src/lib.rs index ca3a70d..c4f2342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,102 @@ -use bevy::prelude::*; -use crate::hand::Hand; +use bevy::{ + ecs::system::SystemId, + prelude::* +}; +use crate::{ + card::Card, + hand::Hand, + properties::TestProperty +}; +pub mod card; pub mod hand; pub mod animal; pub mod properties; +#[derive(Component)] +pub struct JustUpdated; + +#[derive(Resource)] +pub struct Systems { + pub update_hand_dimensions: SystemId +} + pub fn setup( mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut> + asset_server: Res, ) { + let updhd_id = commands.register_system(update_hand_dimensions); + commands.insert_resource(Systems { + update_hand_dimensions: updhd_id + }); commands.spawn(Camera2d); commands.spawn(( Hand::new("Player1".to_string()), Transform::from_xyz(0., -300., 0.), - Mesh2d(meshes.add(Rectangle::new(100., 100.))), - MeshMaterial2d(materials.add(ColorMaterial::from_color(Srgba::rgb(0.5, 0.5, 0.5)))) + Sprite { + image: asset_server.load("sprites/hand_9s.png"), + image_mode: SpriteImageMode::Sliced(TextureSlicer { + border: BorderRect::square(4.), + center_scale_mode: SliceScaleMode::Stretch, + sides_scale_mode: SliceScaleMode::Stretch, + ..default() + }), + ..default() + }, )); } + +pub fn test_setup_hand( + mut commands: Commands, + asset_server: Res, + //systems: Res, + query: Single> +) { + let hand_id = query.into_inner(); + for _ in 0..4 { + create_card(&mut commands, &asset_server, Some(hand_id)); + } +} + +pub fn create_card( + commands: &mut Commands, + asset_server: &Res, + hand_entity: Option, + //systems: &Res +) { + let mut card = commands.spawn(( + Card, + TestProperty, + Transform::from_xyz(0., 0., 0.), + Sprite::from_image(asset_server.load("sprites/card_back_placeholder.png")) + )); + if let Some(hand) = hand_entity { + card.set_parent(hand); + commands.entity(hand).insert(JustUpdated); + //commands.run_system(systems.update_hand_dimensions); + } +} + +pub fn update_hand_dimensions( + mut commands: Commands, + p_query: Single<(Entity, &mut Sprite, &Children), (Added, With)>, + mut c_query: Query<(&mut Transform, &Sprite), (With, Without)>, + assets: Res> +) { + 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 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.; + } + hand.1.custom_size = Some(Vec2::new(hand_total_width, hand_height)); + commands.entity(hand.0).remove::(); +} diff --git a/src/main.rs b/src/main.rs index 91ae78e..9474c9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ use bevy::prelude::*; use evolution_rs::{ properties::plugin::BasePropertiesPlugin, - setup + setup, test_setup_hand, update_hand_dimensions }; fn main() { App::new() .add_plugins((DefaultPlugins,BasePropertiesPlugin)) - .add_systems(Startup, setup) + .add_systems(Startup, (setup, test_setup_hand, update_hand_dimensions).chain()) .run(); }