Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182pub mod map;
use bevy::image::TextureAtlasTemplate;
use crate::{ map::map::{Map, TileType}, prelude::*,};
pub struct MapPlugin;
impl Plugin for MapPlugin { fn build(&self, app: &mut App) { app.insert_resource(Map::new_random(SCREEN_WIDTH, SCREEN_HEIGHT)) .add_systems(PostStartup, spawn_map); }}
fn spawn_map( mut commands: Commands, map: Res<Map>, mut entities: ResMut<MapEntities>, font: Res<FontTextureAtlas>,) { for (x, y, tile_type) in map.iter_idx() { let pos = Position { x, y }; let glyph = tile_type.into(); let background = commands.spawn_scene(background_tile(pos, glyph)).id(); entities.background.insert(pos, background);
let foreground = commands .spawn_scene(foreground_tile(pos, glyph, &font)) .id(); entities.foreground.insert(pos, foreground); }}
fn background_tile(pos: Position, glyph: Glyph) -> impl Scene { bsn! { tile(pos) Sprite { color: {glyph.background}, } }}
fn foreground_tile(pos: Position, glyph: Glyph, font: &FontTextureAtlas) -> impl Scene { bsn! { tile(pos) template_value(glyph) Sprite { image: {font.texture.clone()}, texture_atlas: texture_atlas_template(font.atlas.clone(), glyph.char as usize), color: {glyph.foreground}, } Transform::from_translation(Vec3::new(0., 0., Z_MAP_FOREGROUND)) }}
fn tile(pos: Position) -> impl Scene { bsn! { template_value(pos) MapTile NeedsLayout TileSize::square(1.) Transform::from_translation(Vec3::new(0., 0., Z_MAP_BACKGROUND)) Sprite { custom_size: Vec2::splat(1.) } }}
pub fn texture_atlas_template( layout: Handle<TextureAtlasLayout>, index: usize,) -> TextureAtlasTemplate { TextureAtlasTemplate { layout: layout.into(), index, }}