-
hi, fn main() {
App::build()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::rgb(0.2, 0.2, 0.2)))
.add_startup_system(setup.system())
.run();
}
struct CellCoordinates {
line: u32,
column: u32,
}
fn setup(
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
let term_width = 80;
let term_height = 25;
let cell_size = 20.0;
// 2d camera
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
for column in 0..term_width {
for line in 0..term_height {
let color = [Color::RED, Color::GREEN, Color::BLUE][(line + column) % 3];
commands
.spawn_bundle(SpriteBundle {
material: materials.add(color.into()),
transform: Transform::from_xyz(
column as f32 * cell_size,
line as f32 * cell_size,
0.0,
),
sprite: Sprite::new(Vec2::new(cell_size, cell_size)),
..Default::default()
})
.insert_bundle(Text2dBundle {
text: Text::with_section(
"A",
TextStyle {
font: asset_server.load("fonts/fira.ttf"),
font_size: 20.0,
color: Color::WHITE,
},
TextAlignment {
vertical: VerticalAlign::Center,
horizontal: HorizontalAlign::Center,
},
),
transform: Transform::from_xyz(
column as f32 * cell_size,
line as f32 * cell_size,
10.0,
),
..Default::default()
})
.insert(CellCoordinates {
line: line as u32,
column: column as u32,
});
}
}
} am i doing something wrong ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
When you insert the You can either spawn the |
Beta Was this translation helpful? Give feedback.
-
ok thanks for the answer. i'll see where i can go from there. thanks again very much. |
Beta Was this translation helpful? Give feedback.
When you insert the
Text2dBundle
here, you're overwriting most of the components previously added with theSpriteBundle
.You can either spawn the
Text2dBundle
as a child of the entity holding theSpriteBundle
components withcommands.with_children
(its transform would then be relative to its parent), or as a separate entity.