From bfdaef7850b6ac17bb88f1b314236fb5014aac8e Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 5 Jul 2019 08:59:22 -0500 Subject: now a cells struct whichs layers own, which takes from tileset tiles --- src/map.rs | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) (limited to 'src/map.rs') diff --git a/src/map.rs b/src/map.rs index a3ddfb3..7042f58 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,18 +1,18 @@ use ggez::filesystem::File; use ggez::graphics::spritebatch::SpriteBatch; use ggez::nalgebra::Point2; +use std::collections::HashMap; use xml::reader::XmlEvent::Characters; use crate::constants; use crate::entity::Operable; use crate::layer::Layer; -use crate::tileset::Tileset; +use crate::tileset::{Tile, Tileset}; use crate::xmlelements::XMLElements; #[derive(Clone)] pub struct Map { - width: usize, - height: usize, + dimensions: (usize, usize), layers: Vec, spawns: Vec<(String, Point2)>, } @@ -35,23 +35,25 @@ impl Map { pub fn new(file: File, tileset: &Tileset) -> Map { let elements = XMLElements::new(file); - let width = elements - .get_element_attribute("map", "width") - .unwrap() - .parse() - .unwrap(); - let height = elements - .get_element_attribute("map", "height") - .unwrap() - .parse() - .unwrap(); + let dimensions = ( + elements + .get_element_attribute("map", "width") + .unwrap() + .parse() + .unwrap(), + elements + .get_element_attribute("map", "height") + .unwrap() + .parse() + .unwrap(), + ); let layers: Vec = elements .events .iter() .filter_map(|e| { if let Characters(text) = e { - Some(Layer::new(text, tileset, width, height)) + Some(Layer::new(text, tileset, dimensions)) } else { None } @@ -62,23 +64,22 @@ impl Map { Map { layers, - width, - height, + dimensions, spawns, } } fn get_spawn_points( layers: &[Layer], - spawn_tiles: Vec<(String, usize)>, + spawn_tiles: HashMap, ) -> Vec<(String, Point2)> { let mut spawn_points = Vec::new(); for layer in layers.iter() { - for tile in layer.tiles.iter() { - for spawn_tile in spawn_tiles.iter() { - if spawn_tile.1 == tile.id { - spawn_points.push((spawn_tile.0.clone(), tile.destination)); + for cell in layer.cells.iter() { + for (id, tile) in spawn_tiles.iter() { + if id == &cell.id { + spawn_points.push((tile.property.spawn.clone().unwrap(), cell.destination)); } } } @@ -93,8 +94,8 @@ impl Map { pub fn get_dimensions(&self) -> (f32, f32) { ( - (constants::TILE_WIDTH * constants::TILE_SCALE) * self.width as f32, - (constants::TILE_HEIGHT * constants::TILE_SCALE) * self.height as f32, + (constants::TILE_WIDTH * constants::TILE_SCALE) * self.dimensions.0 as f32, + (constants::TILE_HEIGHT * constants::TILE_SCALE) * self.dimensions.1 as f32, ) } } -- cgit v1.2.3