generated from tipragot/rust
Add a rendering system using temporary images #61
BIN
crates/border-wars/assets/tiles/hill.png
Normal file
BIN
crates/border-wars/assets/tiles/hill.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 170 KiB |
|
@ -10,13 +10,13 @@ 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(
|
||||
app.add_systems(Startup, init_resources_for_rendering)
|
||||
.add_systems(
|
||||
Update,
|
||||
render_map.run_if(in_state(crate::CurrentScene::Game)),
|
||||
);
|
||||
|
@ -25,21 +25,31 @@ impl Plugin for RendererPlugin {
|
|||
|
||||
impl 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 {
|
||||
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,
|
||||
CoCo_Sol marked this conversation as resolved
|
||||
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<AssetServer>) {
|
||||
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<Tile>>,
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut tiles_offset: Res<TilesOffset>,
|
||||
tiles_offset: Res<TilesOffset>,
|
||||
|
||||
) {
|
||||
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()
|
||||
});
|
||||
}
|
||||
}
|
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.