|
|
|
|
@ -1,10 +1,7 @@
|
|
|
|
|
|
|
|
|
|
use bevy::{prelude::*, window::WindowResized};
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
use bevy_prototype_lyon::prelude::*;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct GridLine;
|
|
|
|
|
|
|
|
|
|
#[derive(Resource)]
|
|
|
|
|
struct WorldPluginState
|
|
|
|
|
{
|
|
|
|
|
@ -12,7 +9,6 @@ struct WorldPluginState
|
|
|
|
|
window_height: f32,
|
|
|
|
|
grid_width: f32,
|
|
|
|
|
grid_height: f32,
|
|
|
|
|
top_margin: f32,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -20,7 +16,6 @@ pub struct WorldPlugin
|
|
|
|
|
{
|
|
|
|
|
pub grid_width: f32,
|
|
|
|
|
pub grid_height: f32,
|
|
|
|
|
pub top_margin: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Plugin for WorldPlugin
|
|
|
|
|
@ -38,67 +33,41 @@ impl Plugin for WorldPlugin
|
|
|
|
|
window_width: window.resolution.width(),
|
|
|
|
|
window_height: window.resolution.height(),
|
|
|
|
|
grid_width: self.grid_width,
|
|
|
|
|
grid_height: self.grid_height,
|
|
|
|
|
top_margin: self.top_margin,
|
|
|
|
|
grid_height: self.grid_height
|
|
|
|
|
})
|
|
|
|
|
.add_systems(Update, on_resize)
|
|
|
|
|
.add_systems(Startup, setup);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, settings: Res<WorldPluginState>)
|
|
|
|
|
fn setup(mut commands: Commands, settings: Res<WorldPluginState>, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<ColorMaterial>>)
|
|
|
|
|
{
|
|
|
|
|
// Camera
|
|
|
|
|
//commands.spawn(Camera2dBundle::default());
|
|
|
|
|
|
|
|
|
|
// Add Grid
|
|
|
|
|
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(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,
|
|
|
|
|
));
|
|
|
|
|
// 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
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
// fn add_line(mut commands: Commands, point_a: Vec2, point_b: Vec2, line_size: i32)
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
for (entity, line) in &grid
|
|
|
|
|
{
|
|
|
|
|
commands.entity(entity).despawn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setup(commands, settings.into());
|
|
|
|
|
}
|
|
|
|
|
// 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()
|
|
|
|
|
// });
|
|
|
|
|
// }
|