update
Some checks failed
Rust Checks / checks (push) Failing after 20s
Rust Checks / checks (pull_request) Failing after 5s

This commit is contained in:
CoCo_Sol 2024-03-04 08:21:25 +01:00
parent 5751b0a6ec
commit f71b8dc511
2 changed files with 44 additions and 20 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

View file

@ -10,36 +10,46 @@ struct TilesOffset {
y: f32, y: f32,
} }
/// A plugin to render the map. /// A plugin to render the map.
pub struct RendererPlugin; pub struct RendererPlugin;
impl Plugin for RendererPlugin { impl Plugin for RendererPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, init_resources_for_rendering).add_systems( app.add_systems(Startup, init_resources_for_rendering)
Update, .add_systems(
render_map.run_if(in_state(crate::CurrentScene::Game)), Update,
); render_map.run_if(in_state(crate::CurrentScene::Game)),
);
} }
} }
impl Tile { impl Tile {
/// Returns the texture handle of the tile. /// Returns the texture handle of the tile.
pub 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"), Tile::Grass => asset_server.load("tiles/grass.png"),
Tile::Forest => 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. /// Init resources related to the rendering of the map.
fn init_resources_for_rendering(mut commands: Commands, asset_server: Res<AssetServer>) { fn init_resources_for_rendering(mut commands: Commands) {
commands.insert_resource(TilesOffset { commands.insert_resource(TilesOffset { x: 70., y: 40. })
x: 70.,
y: 40.,
})
} }
/// Renders the map. /// Renders the map.
@ -47,23 +57,37 @@ fn render_map(
query: Query<(Entity, &TilePosition, &Tile), Changed<Tile>>, query: Query<(Entity, &TilePosition, &Tile), Changed<Tile>>,
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut tiles_offset: Res<TilesOffset>, tiles_offset: Res<TilesOffset>,
) { ) {
for (entity, position, tile) in query.iter() { 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 pixel_position_ratio = position.to_pixel_coordinates();
let position_x = tiles_offset.x * pixel_position_ratio.x; let position_x = tiles_offset.x * pixel_position_ratio.x;
let position_y = tiles_offset.y * pixel_position_ratio.y; let position_y = tiles_offset.y * pixel_position_ratio.y;
commands.entity(entity).insert(SpriteBundle { let scale = tile.get_image_size(Vec2 {
transform: Transform { translation: Vec3 { x: 125.0,
y: 100.0,
});
entity.insert(SpriteBundle {
texture: texture.clone(),
..default()
}).insert(Transform {
translation: Vec3 {
x: position_x, x: position_x,
y: position_y, y: position_y,
z: -1.0 / (1.0 + (-position_y * 10_f64.powf(-5.0) as f32).exp()), 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()}, scale: Vec3 {
texture: tile.get_texture(&*asset_server), x: scale.x,
..default() y: scale.y,
z: 1.0,
},
..Default::default()
}); });
} }
} }