From 3825ec4a6a457def09729d5af41caa47d5af760e Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 2 Aug 2023 14:15:52 -0400 Subject: [PATCH] Adds adjustable top margin to world grid --- src/main.rs | 5 +++-- src/world.rs | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5d65816..5df37b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,8 @@ fn main() WorldPlugin { grid_width: 100., - grid_height: 100. + grid_height: 100., + top_margin: 0.0 }, ShapePlugin )) @@ -86,7 +87,7 @@ fn setup(mut commands: Commands, mut meshes: ResMut>, mut materials }, Agent )); - + // UI // Text with multiple sections commands.spawn(( diff --git a/src/world.rs b/src/world.rs index 3c214ac..ae51901 100644 --- a/src/world.rs +++ b/src/world.rs @@ -12,6 +12,7 @@ struct WorldPluginState window_height: f32, grid_width: f32, grid_height: f32, + top_margin: f32, } @@ -19,6 +20,7 @@ pub struct WorldPlugin { pub grid_width: f32, pub grid_height: f32, + pub top_margin: f32, } impl Plugin for WorldPlugin @@ -36,7 +38,8 @@ 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(Startup, setup); } @@ -47,22 +50,22 @@ fn setup(mut commands: Commands, settings: Res) { // Add Grid - let cell_width = settings.window_width / settings.grid_width; - let cell_height = settings.window_height / settings.grid_height; + 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 / 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); - let point_b = Vec2::new(i as f32 * cell_width - half_screen_width, half_screen_height); + 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); - let point_b = Vec2::new(half_screen_width, i as f32 * cell_height - half_screen_height); + 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); } }