generated from tipragot/rust
Add a rendering system using temporary images #61
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
|
@ -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<Image> {
|
||||
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,
|
||||
CoCo_Sol marked this conversation as resolved
|
||||
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<AssetServer>,
|
||||
tiles_offset: Res<TilesOffset>,
|
||||
|
||||
tiles_size: Res<TilesSize>,
|
||||
) {
|
||||
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()
|
||||
}).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()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue
Add a TODO to tell it is temporary. I think we shoud make an image managment system to not hardcode this.