Compare commits

..

3 Commits

Author SHA1 Message Date
Joey Pollack 9d5cb8d243 Adds grid auto resizing when window is resized 2 years ago
Joey Pollack 3825ec4a6a Adds adjustable top margin to world grid 2 years ago
Joey Pollack be88f33055 World grid drawing correctly 2 years ago

@ -59,7 +59,8 @@ fn main()
WorldPlugin {
grid_width: 100.,
grid_height: 100.
grid_height: 100.,
top_margin: 50.0
},
ShapePlugin
))
@ -87,18 +88,6 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
Agent
));
// Line test
let line = shapes::Line(Vec2::ZERO, Vec2::new(100.0, 100.0));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&line),
..default()
},
Stroke::new(Color::ORANGE_RED, 5.0),
//Fill::color(Color::ORANGE_RED),
));
// UI
// Text with multiple sections
commands.spawn((

@ -1,7 +1,10 @@
use bevy::prelude::*;
use bevy::{prelude::*, window::WindowResized};
use bevy_prototype_lyon::prelude::*;
#[derive(Component)]
struct GridLine;
#[derive(Resource)]
struct WorldPluginState
{
@ -9,6 +12,7 @@ struct WorldPluginState
window_height: f32,
grid_width: f32,
grid_height: f32,
top_margin: f32,
}
@ -16,6 +20,7 @@ pub struct WorldPlugin
{
pub grid_width: f32,
pub grid_height: f32,
pub top_margin: f32,
}
impl Plugin for WorldPlugin
@ -33,41 +38,67 @@ impl Plugin for WorldPlugin
window_width: window.resolution.width(),
window_height: window.resolution.height(),
grid_width: self.grid_width,
grid_height: self.grid_height
grid_height: self.grid_height,
top_margin: self.top_margin,
})
.add_systems(Update, on_resize)
.add_systems(Startup, setup);
}
}
}
fn setup(mut commands: Commands, settings: Res<WorldPluginState>, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>)
fn setup(mut commands: Commands, settings: Res<WorldPluginState>)
{
// Camera
//commands.spawn(Camera2dBundle::default());
// Add Grid
// let cell_width = settings.window_width / settings.grid_width;
// let cell_height = settings.window_height / settings.grid_height;
// let line_size = 2;
// for i in 0..settings.grid_width as i32
// {
// for j in 0..settings.grid_height as i32
// {
// }
// }
let cell_width = settings.window_width / (settings.grid_width - 1.0);
let cell_height = (settings.window_height - settings.top_margin) / (settings.grid_height - 1.0);
let line_size = 2.0;
let half_screen_width = settings.window_width / 2.0;
let half_screen_height = (settings.window_height - settings.top_margin) / 2.0;
for i in 0..settings.grid_width as i32
{
let point_a = Vec2::new(i as f32 * cell_width - half_screen_width, -half_screen_height - settings.top_margin);
let point_b = Vec2::new(i as f32 * cell_width - half_screen_width, half_screen_height - settings.top_margin);
add_line(&mut commands, point_a, point_b, line_size);
}
for i in 0..settings.grid_height as i32
{
let point_a = Vec2::new(-half_screen_width, i as f32 * cell_height - half_screen_height - settings.top_margin);
let point_b = Vec2::new(half_screen_width, i as f32 * cell_height - half_screen_height - settings.top_margin);
add_line(&mut commands, point_a, point_b, line_size);
}
}
// fn add_line(mut commands: Commands, point_a: Vec2, point_b: Vec2, line_size: i32)
// {
fn add_line(commands: &mut Commands, point_a: Vec2, point_b: Vec2, line_size: f32)
{
let line = shapes::Line(point_a, point_b);
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&line),
..default()
},
Stroke::new(Color::BLACK, line_size),
GridLine,
));
}
fn on_resize(mut commands: Commands, mut settings: ResMut<WorldPluginState>, mut resize_reader: EventReader<WindowResized>, mut grid: Query<(Entity, &GridLine)> )
{
for e in resize_reader.iter()
{
// When resolution is being changed
// info!("new window size: {:.1} x {:.1}", e.width, e.height);
settings.window_width = e.width;
settings.window_height = e.height;
}
for (entity, line) in &grid
{
commands.entity(entity).despawn();
}
// commands.spawn(MaterialMesh2dBundle {
// mesh: meshes
// .add(shape::Quad::new(Vec2::new(cell_width * i, cell_height * j)).into())
// .into(),
// material: materials.add(ColorMaterial::from(Color::BLACK)),
// transform: Transform::from_translation(Vec3::new(50., 0., 0.)),
// ..default()
// });
// }
setup(commands, settings.into());
}
Loading…
Cancel
Save