update
Some checks failed
Rust Checks / checks (push) Has been cancelled
Rust Checks / checks (pull_request) Has been cancelled

This commit is contained in:
CoCo_Sol 2024-03-05 17:48:31 +01:00
parent f71b8dc511
commit 0b7f48127d
2 changed files with 54 additions and 45 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 122 KiB

View file

@ -1,15 +1,10 @@
//! All functions related to the rendering of the map. //! All functions related to the rendering of the map.
use bevy::prelude::*; use bevy::prelude::*;
use bevy::sprite::Anchor;
use crate::map::{Tile, TilePosition}; use crate::map::{Tile, TilePosition};
#[derive(Resource)]
struct TilesOffset {
x: f32,
y: f32,
}
/// A plugin to render the map. /// A plugin to render the map.
pub struct RendererPlugin; 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 { impl Tile {
/// Returns the texture handle of the tile. /// Returns the texture handle of the tile.
fn get_texture(&self, asset_server: &AssetServer) -> Handle<Image> { fn get_texture(&self, asset_server: &AssetServer) -> Handle<Image> {
match self { match self {
Tile::Grass => asset_server.load("tiles/grass.png"), Self::Grass => asset_server.load("tiles/grass.png"),
Tile::Forest => asset_server.load("tiles/grass.png"), Self::Forest => asset_server.load("tiles/grass.png"),
Tile::Hill => asset_server.load("tiles/hill.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. /// Init resources related to the rendering of the map.
fn init_resources_for_rendering(mut commands: Commands) { 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. /// Renders the map.
@ -58,36 +64,39 @@ fn render_map(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
tiles_offset: Res<TilesOffset>, tiles_offset: Res<TilesOffset>,
tiles_size: Res<TilesSize>,
) { ) {
for (entity, position, tile) in query.iter() { for (entity, position, tile) in query.iter() {
let texture = tile.get_texture(&*asset_server); let texture = tile.get_texture(&asset_server);
let mut entity = commands.entity(entity);
let pixel_position_ratio = position.to_pixel_coordinates(); let translation_2d = tiles_offset.0 * position.to_pixel_coordinates();
let position_x = tiles_offset.x * pixel_position_ratio.x; let translation = Vec3::new(
let position_y = tiles_offset.y * pixel_position_ratio.y; translation_2d.x,
translation_2d.y,
z_position_from_y(translation_2d.y),
);
let scale = tile.get_image_size(Vec2 { let scale_2d = tiles_size.0 / tile.get_image_size();
x: 125.0, let scale = Vec3::new(scale_2d.x, scale_2d.y, 1.0);
y: 100.0,
});
entity.insert(SpriteBundle { commands.entity(entity).insert(SpriteBundle {
texture: texture.clone(), sprite: Sprite {
anchor: Anchor::BottomLeft,
..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,
}, },
texture,
transform: Transform {
translation,
scale,
..Default::default() ..Default::default()
},
..default()
}); });
} }
} }
/// 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())
}