Update hand system (that doesn't work)

This commit is contained in:
Alexey 2025-02-17 17:48:24 +03:00
commit 222bc498f1
3 changed files with 90 additions and 8 deletions

BIN
assets/sprites/hand_9s.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 359 B

View file

@ -1,20 +1,102 @@
use bevy::prelude::*; use bevy::{
use crate::hand::Hand; ecs::system::SystemId,
prelude::*
};
use crate::{
card::Card,
hand::Hand,
properties::TestProperty
};
pub mod card;
pub mod hand; pub mod hand;
pub mod animal; pub mod animal;
pub mod properties; pub mod properties;
#[derive(Component)]
pub struct JustUpdated;
#[derive(Resource)]
pub struct Systems {
pub update_hand_dimensions: SystemId
}
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>
) { ) {
let updhd_id = commands.register_system(update_hand_dimensions);
commands.insert_resource(Systems {
update_hand_dimensions: updhd_id
});
commands.spawn(Camera2d); commands.spawn(Camera2d);
commands.spawn(( commands.spawn((
Hand::new("Player1".to_string()), Hand::new("Player1".to_string()),
Transform::from_xyz(0., -300., 0.), Transform::from_xyz(0., -300., 0.),
Mesh2d(meshes.add(Rectangle::new(100., 100.))), Sprite {
MeshMaterial2d(materials.add(ColorMaterial::from_color(Srgba::rgb(0.5, 0.5, 0.5)))) 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<AssetServer>,
//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));
}
}
pub fn create_card(
commands: &mut Commands,
asset_server: &Res<AssetServer>,
hand_entity: Option<Entity>,
//systems: &Res<Systems>
) {
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<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 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::<JustUpdated>();
}

View file

@ -1,12 +1,12 @@
use bevy::prelude::*; use bevy::prelude::*;
use evolution_rs::{ use evolution_rs::{
properties::plugin::BasePropertiesPlugin, properties::plugin::BasePropertiesPlugin,
setup setup, test_setup_hand, update_hand_dimensions
}; };
fn main() { fn main() {
App::new() App::new()
.add_plugins((DefaultPlugins,BasePropertiesPlugin)) .add_plugins((DefaultPlugins,BasePropertiesPlugin))
.add_systems(Startup, setup) .add_systems(Startup, (setup, test_setup_hand, update_hand_dimensions).chain())
.run(); .run();
} }