forked from tutorials/rust-sokoban
Minor refactors
This commit is contained in:
parent
53406aa9b4
commit
a826121b1f
@ -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 {}
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
";
|
||||
|
@ -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
|
||||
|
@ -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<u8> = 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
|
||||
|
Loading…
Reference in New Issue
Block a user