diff --git a/src/components.rs b/src/components.rs index c3d430b..38bb5f6 100644 --- a/src/components.rs +++ b/src/components.rs @@ -21,12 +21,6 @@ pub struct Renderable { pub path: String, } -impl Renderable { - pub fn new(path: &str) -> Self { - Renderable { path: path.to_string() } - } -} - #[derive(Component)] #[storage(VecStorage)] pub struct Wall {} diff --git a/src/entities.rs b/src/entities.rs index f894184..b240683 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -1,48 +1,40 @@ use crate::components::*; -use specs::{Builder, World, WorldExt}; +use specs::{Builder, World, WorldExt, EntityBuilder}; + +/// Cut down on the common syntax boilerplate +fn common_entity<'w>(world: &'w mut World, position: Position, z: u8, sprite_path: &str) -> EntityBuilder<'w> { + return world + .create_entity() + .with(Position { z, ..position }) + .with(Renderable { path: sprite_path.to_string() }); +} pub fn create_wall(world: &mut World, position: Position) { - world - .create_entity() - .with(Position { z: 10, ..position }) - .with(Renderable::new("/images/wall.png")) + common_entity(world, position, 10, "/images/wall.png") .with(Wall {}) .with(Immovable) .build(); } pub fn create_floor(world: &mut World, position: Position) { - world - .create_entity() - .with(Position { z: 5, ..position }) - .with(Renderable::new("/images/floor.png")) - .build(); + common_entity(world, position, 5, "/images/floor.png").build(); } pub fn create_box(world: &mut World, position: Position) { - world - .create_entity() - .with(Position { z: 10, ..position }) - .with(Renderable::new("/images/box.png")) + common_entity(world, position, 10, "/images/box.png") .with(Box {}) .with(Movable) .build(); } pub fn create_box_spot(world: &mut World, position: Position) { - world - .create_entity() - .with(Position { z: 9, ..position }) - .with(Renderable::new("/images/box_spot.png")) + common_entity(world, position, 9, "/images/box_spot.png") .with(BoxSpot {}) .build(); } pub fn create_player(world: &mut World, position: Position) { - world - .create_entity() - .with(Position { z: 10, ..position }) - .with(Renderable::new("/images/player.png")) + common_entity(world, position, 10, "/images/player.png") .with(Player {}) .with(Movable) .build(); diff --git a/src/main.rs b/src/main.rs index 1434a39..32e15f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,9 +61,9 @@ pub fn initialize_level(world: &mut World) { W W W . . . . W W . . . B . . W W . . . . . . W - W . P . . . . W + W . P . . B . W W . . . . . . W - W . . S . . . W + W . . S . S . W W . . . . . . W W W W W W W W W "; diff --git a/src/systems/gameplay_state_sytem.rs b/src/systems/gameplay_state_sytem.rs index 90535e3..737ec51 100644 --- a/src/systems/gameplay_state_sytem.rs +++ b/src/systems/gameplay_state_sytem.rs @@ -23,7 +23,10 @@ impl<'a> System<'a> for GameplayStateSystem { // get all boxes indexed by position let boxes_by_position: HashMap<(u8, u8), &Box> = (&positions, &boxes) .join() - .map(|t| ((t.0.x, t.0.y), t.1)) + .map(|t| { + let (pos, b) = t; + ((pos.x, pos.y), b) + }) .collect(); // loop through all box spots and check if there is a corresponding diff --git a/src/systems/input_system.rs b/src/systems/input_system.rs index 03eb396..de640b3 100644 --- a/src/systems/input_system.rs +++ b/src/systems/input_system.rs @@ -33,11 +33,12 @@ impl<'a> System<'a> for InputSystem { // Get the first key pressed if let Some(key) = input_queue.keys_pressed.pop() { // get all the movables and immovables - let mov: HashMap<(u8, u8), Index> = (&entities, &movables, &positions) + type MovMap = HashMap<(u8, u8), Index>; + let mov: MovMap = (&entities, &movables, &positions) .join() .map(|t| ((t.2.x, t.2.y), t.0.id())) .collect(); - let immov: HashMap<(u8, u8), Index> = (&entities, &immovables, &positions) + let immov: MovMap = (&entities, &immovables, &positions) .join() .map(|t| ((t.2.x, t.2.y), t.0.id())) .collect(); @@ -52,24 +53,24 @@ impl<'a> System<'a> for InputSystem { _ => continue, }; - let range: Vec<_> = if start < end { + let range: Vec = if start < end { (start..=end).collect() } else { (end..=start).rev().collect() }; - for x_or_y in range { + for value in range { let pos = if is_x { - (x_or_y, position.y) + (value, position.y) } else { - (position.x, x_or_y) + (position.x, value) }; // find a movable // if it exists, we try to move it and continue // if it doesn't exist, we continue and try to find an immovable instead match mov.get(&pos) { - Some(id) => to_move.push((key, id.clone())), + Some(id) => to_move.push((key, *id)), None => { // find an immovable // if it exists, we need to stop and not move anything