diff --git a/crates/border-wars/assets/tiles/hill.png b/crates/border-wars/assets/tiles/hill.png new file mode 100644 index 0000000..994ec5c Binary files /dev/null and b/crates/border-wars/assets/tiles/hill.png differ diff --git a/crates/border-wars/src/map/renderer.rs b/crates/border-wars/src/map/renderer.rs index 5314995..1ca1499 100644 --- a/crates/border-wars/src/map/renderer.rs +++ b/crates/border-wars/src/map/renderer.rs @@ -10,36 +10,46 @@ struct TilesOffset { y: f32, } - /// A plugin to render the map. pub struct RendererPlugin; impl Plugin for RendererPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, init_resources_for_rendering).add_systems( - Update, - render_map.run_if(in_state(crate::CurrentScene::Game)), - ); + app.add_systems(Startup, init_resources_for_rendering) + .add_systems( + Update, + render_map.run_if(in_state(crate::CurrentScene::Game)), + ); } } impl Tile { /// Returns the texture handle of the tile. - pub fn get_texture(&self, asset_server: &AssetServer) -> Handle { + fn get_texture(&self, asset_server: &AssetServer) -> Handle { match self { Tile::Grass => asset_server.load("tiles/grass.png"), Tile::Forest => asset_server.load("tiles/grass.png"), - Tile::Hill => asset_server.load("tiles/grass.png"), + Tile::Hill => asset_server.load("tiles/hill.png"), } } + fn get_image_size(&self, target_size: Vec2) -> Vec2 { + let current_size = match self { + Tile::Grass => Vec2 { x: 1250.0, y: 1000.0 }, + Tile::Forest => Vec2 { x: 1250.0, y: 1000.0 }, + Tile::Hill => Vec2 { x: 1250.0, y: 1500.0 }, + }; + + Vec2 { + x: target_size.x / current_size.x, + y: target_size.y / current_size.y, + } + + } } /// Init resources related to the rendering of the map. -fn init_resources_for_rendering(mut commands: Commands, asset_server: Res) { - commands.insert_resource(TilesOffset { - x: 70., - y: 40., - }) +fn init_resources_for_rendering(mut commands: Commands) { + commands.insert_resource(TilesOffset { x: 70., y: 40. }) } /// Renders the map. @@ -47,23 +57,37 @@ fn render_map( query: Query<(Entity, &TilePosition, &Tile), Changed>, mut commands: Commands, asset_server: Res, - mut tiles_offset: Res, + tiles_offset: Res, + ) { for (entity, position, tile) in query.iter() { + let texture = tile.get_texture(&*asset_server); + let mut entity = commands.entity(entity); + let pixel_position_ratio = position.to_pixel_coordinates(); let position_x = tiles_offset.x * pixel_position_ratio.x; let position_y = tiles_offset.y * pixel_position_ratio.y; - commands.entity(entity).insert(SpriteBundle { - transform: Transform { translation: Vec3 { + let scale = tile.get_image_size(Vec2 { + x: 125.0, + y: 100.0, + }); + + entity.insert(SpriteBundle { + texture: texture.clone(), + ..default() + }).insert(Transform { + translation: Vec3 { x: position_x, y: position_y, z: -1.0 / (1.0 + (-position_y * 10_f64.powf(-5.0) as f32).exp()), - }, - scale: Vec3 {x: 1./10., y: 1./10., z: 1.0}, ..Default::default()}, - texture: tile.get_texture(&*asset_server), - ..default() + scale: Vec3 { + x: scale.x, + y: scale.y, + z: 1.0, + }, + ..Default::default() }); } -} +} \ No newline at end of file