use crate::consts; use crate::components::*; use specs::world::Builder; use std::path::Path; #[derive(Debug, Copy, Clone)] enum DrawLayer { Background, Foreground, Decoration, } static DRAW_LAYERS: [DrawLayer;3] = [ DrawLayer::Background, DrawLayer::Foreground, DrawLayer::Decoration, ]; pub fn world_from_file>(w: &mut specs::World, path: P) { let tiled::Map { layers, .. } = tiled::parse_file(path.as_ref()).unwrap(); for (phase, layer) in DRAW_LAYERS.iter().zip(layers) { for (row, y) in layer.tiles.iter().zip(0..) { for (&n, x) in row.iter().zip(0..) { if n != 0 { let x = x as f32 * consts::TILE_SIZE; let y = y as f32 * consts::TILE_SIZE; let u = ((n - 1) % 32) as u8; let v = ((n - u as u32 - 1) / 32) as u8; let mut e = w.create_entity() .with(Position { x, y }) .with(Sprite { u, v }); e = match phase { DrawLayer::Background => e.with(Background), DrawLayer::Foreground => e.with(Foreground), DrawLayer::Decoration => e.with(Decoration), }; e.build(); } } } } // create the player w.create_entity() .with(Position { x: 3.0 * consts::TILE_SIZE, y: 3.0 * consts::TILE_SIZE, }) .with(Sprite { u: 8, v: 0 }) .with(Velocity { dx: 0.0, dy: 0.0 }) .with(Foreground) .with(Movable) .build(); }