diff --git a/crates/border-wars/assets/tiles/hill.png b/crates/border-wars/assets/tiles/hill.png index 994ec5c..d3a9ab8 100644 Binary files a/crates/border-wars/assets/tiles/hill.png 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 1ca1499..f5bb809 100644 --- a/crates/border-wars/src/map/renderer.rs +++ b/crates/border-wars/src/map/renderer.rs @@ -1,15 +1,10 @@ //! All functions related to the rendering of the map. use bevy::prelude::*; +use bevy::sprite::Anchor; use crate::map::{Tile, TilePosition}; -#[derive(Resource)] -struct TilesOffset { - x: f32, - y: f32, -} - /// A plugin to render the map. pub struct RendererPlugin; @@ -23,33 +18,44 @@ impl Plugin for RendererPlugin { } } +/// The offset between the center of the tiles in the map. +#[derive(Resource)] +struct TilesOffset(Vec2); + +/// The size of the tiles in the map. +#[derive(Resource, Clone, Copy)] +struct TilesSize(Vec2); + impl Tile { /// Returns the texture handle of the tile. 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/hill.png"), + Self::Grass => asset_server.load("tiles/grass.png"), + Self::Forest => asset_server.load("tiles/grass.png"), + Self::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, + /// Returns the size of the image of the tile. + const fn get_image_size(&self) -> Vec2 { + match self { + Self::Grass => Vec2 { + x: 1250.0, + y: 1000.0, + }, + Self::Forest => Vec2 { + x: 1250.0, + y: 1000.0, + }, + Self::Hill => Vec2 { x: 337.0, y: 740.0 }, } - } } /// Init resources related to the rendering of the map. fn init_resources_for_rendering(mut commands: Commands) { - commands.insert_resource(TilesOffset { x: 70., y: 40. }) + commands.insert_resource(TilesOffset(Vec2 { x: 70., y: 40. })); + commands.insert_resource(TilesSize(Vec2 { x: 125., y: 100. })) } /// Renders the map. @@ -58,36 +64,39 @@ fn render_map( mut commands: Commands, asset_server: Res, tiles_offset: Res, - + tiles_size: Res, ) { for (entity, position, tile) in query.iter() { - let texture = tile.get_texture(&*asset_server); - let mut entity = commands.entity(entity); + let texture = tile.get_texture(&asset_server); - 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; + let translation_2d = tiles_offset.0 * position.to_pixel_coordinates(); + let translation = Vec3::new( + translation_2d.x, + translation_2d.y, + z_position_from_y(translation_2d.y), + ); - let scale = tile.get_image_size(Vec2 { - x: 125.0, - y: 100.0, - }); + let scale_2d = tiles_size.0 / tile.get_image_size(); + let scale = Vec3::new(scale_2d.x, scale_2d.y, 1.0); - entity.insert(SpriteBundle { - texture: texture.clone(), + commands.entity(entity).insert(SpriteBundle { + sprite: Sprite { + anchor: Anchor::BottomLeft, + ..default() + }, + texture, + transform: Transform { + translation, + scale, + ..Default::default() + }, ..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: scale.x, - y: scale.y, - z: 1.0, - }, - ..Default::default() }); } -} \ No newline at end of file +} + +/// A simple sigmoid function to convert from y to z position. +/// The return value is between 0 and 1. +fn z_position_from_y(y: f32) -> f32 { + -1.0 / (1.0 + (-y * 110_f64.powi(-5) as f32).exp()) +}