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,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,
|
||||||
CoCo_Sol marked this conversation as resolved
|
|||||||
|
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()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
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.