Sprite background (ish) and correct size for ui elements

main
Elijah Voigt 2 months ago
parent 1c99950e65
commit a310bddce0

@ -89,7 +89,6 @@ fn main() {
sync_singleton_to_ui::<Shape>.run_if(any_component_changed::<Shape>),
),
)
.add_systems(Update, draw_grid)
.run();
}
@ -278,17 +277,34 @@ fn init_world(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let mesh = meshes.add(Rectangle::new(SCALE, SCALE));
let block_material = materials.add(ColorMaterial {
color: WHITE.into(),
..default()
});
let grid_material = materials.add(ColorMaterial {
color: BLACK.into(),
..default()
});
commands.insert_resource(Visuals {
material: materials.add(ColorMaterial {
color: WHITE.into(),
..default()
}),
mesh: meshes.add(Rectangle::new(SCALE, SCALE)),
material: block_material.clone(),
mesh: mesh.clone(),
});
(0..Y_MAX).for_each(|i| {
info!("Spawning line {i}");
commands.spawn((Line(i), LineBlocks::default(), TETRIS));
(0..Y_MAX).for_each(|y| {
// Spawn tiles for this line
(0..X_MAX).for_each(|x| {
commands.spawn((
Mesh2d(mesh.clone()),
MeshMaterial2d(grid_material.clone()),
GridPosition { x, y },
Transform::from_xyz(0.0, 0.0, -1.0),
TETRIS,
));
});
// Spawn line for holding blocks
commands.spawn((Line(y), LineBlocks::default(), TETRIS));
});
}
@ -309,8 +325,7 @@ fn init_cameras(
mut images: ResMut<Assets<Image>>,
mut output_images: ResMut<OutputImages>,
) {
fn render_target_image() -> Image {
let (width, height) = (1028, 1028);
fn render_target_image((width, height): (u32, u32)) -> Image {
let size = Extent3d {
width,
height,
@ -329,7 +344,8 @@ fn init_cameras(
}
{
let img = render_target_image();
let (width, height) = (SCALE as u32 * X_MAX as u32, SCALE as u32 * Y_MAX as u32);
let img = render_target_image((width, height));
let target = {
let handle = images.add(img);
output_images.tetris = handle.clone();
@ -353,7 +369,7 @@ fn init_cameras(
}
{
let img = render_target_image();
let img = render_target_image((512, 512));
let target = {
let handle = images.add(img);
output_images.battler = handle.clone();
@ -377,16 +393,14 @@ fn init_cameras(
}
}
fn init_ui(mut commands: Commands, output_images: Res<OutputImages>) {
fn init_ui(mut commands: Commands, output_images: Res<OutputImages>, images: Res<Assets<Image>>) {
commands
.spawn((
Node {
align_self: AlignSelf::Center,
justify_self: JustifySelf::End,
flex_direction: FlexDirection::Column,
..default()
},
))
.spawn((Node {
align_self: AlignSelf::Center,
justify_self: JustifySelf::End,
flex_direction: FlexDirection::Column,
..default()
},))
.with_children(|parent| {
parent
.spawn((Node {
@ -428,10 +442,12 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>) {
..default()
},
BorderColor(BLACK.into()),
children![(
)).with_children(|parent| {
let img = images.get(&output_images.tetris).unwrap();
parent.spawn((
Node {
width: Val::Px(256.0),
height: Val::Px(256.0),
width: Val::Px(img.size_f32().x * 0.5),
height: Val::Px(img.size_f32().y * 0.5),
..default()
},
ImageNode {
@ -439,8 +455,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>) {
image_mode: NodeImageMode::Stretch,
..default()
},
)]
));
));
});
commands.spawn((
Node {
@ -450,10 +466,12 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>) {
..default()
},
BorderColor(BLACK.into()),
children![(
)).with_children(|parent| {
let img = images.get(&output_images.battler).unwrap();
parent.spawn((
Node {
width: Val::Px(256.0),
height: Val::Px(256.0),
width: Val::Px(img.size_f32().x * 0.5),
height: Val::Px(img.size_f32().y * 0.5),
..default()
},
ImageNode {
@ -461,8 +479,8 @@ fn init_ui(mut commands: Commands, output_images: Res<OutputImages>) {
image_mode: NodeImageMode::Stretch,
..default()
},
)]
));
));
});
commands
.spawn((
@ -697,7 +715,8 @@ fn update_position(
gp, v3
);
t.translation = gp.into();
t.translation.x = v3.x;
t.translation.y = v3.y;
});
}
@ -793,20 +812,9 @@ fn kb_input(
);
}
fn draw_grid(mut gizmos: Gizmos) {
gizmos
.grid_2d(
Isometry2d::IDENTITY,
UVec2::new(X_MAX as u32, Y_MAX as u32),
Vec2::new(SCALE, SCALE),
GREEN,
)
.outer_edges();
}
fn falling(mut shape: Query<Entity, With<Shape>>, mut commands: Commands) {
shape.iter_mut().for_each(|e| {
info!("Making {:?} fall", e);
debug!("Making {:?} fall", e);
commands.entity(e).trigger(Movement::Down);
});
}
@ -911,7 +919,7 @@ fn clear_line(
fn adjust_block_lines(
query: Query<(Entity, &Line), Changed<Line>>,
parent: Query<&LineBlocks>,
mut blocks: Query<&mut GridPosition>,
mut blocks: Query<&mut GridPosition, With<ShapeBlock>>,
) {
query.iter().for_each(|(e, Line(i))| {
parent.iter_descendants(e).for_each(|block| {
@ -939,9 +947,9 @@ enum Movement {
// TODO: When out of bounds left/right, try to move piece away from wall
fn movement(
trigger: Trigger<Movement>,
mut grid_positions: Query<&mut GridPosition, Or<(With<ShapeBlock>, With<ShapeBlocks>)>>,
mut grid_positions: Query<&mut GridPosition, Or<(With<ShapeBlock>, With<ShapeBlocks>, Without<LineBlock>)>>,
mut shape: Query<&mut Shape>,
inactive: Query<&GridPosition, (Without<ShapeBlock>, Without<ShapeBlocks>)>,
inactive: Query<&GridPosition, (Without<ShapeBlock>, Without<ShapeBlocks>, With<LineBlock>)>,
mut commands: Commands,
) {
if let (Ok(this_shape), Ok(center)) = (
@ -1046,7 +1054,7 @@ fn swap(
fn deactivate_shape(
mut events: RemovedComponents<Shape>,
grid_positions: Query<&GridPosition>,
grid_positions: Query<&GridPosition, With<ShapeBlock>>,
parent: Query<&ShapeBlocks>,
lines: Query<(Entity, &Line), With<LineBlocks>>,
mut commands: Commands,

Loading…
Cancel
Save