From 96675b42cf0d0cc6ffdd45758c228eba3f6945bd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 24 Dec 2020 14:01:05 -0500 Subject: [PATCH] Slapping a grid on it --- src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 07ac9f4..9d3d9e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,27 @@ use bevy::prelude::*; +const ARENA_WIDTH: u32 = 10; +const ARENA_HEIGHT: u32 = 10; + +#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] +struct Position { + x: i32, + y: i32, +} + +struct Size { + width: f32, + height: f32, +} +impl Size { + pub fn square(x: f32) -> Self { + Self { + width: x, + height: x, + } + } +} + struct SnakeHead; struct Materials { head_material: Handle, @@ -19,7 +41,9 @@ fn spawn_snake(commands: &mut Commands, materials: Res) { sprite: Sprite::new(Vec2::new(10.0, 10.0)), ..Default::default() }) - .with(SnakeHead); + .with(SnakeHead) + .with(Position { x: 3, y: 3 }) + .with(Size::square(0.8)); } fn snake_movement( @@ -42,11 +66,39 @@ fn snake_movement( } } +fn size_scaling(windows: Res, mut q: Query<(&Size, &mut Sprite)>) { + let window = windows.get_primary().unwrap(); + for (sprite_size, mut sprite) in q.iter_mut() { + sprite.size = Vec2::new( + sprite_size.width / ARENA_WIDTH as f32 * window.width() as f32, + sprite_size.height / ARENA_HEIGHT as f32 * window.height() as f32, + ); + } +} + +fn position_translation(windows: Res, mut q: Query<(&Position, &mut Transform)>) { + fn convert(pos: f32, bound_window: f32, bound_game: f32) -> f32 { + let tile_size = bound_window / bound_game; + pos / bound_game * bound_window - (bound_window / 2.) + (tile_size / 2.) + } + + let window = windows.get_primary().unwrap(); + for (pos, mut transform) in q.iter_mut() { + transform.translation = Vec3::new( + convert(pos.x as f32, window.width() as f32, ARENA_WIDTH as f32), + convert(pos.y as f32, window.height() as f32, ARENA_HEIGHT as f32), + 0.0, + ); + } +} + fn main() { App::build() .add_startup_system(setup.system()) .add_startup_stage("game_setup", SystemStage::single(spawn_snake.system())) .add_system(snake_movement.system()) + .add_system(position_translation.system()) + .add_system(size_scaling.system()) .add_plugins(DefaultPlugins) .run(); }