From ec95776771d061c21af17686491181c78b7955c9 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 20 Mar 2021 12:35:38 +0100 Subject: draw grid --- src/constants.rs | 2 +- src/main.rs | 89 ++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/constants.rs b/src/constants.rs index f9a5003..5952fa5 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,5 +1,5 @@ pub const TILE_SCALE: f32 = 3.0; -pub const TILE_HEIGHT: f32 = 16.0; +pub const TILE_SIZE: f32 = 16.0; pub const TILE_WIDTH: f32 = 16.0; pub const BORDER_SIZE: f32 = 3.0; pub const SHIFT_X: f32 = 50.0; diff --git a/src/main.rs b/src/main.rs index c4e40f9..acecaad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,55 +23,86 @@ impl Cell { } } -struct Position { - x: f32, - y: f32, -} - -pub fn add_cells(commands: &mut Commands) { - for i in 0..8 { - for j in 0..8 { - commands.spawn(( - Cell::new(i, j), - Position { - x: (i as f32 * constants::TILE_HEIGHT), - y: (j as f32 * constants::TILE_WIDTH), - }, - )); - } - } -} - -pub fn add_background( +pub fn setup( commands: &mut Commands, asset_server: Res, mut materials: ResMut>, + mut texture_atlases: ResMut>, ) { - let texture_handle = asset_server.load("background.png"); + let background = asset_server.load("background.png"); + let tileset = asset_server.load("tileset.png"); + + let atlas = TextureAtlas::from_grid(tileset, Vec2::new(16.0, 16.0), 4, 4); + let atlas_handle = texture_atlases.add(atlas); + commands - .spawn(Camera2dBundle { + .spawn(Camera2dBundle::default()) + .spawn(SpriteBundle { + material: materials.add(background.into()), transform: Transform { - translation: Vec3{ - x: -40.0, + translation: Vec3 { + x: 50.0, y: 0.0, z: 0.0, }, - scale: Vec3::splat(0.3), + scale: Vec3::splat(3.5), ..Default::default() }, ..Default::default() }) - .spawn(SpriteBundle { - material: materials.add(texture_handle.into()), + .spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(8), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: 225.0, + y: -200.0, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, + ..Default::default() + }) + .spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(12), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: 225.0, + y: -200.0 + (-16.0) * 3.5, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, ..Default::default() }); + + for i in 0..8 { + for j in 0..8 { + commands.spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(1), + texture_atlas: atlas_handle.clone(), + transform: Transform { + translation: Vec3 { + x: ((i as f32) * 16.0 * 3.5) - 320.0, + y: ((j as f32) * 16.0 * 3.5) - 170.0, + z: 0.0, + }, + scale: Vec3::splat(3.5), + ..Default::default() + }, + ..Default::default() + }); + } + } } pub struct GemsPlugin; impl Plugin for GemsPlugin { fn build(&self, app: &mut AppBuilder) { - app.add_startup_system(add_background.system()); - app.add_startup_system(add_cells.system()); + app.add_startup_system(setup.system()); } } -- cgit v1.2.3