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,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderable {
|
|
||||||
pub fn new(path: &str) -> Self {
|
|
||||||
Renderable { path: path.to_string() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[storage(VecStorage)]
|
#[storage(VecStorage)]
|
||||||
pub struct Wall {}
|
pub struct Wall {}
|
||||||
|
@ -1,48 +1,40 @@
|
|||||||
use crate::components::*;
|
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) {
|
pub fn create_wall(world: &mut World, position: Position) {
|
||||||
world
|
common_entity(world, position, 10, "/images/wall.png")
|
||||||
.create_entity()
|
|
||||||
.with(Position { z: 10, ..position })
|
|
||||||
.with(Renderable::new("/images/wall.png"))
|
|
||||||
.with(Wall {})
|
.with(Wall {})
|
||||||
.with(Immovable)
|
.with(Immovable)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_floor(world: &mut World, position: Position) {
|
pub fn create_floor(world: &mut World, position: Position) {
|
||||||
world
|
common_entity(world, position, 5, "/images/floor.png").build();
|
||||||
.create_entity()
|
|
||||||
.with(Position { z: 5, ..position })
|
|
||||||
.with(Renderable::new("/images/floor.png"))
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_box(world: &mut World, position: Position) {
|
pub fn create_box(world: &mut World, position: Position) {
|
||||||
world
|
common_entity(world, position, 10, "/images/box.png")
|
||||||
.create_entity()
|
|
||||||
.with(Position { z: 10, ..position })
|
|
||||||
.with(Renderable::new("/images/box.png"))
|
|
||||||
.with(Box {})
|
.with(Box {})
|
||||||
.with(Movable)
|
.with(Movable)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_box_spot(world: &mut World, position: Position) {
|
pub fn create_box_spot(world: &mut World, position: Position) {
|
||||||
world
|
common_entity(world, position, 9, "/images/box_spot.png")
|
||||||
.create_entity()
|
|
||||||
.with(Position { z: 9, ..position })
|
|
||||||
.with(Renderable::new("/images/box_spot.png"))
|
|
||||||
.with(BoxSpot {})
|
.with(BoxSpot {})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_player(world: &mut World, position: Position) {
|
pub fn create_player(world: &mut World, position: Position) {
|
||||||
world
|
common_entity(world, position, 10, "/images/player.png")
|
||||||
.create_entity()
|
|
||||||
.with(Position { z: 10, ..position })
|
|
||||||
.with(Renderable::new("/images/player.png"))
|
|
||||||
.with(Player {})
|
.with(Player {})
|
||||||
.with(Movable)
|
.with(Movable)
|
||||||
.build();
|
.build();
|
||||||
|
@ -61,9 +61,9 @@ pub fn initialize_level(world: &mut World) {
|
|||||||
W W W . . . . W
|
W W W . . . . W
|
||||||
W . . . B . . W
|
W . . . B . . W
|
||||||
W . . . . . . W
|
W . . . . . . W
|
||||||
W . P . . . . W
|
W . P . . B . W
|
||||||
W . . . . . . W
|
W . . . . . . W
|
||||||
W . . S . . . W
|
W . . S . S . W
|
||||||
W . . . . . . W
|
W . . . . . . W
|
||||||
W W W W W 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
|
// get all boxes indexed by position
|
||||||
let boxes_by_position: HashMap<(u8, u8), &Box> = (&positions, &boxes)
|
let boxes_by_position: HashMap<(u8, u8), &Box> = (&positions, &boxes)
|
||||||
.join()
|
.join()
|
||||||
.map(|t| ((t.0.x, t.0.y), t.1))
|
.map(|t| {
|
||||||
|
let (pos, b) = t;
|
||||||
|
((pos.x, pos.y), b)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// loop through all box spots and check if there is a corresponding
|
// 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
|
// Get the first key pressed
|
||||||
if let Some(key) = input_queue.keys_pressed.pop() {
|
if let Some(key) = input_queue.keys_pressed.pop() {
|
||||||
// get all the movables and immovables
|
// 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()
|
.join()
|
||||||
.map(|t| ((t.2.x, t.2.y), t.0.id()))
|
.map(|t| ((t.2.x, t.2.y), t.0.id()))
|
||||||
.collect();
|
.collect();
|
||||||
let immov: HashMap<(u8, u8), Index> = (&entities, &immovables, &positions)
|
let immov: MovMap = (&entities, &immovables, &positions)
|
||||||
.join()
|
.join()
|
||||||
.map(|t| ((t.2.x, t.2.y), t.0.id()))
|
.map(|t| ((t.2.x, t.2.y), t.0.id()))
|
||||||
.collect();
|
.collect();
|
||||||
@ -52,24 +53,24 @@ impl<'a> System<'a> for InputSystem {
|
|||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
let range: Vec<_> = if start < end {
|
let range: Vec<u8> = if start < end {
|
||||||
(start..=end).collect()
|
(start..=end).collect()
|
||||||
} else {
|
} else {
|
||||||
(end..=start).rev().collect()
|
(end..=start).rev().collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
for x_or_y in range {
|
for value in range {
|
||||||
let pos = if is_x {
|
let pos = if is_x {
|
||||||
(x_or_y, position.y)
|
(value, position.y)
|
||||||
} else {
|
} else {
|
||||||
(position.x, x_or_y)
|
(position.x, value)
|
||||||
};
|
};
|
||||||
|
|
||||||
// find a movable
|
// find a movable
|
||||||
// if it exists, we try to move it and continue
|
// if it exists, we try to move it and continue
|
||||||
// if it doesn't exist, we continue and try to find an immovable instead
|
// if it doesn't exist, we continue and try to find an immovable instead
|
||||||
match mov.get(&pos) {
|
match mov.get(&pos) {
|
||||||
Some(id) => to_move.push((key, id.clone())),
|
Some(id) => to_move.push((key, *id)),
|
||||||
None => {
|
None => {
|
||||||
// find an immovable
|
// find an immovable
|
||||||
// if it exists, we need to stop and not move anything
|
// if it exists, we need to stop and not move anything
|
||||||
|
Loading…
Reference in New Issue
Block a user