-
What's your problemI am learning bevy0.6 by building a 2048 game. I need a sprite that can display changing number to make block. But none of bevy's existing bundles meets the requirement(or I didn't find it). Then I tried to use hierarchy. However, I encountered a problem that is not sure if it is a bug. The number(created by Show me the codeThe following is the problem part that I extracted as a demo. use bevy::prelude::*;
use rand::prelude::*;
#[derive(PartialEq)]
struct GameRunInfo {
block_index: Box<Vec<Vec<Option<Entity>>>>,
reset: bool,
adding: bool,
count: usize,
}
#[derive(Component, Clone)]
struct Block;
fn random_generate_block(
commands: &mut Commands,
game_info: &mut GameRunInfo,
font: &Handle<Font>,
) {
let mut rng = thread_rng();
if game_info.count < 16 {
loop {
let x = rng.gen_range(0..16usize);
let row = x / 4;
let col = x % 4;
if game_info.block_index[row][col] == None {
let r_num = rng.gen_range(1..=2usize) * 2;
let block_id = commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::hex("cff0b455").unwrap(),
custom_size: Some(Vec2::new(65., 65.)),
..Default::default()
},
transform: Transform {
translation: Vec3::new(
col as f32 * 75. + 37.5 - 150.,
row as f32 * 75. + 37.5 - 150.,
0.01,
),
..Default::default()
},
..Default::default()
})
.insert(Block)
.id();
// add number as block's child
commands
.spawn_bundle(Text2dBundle {
text: Text::with_section(
r_num.to_string(),
TextStyle {
font: font.clone(),
font_size: 40.0,
color: Color::WHITE,
},
TextAlignment {
vertical: VerticalAlign::Center,
horizontal: HorizontalAlign::Center,
},
),
transform: Transform {
translation: Vec3::new(0., 0., 0.02),
..Default::default()
},
..Default::default()
})
.insert(Parent(block_id));
// record
game_info.block_index[row][col] = Some(block_id);
game_info.count += 1;
println!("new block {}: {} {}", r_num, row, col);
break;
}
}
} else {
println!("FULL! R to restart");
}
}
fn reset_system(
mut commands: Commands,
mut game_info: ResMut<GameRunInfo>,
mut num_blocks: Query<Entity, With<Block>>,
font: Res<Handle<Font>>,
ky: Res<Input<KeyCode>>,
) {
if !game_info.reset && ky.just_pressed(KeyCode::R) {
println!("Press R");
// delete all block and num
for id in num_blocks.iter_mut() {
commands.entity(id).despawn_recursive();
}
// re-create index table
game_info.block_index = Box::new(vec![vec![Option::<Entity>::None; 4]; 4]);
game_info.count = 0;
// re-generate block
random_generate_block(&mut commands, &mut game_info, &font);
game_info.reset = true;
}
if ky.just_released(KeyCode::R) {
game_info.reset = false;
}
}
fn random_generate_system(
mut commands: Commands,
mut game_info: ResMut<GameRunInfo>,
font: Res<Handle<Font>>,
ky: Res<Input<KeyCode>>,
) {
if !game_info.adding && ky.just_pressed(KeyCode::A) {
println!("Press A");
// re-generate block
random_generate_block(&mut commands, &mut game_info, &font);
game_info.adding = true;
}
if ky.just_released(KeyCode::A) {
game_info.adding = false;
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let mut game_info = GameRunInfo {
block_index: Box::new(vec![vec![Option::<Entity>::None; 4]; 4]),
reset: false,
adding: false,
count: 0,
};
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
// cameras
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
random_generate_block(&mut commands, &mut game_info, &font);
commands.insert_resource(game_info);
commands.insert_resource(font);
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 300.,
height: 420.,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(random_generate_system)
.add_system(reset_system)
.run();
} More informationVersion: bevy-0.6-nightly If it is my fault, please point out my mistake, thanks. And if it is a bug, I will create an issue. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I believe you're seeing #1807 and the workaround mentioned in that issue should fix you up. |
Beta Was this translation helpful? Give feedback.
I believe you're seeing #1807 and the workaround mentioned in that issue should fix you up.